[libc-commits] [libc] [libc][math][c23] Add modff128 C23 math function. (PR #84532)

via libc-commits libc-commits at lists.llvm.org
Sat Mar 9 08:43:39 PST 2024


https://github.com/lntue updated https://github.com/llvm/llvm-project/pull/84532

>From cdc4f10e86ee5d9942d4789ba040b6f26513a00d Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Fri, 8 Mar 2024 13:42:10 -0500
Subject: [PATCH 1/2] [libc][math][c23] Add modff128 C23 math function.

---
 libc/config/linux/aarch64/entrypoints.txt  |  1 +
 libc/config/linux/riscv/entrypoints.txt    |  1 +
 libc/config/linux/x86_64/entrypoints.txt   |  1 +
 libc/docs/math/index.rst                   |  2 ++
 libc/spec/spec.td                          |  1 +
 libc/spec/stdc.td                          |  1 +
 libc/src/math/CMakeLists.txt               |  1 +
 libc/src/math/generic/CMakeLists.txt       | 19 ++++++++++++++++---
 libc/src/math/generic/modff128.cpp         | 19 +++++++++++++++++++
 libc/src/math/modff128.h                   | 20 ++++++++++++++++++++
 libc/test/src/math/smoke/CMakeLists.txt    | 19 +++++++++++++++----
 libc/test/src/math/smoke/ModfTest.h        |  6 ++++--
 libc/test/src/math/smoke/modff128_test.cpp | 13 +++++++++++++
 13 files changed, 95 insertions(+), 9 deletions(-)
 create mode 100644 libc/src/math/generic/modff128.cpp
 create mode 100644 libc/src/math/modff128.h
 create mode 100644 libc/test/src/math/smoke/modff128_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index fa15ddd17aefee..c24703840183af 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -434,6 +434,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
     libc.src.math.llroundf128
     libc.src.math.lrintf128
     libc.src.math.lroundf128
+    libc.src.math.modff128
     libc.src.math.rintf128
     libc.src.math.roundf128
     libc.src.math.sqrtf128
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 924cf2f1d68b10..f7a65615115f34 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -442,6 +442,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
     libc.src.math.llroundf128
     libc.src.math.lrintf128
     libc.src.math.lroundf128
+    libc.src.math.modff128
     libc.src.math.rintf128
     libc.src.math.roundf128
     libc.src.math.sqrtf128
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 0880c372b37390..a7894af4b9ca62 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -472,6 +472,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
     libc.src.math.llroundf128
     libc.src.math.lrintf128
     libc.src.math.lroundf128
+    libc.src.math.modff128
     libc.src.math.rintf128
     libc.src.math.roundf128
     libc.src.math.sqrtf128
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index 81d95d9b6cfa66..7f2a1b2f3e2824 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -249,6 +249,8 @@ Basic Operations
 +--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
 | modfl        | |check| | |check| | |check| | |check| | |check| |         |         | |check| | |check| | |check| |         |         |
 +--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
+| modff128     | |check| | |check| |         | |check| |         |         |         |         |         |         |         |         |
++--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
 | nan          | |check| | |check| | |check| | |check| | |check| |         |         | |check| | |check| | |check| |         |         |
 +--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
 | nanf         | |check| | |check| | |check| | |check| | |check| |         |         | |check| | |check| | |check| |         |         |
diff --git a/libc/spec/spec.td b/libc/spec/spec.td
index 998f37fb26deed..a44a7ae131b5d9 100644
--- a/libc/spec/spec.td
+++ b/libc/spec/spec.td
@@ -111,6 +111,7 @@ def IntPtr : PtrType<IntType>;
 def RestrictedIntPtr : RestrictedPtrType<IntType>;
 def FloatPtr : PtrType<FloatType>;
 def DoublePtr : PtrType<DoubleType>;
+def Float128Ptr : PtrType<Float128Type>;
 def UnsignedCharPtr : PtrType<UnsignedCharType>;
 
 def SigHandlerT : NamedType<"__sighandler_t">;
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index cc845a93a3330b..99fcd23c9fd5e0 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -451,6 +451,7 @@ def StdC : StandardSpec<"stdc"> {
           FunctionSpec<"modf", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoublePtr>]>,
           FunctionSpec<"modff", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatPtr>]>,
           FunctionSpec<"modfl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoublePtr>]>,
+          FunctionSpec<"modff128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Ptr>]>,
 
           FunctionSpec<"cos", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
           FunctionSpec<"cosf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 035eefd82d3653..6c06d383ec2b04 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -183,6 +183,7 @@ add_math_entrypoint_object(lroundf128)
 add_math_entrypoint_object(modf)
 add_math_entrypoint_object(modff)
 add_math_entrypoint_object(modfl)
