[libc-commits] [libc] [libc][math][c23] Add f16div{, l, f128} C23 math functions (PR #97054)

via libc-commits libc-commits at lists.llvm.org
Fri Jun 28 06:08:05 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: OverMighty (overmighty)

<details>
<summary>Changes</summary>

Part of #<!-- -->93566.

---
Full diff: https://github.com/llvm/llvm-project/pull/97054.diff


20 Files Affected:

- (modified) libc/config/linux/aarch64/entrypoints.txt (+1) 
- (modified) libc/config/linux/x86_64/entrypoints.txt (+3) 
- (modified) libc/docs/math/index.rst (+1-1) 
- (modified) libc/spec/stdc.td (+3) 
- (modified) libc/src/math/CMakeLists.txt (+3) 
- (added) libc/src/math/f16div.h (+20) 
- (added) libc/src/math/f16divf128.h (+20) 
- (added) libc/src/math/f16divl.h (+20) 
- (modified) libc/src/math/generic/CMakeLists.txt (+39) 
- (added) libc/src/math/generic/f16div.cpp (+19) 
- (added) libc/src/math/generic/f16divf128.cpp (+19) 
- (added) libc/src/math/generic/f16divl.cpp (+19) 
- (modified) libc/test/src/math/CMakeLists.txt (+26) 
- (added) libc/test/src/math/f16div_test.cpp (+13) 
- (added) libc/test/src/math/f16divl_test.cpp (+13) 
- (modified) libc/test/src/math/smoke/CMakeLists.txt (+42) 
- (added) libc/test/src/math/smoke/f16div_test.cpp (+13) 
- (added) libc/test/src/math/smoke/f16divf128_test.cpp (+13) 
- (added) libc/test/src/math/smoke/f16divl_test.cpp (+13) 
- (modified) libc/utils/MPFRWrapper/MPFRUtils.cpp (+11) 


``````````diff
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index fbf8c4b5a7581..8a26536cea9a0 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.f16div
     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 9581f7f2604c4..e1922ca94b97e 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -537,7 +537,9 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.math.canonicalizef16
     libc.src.math.ceilf16
     libc.src.math.copysignf16
+    libc.src.math.f16div
     libc.src.math.f16divf
+    libc.src.math.f16divl
     libc.src.math.f16fma
     libc.src.math.f16fmaf
     libc.src.math.f16fmal
@@ -595,6 +597,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
     list(APPEND TARGET_LIBM_ENTRYPOINTS
       # math.h C23 mixed _Float16 and _Float128 entrypoints
       libc.src.math.f16fmaf128
+      libc.src.math.f16divf128
     )
   endif()
 endif()
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index 56cc8d658257d..4aa7a327a7f73 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                    |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| f16div           | |check|          |                 |                        | N/A                  |                        | 7.12.14.4              | F.10.11                    |
+| f16div           | |check|          | |check|         | |check|                | N/A                  | |check|                | 7.12.14.4              | F.10.11                    |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
 | f16fma           | |check|          | |check|         | |check|                | N/A                  | |check|                | 7.12.14.5              | F.10.11                    |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index adac7d5932428..673661cd3d3a7 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -729,7 +729,10 @@ def StdC : StandardSpec<"stdc"> {
 
           GuardedFunctionSpec<"setpayloadsigf16", RetValSpec<IntType>, [ArgSpec<Float16Ptr>, ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
 
+          GuardedFunctionSpec<"f16div", RetValSpec<Float16Type>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>], "LIBC_TYPES_HAS_FLOAT16">,
           GuardedFunctionSpec<"f16divf", RetValSpec<Float16Type>, [ArgSpec<FloatType>, ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
+          GuardedFunctionSpec<"f16divl", RetValSpec<Float16Type>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>], "LIBC_TYPES_HAS_FLOAT16">,
+          GuardedFunctionSpec<"f16divf128", RetValSpec<Float16Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128">,
 
           GuardedFunctionSpec<"f16sqrtf", RetValSpec<Float16Type>, [ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
       ]
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 3dfc4ac94827d..fb0d971e88733 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -99,7 +99,10 @@ add_math_entrypoint_object(exp10f)
 add_math_entrypoint_object(expm1)
 add_math_entrypoint_object(expm1f)
 
+add_math_entrypoint_object(f16div)
 add_math_entrypoint_object(f16divf)
+add_math_entrypoint_object(f16divl)
+add_math_entrypoint_object(f16divf128)
 
 add_math_entrypoint_object(f16fma)
 add_math_entrypoint_object(f16fmaf)
diff --git a/libc/src/math/f16div.h b/libc/src/math/f16div.h
new file mode 100644
index 0000000000000..3807bc02276c9
--- /dev/null
+++ b/libc/src/math/f16div.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for f16div ------------------------*- 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_F16DIV_H
+#define LLVM_LIBC_SRC_MATH_F16DIV_H
+
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE {
+
+float16 f16div(double x, double y);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_F16DIV_H
diff --git a/libc/src/math/f16divf128.h b/libc/src/math/f16divf128.h
new file mode 100644
index 0000000000000..2f63535ca27ce
--- /dev/null
+++ b/libc/src/math/f16divf128.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for f16divf128 --------------------*- 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_F16DIVF128_H
+#define LLVM_LIBC_SRC_MATH_F16DIVF128_H
+
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE {
+
+float16 f16divf128(float128 x, float128 y);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_F16DIVF128_H
diff --git a/libc/src/math/f16divl.h b/libc/src/math/f16divl.h
new file mode 100644
index 0000000000000..ad9999135b588
--- /dev/null
+++ b/libc/src/math/f16divl.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for f16divl -----------------------*- 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_F16DIVL_H
+#define LLVM_LIBC_SRC_MATH_F16DIVL_H
+
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE {
+
+float16 f16divl(long double x, long double y);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_F16DIVL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 3773a2b49c416..0e4893cd42ee1 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(
+  f16div
+  SRCS
+    f16div.cpp
+  HDRS
+    ../f16div.h
+  DEPENDS
+    libc.src.__support.macros.properties.types
+    libc.src.__support.FPUtil.generic.div
+  COMPILE_OPTIONS
+    -O3
+)
+
 add_entrypoint_object(
   f16divf
   SRCS
@@ -3789,6 +3802,32 @@ add_entrypoint_object(
     -O3
 )
 
+add_entrypoint_object(
+  f16divl
+  SRCS
+    f16divl.cpp
+  HDRS
+    ../f16divl.h
+  DEPENDS
+    libc.src.__support.macros.properties.types
+    libc.src.__support.FPUtil.generic.div
+  COMPILE_OPTIONS
+    -O3
+)
+
+add_entrypoint_object(
+  f16divf128
+  SRCS
+    f16divf128.cpp
+  HDRS
+    ../f16divf128.h
+  DEPENDS
+    libc.src.__support.macros.properties.types
+    libc.src.__support.FPUtil.generic.div
+  COMPILE_OPTIONS
+    -O3
+)
+
 add_entrypoint_object(
   f16fma
   SRCS
diff --git a/libc/src/math/generic/f16div.cpp b/libc/src/math/generic/f16div.cpp
new file mode 100644
index 0000000000000..2ff0ff7865539
--- /dev/null
+++ b/libc/src/math/generic/f16div.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of f16div 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/f16div.h"
+#include "src/__support/FPUtil/generic/div.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float16, f16div, (double x, double y)) {
+  return fputil::generic::div<float16>(x, y);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/generic/f16divf128.cpp b/libc/src/math/generic/f16divf128.cpp
new file mode 100644
index 0000000000000..1d37ad8aa2366
--- /dev/null
+++ b/libc/src/math/generic/f16divf128.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of f16divf128 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/f16divf128.h"
+#include "src/__support/FPUtil/generic/div.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float16, f16divf128, (float128 x, float128 y)) {
+  return fputil::generic::div<float16>(x, y);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/generic/f16divl.cpp b/libc/src/math/generic/f16divl.cpp
new file mode 100644
index 0000000000000..3fb9c7891f5d1
--- /dev/null
+++ b/libc/src/math/generic/f16divl.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of f16divl 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/f16divl.h"
+#include "src/__support/FPUtil/generic/div.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float16, f16divl, (long double x, long double y)) {
+  return fputil::generic::div<float16>(x, y);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 36d2a2fbfd302..061feeadb40d5 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(
+  f16div_test
+  NEED_MPFR
+  SUITE
+    libc-math-unittests
+  SRCS
+    f16div_test.cpp
+  HDRS
+    DivTest.h
+  DEPENDS
+    libc.src.math.f16div
+)
+
 add_fp_unittest(
   f16divf_test
   NEED_MPFR
@@ -1915,6 +1928,19 @@ add_fp_unittest(
     libc.src.math.f16divf
 )
 
+add_fp_unittest(
+  f16divl_test
+  NEED_MPFR
+  SUITE
+    libc-math-unittests
+  SRCS
+    f16divl_test.cpp
+  HDRS
+    DivTest.h
+  DEPENDS
+    libc.src.math.f16divl
+)
+
 add_fp_unittest(
   f16fma_test
   NEED_MPFR
diff --git a/libc/test/src/math/f16div_test.cpp b/libc/test/src/math/f16div_test.cpp
new file mode 100644
index 0000000000000..0bfa69f9b8028
--- /dev/null
+++ b/libc/test/src/math/f16div_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for f16div ----------------------------------------------===//
+//
+// 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 "DivTest.h"
+
+#include "src/math/f16div.h"
+
+LIST_DIV_TESTS(float16, double, LIBC_NAMESPACE::f16div)
diff --git a/libc/test/src/math/f16divl_test.cpp b/libc/test/src/math/f16divl_test.cpp
new file mode 100644
index 0000000000000..bad3e70a477b4
--- /dev/null
+++ b/libc/test/src/math/f16divl_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for f16divl ---------------------------------------------===//
+//
+// 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 "DivTest.h"
+
+#include "src/math/f16divl.h"
+
+LIST_DIV_TESTS(float16, long double, LIBC_NAMESPACE::f16divl)
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index ee6f1595fe0fd..e3d8a14191ad2 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3630,6 +3630,20 @@ add_fp_unittest(
     libc.src.math.setpayloadsigf16
 )
 
+add_fp_unittest(
+  f16div_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    f16div_test.cpp
+  HDRS
+    DivTest.h
+  DEPENDS
+    libc.hdr.fenv_macros
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.math.f16div
+)
+
 add_fp_unittest(
   f16divf_test
   SUITE
@@ -3644,6 +3658,34 @@ add_fp_unittest(
     libc.src.math.f16divf
 )
 
+add_fp_unittest(
+  f16divl_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    f16divl_test.cpp
+  HDRS
+    DivTest.h
+  DEPENDS
+    libc.hdr.fenv_macros
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.math.f16divl
+)
+
+add_fp_unittest(
+  f16divf128_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    f16divf128_test.cpp
+  HDRS
+    DivTest.h
+  DEPENDS
+    libc.hdr.fenv_macros
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.math.f16divf128
+)
+
 add_fp_unittest(
   f16fma_test
   SUITE
diff --git a/libc/test/src/math/smoke/f16div_test.cpp b/libc/test/src/math/smoke/f16div_test.cpp
new file mode 100644
index 0000000000000..0bfa69f9b8028
--- /dev/null
+++ b/libc/test/src/math/smoke/f16div_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for f16div ----------------------------------------------===//
+//
+// 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 "DivTest.h"
+
+#include "src/math/f16div.h"
+
+LIST_DIV_TESTS(float16, double, LIBC_NAMESPACE::f16div)
diff --git a/libc/test/src/math/smoke/f16divf128_test.cpp b/libc/test/src/math/smoke/f16divf128_test.cpp
new file mode 100644
index 0000000000000..d2ea971824621
--- /dev/null
+++ b/libc/test/src/math/smoke/f16divf128_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for f16divf128 ------------------------------------------===//
+//
+// 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 "DivTest.h"
+
+#include "src/math/f16divf128.h"
+
+LIST_DIV_TESTS(float16, float128, LIBC_NAMESPACE::f16divf128)
diff --git a/libc/test/src/math/smoke/f16divl_test.cpp b/libc/test/src/math/smoke/f16divl_test.cpp
new file mode 100644
index 0000000000000..bad3e70a477b4
--- /dev/null
+++ b/libc/test/src/math/smoke/f16divl_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for f16divl ---------------------------------------------===//
+//
+// 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 "DivTest.h"
+
+#include "src/math/f16divl.h"
+
+LIST_DIV_TESTS(float16, long double, LIBC_NAMESPACE::f16divl)
diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp
index d69309077e099..0bfe49984e7d2 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -935,6 +935,10 @@ 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);
+template void explain_binary_operation_one_output_error(
+    Operation, const BinaryInput<long double> &, float16, double, RoundingMode);
 #endif
 
 template <typename InputType, typename OutputType>
@@ -1104,6 +1108,13 @@ 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);
+template bool
+compare_binary_operation_one_output(Operation, const BinaryInput<long double> &,
+                                    float16, double, RoundingMode);
 #endif
 
 template <typename InputType, typename OutputType>

``````````

</details>


https://github.com/llvm/llvm-project/pull/97054


More information about the libc-commits mailing list