[libc-commits] [libc] [libc][math][c23] Add acospif16() function (PR #132754)

via libc-commits libc-commits at lists.llvm.org
Mon Mar 24 11:52:02 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Anton (amemov)

<details>
<summary>Changes</summary>

Addresses #<!-- -->132211 
Part of #<!-- -->95250


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


12 Files Affected:

- (modified) libc/config/linux/x86_64/entrypoints.txt (+1) 
- (modified) libc/docs/headers/math/index.rst (+1-1) 
- (modified) libc/include/math.yaml (+7) 
- (modified) libc/src/math/CMakeLists.txt (+1) 
- (added) libc/src/math/acospif16.h (+22) 
- (modified) libc/src/math/generic/CMakeLists.txt (+20) 
- (added) libc/src/math/generic/acospif16.cpp (+21) 
- (modified) libc/test/src/math/CMakeLists.txt (+12) 
- (modified) libc/test/src/math/acosf16_test.cpp (+1-1) 
- (added) libc/test/src/math/acospif16_test.cpp (+42) 
- (modified) libc/test/src/math/smoke/CMakeLists.txt (+12) 
- (added) libc/test/src/math/smoke/acospif16_test.cpp (+39) 


``````````diff
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 124b80d03d846..2f1e16cd20a0e 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -654,6 +654,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
     # math.h C23 _Float16 entrypoints
     libc.src.math.asinf16
     libc.src.math.acosf16
+    libc.src.math.acospif16
     libc.src.math.canonicalizef16
     libc.src.math.ceilf16
     libc.src.math.copysignf16
diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index 85585f4fc5f3d..f59828f22a3a2 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -253,7 +253,7 @@ Higher Math Functions
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
 | acosh     | |check|          |                 |                        |                      |                        | 7.12.5.1               | F.10.2.1                   |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| acospi    |                  |                 |                        |                      |                        | 7.12.4.8               | F.10.1.8                   |
+| acospi    |                  |                 |                        | |check|              |                        | 7.12.4.8               | F.10.1.8                   |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
 | asin      | |check|          |                 |                        | |check|              |                        | 7.12.4.2               | F.10.1.2                   |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/include/math.yaml b/libc/include/math.yaml
index fbfc51348411a..dfe04732fbd03 100644
--- a/libc/include/math.yaml
+++ b/libc/include/math.yaml
@@ -21,6 +21,13 @@ functions:
     arguments:
       - type: _Float16
     guard: LIBC_TYPES_HAS_FLOAT16
+  - name: acospif16
+    standards:
+      - stdc
+    return_type: _Float16
+    arguments:
+      - type: _Float16
+    guard: LIBC_TYPES_HAS_FLOAT16
   - name: acoshf
     standards:
       - stdc
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index f18a73d46f9aa..6f22c51e06933 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -43,6 +43,7 @@ endfunction()
 add_math_entrypoint_object(acos)
 add_math_entrypoint_object(acosf)
 add_math_entrypoint_object(acosf16)
+add_math_entrypoint_object(acospif16)
 
 add_math_entrypoint_object(acosh)
 add_math_entrypoint_object(acoshf)
diff --git a/libc/src/math/acospif16.h b/libc/src/math/acospif16.h
new file mode 100644
index 0000000000000..38b1fe0a66be5
--- /dev/null
+++ b/libc/src/math/acospif16.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for acospif16 ---------------------*- 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_ACOSPIF16_H
+#define LLVM_LIBC_SRC_MATH_ACOSPIF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+#include "src/math/acosf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float16 acospif16(float16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_ACOSPIF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index db07bd1a098cc..0825551baac25 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4054,6 +4054,26 @@ add_entrypoint_object(
     libc.src.__support.macros.properties.types  
 )
 
