[libc-commits] [libc] 1f20bc2 - [libc][math] Add C23 math function fdimf128. (#81074)
via libc-commits
libc-commits at lists.llvm.org
Fri Feb 9 08:21:08 PST 2024
Author: lntue
Date: 2024-02-09T11:21:04-05:00
New Revision: 1f20bc2cd273dd21459b9007a10c6aa67e5da1e2
URL: https://github.com/llvm/llvm-project/commit/1f20bc2cd273dd21459b9007a10c6aa67e5da1e2
DIFF: https://github.com/llvm/llvm-project/commit/1f20bc2cd273dd21459b9007a10c6aa67e5da1e2.diff
LOG: [libc][math] Add C23 math function fdimf128. (#81074)
Added:
libc/src/math/fdimf128.h
libc/src/math/generic/fdimf128.cpp
libc/test/src/math/smoke/fdimf128_test.cpp
Modified:
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/riscv/entrypoints.txt
libc/config/linux/x86_64/entrypoints.txt
libc/docs/math/index.rst
libc/spec/stdc.td
libc/src/math/CMakeLists.txt
libc/src/math/generic/CMakeLists.txt
libc/test/src/math/smoke/CMakeLists.txt
libc/test/src/math/smoke/FDimTest.h
libc/test/src/math/smoke/fdim_test.cpp
libc/test/src/math/smoke/fdimf_test.cpp
libc/test/src/math/smoke/fdiml_test.cpp
Removed:
################################################################################
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 5b03080787dc89..f75b2679313996 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -382,6 +382,7 @@ if(LIBC_COMPILER_HAS_FLOAT128)
libc.src.math.ceilf128
libc.src.math.copysignf128
libc.src.math.fabsf128
+ libc.src.math.fdimf128
libc.src.math.floorf128
libc.src.math.fmaxf128
libc.src.math.fminf128
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 5e98538c91b4e9..762beb9040154a 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -391,6 +391,7 @@ if(LIBC_COMPILER_HAS_FLOAT128)
libc.src.math.ceilf128
libc.src.math.copysignf128
libc.src.math.fabsf128
+ libc.src.math.fdimf128
libc.src.math.floorf128
libc.src.math.fmaxf128
libc.src.math.fminf128
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index b35fc9fc6866b4..52a3ce0132bdcb 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -410,6 +410,7 @@ if(LIBC_COMPILER_HAS_FLOAT128)
libc.src.math.ceilf128
libc.src.math.copysignf128
libc.src.math.fabsf128
+ libc.src.math.fdimf128
libc.src.math.floorf128
libc.src.math.fmaxf128
libc.src.math.fminf128
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index 3af7e106fc9938..2758b42610d0ed 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -138,6 +138,8 @@ Basic Operations
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| fdiml | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
+| fdimf128 | |check| | |check| | | |check| | | | | | | | | |
++--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| floor | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| floorf | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index e37f95a7c2daf0..9c8b5e5c466273 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -374,6 +374,7 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"fdim", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
FunctionSpec<"fdimf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
FunctionSpec<"fdiml", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,
+ GuardedFunctionSpec<"fdimf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>], "LIBC_COMPILER_HAS_FLOAT128">,
FunctionSpec<"floor", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"floorf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index d4dbeebb7b2195..8cdd84a0f67110 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -111,6 +111,7 @@ add_math_entrypoint_object(fabsf128)
add_math_entrypoint_object(fdim)
add_math_entrypoint_object(fdimf)
add_math_entrypoint_object(fdiml)
+add_math_entrypoint_object(fdimf128)
add_math_entrypoint_object(floor)
add_math_entrypoint_object(floorf)
diff --git a/libc/src/math/fdimf128.h b/libc/src/math/fdimf128.h
new file mode 100644
index 00000000000000..c6f488a586dc03
--- /dev/null
+++ b/libc/src/math/fdimf128.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for fdimf128 ----------------------*- 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_FDIMF128_H
+#define LLVM_LIBC_SRC_MATH_FDIMF128_H
+
+#include "src/__support/macros/properties/float.h"
+
+namespace LIBC_NAMESPACE {
+
+float128 fdimf128(float128 x, float128 y);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_FDIMF128_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 05b70be7b7b9fd..3216ec39401f31 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -43,6 +43,7 @@ add_entrypoint_object(
COMPILE_OPTIONS
-O3
DEPENDS
+ libc.src.__support.macros.properties.float
libc.src.__support.FPUtil.nearest_integer_operations
)
@@ -215,6 +216,7 @@ add_entrypoint_object(
HDRS
../fabsf128.h
DEPENDS
+ libc.src.__support.macros.properties.float
libc.src.__support.FPUtil.basic_operations
COMPILE_OPTIONS
-O3
@@ -265,6 +267,7 @@ add_entrypoint_object(
COMPILE_OPTIONS
-O3
DEPENDS
+ libc.src.__support.macros.properties.float
libc.src.__support.FPUtil.nearest_integer_operations
)
@@ -313,6 +316,7 @@ add_entrypoint_object(
COMPILE_OPTIONS
-O3
DEPENDS
+ libc.src.__support.macros.properties.float
libc.src.__support.FPUtil.nearest_integer_operations
)
@@ -361,6 +365,7 @@ add_entrypoint_object(
COMPILE_OPTIONS
-O3
DEPENDS
+ libc.src.__support.macros.properties.float
libc.src.__support.FPUtil.nearest_integer_operations
)
@@ -899,6 +904,7 @@ add_entrypoint_object(
HDRS
../copysignf128.h
DEPENDS
+ libc.src.__support.macros.properties.float
libc.src.__support.FPUtil.manipulation_functions
COMPILE_OPTIONS
-O3
@@ -1298,6 +1304,7 @@ add_entrypoint_object(
HDRS
../fminf128.h
DEPENDS
+ libc.src.__support.macros.properties.float
libc.src.__support.FPUtil.basic_operations
COMPILE_OPTIONS
-O3
@@ -1346,6 +1353,7 @@ add_entrypoint_object(
HDRS
../fmaxf128.h
DEPENDS
+ libc.src.__support.macros.properties.float
libc.src.__support.FPUtil.basic_operations
COMPILE_OPTIONS
-O3
@@ -1394,6 +1402,7 @@ add_entrypoint_object(
HDRS
../sqrtf128.h
DEPENDS
+ libc.src.__support.macros.properties.float
libc.src.__support.FPUtil.sqrt
COMPILE_OPTIONS
-O3
@@ -1491,10 +1500,10 @@ add_entrypoint_object(
fdim.cpp
HDRS
../fdim.h
+ COMPILE_OPTIONS
+ -O3
DEPENDS
libc.src.__support.FPUtil.basic_operations
- COMPILE_OPTIONS
- -O2
)
add_entrypoint_object(
@@ -1503,10 +1512,10 @@ add_entrypoint_object(
fdimf.cpp
HDRS
../fdimf.h
+ COMPILE_OPTIONS
+ -O3
DEPENDS
libc.src.__support.FPUtil.basic_operations
- COMPILE_OPTIONS
- -O2
)
add_entrypoint_object(
@@ -1515,10 +1524,23 @@ add_entrypoint_object(
fdiml.cpp
HDRS
../fdiml.h
+ COMPILE_OPTIONS
+ -O3
DEPENDS
libc.src.__support.FPUtil.basic_operations
+)
+
+add_entrypoint_object(
+ fdimf128
+ SRCS
+ fdimf128.cpp
+ HDRS
+ ../fdimf128.h
COMPILE_OPTIONS
- -O2
+ -O3
+ DEPENDS
+ libc.src.__support.macros.properties.float
+ libc.src.__support.FPUtil.basic_operations
)
add_entrypoint_object(
diff --git a/libc/src/math/generic/fdimf128.cpp b/libc/src/math/generic/fdimf128.cpp
new file mode 100644
index 00000000000000..a3ea9e591610c2
--- /dev/null
+++ b/libc/src/math/generic/fdimf128.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of fdimf128 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/fdimf128.h"
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float128, fdimf128, (float128 x, float128 y)) {
+ return fputil::fdim(x, y);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 4ee81ec1cccf6e..93ce0b716cce86 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -1007,7 +1007,6 @@ add_fp_unittest(
HDRS
FDimTest.h
DEPENDS
- libc.include.math
libc.src.math.fdimf
libc.src.__support.FPUtil.basic_operations
libc.src.__support.FPUtil.fp_bits
@@ -1022,7 +1021,6 @@ add_fp_unittest(
HDRS
FDimTest.h
DEPENDS
- libc.include.math
libc.src.math.fdim
libc.src.__support.FPUtil.basic_operations
libc.src.__support.FPUtil.fp_bits
@@ -1037,12 +1035,25 @@ add_fp_unittest(
HDRS
FDimTest.h
DEPENDS
- libc.include.math
libc.src.math.fdiml
libc.src.__support.FPUtil.basic_operations
libc.src.__support.FPUtil.fp_bits
)
+add_fp_unittest(
+ fdimf128_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ fdimf128_test.cpp
+ HDRS
+ FDimTest.h
+ DEPENDS
+ libc.src.math.fdimf128
+ libc.src.__support.FPUtil.basic_operations
+ libc.src.__support.FPUtil.fp_bits
+)
+
# FIXME: These tests are currently broken on the GPU.
if(NOT LIBC_TARGET_ARCHITECTURE_IS_GPU)
add_fp_unittest(
diff --git a/libc/test/src/math/smoke/FDimTest.h b/libc/test/src/math/smoke/FDimTest.h
index e00b4fd5c42ec0..5cb3dd11734848 100644
--- a/libc/test/src/math/smoke/FDimTest.h
+++ b/libc/test/src/math/smoke/FDimTest.h
@@ -10,7 +10,6 @@
#include "src/__support/FPUtil/FPBits.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
-#include <math.h>
template <typename T>
class FDimTestTemplate : public LIBC_NAMESPACE::testing::Test {
@@ -26,7 +25,7 @@ class FDimTestTemplate : public LIBC_NAMESPACE::testing::Test {
const T neg_zero = FPBits::zero(Sign::NEG).get_val();
const T nan = FPBits::quiet_nan().get_val();
- void test_na_n_arg(FuncPtr func) {
+ void test_nan_arg(FuncPtr func) {
EXPECT_FP_EQ(nan, func(nan, inf));
EXPECT_FP_EQ(nan, func(neg_inf, nan));
EXPECT_FP_EQ(nan, func(nan, zero));
@@ -66,12 +65,15 @@ class FDimTestTemplate : public LIBC_NAMESPACE::testing::Test {
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
++i, v += STEP, w -= STEP) {
- T x = FPBits(v).get_val(), y = FPBits(w).get_val();
- if (isnan(x) || isinf(x))
+ FPBits xbits(v), ybits(w);
+ if (xbits.is_inf_or_nan())
continue;
- if (isnan(y) || isinf(y))
+ if (ybits.is_inf_or_nan())
continue;
+ T x = xbits.get_val();
+ T y = ybits.get_val();
+
if (x > y) {
EXPECT_FP_EQ(x - y, func(x, y));
} else {
@@ -80,3 +82,12 @@ class FDimTestTemplate : public LIBC_NAMESPACE::testing::Test {
}
}
};
+
+#define LIST_FDIM_TESTS(T, func) \
+ using LlvmLibcFDimTest = FDimTestTemplate<T>; \
+ TEST_F(LlvmLibcFDimTest, NaNArg) { test_nan_arg(&func); } \
+ TEST_F(LlvmLibcFDimTest, InfArg) { test_inf_arg(&func); } \
+ TEST_F(LlvmLibcFDimTest, NegInfArg) { test_neg_inf_arg(&func); } \
+ TEST_F(LlvmLibcFDimTest, BothZero) { test_both_zero(&func); } \
+ TEST_F(LlvmLibcFDimTest, InFloatRange) { test_in_range(&func); } \
+ static_assert(true, "Require semicolon.")
diff --git a/libc/test/src/math/smoke/fdim_test.cpp b/libc/test/src/math/smoke/fdim_test.cpp
index 2f00a30ad1ee6f..e1c150da90af35 100644
--- a/libc/test/src/math/smoke/fdim_test.cpp
+++ b/libc/test/src/math/smoke/fdim_test.cpp
@@ -8,26 +8,6 @@
#include "FDimTest.h"
-#include "src/__support/FPUtil/FPBits.h"
#include "src/math/fdim.h"
-#include "test/UnitTest/FPMatcher.h"
-#include "test/UnitTest/Test.h"
-#include <math.h>
-using LlvmLibcFDimTest = FDimTestTemplate<double>;
-
-TEST_F(LlvmLibcFDimTest, NaNArg_fdim) { test_na_n_arg(&LIBC_NAMESPACE::fdim); }
-
-TEST_F(LlvmLibcFDimTest, InfArg_fdim) { test_inf_arg(&LIBC_NAMESPACE::fdim); }
-
-TEST_F(LlvmLibcFDimTest, NegInfArg_fdim) {
- test_neg_inf_arg(&LIBC_NAMESPACE::fdim);
-}
-
-TEST_F(LlvmLibcFDimTest, BothZero_fdim) {
- test_both_zero(&LIBC_NAMESPACE::fdim);
-}
-
-TEST_F(LlvmLibcFDimTest, InDoubleRange_fdim) {
- test_in_range(&LIBC_NAMESPACE::fdim);
-}
+LIST_FDIM_TESTS(double, LIBC_NAMESPACE::fdim);
diff --git a/libc/test/src/math/smoke/fdimf128_test.cpp b/libc/test/src/math/smoke/fdimf128_test.cpp
new file mode 100644
index 00000000000000..8e65c2b4a1ec54
--- /dev/null
+++ b/libc/test/src/math/smoke/fdimf128_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for fdimf128 --------------------------------------------===//
+//
+// 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 "FDimTest.h"
+
+#include "src/math/fdimf128.h"
+
+LIST_FDIM_TESTS(float128, LIBC_NAMESPACE::fdimf128);
diff --git a/libc/test/src/math/smoke/fdimf_test.cpp b/libc/test/src/math/smoke/fdimf_test.cpp
index 27511baf25b6da..9c27c1d2fc2025 100644
--- a/libc/test/src/math/smoke/fdimf_test.cpp
+++ b/libc/test/src/math/smoke/fdimf_test.cpp
@@ -8,28 +8,6 @@
#include "FDimTest.h"
-#include "src/__support/FPUtil/FPBits.h"
#include "src/math/fdimf.h"
-#include "test/UnitTest/FPMatcher.h"
-#include "test/UnitTest/Test.h"
-#include <math.h>
-using LlvmLibcFDimTest = FDimTestTemplate<float>;
-
-TEST_F(LlvmLibcFDimTest, NaNArg_fdimf) {
- test_na_n_arg(&LIBC_NAMESPACE::fdimf);
-}
-
-TEST_F(LlvmLibcFDimTest, InfArg_fdimf) { test_inf_arg(&LIBC_NAMESPACE::fdimf); }
-
-TEST_F(LlvmLibcFDimTest, NegInfArg_fdimf) {
- test_neg_inf_arg(&LIBC_NAMESPACE::fdimf);
-}
-
-TEST_F(LlvmLibcFDimTest, BothZero_fdimf) {
- test_both_zero(&LIBC_NAMESPACE::fdimf);
-}
-
-TEST_F(LlvmLibcFDimTest, InFloatRange_fdimf) {
- test_in_range(&LIBC_NAMESPACE::fdimf);
-}
+LIST_FDIM_TESTS(float, LIBC_NAMESPACE::fdimf);
diff --git a/libc/test/src/math/smoke/fdiml_test.cpp b/libc/test/src/math/smoke/fdiml_test.cpp
index 45aedb0a1cdea6..ed448a612ec76f 100644
--- a/libc/test/src/math/smoke/fdiml_test.cpp
+++ b/libc/test/src/math/smoke/fdiml_test.cpp
@@ -8,28 +8,6 @@
#include "FDimTest.h"
-#include "src/__support/FPUtil/FPBits.h"
#include "src/math/fdiml.h"
-#include "test/UnitTest/FPMatcher.h"
-#include "test/UnitTest/Test.h"
-#include <math.h>
-using LlvmLibcFDimTest = FDimTestTemplate<long double>;
-
-TEST_F(LlvmLibcFDimTest, NaNArg_fdiml) {
- test_na_n_arg(&LIBC_NAMESPACE::fdiml);
-}
-
-TEST_F(LlvmLibcFDimTest, InfArg_fdiml) { test_inf_arg(&LIBC_NAMESPACE::fdiml); }
-
-TEST_F(LlvmLibcFDimTest, NegInfArg_fdiml) {
- test_neg_inf_arg(&LIBC_NAMESPACE::fdiml);
-}
-
-TEST_F(LlvmLibcFDimTest, BothZero_fdiml) {
- test_both_zero(&LIBC_NAMESPACE::fdiml);
-}
-
-TEST_F(LlvmLibcFDimTest, InLongDoubleRange_fdiml) {
- test_in_range(&LIBC_NAMESPACE::fdiml);
-}
+LIST_FDIM_TESTS(long double, LIBC_NAMESPACE::fdiml);
More information about the libc-commits
mailing list