[libc-commits] [libc] [libc][math][c23] Add f16{add, sub}{, l, f128} C23 math functions (PR #97072)
via libc-commits
libc-commits at lists.llvm.org
Fri Jun 28 08:29:48 PDT 2024
https://github.com/overmighty created https://github.com/llvm/llvm-project/pull/97072
Part of #93566.
>From f5e03f4c99987a9844d108a4286c600844c2796d Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Sat, 22 Jun 2024 03:19:59 +0200
Subject: [PATCH 01/11] [libc][math][c23] Add f16addf C23 math function
---
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/docs/math/index.rst | 2 +
libc/spec/stdc.td | 2 +
.../__support/FPUtil/generic/CMakeLists.txt | 19 ++
libc/src/__support/FPUtil/generic/add.h | 171 ++++++++++++++++++
libc/src/math/CMakeLists.txt | 2 +
libc/src/math/f16addf.h | 20 ++
libc/src/math/generic/CMakeLists.txt | 13 ++
libc/src/math/generic/f16addf.cpp | 19 ++
libc/test/src/math/AddTest.h | 74 ++++++++
libc/test/src/math/CMakeLists.txt | 13 ++
libc/test/src/math/f16addf_test.cpp | 13 ++
libc/test/src/math/smoke/AddTest.h | 156 ++++++++++++++++
libc/test/src/math/smoke/CMakeLists.txt | 15 ++
libc/test/src/math/smoke/f16addf_test.cpp | 13 ++
libc/utils/MPFRWrapper/MPFRUtils.cpp | 8 +
libc/utils/MPFRWrapper/MPFRUtils.h | 5 +-
18 files changed, 546 insertions(+), 1 deletion(-)
create mode 100644 libc/src/__support/FPUtil/generic/add.h
create mode 100644 libc/src/math/f16addf.h
create mode 100644 libc/src/math/generic/f16addf.cpp
create mode 100644 libc/test/src/math/AddTest.h
create mode 100644 libc/test/src/math/f16addf_test.cpp
create mode 100644 libc/test/src/math/smoke/AddTest.h
create mode 100644 libc/test/src/math/smoke/f16addf_test.cpp
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 746cc675e8fcd..a40a4baaacb15 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -506,6 +506,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.canonicalizef16
libc.src.math.ceilf16
libc.src.math.copysignf16
+ libc.src.math.f16addf
libc.src.math.f16divf
libc.src.math.f16fmaf
libc.src.math.f16sqrtf
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 34748ff5950ad..18d9812b7e6fa 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -536,6 +536,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.canonicalizef16
libc.src.math.ceilf16
libc.src.math.copysignf16
+ libc.src.math.f16addf
libc.src.math.f16divf
libc.src.math.f16fmaf
libc.src.math.f16sqrtf
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index e05149d8e1dc9..9162b7ff74abe 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -124,6 +124,8 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| dsub | N/A | N/A | | N/A | | 7.12.14.2 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
+| f16add | |check| | | | N/A | | 7.12.14.1 | F.10.11 |
++------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| f16div | |check| | | | N/A | | 7.12.14.4 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| f16fma | |check| | | | N/A | | 7.12.14.5 | F.10.11 |
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 651f49deef4c1..a59db77aef3b9 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -726,6 +726,8 @@ def StdC : StandardSpec<"stdc"> {
GuardedFunctionSpec<"setpayloadsigf16", RetValSpec<IntType>, [ArgSpec<Float16Ptr>, ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
+ GuardedFunctionSpec<"f16addf", RetValSpec<Float16Type>, [ArgSpec<FloatType>, ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
+
GuardedFunctionSpec<"f16divf", RetValSpec<Float16Type>, [ArgSpec<FloatType>, ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"f16sqrtf", RetValSpec<Float16Type>, [ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
diff --git a/libc/src/__support/FPUtil/generic/CMakeLists.txt b/libc/src/__support/FPUtil/generic/CMakeLists.txt
index 33b2564bfa087..37968eb847aaa 100644
--- a/libc/src/__support/FPUtil/generic/CMakeLists.txt
+++ b/libc/src/__support/FPUtil/generic/CMakeLists.txt
@@ -46,6 +46,25 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ add
+ HDRS
+ add.h
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.fenv_macros
+ libc.src.__support.CPP.algorithm
+ libc.src.__support.CPP.bit
+ libc.src.__support.CPP.type_traits
+ libc.src.__support.FPUtil.basic_operations
+ libc.src.__support.FPUtil.fenv_impl
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.dyadic_float
+ libc.src.__support.FPUtil.rounding_mode
+ libc.src.__support.macros.attributes
+ libc.src.__support.macros.optimization
+)
+
add_header_library(
div
HDRS
diff --git a/libc/src/__support/FPUtil/generic/add.h b/libc/src/__support/FPUtil/generic/add.h
new file mode 100644
index 0000000000000..a987c38236396
--- /dev/null
+++ b/libc/src/__support/FPUtil/generic/add.h
@@ -0,0 +1,171 @@
+//===-- Addition of IEEE 754 floating-point numbers -------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_ADD_H
+#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_ADD_H
+
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/CPP/algorithm.h"
+#include "src/__support/CPP/bit.h"
+#include "src/__support/CPP/type_traits.h"
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/dyadic_float.h"
+#include "src/__support/FPUtil/rounding_mode.h"
+#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/optimization.h"
+
+namespace LIBC_NAMESPACE::fputil::generic {
+
+template <typename OutType, typename InType>
+LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<OutType> &&
+ cpp::is_floating_point_v<InType> &&
+ sizeof(OutType) <= sizeof(InType),
+ OutType>
+add(InType x, InType y) {
+ using OutFPBits = FPBits<OutType>;
+ using OutStorageType = typename OutFPBits::StorageType;
+ using InFPBits = FPBits<InType>;
+ using InStorageType = typename InFPBits::StorageType;
+
+ constexpr int GUARD_BITS_LEN = 3;
+ constexpr int RESULT_FRACTION_LEN = InFPBits::FRACTION_LEN + GUARD_BITS_LEN;
+ constexpr int RESULT_MANTISSA_LEN = RESULT_FRACTION_LEN + 1;
+
+ using DyadicFloat =
+ DyadicFloat<cpp::bit_ceil(static_cast<size_t>(RESULT_MANTISSA_LEN))>;
+
+ InFPBits x_bits(x);
+ InFPBits y_bits(y);
+
+ if (LIBC_UNLIKELY(x_bits.is_inf_or_nan() || y_bits.is_inf_or_nan() ||
+ x_bits.is_zero() || y_bits.is_zero())) {
+ if (x_bits.is_nan() || y_bits.is_nan()) {
+ if (x_bits.is_signaling_nan() || y_bits.is_signaling_nan())
+ raise_except_if_required(FE_INVALID);
+
+ if (x_bits.is_quiet_nan()) {
+ InStorageType x_payload = static_cast<InStorageType>(getpayload(x));
+ if ((x_payload & ~(OutFPBits::FRACTION_MASK >> 1)) == 0)
+ return OutFPBits::quiet_nan(x_bits.sign(),
+ static_cast<OutStorageType>(x_payload))
+ .get_val();
+ }
+
+ if (y_bits.is_quiet_nan()) {
+ InStorageType y_payload = static_cast<InStorageType>(getpayload(y));
+ if ((y_payload & ~(OutFPBits::FRACTION_MASK >> 1)) == 0)
+ return OutFPBits::quiet_nan(y_bits.sign(),
+ static_cast<OutStorageType>(y_payload))
+ .get_val();
+ }
+
+ return OutFPBits::quiet_nan().get_val();
+ }
+
+ if (x_bits.is_inf()) {
+ if (y_bits.is_inf()) {
+ if (x_bits.sign() != y_bits.sign()) {
+ raise_except_if_required(FE_INVALID);
+ return OutFPBits::quiet_nan().get_val();
+ }
+
+ return OutFPBits::inf(x_bits.sign()).get_val();
+ }
+
+ return OutFPBits::inf(x_bits.sign()).get_val();
+ }
+
+ if (y_bits.is_inf())
+ return OutFPBits::inf(y_bits.sign()).get_val();
+
+ if (x_bits.is_zero()) {
+ if (y_bits.is_zero()) {
+ switch (quick_get_round()) {
+ case FE_DOWNWARD:
+ return OutFPBits::zero(Sign::NEG).get_val();
+ default:
+ return OutFPBits::zero(Sign::POS).get_val();
+ }
+ }
+
+ return static_cast<OutType>(y);
+ }
+
+ if (y_bits.is_zero())
+ return static_cast<OutType>(x);
+ }
+
+ InType x_abs = x_bits.abs().get_val();
+ InType y_abs = y_bits.abs().get_val();
+
+ if (x_abs == y_abs && x_bits.sign() != y_bits.sign()) {
+ switch (quick_get_round()) {
+ case FE_DOWNWARD:
+ return OutFPBits::zero(Sign::NEG).get_val();
+ default:
+ return OutFPBits::zero(Sign::POS).get_val();
+ }
+ }
+
+ Sign result_sign = Sign::POS;
+
+ if (x_abs > y_abs)
+ result_sign = x_bits.sign();
+ else if (x_abs < y_abs)
+ result_sign = y_bits.sign();
+ else if (x_bits.sign() == y_bits.sign())
+ result_sign = x_bits.sign();
+
+ InFPBits max_bits(cpp::max(x_abs, y_abs));
+ InFPBits min_bits(cpp::min(x_abs, y_abs));
+
+ InStorageType result_mant;
+
+ if (max_bits.is_subnormal()) {
+ // min_bits must be subnormal too.
+ if (max_bits.sign() == min_bits.sign())
+ result_mant = max_bits.get_mantissa() + min_bits.get_mantissa();
+ else
+ result_mant = max_bits.get_mantissa() - min_bits.get_mantissa();
+
+ result_mant <<= GUARD_BITS_LEN;
+ } else {
+ InStorageType max_mant = max_bits.get_explicit_mantissa() << GUARD_BITS_LEN;
+ InStorageType min_mant = min_bits.get_explicit_mantissa() << GUARD_BITS_LEN;
+ int alignment =
+ max_bits.get_biased_exponent() - min_bits.get_biased_exponent();
+
+ InStorageType aligned_min_mant =
+ min_mant >> cpp::min(alignment, RESULT_MANTISSA_LEN);
+ bool aligned_min_mant_sticky;
+
+ if (alignment <= 3)
+ aligned_min_mant_sticky = false;
+ else if (alignment <= InFPBits::FRACTION_LEN + 3)
+ aligned_min_mant_sticky =
+ (min_mant << (InFPBits::STORAGE_LEN - alignment)) != 0;
+ else
+ aligned_min_mant_sticky = true;
+
+ if (max_bits.sign() == min_bits.sign())
+ result_mant = max_mant + (aligned_min_mant | aligned_min_mant_sticky);
+ else
+ result_mant = max_mant - (aligned_min_mant | aligned_min_mant_sticky);
+ }
+
+ int result_exp = max_bits.get_exponent() - RESULT_FRACTION_LEN;
+ DyadicFloat result(result_sign, result_exp, result_mant);
+ return result.template as<OutType, /*ShouldSignalExceptions=*/true>();
+}
+
+} // namespace LIBC_NAMESPACE::fputil::generic
+
+#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_ADD_H
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 711cbf8bbfdca..bdffff8c8b7bb 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -99,6 +99,8 @@ add_math_entrypoint_object(exp10f)
add_math_entrypoint_object(expm1)
add_math_entrypoint_object(expm1f)
+add_math_entrypoint_object(f16addf)
+
add_math_entrypoint_object(f16divf)
add_math_entrypoint_object(f16fmaf)
diff --git a/libc/src/math/f16addf.h b/libc/src/math/f16addf.h
new file mode 100644
index 0000000000000..31d0e786584e6
--- /dev/null
+++ b/libc/src/math/f16addf.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for f16addf -----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_F16ADDF_H
+#define LLVM_LIBC_SRC_MATH_F16ADDF_H
+
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE {
+
+float16 f16addf(float x, float y);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_F16ADDF_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 41a77c8710f6b..252dadee63a19 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3754,6 +3754,19 @@ add_entrypoint_object(
-O3
)
+add_entrypoint_object(
+ f16addf
+ SRCS
+ f16addf.cpp
+ HDRS
+ ../f16addf.h
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.generic.add
+ COMPILE_OPTIONS
+ -O3
+)
+
add_entrypoint_object(
f16divf
SRCS
diff --git a/libc/src/math/generic/f16addf.cpp b/libc/src/math/generic/f16addf.cpp
new file mode 100644
index 0000000000000..de2f666cdc9fa
--- /dev/null
+++ b/libc/src/math/generic/f16addf.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of f16addf function --------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/f16addf.h"
+#include "src/__support/FPUtil/generic/add.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float16, f16addf, (float x, float y)) {
+ return fputil::generic::add<float16>(x, y);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/test/src/math/AddTest.h b/libc/test/src/math/AddTest.h
new file mode 100644
index 0000000000000..df0ef66cfeefa
--- /dev/null
+++ b/libc/test/src/math/AddTest.h
@@ -0,0 +1,74 @@
+//===-- Utility class to test different flavors of float add ----*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_TEST_SRC_MATH_ADDTEST_H
+#define LLVM_LIBC_TEST_SRC_MATH_ADDTEST_H
+
+#include "test/UnitTest/FEnvSafeTest.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+template <typename OutType, typename InType>
+class AddTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+
+ struct InConstants {
+ DECLARE_SPECIAL_CONSTANTS(InType)
+ };
+
+ using InFPBits = typename InConstants::FPBits;
+ using InStorageType = typename InConstants::StorageType;
+
+ static constexpr InStorageType IN_MAX_NORMAL_U =
+ InFPBits::max_normal().uintval();
+ static constexpr InStorageType IN_MIN_NORMAL_U =
+ InFPBits::min_normal().uintval();
+ static constexpr InStorageType IN_MAX_SUBNORMAL_U =
+ InFPBits::max_subnormal().uintval();
+ static constexpr InStorageType IN_MIN_SUBNORMAL_U =
+ InFPBits::min_subnormal().uintval();
+
+public:
+ typedef OutType (*AddFunc)(InType, InType);
+
+ void test_subnormal_range(AddFunc func) {
+ constexpr InStorageType COUNT = 100'001;
+ constexpr InStorageType STEP =
+ (IN_MAX_SUBNORMAL_U - IN_MIN_SUBNORMAL_U) / COUNT;
+ for (InStorageType i = 0, v = 0, w = IN_MAX_SUBNORMAL_U; i <= COUNT;
+ ++i, v += STEP, w -= STEP) {
+ InType x = InFPBits(v).get_val();
+ InType y = InFPBits(w).get_val();
+ mpfr::BinaryInput<InType> input{x, y};
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Add, input, func(x, y),
+ 0.5);
+ }
+ }
+
+ void test_normal_range(AddFunc func) {
+ constexpr InStorageType COUNT = 100'001;
+ constexpr InStorageType STEP = (IN_MAX_NORMAL_U - IN_MIN_NORMAL_U) / COUNT;
+ for (InStorageType i = 0, v = 0, w = IN_MAX_NORMAL_U; i <= COUNT;
+ ++i, v += STEP, w -= STEP) {
+ InType x = InFPBits(v).get_val();
+ InType y = InFPBits(w).get_val();
+ mpfr::BinaryInput<InType> input{x, y};
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Add, input, func(x, y),
+ 0.5);
+ }
+ }
+};
+
+#define LIST_ADD_TESTS(OutType, InType, func) \
+ using LlvmLibcAddTest = AddTest<OutType, InType>; \
+ TEST_F(LlvmLibcAddTest, SubnormalRange) { test_subnormal_range(&func); } \
+ TEST_F(LlvmLibcAddTest, NormalRange) { test_normal_range(&func); }
+
+#endif // LLVM_LIBC_TEST_SRC_MATH_ADDTEST_H
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index ba588662f469e..277ab2236a615 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -1890,6 +1890,19 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
+add_fp_unittest(
+ f16addf_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ f16addf_test.cpp
+ HDRS
+ AddTest.h
+ DEPENDS
+ libc.src.math.f16addf
+)
+
add_fp_unittest(
f16divf_test
NEED_MPFR
diff --git a/libc/test/src/math/f16addf_test.cpp b/libc/test/src/math/f16addf_test.cpp
new file mode 100644
index 0000000000000..1e8b4323114ad
--- /dev/null
+++ b/libc/test/src/math/f16addf_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for f16addf ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "AddTest.h"
+
+#include "src/math/f16addf.h"
+
+LIST_ADD_TESTS(float16, float, LIBC_NAMESPACE::f16addf)
diff --git a/libc/test/src/math/smoke/AddTest.h b/libc/test/src/math/smoke/AddTest.h
new file mode 100644
index 0000000000000..c713c5a88763c
--- /dev/null
+++ b/libc/test/src/math/smoke/AddTest.h
@@ -0,0 +1,156 @@
+//===-- Utility class to test different flavors of float add ----*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_TEST_SRC_MATH_SMOKE_ADDTEST_H
+#define LLVM_LIBC_TEST_SRC_MATH_SMOKE_ADDTEST_H
+
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "test/UnitTest/FEnvSafeTest.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+template <typename OutType, typename InType>
+class AddTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+
+ DECLARE_SPECIAL_CONSTANTS(OutType)
+
+ struct InConstants {
+ DECLARE_SPECIAL_CONSTANTS(InType)
+ };
+
+ using InFPBits = typename InConstants::FPBits;
+ using InStorageType = typename InConstants::StorageType;
+
+ InConstants in;
+
+public:
+ using AddFunc = OutType (*)(InType, InType);
+
+ void test_special_numbers(AddFunc func) {
+ EXPECT_FP_IS_NAN(func(aNaN, aNaN));
+ EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(sNaN, sNaN), FE_INVALID);
+
+ InType qnan_42 = InFPBits::quiet_nan(Sign::POS, 0x42).get_val();
+ EXPECT_FP_EQ(InType(0x42.0p+0),
+ LIBC_NAMESPACE::fputil::getpayload(func(qnan_42, zero)));
+ EXPECT_FP_EQ(InType(0x42.0p+0),
+ LIBC_NAMESPACE::fputil::getpayload(func(zero, qnan_42)));
+
+ if constexpr (sizeof(OutType) < sizeof(InType)) {
+ InStorageType max_payload = InFPBits::FRACTION_MASK >> 1;
+ InType qnan_max = InFPBits::quiet_nan(Sign::POS, max_payload).get_val();
+ EXPECT_FP_EQ(zero,
+ LIBC_NAMESPACE::fputil::getpayload(func(qnan_max, zero)));
+ EXPECT_FP_EQ(zero,
+ LIBC_NAMESPACE::fputil::getpayload(func(zero, qnan_max)));
+ EXPECT_FP_EQ(InType(0x42.0p+0),
+ LIBC_NAMESPACE::fputil::getpayload(func(qnan_max, qnan_42)));
+ EXPECT_FP_EQ(InType(0x42.0p+0),
+ LIBC_NAMESPACE::fputil::getpayload(func(qnan_42, qnan_max)));
+ }
+
+ EXPECT_FP_EQ(inf, func(inf, zero));
+ EXPECT_FP_EQ(neg_inf, func(neg_inf, zero));
+ EXPECT_FP_EQ(inf, func(inf, neg_zero));
+ EXPECT_FP_EQ(neg_inf, func(neg_inf, neg_zero));
+ }
+
+ void test_invalid_operations(AddFunc func) {
+ EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(inf, neg_inf), FE_INVALID);
+ EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(neg_inf, inf), FE_INVALID);
+ }
+
+ void test_range_errors(AddFunc func) {
+ using namespace LIBC_NAMESPACE::fputil::testing;
+
+ if (ForceRoundingMode r(RoundingMode::Nearest); r.success) {
+ EXPECT_FP_EQ_WITH_EXCEPTION(inf, func(max_normal, max_normal),
+ FE_OVERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(ERANGE);
+ EXPECT_FP_EQ_WITH_EXCEPTION(-inf, func(neg_max_normal, neg_max_normal),
+ FE_OVERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(ERANGE);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(zero, func(in.min_denormal, in.min_denormal),
+ FE_UNDERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(ERANGE);
+ EXPECT_FP_EQ_WITH_EXCEPTION(
+ neg_zero, func(in.neg_min_denormal, in.neg_min_denormal),
+ FE_UNDERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(ERANGE);
+ }
+
+ if (ForceRoundingMode r(RoundingMode::TowardZero); r.success) {
+ EXPECT_FP_EQ_WITH_EXCEPTION(max_normal, func(max_normal, max_normal),
+ FE_OVERFLOW | FE_INEXACT);
+ EXPECT_FP_EQ_WITH_EXCEPTION(neg_max_normal,
+ func(neg_max_normal, neg_max_normal),
+ FE_OVERFLOW | FE_INEXACT);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(zero, func(in.min_denormal, in.min_denormal),
+ FE_UNDERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(ERANGE);
+ EXPECT_FP_EQ_WITH_EXCEPTION(
+ neg_zero, func(in.neg_min_denormal, in.neg_min_denormal),
+ FE_UNDERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(ERANGE);
+ }
+
+ if (ForceRoundingMode r(RoundingMode::Downward); r.success) {
+ EXPECT_FP_EQ_WITH_EXCEPTION(max_normal, func(max_normal, max_normal),
+ FE_OVERFLOW | FE_INEXACT);
+ EXPECT_FP_EQ_WITH_EXCEPTION(-inf, func(neg_max_normal, neg_max_normal),
+ FE_OVERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(ERANGE);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(zero, func(in.min_denormal, in.min_denormal),
+ FE_UNDERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(ERANGE);
+ EXPECT_FP_EQ_WITH_EXCEPTION(
+ neg_min_denormal, func(in.neg_min_denormal, in.neg_min_denormal),
+ FE_UNDERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(ERANGE);
+ }
+
+ if (ForceRoundingMode r(RoundingMode::Upward); r.success) {
+ EXPECT_FP_EQ_WITH_EXCEPTION(inf, func(max_normal, max_normal),
+ FE_OVERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(ERANGE);
+ EXPECT_FP_EQ_WITH_EXCEPTION(neg_max_normal,
+ func(neg_max_normal, neg_max_normal),
+ FE_OVERFLOW | FE_INEXACT);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(min_denormal,
+ func(in.min_denormal, in.min_denormal),
+ FE_UNDERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(ERANGE);
+ EXPECT_FP_EQ_WITH_EXCEPTION(
+ neg_zero, func(in.neg_min_denormal, in.neg_min_denormal),
+ FE_UNDERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(ERANGE);
+ }
+ }
+
+ void test_inexact_results(AddFunc func) {
+ func(InType(1.0), min_denormal);
+ EXPECT_FP_EXCEPTION(FE_INEXACT);
+ }
+};
+
+#define LIST_ADD_TESTS(OutType, InType, func) \
+ using LlvmLibcAddTest = AddTest<OutType, InType>; \
+ TEST_F(LlvmLibcAddTest, SpecialNumbers) { test_special_numbers(&func); } \
+ TEST_F(LlvmLibcAddTest, InvalidOperations) { \
+ test_invalid_operations(&func); \
+ } \
+ TEST_F(LlvmLibcAddTest, RangeErrors) { test_range_errors(&func); } \
+ TEST_F(LlvmLibcAddTest, InexactResults) { test_inexact_results(&func); }
+
+#endif // LLVM_LIBC_TEST_SRC_MATH_SMOKE_ADDTEST_H
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index d79e296913e32..5f52da1d2222c 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3630,6 +3630,21 @@ add_fp_unittest(
libc.src.math.setpayloadsigf16
)
+add_fp_unittest(
+ f16addf_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ f16addf_test.cpp
+ HDRS
+ AddTest.h
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.fenv_macros
+ libc.src.__support.FPUtil.basic_operations
+ libc.src.math.f16addf
+)
+
add_fp_unittest(
f16divf_test
SUITE
diff --git a/libc/test/src/math/smoke/f16addf_test.cpp b/libc/test/src/math/smoke/f16addf_test.cpp
new file mode 100644
index 0000000000000..1e8b4323114ad
--- /dev/null
+++ b/libc/test/src/math/smoke/f16addf_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for f16addf ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "AddTest.h"
+
+#include "src/math/f16addf.h"
+
+LIST_ADD_TESTS(float16, float, LIBC_NAMESPACE::f16addf)
diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp
index 88aef3e6e10c5..f42c2e3f571a8 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -188,6 +188,12 @@ class MPFRNumber {
return result;
}
+ MPFRNumber add(const MPFRNumber &b) const {
+ MPFRNumber result(*this);
+ mpfr_add(result.value, value, b.value, mpfr_rounding);
+ return result;
+ }
+
MPFRNumber asin() const {
MPFRNumber result(*this);
mpfr_asin(result.value, value, mpfr_rounding);
@@ -712,6 +718,8 @@ binary_operation_one_output(Operation op, InputType x, InputType y,
MPFRNumber inputX(x, precision, rounding);
MPFRNumber inputY(y, precision, rounding);
switch (op) {
+ case Operation::Add:
+ return inputX.add(inputY);
case Operation::Atan2:
return inputX.atan2(inputY);
case Operation::Div:
diff --git a/libc/utils/MPFRWrapper/MPFRUtils.h b/libc/utils/MPFRWrapper/MPFRUtils.h
index 46f3375fd4b7e..e96e5fd4795ef 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.h
+++ b/libc/utils/MPFRWrapper/MPFRUtils.h
@@ -70,6 +70,7 @@ enum class Operation : int {
// input and produce a single floating point number of the same type as
// output.
BeginBinaryOperationsSingleOutput,
+ Add,
Atan2,
Div,
Fmod,
@@ -303,7 +304,9 @@ constexpr bool is_valid_operation() {
(op == Operation::Sqrt && cpp::is_floating_point_v<InputType> &&
cpp::is_floating_point_v<OutputType> &&
sizeof(OutputType) <= sizeof(InputType)) ||
- (op == Operation::Div && internal::IsBinaryInput<InputType>::VALUE &&
+ (Operation::BeginBinaryOperationsSingleOutput < op &&
+ op < Operation::EndBinaryOperationsSingleOutput &&
+ internal::IsBinaryInput<InputType>::VALUE &&
cpp::is_floating_point_v<
typename internal::MakeScalarInput<InputType>::type> &&
cpp::is_floating_point_v<OutputType>) ||
>From 278b3ef950d4e960060ddff13c8213f35cab5869 Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Wed, 26 Jun 2024 16:00:00 +0200
Subject: [PATCH 02/11] [libc][math][c23] Add f16subf C23 math function
---
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/docs/math/index.rst | 2 +
libc/spec/stdc.td | 2 +
.../__support/FPUtil/generic/CMakeLists.txt | 4 +-
.../FPUtil/generic/{add.h => add_sub.h} | 53 ++++--
libc/src/math/CMakeLists.txt | 2 +
libc/src/math/f16subf.h | 20 +++
libc/src/math/generic/CMakeLists.txt | 15 +-
libc/src/math/generic/f16addf.cpp | 2 +-
libc/src/math/generic/f16subf.cpp | 19 +++
libc/test/src/math/CMakeLists.txt | 13 ++
libc/test/src/math/SubTest.h | 73 ++++++++
libc/test/src/math/f16subf_test.cpp | 13 ++
libc/test/src/math/smoke/CMakeLists.txt | 14 ++
libc/test/src/math/smoke/SubTest.h | 158 ++++++++++++++++++
libc/test/src/math/smoke/f16subf_test.cpp | 13 ++
libc/utils/MPFRWrapper/MPFRUtils.cpp | 8 +
libc/utils/MPFRWrapper/MPFRUtils.h | 1 +
19 files changed, 396 insertions(+), 18 deletions(-)
rename libc/src/__support/FPUtil/generic/{add.h => add_sub.h} (78%)
create mode 100644 libc/src/math/f16subf.h
create mode 100644 libc/src/math/generic/f16subf.cpp
create mode 100644 libc/test/src/math/SubTest.h
create mode 100644 libc/test/src/math/f16subf_test.cpp
create mode 100644 libc/test/src/math/smoke/SubTest.h
create mode 100644 libc/test/src/math/smoke/f16subf_test.cpp
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index a40a4baaacb15..79fa361b15190 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -510,6 +510,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.f16divf
libc.src.math.f16fmaf
libc.src.math.f16sqrtf
+ libc.src.math.f16subf
libc.src.math.fabsf16
libc.src.math.fdimf16
libc.src.math.floorf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 18d9812b7e6fa..d94a40705b107 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -540,6 +540,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.f16divf
libc.src.math.f16fmaf
libc.src.math.f16sqrtf
+ libc.src.math.f16subf
libc.src.math.fabsf16
libc.src.math.fdimf16
libc.src.math.floorf16
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index 9162b7ff74abe..6f2b6f01e190c 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -130,6 +130,8 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| f16fma | |check| | | | N/A | | 7.12.14.5 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
+| f16sub | |check| | | | N/A | | 7.12.14.2 | F.10.11 |
++------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| fabs | |check| | |check| | |check| | |check| | |check| | 7.12.7.3 | F.10.4.3 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| fadd | N/A | | | N/A | | 7.12.14.1 | F.10.11 |
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index a59db77aef3b9..39ea8ff68bb4f 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -728,6 +728,8 @@ def StdC : StandardSpec<"stdc"> {
GuardedFunctionSpec<"f16addf", RetValSpec<Float16Type>, [ArgSpec<FloatType>, ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
+ GuardedFunctionSpec<"f16subf", RetValSpec<Float16Type>, [ArgSpec<FloatType>, ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
+
GuardedFunctionSpec<"f16divf", RetValSpec<Float16Type>, [ArgSpec<FloatType>, ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"f16sqrtf", RetValSpec<Float16Type>, [ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
diff --git a/libc/src/__support/FPUtil/generic/CMakeLists.txt b/libc/src/__support/FPUtil/generic/CMakeLists.txt
index 37968eb847aaa..5b8e09e5a47b4 100644
--- a/libc/src/__support/FPUtil/generic/CMakeLists.txt
+++ b/libc/src/__support/FPUtil/generic/CMakeLists.txt
@@ -47,9 +47,9 @@ add_header_library(
)
add_header_library(
- add
+ add_sub
HDRS
- add.h
+ add_sub.h
DEPENDS
libc.hdr.errno_macros
libc.hdr.fenv_macros
diff --git a/libc/src/__support/FPUtil/generic/add.h b/libc/src/__support/FPUtil/generic/add_sub.h
similarity index 78%
rename from libc/src/__support/FPUtil/generic/add.h
rename to libc/src/__support/FPUtil/generic/add_sub.h
index a987c38236396..1a632ff262408 100644
--- a/libc/src/__support/FPUtil/generic/add.h
+++ b/libc/src/__support/FPUtil/generic/add_sub.h
@@ -1,4 +1,4 @@
-//===-- Addition of IEEE 754 floating-point numbers -------------*- C++ -*-===//
+//===-- Add and subtract IEEE 754 floating-point numbers --------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
@@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_ADD_H
-#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_ADD_H
+#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_ADD_SUB_H
+#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_ADD_SUB_H
#include "hdr/errno_macros.h"
#include "hdr/fenv_macros.h"
@@ -24,12 +24,12 @@
namespace LIBC_NAMESPACE::fputil::generic {
-template <typename OutType, typename InType>
+template <bool IsSub, typename OutType, typename InType>
LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<OutType> &&
cpp::is_floating_point_v<InType> &&
sizeof(OutType) <= sizeof(InType),
OutType>
-add(InType x, InType y) {
+add_or_sub(InType x, InType y) {
using OutFPBits = FPBits<OutType>;
using OutStorageType = typename OutFPBits::StorageType;
using InFPBits = FPBits<InType>;
@@ -45,6 +45,8 @@ add(InType x, InType y) {
InFPBits x_bits(x);
InFPBits y_bits(y);
+ bool is_effectively_add = (x_bits.sign() == y_bits.sign()) ^ IsSub;
+
if (LIBC_UNLIKELY(x_bits.is_inf_or_nan() || y_bits.is_inf_or_nan() ||
x_bits.is_zero() || y_bits.is_zero())) {
if (x_bits.is_nan() || y_bits.is_nan()) {
@@ -72,7 +74,7 @@ add(InType x, InType y) {
if (x_bits.is_inf()) {
if (y_bits.is_inf()) {
- if (x_bits.sign() != y_bits.sign()) {
+ if (!is_effectively_add) {
raise_except_if_required(FE_INVALID);
return OutFPBits::quiet_nan().get_val();
}
@@ -106,7 +108,7 @@ add(InType x, InType y) {
InType x_abs = x_bits.abs().get_val();
InType y_abs = y_bits.abs().get_val();
- if (x_abs == y_abs && x_bits.sign() != y_bits.sign()) {
+ if (x_abs == y_abs && !is_effectively_add) {
switch (quick_get_round()) {
case FE_DOWNWARD:
return OutFPBits::zero(Sign::NEG).get_val();
@@ -117,12 +119,16 @@ add(InType x, InType y) {
Sign result_sign = Sign::POS;
- if (x_abs > y_abs)
+ if (x_abs > y_abs) {
result_sign = x_bits.sign();
- else if (x_abs < y_abs)
- result_sign = y_bits.sign();
- else if (x_bits.sign() == y_bits.sign())
+ } else if (x_abs < y_abs) {
+ if (is_effectively_add)
+ result_sign = y_bits.sign();
+ else if (y_bits.is_pos())
+ result_sign = Sign::NEG;
+ } else if (is_effectively_add) {
result_sign = x_bits.sign();
+ }
InFPBits max_bits(cpp::max(x_abs, y_abs));
InFPBits min_bits(cpp::min(x_abs, y_abs));
@@ -131,7 +137,8 @@ add(InType x, InType y) {
if (max_bits.is_subnormal()) {
// min_bits must be subnormal too.
- if (max_bits.sign() == min_bits.sign())
+
+ if (is_effectively_add)
result_mant = max_bits.get_mantissa() + min_bits.get_mantissa();
else
result_mant = max_bits.get_mantissa() - min_bits.get_mantissa();
@@ -155,7 +162,7 @@ add(InType x, InType y) {
else
aligned_min_mant_sticky = true;
- if (max_bits.sign() == min_bits.sign())
+ if (is_effectively_add)
result_mant = max_mant + (aligned_min_mant | aligned_min_mant_sticky);
else
result_mant = max_mant - (aligned_min_mant | aligned_min_mant_sticky);
@@ -166,6 +173,24 @@ add(InType x, InType y) {
return result.template as<OutType, /*ShouldSignalExceptions=*/true>();
}
+template <typename OutType, typename InType>
+LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<OutType> &&
+ cpp::is_floating_point_v<InType> &&
+ sizeof(OutType) <= sizeof(InType),
+ OutType>
+add(InType x, InType y) {
+ return add_or_sub</*IsSub=*/false, OutType>(x, y);
+}
+
+template <typename OutType, typename InType>
+LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<OutType> &&
+ cpp::is_floating_point_v<InType> &&
+ sizeof(OutType) <= sizeof(InType),
+ OutType>
+sub(InType x, InType y) {
+ return add_or_sub</*IsSub=*/true, OutType>(x, y);
+}
+
} // namespace LIBC_NAMESPACE::fputil::generic
-#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_ADD_H
+#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_ADD_SUB_H
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index bdffff8c8b7bb..d3af54194b567 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -107,6 +107,8 @@ add_math_entrypoint_object(f16fmaf)
add_math_entrypoint_object(f16sqrtf)
+add_math_entrypoint_object(f16subf)
+
add_math_entrypoint_object(fabs)
add_math_entrypoint_object(fabsf)
add_math_entrypoint_object(fabsl)
diff --git a/libc/src/math/f16subf.h b/libc/src/math/f16subf.h
new file mode 100644
index 0000000000000..1d04a4c952d17
--- /dev/null
+++ b/libc/src/math/f16subf.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for f16subf -----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_F16SUBF_H
+#define LLVM_LIBC_SRC_MATH_F16SUBF_H
+
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE {
+
+float16 f16subf(float x, float y);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_F16SUBF_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 252dadee63a19..a143730aaf9bb 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3762,7 +3762,20 @@ add_entrypoint_object(
../f16addf.h
DEPENDS
libc.src.__support.macros.properties.types
- libc.src.__support.FPUtil.generic.add
+ libc.src.__support.FPUtil.generic.add_sub
+ COMPILE_OPTIONS
+ -O3
+)
+
+add_entrypoint_object(
+ f16subf
+ SRCS
+ f16subf.cpp
+ HDRS
+ ../f16subf.h
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.generic.add_sub
COMPILE_OPTIONS
-O3
)
diff --git a/libc/src/math/generic/f16addf.cpp b/libc/src/math/generic/f16addf.cpp
index de2f666cdc9fa..f1761a193985d 100644
--- a/libc/src/math/generic/f16addf.cpp
+++ b/libc/src/math/generic/f16addf.cpp
@@ -7,7 +7,7 @@
//===----------------------------------------------------------------------===//
#include "src/math/f16addf.h"
-#include "src/__support/FPUtil/generic/add.h"
+#include "src/__support/FPUtil/generic/add_sub.h"
#include "src/__support/common.h"
namespace LIBC_NAMESPACE {
diff --git a/libc/src/math/generic/f16subf.cpp b/libc/src/math/generic/f16subf.cpp
new file mode 100644
index 0000000000000..e4532a456b7b8
--- /dev/null
+++ b/libc/src/math/generic/f16subf.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of f16subf function --------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/f16subf.h"
+#include "src/__support/FPUtil/generic/add_sub.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float16, f16subf, (float x, float y)) {
+ return fputil::generic::sub<float16>(x, y);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 277ab2236a615..77ba0b0bcaacd 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -1903,6 +1903,19 @@ add_fp_unittest(
libc.src.math.f16addf
)
+add_fp_unittest(
+ f16subf_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ f16subf_test.cpp
+ HDRS
+ SubTest.h
+ DEPENDS
+ libc.src.math.f16subf
+)
+
add_fp_unittest(
f16divf_test
NEED_MPFR
diff --git a/libc/test/src/math/SubTest.h b/libc/test/src/math/SubTest.h
new file mode 100644
index 0000000000000..2d1e475a9b538
--- /dev/null
+++ b/libc/test/src/math/SubTest.h
@@ -0,0 +1,73 @@
+//===-- Utility class to test different flavors of float sub ----*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_TEST_SRC_MATH_SUBTEST_H
+#define LLVM_LIBC_TEST_SRC_MATH_SUBTEST_H
+
+#include "test/UnitTest/FEnvSafeTest.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+template <typename OutType, typename InType>
+class SubTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+
+ struct InConstants {
+ DECLARE_SPECIAL_CONSTANTS(InType)
+ };
+
+ using InFPBits = typename InConstants::FPBits;
+ using InStorageType = typename InConstants::StorageType;
+
+ static constexpr InStorageType IN_MAX_NORMAL_U =
+ InFPBits::max_normal().uintval();
+ static constexpr InStorageType IN_MIN_NORMAL_U =
+ InFPBits::min_normal().uintval();
+ static constexpr InStorageType IN_MAX_SUBNORMAL_U =
+ InFPBits::max_subnormal().uintval();
+ static constexpr InStorageType IN_MIN_SUBNORMAL_U =
+ InFPBits::min_subnormal().uintval();
+
+public:
+ typedef OutType (*AddFunc)(InType, InType);
+
+ void test_subnormal_range(AddFunc func) {
+ constexpr InStorageType STEP =
+ (IN_MAX_SUBNORMAL_U - IN_MIN_SUBNORMAL_U) / COUNT;
+ for (InStorageType i = 0, v = 0, w = IN_MAX_SUBNORMAL_U; i <= COUNT;
+ ++i, v += STEP, w -= STEP) {
+ InType x = InFPBits(v).get_val();
+ InType y = InFPBits(w).get_val();
+ mpfr::BinaryInput<InType> input{x, y};
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sub, input, func(x, y),
+ 0.5);
+ }
+ }
+
+ void test_normal_range(AddFunc func) {
+ constexpr InStorageType COUNT = 100'001;
+ constexpr InStorageType STEP = (IN_MAX_NORMAL_U - IN_MIN_NORMAL_U) / COUNT;
+ for (InStorageType i = 0, v = 0, w = IN_MAX_NORMAL_U; i <= COUNT;
+ ++i, v += STEP, w -= STEP) {
+ InType x = InFPBits(v).get_val();
+ InType y = InFPBits(w).get_val();
+ mpfr::BinaryInput<InType> input{x, y};
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sub, input, func(x, y),
+ 0.5);
+ }
+ }
+};
+
+#define LIST_SUB_TESTS(OutType, InType, func) \
+ using LlvmLibcSubTest = SubTest<OutType, InType>; \
+ TEST_F(LlvmLibcSubTest, SubnormalRange) { test_subnormal_range(&func); } \
+ TEST_F(LlvmLibcSubTest, NormalRange) { test_normal_range(&func); }
+
+#endif // LLVM_LIBC_TEST_SRC_MATH_ADDTEST_H
diff --git a/libc/test/src/math/f16subf_test.cpp b/libc/test/src/math/f16subf_test.cpp
new file mode 100644
index 0000000000000..68ad9482ab1e4
--- /dev/null
+++ b/libc/test/src/math/f16subf_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for f16subf ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "SubTest.h"
+
+#include "src/math/f16subf.h"
+
+LIST_SUB_TESTS(float16, float, LIBC_NAMESPACE::f16subf)
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 5f52da1d2222c..5df03edfadcdb 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3645,6 +3645,20 @@ add_fp_unittest(
libc.src.math.f16addf
)
+add_fp_unittest(
+ f16subf_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ f16subf_test.cpp
+ HDRS
+ SubTest.h
+ DEPENDS
+ libc.hdr.fenv_macros
+ libc.src.__support.FPUtil.basic_operations
+ libc.src.math.f16subf
+)
+
add_fp_unittest(
f16divf_test
SUITE
diff --git a/libc/test/src/math/smoke/SubTest.h b/libc/test/src/math/smoke/SubTest.h
new file mode 100644
index 0000000000000..b7d62b109b8a9
--- /dev/null
+++ b/libc/test/src/math/smoke/SubTest.h
@@ -0,0 +1,158 @@
+//===-- Utility class to test different flavors of float sub ----*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_TEST_SRC_MATH_SMOKE_SUBTEST_H
+#define LLVM_LIBC_TEST_SRC_MATH_SMOKE_SUBTEST_H
+
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "test/UnitTest/FEnvSafeTest.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+template <typename OutType, typename InType>
+class SubTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+
+ DECLARE_SPECIAL_CONSTANTS(OutType)
+
+ struct InConstants {
+ DECLARE_SPECIAL_CONSTANTS(InType)
+ };
+
+ using InFPBits = typename InConstants::FPBits;
+ using InStorageType = typename InConstants::StorageType;
+
+ InConstants in;
+
+public:
+ using SubFunc = OutType (*)(InType, InType);
+
+ void test_special_numbers(SubFunc func) {
+ EXPECT_FP_IS_NAN(func(aNaN, aNaN));
+ EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(sNaN, sNaN), FE_INVALID);
+
+ InType qnan_42 = InFPBits::quiet_nan(Sign::POS, 0x42).get_val();
+ EXPECT_FP_EQ(InType(0x42.0p+0),
+ LIBC_NAMESPACE::fputil::getpayload(func(qnan_42, zero)));
+ EXPECT_FP_EQ(InType(0x42.0p+0),
+ LIBC_NAMESPACE::fputil::getpayload(func(zero, qnan_42)));
+
+ if constexpr (sizeof(OutType) < sizeof(InType)) {
+ InStorageType max_payload = InFPBits::FRACTION_MASK >> 1;
+ InType qnan_max = InFPBits::quiet_nan(Sign::POS, max_payload).get_val();
+ EXPECT_FP_EQ(zero,
+ LIBC_NAMESPACE::fputil::getpayload(func(qnan_max, zero)));
+ EXPECT_FP_EQ(zero,
+ LIBC_NAMESPACE::fputil::getpayload(func(zero, qnan_max)));
+ EXPECT_FP_EQ(InType(0x42.0p+0),
+ LIBC_NAMESPACE::fputil::getpayload(func(qnan_max, qnan_42)));
+ EXPECT_FP_EQ(InType(0x42.0p+0),
+ LIBC_NAMESPACE::fputil::getpayload(func(qnan_42, qnan_max)));
+ }
+
+ EXPECT_FP_EQ(inf, func(inf, zero));
+ EXPECT_FP_EQ(neg_inf, func(neg_inf, zero));
+ EXPECT_FP_EQ(inf, func(inf, neg_zero));
+ EXPECT_FP_EQ(neg_inf, func(neg_inf, neg_zero));
+ }
+
+ void test_invalid_operations(SubFunc func) {
+ EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(inf, inf), FE_INVALID);
+ EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(neg_inf, neg_inf), FE_INVALID);
+ }
+
+ void test_range_errors(SubFunc func) {
+ using namespace LIBC_NAMESPACE::fputil::testing;
+
+ if (ForceRoundingMode r(RoundingMode::Nearest); r.success) {
+ EXPECT_FP_EQ_WITH_EXCEPTION(inf, func(max_normal, neg_max_normal),
+ FE_OVERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(ERANGE);
+ EXPECT_FP_EQ_WITH_EXCEPTION(-inf, func(neg_max_normal, max_normal),
+ FE_OVERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(ERANGE);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(zero,
+ func(in.min_denormal, in.neg_min_denormal),
+ FE_UNDERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(ERANGE);
+ EXPECT_FP_EQ_WITH_EXCEPTION(neg_zero,
+ func(in.neg_min_denormal, in.min_denormal),
+ FE_UNDERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(ERANGE);
+ }
+
+ if (ForceRoundingMode r(RoundingMode::TowardZero); r.success) {
+ EXPECT_FP_EQ_WITH_EXCEPTION(max_normal, func(max_normal, neg_max_normal),
+ FE_OVERFLOW | FE_INEXACT);
+ EXPECT_FP_EQ_WITH_EXCEPTION(neg_max_normal,
+ func(neg_max_normal, max_normal),
+ FE_OVERFLOW | FE_INEXACT);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(zero,
+ func(in.min_denormal, in.neg_min_denormal),
+ FE_UNDERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(ERANGE);
+ EXPECT_FP_EQ_WITH_EXCEPTION(neg_zero,
+ func(in.neg_min_denormal, in.min_denormal),
+ FE_UNDERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(ERANGE);
+ }
+
+ if (ForceRoundingMode r(RoundingMode::Downward); r.success) {
+ EXPECT_FP_EQ_WITH_EXCEPTION(max_normal, func(max_normal, neg_max_normal),
+ FE_OVERFLOW | FE_INEXACT);
+ EXPECT_FP_EQ_WITH_EXCEPTION(-inf, func(neg_max_normal, max_normal),
+ FE_OVERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(ERANGE);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(zero,
+ func(in.min_denormal, in.neg_min_denormal),
+ FE_UNDERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(ERANGE);
+ EXPECT_FP_EQ_WITH_EXCEPTION(neg_min_denormal,
+ func(in.neg_min_denormal, in.min_denormal),
+ FE_UNDERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(ERANGE);
+ }
+
+ if (ForceRoundingMode r(RoundingMode::Upward); r.success) {
+ EXPECT_FP_EQ_WITH_EXCEPTION(inf, func(max_normal, neg_max_normal),
+ FE_OVERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(ERANGE);
+ EXPECT_FP_EQ_WITH_EXCEPTION(neg_max_normal,
+ func(neg_max_normal, max_normal),
+ FE_OVERFLOW | FE_INEXACT);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(min_denormal,
+ func(in.min_denormal, in.neg_min_denormal),
+ FE_UNDERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(ERANGE);
+ EXPECT_FP_EQ_WITH_EXCEPTION(neg_zero,
+ func(in.neg_min_denormal, in.min_denormal),
+ FE_UNDERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(ERANGE);
+ }
+ }
+
+ void test_inexact_results(SubFunc func) {
+ func(InType(1.0), min_denormal);
+ EXPECT_FP_EXCEPTION(FE_INEXACT);
+ }
+};
+
+#define LIST_SUB_TESTS(OutType, InType, func) \
+ using LlvmLibcSubTest = SubTest<OutType, InType>; \
+ TEST_F(LlvmLibcSubTest, SpecialNumbers) { test_special_numbers(&func); } \
+ TEST_F(LlvmLibcSubTest, InvalidOperations) { \
+ test_invalid_operations(&func); \
+ } \
+ TEST_F(LlvmLibcSubTest, RangeErrors) { test_range_errors(&func); } \
+ TEST_F(LlvmLibcSubTest, InexactResults) { test_inexact_results(&func); }
+
+#endif // LLVM_LIBC_TEST_SRC_MATH_SMOKE_ADDTEST_H
diff --git a/libc/test/src/math/smoke/f16subf_test.cpp b/libc/test/src/math/smoke/f16subf_test.cpp
new file mode 100644
index 0000000000000..68ad9482ab1e4
--- /dev/null
+++ b/libc/test/src/math/smoke/f16subf_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for f16subf ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "SubTest.h"
+
+#include "src/math/f16subf.h"
+
+LIST_SUB_TESTS(float16, float, LIBC_NAMESPACE::f16subf)
diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp
index f42c2e3f571a8..c0107b573168f 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -455,6 +455,12 @@ class MPFRNumber {
return result;
}
+ MPFRNumber sub(const MPFRNumber &b) const {
+ MPFRNumber result(*this);
+ mpfr_sub(result.value, value, b.value, mpfr_rounding);
+ return result;
+ }
+
MPFRNumber tan() const {
MPFRNumber result(*this);
mpfr_tan(result.value, value, mpfr_rounding);
@@ -730,6 +736,8 @@ binary_operation_one_output(Operation op, InputType x, InputType y,
return inputX.hypot(inputY);
case Operation::Pow:
return inputX.pow(inputY);
+ case Operation::Sub:
+ return inputX.sub(inputY);
default:
__builtin_unreachable();
}
diff --git a/libc/utils/MPFRWrapper/MPFRUtils.h b/libc/utils/MPFRWrapper/MPFRUtils.h
index e96e5fd4795ef..d8f96d4cfe6e4 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.h
+++ b/libc/utils/MPFRWrapper/MPFRUtils.h
@@ -76,6 +76,7 @@ enum class Operation : int {
Fmod,
Hypot,
Pow,
+ Sub,
EndBinaryOperationsSingleOutput,
// Operations which take two floating point numbers of the same type as
>From 8d22a0fa5a80689d1506752fb3b84af117071eb6 Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Thu, 27 Jun 2024 15:28:18 +0200
Subject: [PATCH 03/11] fixup! [libc][math][c23] Add f16subf C23 math function
---
libc/test/src/math/SubTest.h | 9 +++++----
libc/test/src/math/smoke/SubTest.h | 2 +-
2 files changed, 6 insertions(+), 5 deletions(-)
diff --git a/libc/test/src/math/SubTest.h b/libc/test/src/math/SubTest.h
index 2d1e475a9b538..9b4035344b468 100644
--- a/libc/test/src/math/SubTest.h
+++ b/libc/test/src/math/SubTest.h
@@ -36,9 +36,10 @@ class SubTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
InFPBits::min_subnormal().uintval();
public:
- typedef OutType (*AddFunc)(InType, InType);
+ using SubFunc = OutType (*)(InType, InType);
- void test_subnormal_range(AddFunc func) {
+ void test_subnormal_range(SubFunc func) {
+ constexpr InStorageType COUNT = 100'001;
constexpr InStorageType STEP =
(IN_MAX_SUBNORMAL_U - IN_MIN_SUBNORMAL_U) / COUNT;
for (InStorageType i = 0, v = 0, w = IN_MAX_SUBNORMAL_U; i <= COUNT;
@@ -51,7 +52,7 @@ class SubTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
}
}
- void test_normal_range(AddFunc func) {
+ void test_normal_range(SubFunc func) {
constexpr InStorageType COUNT = 100'001;
constexpr InStorageType STEP = (IN_MAX_NORMAL_U - IN_MIN_NORMAL_U) / COUNT;
for (InStorageType i = 0, v = 0, w = IN_MAX_NORMAL_U; i <= COUNT;
@@ -70,4 +71,4 @@ class SubTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
TEST_F(LlvmLibcSubTest, SubnormalRange) { test_subnormal_range(&func); } \
TEST_F(LlvmLibcSubTest, NormalRange) { test_normal_range(&func); }
-#endif // LLVM_LIBC_TEST_SRC_MATH_ADDTEST_H
+#endif // LLVM_LIBC_TEST_SRC_MATH_SUBTEST_H
diff --git a/libc/test/src/math/smoke/SubTest.h b/libc/test/src/math/smoke/SubTest.h
index b7d62b109b8a9..a4b3822aca43c 100644
--- a/libc/test/src/math/smoke/SubTest.h
+++ b/libc/test/src/math/smoke/SubTest.h
@@ -155,4 +155,4 @@ class SubTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
TEST_F(LlvmLibcSubTest, RangeErrors) { test_range_errors(&func); } \
TEST_F(LlvmLibcSubTest, InexactResults) { test_inexact_results(&func); }
-#endif // LLVM_LIBC_TEST_SRC_MATH_SMOKE_ADDTEST_H
+#endif // LLVM_LIBC_TEST_SRC_MATH_SMOKE_SUBTEST_H
>From 9cc01df1c2252bfddbcdc1278802c83d279f1b66 Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Thu, 27 Jun 2024 15:29:10 +0200
Subject: [PATCH 04/11] fixup! [libc][math][c23] Add f16subf C23 math function
Fix conversion to output type.
---
libc/src/__support/FPUtil/dyadic_float.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libc/src/__support/FPUtil/dyadic_float.h b/libc/src/__support/FPUtil/dyadic_float.h
index 1e1bec676d444..fb1b22467f940 100644
--- a/libc/src/__support/FPUtil/dyadic_float.h
+++ b/libc/src/__support/FPUtil/dyadic_float.h
@@ -156,13 +156,13 @@ template <size_t Bits> struct DyadicFloat {
// d_lo is denormal, but the output is normal.
int scale_up_exponent = 1 - exp_lo;
T scale_up_factor =
- FPBits<T>::create_value(sign,
+ FPBits<T>::create_value(Sign::POS,
static_cast<output_bits_t>(
FPBits<T>::EXP_BIAS + scale_up_exponent),
IMPLICIT_MASK)
.get_val();
T scale_down_factor =
- FPBits<T>::create_value(sign,
+ FPBits<T>::create_value(Sign::POS,
static_cast<output_bits_t>(
FPBits<T>::EXP_BIAS - scale_up_exponent),
IMPLICIT_MASK)
>From 2219e9ef624d58f2f20316c98d953eb21087a977 Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Fri, 28 Jun 2024 14:35:07 +0200
Subject: [PATCH 05/11] fixup! [libc][math][c23] Add f16subf C23 math function
Fix double rounding error.
---
libc/src/__support/FPUtil/generic/add_sub.h | 16 +++++++++++++---
1 file changed, 13 insertions(+), 3 deletions(-)
diff --git a/libc/src/__support/FPUtil/generic/add_sub.h b/libc/src/__support/FPUtil/generic/add_sub.h
index 1a632ff262408..6d640026e2927 100644
--- a/libc/src/__support/FPUtil/generic/add_sub.h
+++ b/libc/src/__support/FPUtil/generic/add_sub.h
@@ -98,11 +98,21 @@ add_or_sub(InType x, InType y) {
}
}
- return static_cast<OutType>(y);
+ // volatile prevents Clang from converting tmp to OutType and then
+ // immediately back to InType before negating it, resulting in double
+ // rounding.
+ volatile InType tmp = y;
+ if constexpr (IsSub)
+ tmp = -tmp;
+ return static_cast<OutType>(tmp);
}
- if (y_bits.is_zero())
- return static_cast<OutType>(x);
+ if (y_bits.is_zero()) {
+ volatile InType tmp = y;
+ if constexpr (IsSub)
+ tmp = -tmp;
+ return static_cast<OutType>(tmp);
+ }
}
InType x_abs = x_bits.abs().get_val();
>From c64e25dfce6536e966b86cef37bf28a55277ac2c Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Fri, 28 Jun 2024 16:56:43 +0200
Subject: [PATCH 06/11] [libc][math][c23] Add f16add C23 math function
---
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/docs/math/index.rst | 2 +-
libc/spec/stdc.td | 1 +
libc/src/math/CMakeLists.txt | 1 +
libc/src/math/f16add.h | 20 ++++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 13 +++++++++++++
libc/src/math/generic/f16add.cpp | 19 +++++++++++++++++++
libc/test/src/math/CMakeLists.txt | 13 +++++++++++++
libc/test/src/math/f16add_test.cpp | 13 +++++++++++++
libc/test/src/math/smoke/CMakeLists.txt | 15 +++++++++++++++
libc/test/src/math/smoke/f16add_test.cpp | 13 +++++++++++++
libc/utils/MPFRWrapper/MPFRUtils.cpp | 6 ++++++
13 files changed, 117 insertions(+), 1 deletion(-)
create mode 100644 libc/src/math/f16add.h
create mode 100644 libc/src/math/generic/f16add.cpp
create mode 100644 libc/test/src/math/f16add_test.cpp
create mode 100644 libc/test/src/math/smoke/f16add_test.cpp
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index f8ac6af945a39..032c7c4030388 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -507,6 +507,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.canonicalizef16
libc.src.math.ceilf16
libc.src.math.copysignf16
+ libc.src.math.f16add
libc.src.math.f16addf
libc.src.math.f16divf
libc.src.math.f16fmaf
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index f7edb42ab6109..757e9d854f36f 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -537,6 +537,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.canonicalizef16
libc.src.math.ceilf16
libc.src.math.copysignf16
+ libc.src.math.f16add
libc.src.math.f16addf
libc.src.math.f16divf
libc.src.math.f16fma
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index 68236575ef7c9..2dff2e4b85f6c 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -124,7 +124,7 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| dsub | N/A | N/A | | N/A | | 7.12.14.2 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| f16add | |check| | | | N/A | | 7.12.14.1 | F.10.11 |
+| f16add | |check| | |check| | | N/A | | 7.12.14.1 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| f16div | |check| | | | N/A | | 7.12.14.4 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 4aa892c3e696f..300aadd295906 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -729,6 +729,7 @@ def StdC : StandardSpec<"stdc"> {
GuardedFunctionSpec<"setpayloadsigf16", RetValSpec<IntType>, [ArgSpec<Float16Ptr>, ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
+ GuardedFunctionSpec<"f16add", RetValSpec<Float16Type>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"f16addf", RetValSpec<Float16Type>, [ArgSpec<FloatType>, ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"f16subf", RetValSpec<Float16Type>, [ArgSpec<FloatType>, ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index cfaeca1b0d892..222dac702a375 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -99,6 +99,7 @@ add_math_entrypoint_object(exp10f)
add_math_entrypoint_object(expm1)
add_math_entrypoint_object(expm1f)
+add_math_entrypoint_object(f16add)
add_math_entrypoint_object(f16addf)
add_math_entrypoint_object(f16divf)
diff --git a/libc/src/math/f16add.h b/libc/src/math/f16add.h
new file mode 100644
index 0000000000000..763a0787d860d
--- /dev/null
+++ b/libc/src/math/f16add.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for f16add ------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_F16ADD_H
+#define LLVM_LIBC_SRC_MATH_F16ADD_H
+
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE {
+
+float16 f16add(double x, double y);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_F16ADD_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 6a9542bc4b438..2b30b8c41b71a 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3776,6 +3776,19 @@ add_entrypoint_object(
-O3
)
+add_entrypoint_object(
+ f16add
+ SRCS
+ f16add.cpp
+ HDRS
+ ../f16add.h
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.generic.add_sub
+ COMPILE_OPTIONS
+ -O3
+)
+
add_entrypoint_object(
f16addf
SRCS
diff --git a/libc/src/math/generic/f16add.cpp b/libc/src/math/generic/f16add.cpp
new file mode 100644
index 0000000000000..ef9b43e9f46aa
--- /dev/null
+++ b/libc/src/math/generic/f16add.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of f16add function ---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/f16add.h"
+#include "src/__support/FPUtil/generic/add_sub.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float16, f16add, (double x, double y)) {
+ return fputil::generic::add<float16>(x, y);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 7a46b1b5dadc4..321555d29915f 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -1902,6 +1902,19 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
+add_fp_unittest(
+ f16add_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ f16add_test.cpp
+ HDRS
+ AddTest.h
+ DEPENDS
+ libc.src.math.f16add
+)
+
add_fp_unittest(
f16addf_test
NEED_MPFR
diff --git a/libc/test/src/math/f16add_test.cpp b/libc/test/src/math/f16add_test.cpp
new file mode 100644
index 0000000000000..c47ece2a92555
--- /dev/null
+++ b/libc/test/src/math/f16add_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for f16add ----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "AddTest.h"
+
+#include "src/math/f16add.h"
+
+LIST_ADD_TESTS(float16, double, LIBC_NAMESPACE::f16add)
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index f4afadd5e891d..83d288236d8d1 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3630,6 +3630,21 @@ add_fp_unittest(
libc.src.math.setpayloadsigf16
)
+add_fp_unittest(
+ f16add_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ f16add_test.cpp
+ HDRS
+ AddTest.h
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.fenv_macros
+ libc.src.__support.FPUtil.basic_operations
+ libc.src.math.f16add
+)
+
add_fp_unittest(
f16addf_test
SUITE
diff --git a/libc/test/src/math/smoke/f16add_test.cpp b/libc/test/src/math/smoke/f16add_test.cpp
new file mode 100644
index 0000000000000..c47ece2a92555
--- /dev/null
+++ b/libc/test/src/math/smoke/f16add_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for f16add ----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "AddTest.h"
+
+#include "src/math/f16add.h"
+
+LIST_ADD_TESTS(float16, double, LIBC_NAMESPACE::f16add)
diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp
index a325d874e46ad..1cd2dc4597c98 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -951,6 +951,8 @@ template void explain_binary_operation_one_output_error(
template void
explain_binary_operation_one_output_error(Operation, const BinaryInput<float> &,
float16, double, RoundingMode);
+template void explain_binary_operation_one_output_error(
+ Operation, const BinaryInput<double> &, float16, double, RoundingMode);
#endif
template <typename InputType, typename OutputType>
@@ -1120,6 +1122,10 @@ template bool compare_binary_operation_one_output(Operation,
const BinaryInput<float> &,
float16, double,
RoundingMode);
+template bool compare_binary_operation_one_output(Operation,
+ const BinaryInput<double> &,
+ float16, double,
+ RoundingMode);
#endif
template <typename InputType, typename OutputType>
>From 574c5b22b709e100b2d787dce9e8881adbc6c146 Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Fri, 28 Jun 2024 17:01:13 +0200
Subject: [PATCH 07/11] [libc][math][c23] Add f16addf128 C23 math function
---
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/docs/math/index.rst | 2 +-
libc/spec/stdc.td | 1 +
libc/src/math/CMakeLists.txt | 1 +
libc/src/math/f16addf128.h | 20 ++++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 13 +++++++++++++
libc/src/math/generic/f16addf128.cpp | 19 +++++++++++++++++++
libc/test/src/math/smoke/CMakeLists.txt | 15 +++++++++++++++
libc/test/src/math/smoke/f16addf128_test.cpp | 13 +++++++++++++
9 files changed, 84 insertions(+), 1 deletion(-)
create mode 100644 libc/src/math/f16addf128.h
create mode 100644 libc/src/math/generic/f16addf128.cpp
create mode 100644 libc/test/src/math/smoke/f16addf128_test.cpp
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 757e9d854f36f..560ce065e1b06 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -597,6 +597,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
if(LIBC_TYPES_HAS_FLOAT128)
list(APPEND TARGET_LIBM_ENTRYPOINTS
# math.h C23 mixed _Float16 and _Float128 entrypoints
+ libc.src.math.f16addf128
libc.src.math.f16fmaf128
)
endif()
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index 2dff2e4b85f6c..e72cbebf0ed7e 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -124,7 +124,7 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| dsub | N/A | N/A | | N/A | | 7.12.14.2 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| f16add | |check| | |check| | | N/A | | 7.12.14.1 | F.10.11 |
+| f16add | |check| | |check| | | N/A | |check| | 7.12.14.1 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| f16div | |check| | | | N/A | | 7.12.14.4 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 300aadd295906..29a6a4cc9a9ae 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -731,6 +731,7 @@ def StdC : StandardSpec<"stdc"> {
GuardedFunctionSpec<"f16add", RetValSpec<Float16Type>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"f16addf", RetValSpec<Float16Type>, [ArgSpec<FloatType>, ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
+ GuardedFunctionSpec<"f16addf128", RetValSpec<Float16Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128">,
GuardedFunctionSpec<"f16subf", RetValSpec<Float16Type>, [ArgSpec<FloatType>, ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 222dac702a375..45fd118803318 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -101,6 +101,7 @@ add_math_entrypoint_object(expm1f)
add_math_entrypoint_object(f16add)
add_math_entrypoint_object(f16addf)
+add_math_entrypoint_object(f16addf128)
add_math_entrypoint_object(f16divf)
diff --git a/libc/src/math/f16addf128.h b/libc/src/math/f16addf128.h
new file mode 100644
index 0000000000000..284ce1d303775
--- /dev/null
+++ b/libc/src/math/f16addf128.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for f16addf128 --------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_F16ADDF128_H
+#define LLVM_LIBC_SRC_MATH_F16ADDF128_H
+
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE {
+
+float16 f16addf128(float128 x, float128 y);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_F16ADDF128_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 2b30b8c41b71a..8171532c36c9c 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3802,6 +3802,19 @@ add_entrypoint_object(
-O3
)
+add_entrypoint_object(
+ f16addf128
+ SRCS
+ f16addf128.cpp
+ HDRS
+ ../f16addf128.h
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.generic.add_sub
+ COMPILE_OPTIONS
+ -O3
+)
+
add_entrypoint_object(
f16subf
SRCS
diff --git a/libc/src/math/generic/f16addf128.cpp b/libc/src/math/generic/f16addf128.cpp
new file mode 100644
index 0000000000000..61c458f7d5de1
--- /dev/null
+++ b/libc/src/math/generic/f16addf128.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of f16addf128 function -----------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/f16addf128.h"
+#include "src/__support/FPUtil/generic/add_sub.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float16, f16addf128, (float128 x, float128 y)) {
+ return fputil::generic::add<float16>(x, y);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 83d288236d8d1..f2c4102fcd113 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3660,6 +3660,21 @@ add_fp_unittest(
libc.src.math.f16addf
)
+add_fp_unittest(
+ f16addf128_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ f16addf128_test.cpp
+ HDRS
+ AddTest.h
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.fenv_macros
+ libc.src.__support.FPUtil.basic_operations
+ libc.src.math.f16addf128
+)
+
add_fp_unittest(
f16subf_test
SUITE
diff --git a/libc/test/src/math/smoke/f16addf128_test.cpp b/libc/test/src/math/smoke/f16addf128_test.cpp
new file mode 100644
index 0000000000000..8ed123b4ff1e8
--- /dev/null
+++ b/libc/test/src/math/smoke/f16addf128_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for f16addf128 ------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "AddTest.h"
+
+#include "src/math/f16addf128.h"
+
+LIST_ADD_TESTS(float16, float128, LIBC_NAMESPACE::f16addf128)
>From c39428326a39700da3ad9f6ba8c48c19d7edb48c Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Fri, 28 Jun 2024 17:16:09 +0200
Subject: [PATCH 08/11] [libc][math][c23] Add f16addl C23 math function
---
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/docs/math/index.rst | 2 +-
libc/spec/stdc.td | 1 +
libc/src/math/CMakeLists.txt | 1 +
libc/src/math/f16addl.h | 20 ++++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 13 +++++++++++++
libc/src/math/generic/f16addl.cpp | 19 +++++++++++++++++++
libc/test/src/math/CMakeLists.txt | 13 +++++++++++++
libc/test/src/math/f16addl_test.cpp | 13 +++++++++++++
libc/test/src/math/smoke/CMakeLists.txt | 15 +++++++++++++++
libc/test/src/math/smoke/f16addl_test.cpp | 13 +++++++++++++
libc/utils/MPFRWrapper/MPFRUtils.cpp | 5 +++++
12 files changed, 115 insertions(+), 1 deletion(-)
create mode 100644 libc/src/math/f16addl.h
create mode 100644 libc/src/math/generic/f16addl.cpp
create mode 100644 libc/test/src/math/f16addl_test.cpp
create mode 100644 libc/test/src/math/smoke/f16addl_test.cpp
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 560ce065e1b06..847ea5928fc8d 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -539,6 +539,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.copysignf16
libc.src.math.f16add
libc.src.math.f16addf
+ libc.src.math.f16addl
libc.src.math.f16divf
libc.src.math.f16fma
libc.src.math.f16fmaf
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index e72cbebf0ed7e..62f5e7559c1d7 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -124,7 +124,7 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| dsub | N/A | N/A | | N/A | | 7.12.14.2 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| f16add | |check| | |check| | | N/A | |check| | 7.12.14.1 | F.10.11 |
+| f16add | |check| | |check| | |check| | N/A | |check| | 7.12.14.1 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| f16div | |check| | | | N/A | | 7.12.14.4 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 29a6a4cc9a9ae..ef35a03e9f9fa 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -731,6 +731,7 @@ def StdC : StandardSpec<"stdc"> {
GuardedFunctionSpec<"f16add", RetValSpec<Float16Type>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"f16addf", RetValSpec<Float16Type>, [ArgSpec<FloatType>, ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
+ GuardedFunctionSpec<"f16addl", RetValSpec<Float16Type>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"f16addf128", RetValSpec<Float16Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128">,
GuardedFunctionSpec<"f16subf", RetValSpec<Float16Type>, [ArgSpec<FloatType>, ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 45fd118803318..9af91064596fb 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -101,6 +101,7 @@ add_math_entrypoint_object(expm1f)
add_math_entrypoint_object(f16add)
add_math_entrypoint_object(f16addf)
+add_math_entrypoint_object(f16addl)
add_math_entrypoint_object(f16addf128)
add_math_entrypoint_object(f16divf)
diff --git a/libc/src/math/f16addl.h b/libc/src/math/f16addl.h
new file mode 100644
index 0000000000000..6a7267a10d0c6
--- /dev/null
+++ b/libc/src/math/f16addl.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for f16addl -----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_F16ADDL_H
+#define LLVM_LIBC_SRC_MATH_F16ADDL_H
+
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE {
+
+float16 f16addl(long double x, long double y);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_F16ADDL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 8171532c36c9c..cb0e9104558a4 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3802,6 +3802,19 @@ add_entrypoint_object(
-O3
)
+add_entrypoint_object(
+ f16addl
+ SRCS
+ f16addl.cpp
+ HDRS
+ ../f16addl.h
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.generic.add_sub
+ COMPILE_OPTIONS
+ -O3
+)
+
add_entrypoint_object(
f16addf128
SRCS
diff --git a/libc/src/math/generic/f16addl.cpp b/libc/src/math/generic/f16addl.cpp
new file mode 100644
index 0000000000000..d32d09d0dbb83
--- /dev/null
+++ b/libc/src/math/generic/f16addl.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of f16addl function --------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/f16addl.h"
+#include "src/__support/FPUtil/generic/add_sub.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float16, f16addl, (long double x, long double y)) {
+ return fputil::generic::add<float16>(x, y);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 321555d29915f..828deeeadc568 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -1928,6 +1928,19 @@ add_fp_unittest(
libc.src.math.f16addf
)
+add_fp_unittest(
+ f16addl_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ f16addl_test.cpp
+ HDRS
+ AddTest.h
+ DEPENDS
+ libc.src.math.f16addl
+)
+
add_fp_unittest(
f16subf_test
NEED_MPFR
diff --git a/libc/test/src/math/f16addl_test.cpp b/libc/test/src/math/f16addl_test.cpp
new file mode 100644
index 0000000000000..f8e0d9ba6b4de
--- /dev/null
+++ b/libc/test/src/math/f16addl_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for f16addl ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "AddTest.h"
+
+#include "src/math/f16addl.h"
+
+LIST_ADD_TESTS(float16, long double, LIBC_NAMESPACE::f16addl)
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index f2c4102fcd113..9fe44845ff1ec 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3660,6 +3660,21 @@ add_fp_unittest(
libc.src.math.f16addf
)
+add_fp_unittest(
+ f16addl_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ f16addl_test.cpp
+ HDRS
+ AddTest.h
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.fenv_macros
+ libc.src.__support.FPUtil.basic_operations
+ libc.src.math.f16addl
+)
+
add_fp_unittest(
f16addf128_test
SUITE
diff --git a/libc/test/src/math/smoke/f16addl_test.cpp b/libc/test/src/math/smoke/f16addl_test.cpp
new file mode 100644
index 0000000000000..f8e0d9ba6b4de
--- /dev/null
+++ b/libc/test/src/math/smoke/f16addl_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for f16addl ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "AddTest.h"
+
+#include "src/math/f16addl.h"
+
+LIST_ADD_TESTS(float16, long double, LIBC_NAMESPACE::f16addl)
diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp
index 1cd2dc4597c98..a0888a5c480e9 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -953,6 +953,8 @@ explain_binary_operation_one_output_error(Operation, const BinaryInput<float> &,
float16, double, RoundingMode);
template void explain_binary_operation_one_output_error(
Operation, const BinaryInput<double> &, float16, double, RoundingMode);
+template void explain_binary_operation_one_output_error(
+ Operation, const BinaryInput<long double> &, float16, double, RoundingMode);
#endif
template <typename InputType, typename OutputType>
@@ -1126,6 +1128,9 @@ template bool compare_binary_operation_one_output(Operation,
const BinaryInput<double> &,
float16, double,
RoundingMode);
+template bool
+compare_binary_operation_one_output(Operation, const BinaryInput<long double> &,
+ float16, double, RoundingMode);
#endif
template <typename InputType, typename OutputType>
>From 07c7061c724cd1f27618224be8343760367d2dcb Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Fri, 28 Jun 2024 17:20:44 +0200
Subject: [PATCH 09/11] [libc][math][c23] Add f16sub C23 math function
---
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/docs/math/index.rst | 2 +-
libc/spec/stdc.td | 1 +
libc/src/math/CMakeLists.txt | 1 +
libc/src/math/f16sub.h | 20 ++++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 13 +++++++++++++
libc/src/math/generic/f16sub.cpp | 19 +++++++++++++++++++
libc/test/src/math/CMakeLists.txt | 13 +++++++++++++
libc/test/src/math/f16sub_test.cpp | 13 +++++++++++++
libc/test/src/math/smoke/CMakeLists.txt | 14 ++++++++++++++
libc/test/src/math/smoke/f16sub_test.cpp | 13 +++++++++++++
12 files changed, 110 insertions(+), 1 deletion(-)
create mode 100644 libc/src/math/f16sub.h
create mode 100644 libc/src/math/generic/f16sub.cpp
create mode 100644 libc/test/src/math/f16sub_test.cpp
create mode 100644 libc/test/src/math/smoke/f16sub_test.cpp
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 032c7c4030388..dbe4060fa2053 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -512,6 +512,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.f16divf
libc.src.math.f16fmaf
libc.src.math.f16sqrtf
+ libc.src.math.f16sub
libc.src.math.f16subf
libc.src.math.fabsf16
libc.src.math.fdimf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 847ea5928fc8d..ced3f90feb2c7 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -545,6 +545,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.f16fmaf
libc.src.math.f16fmal
libc.src.math.f16sqrtf
+ libc.src.math.f16sub
libc.src.math.f16subf
libc.src.math.fabsf16
libc.src.math.fdimf16
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index 62f5e7559c1d7..dd7e0d15ae193 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -130,7 +130,7 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| f16fma | |check| | |check| | |check| | N/A | |check| | 7.12.14.5 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| f16sub | |check| | | | N/A | | 7.12.14.2 | F.10.11 |
+| f16sub | |check| | |check| | | N/A | | 7.12.14.2 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| fabs | |check| | |check| | |check| | |check| | |check| | 7.12.7.3 | F.10.4.3 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index ef35a03e9f9fa..84e9cec463119 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -734,6 +734,7 @@ def StdC : StandardSpec<"stdc"> {
GuardedFunctionSpec<"f16addl", RetValSpec<Float16Type>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"f16addf128", RetValSpec<Float16Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128">,
+ GuardedFunctionSpec<"f16sub", RetValSpec<Float16Type>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"f16subf", RetValSpec<Float16Type>, [ArgSpec<FloatType>, ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"f16divf", RetValSpec<Float16Type>, [ArgSpec<FloatType>, ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 9af91064596fb..f19cb3813cd1b 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -113,6 +113,7 @@ add_math_entrypoint_object(f16fmaf128)
add_math_entrypoint_object(f16sqrtf)
+add_math_entrypoint_object(f16sub)
add_math_entrypoint_object(f16subf)
add_math_entrypoint_object(fabs)
diff --git a/libc/src/math/f16sub.h b/libc/src/math/f16sub.h
new file mode 100644
index 0000000000000..66f82daada019
--- /dev/null
+++ b/libc/src/math/f16sub.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for f16sub ------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_F16SUB_H
+#define LLVM_LIBC_SRC_MATH_F16SUB_H
+
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE {
+
+float16 f16sub(double x, double y);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_F16SUB_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index cb0e9104558a4..a9df668dbb329 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3828,6 +3828,19 @@ add_entrypoint_object(
-O3
)
+add_entrypoint_object(
+ f16sub
+ SRCS
+ f16sub.cpp
+ HDRS
+ ../f16sub.h
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.generic.add_sub
+ COMPILE_OPTIONS
+ -O3
+)
+
add_entrypoint_object(
f16subf
SRCS
diff --git a/libc/src/math/generic/f16sub.cpp b/libc/src/math/generic/f16sub.cpp
new file mode 100644
index 0000000000000..114c8ad3155e1
--- /dev/null
+++ b/libc/src/math/generic/f16sub.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of f16sub function ---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/f16sub.h"
+#include "src/__support/FPUtil/generic/add_sub.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float16, f16sub, (double x, double y)) {
+ return fputil::generic::sub<float16>(x, y);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 828deeeadc568..76ee4467df6f3 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -1941,6 +1941,19 @@ add_fp_unittest(
libc.src.math.f16addl
)
+add_fp_unittest(
+ f16sub_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ f16sub_test.cpp
+ HDRS
+ SubTest.h
+ DEPENDS
+ libc.src.math.f16sub
+)
+
add_fp_unittest(
f16subf_test
NEED_MPFR
diff --git a/libc/test/src/math/f16sub_test.cpp b/libc/test/src/math/f16sub_test.cpp
new file mode 100644
index 0000000000000..37b970952fe5e
--- /dev/null
+++ b/libc/test/src/math/f16sub_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for f16sub ----------------------------------------------===/
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "SubTest.h"
+
+#include "src/math/f16sub.h"
+
+LIST_SUB_TESTS(float16, double, LIBC_NAMESPACE::f16sub)
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 9fe44845ff1ec..8178520dc9def 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3690,6 +3690,20 @@ add_fp_unittest(
libc.src.math.f16addf128
)
+add_fp_unittest(
+ f16sub_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ f16sub_test.cpp
+ HDRS
+ SubTest.h
+ DEPENDS
+ libc.hdr.fenv_macros
+ libc.src.__support.FPUtil.basic_operations
+ libc.src.math.f16sub
+)
+
add_fp_unittest(
f16subf_test
SUITE
diff --git a/libc/test/src/math/smoke/f16sub_test.cpp b/libc/test/src/math/smoke/f16sub_test.cpp
new file mode 100644
index 0000000000000..4ab347ba614fb
--- /dev/null
+++ b/libc/test/src/math/smoke/f16sub_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for f16sub ----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "SubTest.h"
+
+#include "src/math/f16sub.h"
+
+LIST_SUB_TESTS(float16, double, LIBC_NAMESPACE::f16sub)
>From 32f5177727888e829a391de09bdf99eb930c5751 Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Fri, 28 Jun 2024 17:23:54 +0200
Subject: [PATCH 10/11] [libc][math][c23] Add f16subf128 C23 math function
---
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/docs/math/index.rst | 2 +-
libc/spec/stdc.td | 1 +
libc/src/math/CMakeLists.txt | 1 +
libc/src/math/f16subf128.h | 20 ++++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 13 +++++++++++++
libc/src/math/generic/f16subf128.cpp | 19 +++++++++++++++++++
libc/test/src/math/smoke/CMakeLists.txt | 14 ++++++++++++++
libc/test/src/math/smoke/f16subf128_test.cpp | 13 +++++++++++++
9 files changed, 83 insertions(+), 1 deletion(-)
create mode 100644 libc/src/math/f16subf128.h
create mode 100644 libc/src/math/generic/f16subf128.cpp
create mode 100644 libc/test/src/math/smoke/f16subf128_test.cpp
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index ced3f90feb2c7..24802798bcc47 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -601,6 +601,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
# math.h C23 mixed _Float16 and _Float128 entrypoints
libc.src.math.f16addf128
libc.src.math.f16fmaf128
+ libc.src.math.f16subf128
)
endif()
endif()
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index dd7e0d15ae193..41a0476fa189c 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -130,7 +130,7 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| f16fma | |check| | |check| | |check| | N/A | |check| | 7.12.14.5 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| f16sub | |check| | |check| | | N/A | | 7.12.14.2 | F.10.11 |
+| f16sub | |check| | |check| | | N/A | |check| | 7.12.14.2 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| fabs | |check| | |check| | |check| | |check| | |check| | 7.12.7.3 | F.10.4.3 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 84e9cec463119..369119a7213b0 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -736,6 +736,7 @@ def StdC : StandardSpec<"stdc"> {
GuardedFunctionSpec<"f16sub", RetValSpec<Float16Type>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"f16subf", RetValSpec<Float16Type>, [ArgSpec<FloatType>, ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
+ GuardedFunctionSpec<"f16subf128", RetValSpec<Float16Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128">,
GuardedFunctionSpec<"f16divf", RetValSpec<Float16Type>, [ArgSpec<FloatType>, ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index f19cb3813cd1b..fc6d398961918 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -115,6 +115,7 @@ add_math_entrypoint_object(f16sqrtf)
add_math_entrypoint_object(f16sub)
add_math_entrypoint_object(f16subf)
+add_math_entrypoint_object(f16subf128)
add_math_entrypoint_object(fabs)
add_math_entrypoint_object(fabsf)
diff --git a/libc/src/math/f16subf128.h b/libc/src/math/f16subf128.h
new file mode 100644
index 0000000000000..eb674297ba266
--- /dev/null
+++ b/libc/src/math/f16subf128.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for f16subf128 --------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_F16SUBF128_H
+#define LLVM_LIBC_SRC_MATH_F16SUBF128_H
+
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE {
+
+float16 f16subf128(float128 x, float128 y);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_F16SUBF128_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index a9df668dbb329..825d94ee8dd46 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3854,6 +3854,19 @@ add_entrypoint_object(
-O3
)
+add_entrypoint_object(
+ f16subf128
+ SRCS
+ f16subf128.cpp
+ HDRS
+ ../f16subf128.h
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.generic.add_sub
+ COMPILE_OPTIONS
+ -O3
+)
+
add_entrypoint_object(
f16divf
SRCS
diff --git a/libc/src/math/generic/f16subf128.cpp b/libc/src/math/generic/f16subf128.cpp
new file mode 100644
index 0000000000000..1f9ff28abdf29
--- /dev/null
+++ b/libc/src/math/generic/f16subf128.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of f16subf128 function -----------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/f16subf128.h"
+#include "src/__support/FPUtil/generic/add_sub.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float16, f16subf128, (float128 x, float128 y)) {
+ return fputil::generic::sub<float16>(x, y);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 8178520dc9def..8237c2ad18349 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3718,6 +3718,20 @@ add_fp_unittest(
libc.src.math.f16subf
)
+add_fp_unittest(
+ f16subf128_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ f16subf128_test.cpp
+ HDRS
+ SubTest.h
+ DEPENDS
+ libc.hdr.fenv_macros
+ libc.src.__support.FPUtil.basic_operations
+ libc.src.math.f16subf128
+)
+
add_fp_unittest(
f16divf_test
SUITE
diff --git a/libc/test/src/math/smoke/f16subf128_test.cpp b/libc/test/src/math/smoke/f16subf128_test.cpp
new file mode 100644
index 0000000000000..4936d89c1f904
--- /dev/null
+++ b/libc/test/src/math/smoke/f16subf128_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for f16subf128 ------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "SubTest.h"
+
+#include "src/math/f16subf128.h"
+
+LIST_SUB_TESTS(float16, float128, LIBC_NAMESPACE::f16subf128)
>From cedb6d7098adec54eea234ebf021c60db8809212 Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Fri, 28 Jun 2024 17:28:07 +0200
Subject: [PATCH 11/11] [libc][math][c23] Add f16subl C23 math function
---
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/docs/math/index.rst | 2 +-
libc/spec/stdc.td | 1 +
libc/src/math/CMakeLists.txt | 1 +
libc/src/math/f16subl.h | 20 ++++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 13 +++++++++++++
libc/src/math/generic/f16subl.cpp | 19 +++++++++++++++++++
libc/test/src/math/CMakeLists.txt | 13 +++++++++++++
libc/test/src/math/f16subl_test.cpp | 13 +++++++++++++
libc/test/src/math/smoke/CMakeLists.txt | 14 ++++++++++++++
libc/test/src/math/smoke/f16subl_test.cpp | 13 +++++++++++++
11 files changed, 109 insertions(+), 1 deletion(-)
create mode 100644 libc/src/math/f16subl.h
create mode 100644 libc/src/math/generic/f16subl.cpp
create mode 100644 libc/test/src/math/f16subl_test.cpp
create mode 100644 libc/test/src/math/smoke/f16subl_test.cpp
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 24802798bcc47..4342e1a0bff04 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -547,6 +547,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.f16sqrtf
libc.src.math.f16sub
libc.src.math.f16subf
+ libc.src.math.f16subl
libc.src.math.fabsf16
libc.src.math.fdimf16
libc.src.math.floorf16
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index 41a0476fa189c..c730e8418a958 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -130,7 +130,7 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| f16fma | |check| | |check| | |check| | N/A | |check| | 7.12.14.5 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| f16sub | |check| | |check| | | N/A | |check| | 7.12.14.2 | F.10.11 |
+| f16sub | |check| | |check| | |check| | N/A | |check| | 7.12.14.2 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| fabs | |check| | |check| | |check| | |check| | |check| | 7.12.7.3 | F.10.4.3 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 369119a7213b0..399e10b1ab04e 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -736,6 +736,7 @@ def StdC : StandardSpec<"stdc"> {
GuardedFunctionSpec<"f16sub", RetValSpec<Float16Type>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"f16subf", RetValSpec<Float16Type>, [ArgSpec<FloatType>, ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
+ GuardedFunctionSpec<"f16subl", RetValSpec<Float16Type>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"f16subf128", RetValSpec<Float16Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128">,
GuardedFunctionSpec<"f16divf", RetValSpec<Float16Type>, [ArgSpec<FloatType>, ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index fc6d398961918..4e9d1f5753352 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -115,6 +115,7 @@ add_math_entrypoint_object(f16sqrtf)
add_math_entrypoint_object(f16sub)
add_math_entrypoint_object(f16subf)
+add_math_entrypoint_object(f16subl)
add_math_entrypoint_object(f16subf128)
add_math_entrypoint_object(fabs)
diff --git a/libc/src/math/f16subl.h b/libc/src/math/f16subl.h
new file mode 100644
index 0000000000000..43b44a57a604e
--- /dev/null
+++ b/libc/src/math/f16subl.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for f16subl -----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_F16SUBL_H
+#define LLVM_LIBC_SRC_MATH_F16SUBL_H
+
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE {
+
+float16 f16subl(long double x, long double y);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_F16SUBL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 825d94ee8dd46..950c10627f5fa 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3854,6 +3854,19 @@ add_entrypoint_object(
-O3
)
+add_entrypoint_object(
+ f16subl
+ SRCS
+ f16subl.cpp
+ HDRS
+ ../f16subl.h
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.generic.add_sub
+ COMPILE_OPTIONS
+ -O3
+)
+
add_entrypoint_object(
f16subf128
SRCS
diff --git a/libc/src/math/generic/f16subl.cpp b/libc/src/math/generic/f16subl.cpp
new file mode 100644
index 0000000000000..31970af9a2366
--- /dev/null
+++ b/libc/src/math/generic/f16subl.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of f16subl function --------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/f16subl.h"
+#include "src/__support/FPUtil/generic/add_sub.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float16, f16subl, (long double x, long double y)) {
+ return fputil::generic::sub<float16>(x, y);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 76ee4467df6f3..4aa89b953f52f 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -1967,6 +1967,19 @@ add_fp_unittest(
libc.src.math.f16subf
)
+add_fp_unittest(
+ f16subl_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ f16subl_test.cpp
+ HDRS
+ SubTest.h
+ DEPENDS
+ libc.src.math.f16subl
+)
+
add_fp_unittest(
f16divf_test
NEED_MPFR
diff --git a/libc/test/src/math/f16subl_test.cpp b/libc/test/src/math/f16subl_test.cpp
new file mode 100644
index 0000000000000..c41e6e97bab09
--- /dev/null
+++ b/libc/test/src/math/f16subl_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for f16subl ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "SubTest.h"
+
+#include "src/math/f16subl.h"
+
+LIST_SUB_TESTS(float16, long double, LIBC_NAMESPACE::f16subl)
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 8237c2ad18349..e1b1327434c89 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3718,6 +3718,20 @@ add_fp_unittest(
libc.src.math.f16subf
)
+add_fp_unittest(
+ f16subl_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ f16subl_test.cpp
+ HDRS
+ SubTest.h
+ DEPENDS
+ libc.hdr.fenv_macros
+ libc.src.__support.FPUtil.basic_operations
+ libc.src.math.f16subl
+)
+
add_fp_unittest(
f16subf128_test
SUITE
diff --git a/libc/test/src/math/smoke/f16subl_test.cpp b/libc/test/src/math/smoke/f16subl_test.cpp
new file mode 100644
index 0000000000000..c41e6e97bab09
--- /dev/null
+++ b/libc/test/src/math/smoke/f16subl_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for f16subl ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "SubTest.h"
+
+#include "src/math/f16subl.h"
+
+LIST_SUB_TESTS(float16, long double, LIBC_NAMESPACE::f16subl)
More information about the libc-commits
mailing list