+add_entrypoint_object(
+  acospif16
+  SRCS
+    acospif16.cpp
+  HDRS
+    ../acospif16.h
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.fenv_macros
+    libc.src.__support.FPUtil.cast
+    libc.src.__support.FPUtil.except_value_utils
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.multiply_add
+    libc.src.__support.FPUtil.polyeval
+    libc.src.__support.FPUtil.sqrt
+    libc.src.__support.macros.optimization
+    libc.src.__support.macros.properties.types  
+)
+
 add_header_library(
   atan_utils
   HDRS
diff --git a/libc/src/math/generic/acospif16.cpp b/libc/src/math/generic/acospif16.cpp
new file mode 100644
index 0000000000000..2fab197fff5c2
--- /dev/null
+++ b/libc/src/math/generic/acospif16.cpp
@@ -0,0 +1,21 @@
+//===-- Half-precision acospif16(x) 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/acospif16.h"
+#include "src/__support/FPUtil/cast.h"
+
+namespace LIBC_NAMESPACE_DECL {
+// Generated by Sollya using the following command:
+// > round(pi, SG, RN);
+static constexpr float PI = 0x1.921fb6p1f;
+
+LLVM_LIBC_FUNCTION(float16, acospif16, (float16 x)) {
+    return fputil::cast<float16>(acosf16(x) / PI);  
+}
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index beafa87e03a77..b24d4a663693e 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -2221,6 +2221,18 @@ add_fp_unittest(
     libc.src.math.acosf16  
 )
 
+add_fp_unittest(
+  acospif16_test
+  NEED_MPFR
+  SUITE
+    libc-math-unittests
+  SRCS
+    acospif16_test.cpp
+  DEPENDS
+    libc.src.math.acospif16
+    libc.src.math.acosf16  
+)
+
 add_fp_unittest(
   atanf_test
   NEED_MPFR
diff --git a/libc/test/src/math/acosf16_test.cpp b/libc/test/src/math/acosf16_test.cpp
index f4890c81b0bcb..83f986bce9991 100644
--- a/libc/test/src/math/acosf16_test.cpp
+++ b/libc/test/src/math/acosf16_test.cpp
@@ -10,7 +10,7 @@
 #include "test/UnitTest/FPMatcher.h"
 #include "test/UnitTest/Test.h"
 #include "utils/MPFRWrapper/MPFRUtils.h"
-
+ 
 using LlvmLibcAcosf16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
 
 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
diff --git a/libc/test/src/math/acospif16_test.cpp b/libc/test/src/math/acospif16_test.cpp
new file mode 100644
index 0000000000000..1998dfb38a55e
--- /dev/null
+++ b/libc/test/src/math/acospif16_test.cpp
@@ -0,0 +1,42 @@
+//===-- Exhaustive test for acospif16 -------------------------------------===//
+//
+// 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/acospif16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcAcospif16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+// Range: [0, Inf]
+static constexpr uint16_t POS_START = 0x0000U;
+static constexpr uint16_t POS_STOP = 0x7c00U;
+
+// Range: [-Inf, 0]
+static constexpr uint16_t NEG_START = 0x8000U;
+static constexpr uint16_t NEG_STOP = 0xfc00U;
+
+TEST_F(LlvmLibcAcospif16Test, PositiveRange) {
+  for (uint16_t v = POS_START; v <= POS_STOP; ++v) {
+    float16 x = FPBits(v).get_val();
+
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acos, x,
+                                   LIBC_NAMESPACE::acospif16(x), 0.5);
+  }
+}
+
+TEST_F(LlvmLibcAcospif16Test, NegativeRange) {
+  for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) {
+    float16 x = FPBits(v).get_val();
+
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acos, x,
+                                   LIBC_NAMESPACE::acospif16(x), 0.5);
+  }
+}
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 94ec099ddfcbc..c7b88a33d37b9 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3991,6 +3991,18 @@ add_fp_unittest(
     libc.src.math.acosf16  
 )
 
+add_fp_unittest(
+  acospif16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    acospif16_test.cpp
+  DEPENDS
+    libc.src.errno.errno
+    libc.src.math.acospif16  
+    libc.src.math.acosf16 
+)
+
 add_fp_unittest(
   atanf_test
   SUITE
diff --git a/libc/test/src/math/smoke/acospif16_test.cpp b/libc/test/src/math/smoke/acospif16_test.cpp
new file mode 100644
index 0000000000000..d704af607291e
--- /dev/null
+++ b/libc/test/src/math/smoke/acospif16_test.cpp
@@ -0,0 +1,39 @@
+//===-- Unittests for acospif16 -------------------------------------------===//
+//
+//
+// 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/errno/libc_errno.h"
+#include "src/math/acospif16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcAcospif16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
+TEST_F(LlvmLibcAcospif16Test, SpecialNumbers) {
+    LIBC_NAMESPACE::libc_errno = 0;
+    EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acospif16(aNaN));
+    EXPECT_MATH_ERRNO(0);
+  
+    EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::acospif16(sNaN), FE_INVALID);
+    EXPECT_MATH_ERRNO(0);
+  
+    EXPECT_FP_EQ(zero, LIBC_NAMESPACE::acospif16(1.0f));
+    EXPECT_MATH_ERRNO(0);
+  
+    EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acospif16(inf));
+    EXPECT_MATH_ERRNO(EDOM);
+  
+    EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acospif16(neg_inf));
+    EXPECT_MATH_ERRNO(EDOM);
+  
+    EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acospif16(2.0f));
+    EXPECT_MATH_ERRNO(EDOM);
+  
+    EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acospif16(-2.0f));
+    EXPECT_MATH_ERRNO(EDOM);
+  }
+  
\ No newline at end of file

``````````

</details>


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


More information about the libc-commits mailing list