+add_math_entrypoint_object(modff128)
 
 add_math_entrypoint_object(nan)
 add_math_entrypoint_object(nanf)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index a7b7065980b1f1..933a05dad157c9 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1407,7 +1407,7 @@ add_entrypoint_object(
   DEPENDS
     libc.src.__support.FPUtil.manipulation_functions
   COMPILE_OPTIONS
-    -O2
+    -O3
 )
 
 add_entrypoint_object(
@@ -1419,7 +1419,7 @@ add_entrypoint_object(
   DEPENDS
     libc.src.__support.FPUtil.manipulation_functions
   COMPILE_OPTIONS
-    -O2
+    -O3
 )
 
 add_entrypoint_object(
@@ -1431,7 +1431,20 @@ add_entrypoint_object(
   DEPENDS
     libc.src.__support.FPUtil.manipulation_functions
   COMPILE_OPTIONS
-    -O2
+    -O3
+)
+
+add_entrypoint_object(
+  modff128
+  SRCS
+    modff128.cpp
+  HDRS
+    ../modff128.h
+  DEPENDS
+    libc.src.__support.macros.properties.types
+    libc.src.__support.FPUtil.manipulation_functions
+  COMPILE_OPTIONS
+    -O3
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/modff128.cpp b/libc/src/math/generic/modff128.cpp
new file mode 100644
index 00000000000000..6aef5f510a9533
--- /dev/null
+++ b/libc/src/math/generic/modff128.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of modff128 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/modff128.h"
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float128, modff128, (float128 x, float128 *iptr)) {
+  return fputil::modf(x, *iptr);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/modff128.h b/libc/src/math/modff128.h
new file mode 100644
index 00000000000000..48e614be95b982
--- /dev/null
+++ b/libc/src/math/modff128.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for modff128 -----------------------*- 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_MODFF128_H
+#define LLVM_LIBC_SRC_MATH_MODFF128_H
+
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE {
+
+float128 modff128(float128 x, float128 *iptr);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_MODFF128_H
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 63faaa9d4e4cd5..8d3871dd427aaa 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -1078,8 +1078,6 @@ add_fp_unittest(
     libc.src.math.modf
     libc.src.__support.FPUtil.basic_operations
     libc.src.__support.FPUtil.nearest_integer_operations
-  # Requires C++ limits.
-  UNIT_TEST_ONLY
 )
 
 add_fp_unittest(
@@ -1095,8 +1093,6 @@ add_fp_unittest(
     libc.src.math.modff
     libc.src.__support.FPUtil.basic_operations
     libc.src.__support.FPUtil.nearest_integer_operations
-  # Requires C++ limits.
-  UNIT_TEST_ONLY
 )
 
 add_fp_unittest(
@@ -1114,6 +1110,21 @@ add_fp_unittest(
     libc.src.__support.FPUtil.nearest_integer_operations
 )
 
+add_fp_unittest(
+  modff128_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    modff128_test.cpp
+  HDRS
+    ModfTest.h
+  DEPENDS
+    libc.include.math
+    libc.src.math.modff128
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.FPUtil.nearest_integer_operations
+)
+
 add_fp_unittest(
   fdimf_test
   SUITE
diff --git a/libc/test/src/math/smoke/ModfTest.h b/libc/test/src/math/smoke/ModfTest.h
index a73e5ae4298faa..d7e15a7ed68206 100644
--- a/libc/test/src/math/smoke/ModfTest.h
+++ b/libc/test/src/math/smoke/ModfTest.h
@@ -84,10 +84,12 @@ template <typename T> class ModfTest : public LIBC_NAMESPACE::testing::Test {
     constexpr StorageType COUNT = 100'000;
     constexpr StorageType STEP = STORAGE_MAX / COUNT;
     for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
-      T x = FPBits(v).get_val();
-      if (isnan(x) || isinf(x) || x == T(0.0))
+      FPBits x_bits = FPBits(v);
+      if (x_bits.is_zero() || x_bits.is_inf_or_nan())
         continue;
 
+      T x = x_bits.get_val();
+
       T integral;
       T frac = func(x, &integral);
       ASSERT_TRUE(LIBC_NAMESPACE::fputil::abs(frac) < 1.0l);
diff --git a/libc/test/src/math/smoke/modff128_test.cpp b/libc/test/src/math/smoke/modff128_test.cpp
new file mode 100644
index 00000000000000..062d40138e7293
--- /dev/null
+++ b/libc/test/src/math/smoke/modff128_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for modff128 --------------------------------------------===//
+//
+// 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 "ModfTest.h"
+
+#include "src/math/modff128.h"
+
+LIST_MODF_TESTS(float128, LIBC_NAMESPACE::modff128)

>From 2172b87809b44e4eea479d50d87a65a7aedb64ef Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Sat, 9 Mar 2024 11:43:16 -0500
Subject: [PATCH 2/2] Use GuardedFunctionSpec.

---
 libc/spec/stdc.td | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 99fcd23c9fd5e0..766668c51e3e0c 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -451,7 +451,7 @@ def StdC : StandardSpec<"stdc"> {
           FunctionSpec<"modf", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoublePtr>]>,
           FunctionSpec<"modff", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatPtr>]>,
           FunctionSpec<"modfl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoublePtr>]>,
-          FunctionSpec<"modff128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Ptr>]>,
+          GuardedFunctionSpec<"modff128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Ptr>], "LIBC_TYPES_HAS_FLOAT128">,
 
           FunctionSpec<"cos", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
           FunctionSpec<"cosf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,



More information about the libc-commits mailing list