Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 0 commits into from

Conversation

amemov
Copy link
Contributor

@amemov amemov commented Mar 24, 2025

Addresses #132211
Part of #95250

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@amemov amemov marked this pull request as ready for review March 24, 2025 18:51
@llvmbot llvmbot added the libc label Mar 24, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 24, 2025

@llvm/pr-subscribers-backend-spir-v
@llvm/pr-subscribers-lld-coff
@llvm/pr-subscribers-lld-macho
@llvm/pr-subscribers-clangir
@llvm/pr-subscribers-flang-parser
@llvm/pr-subscribers-flang-openmp
@llvm/pr-subscribers-compiler-rt-sanitizer
@llvm/pr-subscribers-lld
@llvm/pr-subscribers-clang-tidy
@llvm/pr-subscribers-clang-tools-extra
@llvm/pr-subscribers-clangd

@llvm/pr-subscribers-libc

Author: Anton (amemov)

Changes

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 --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

@amemov
Copy link
Contributor Author

amemov commented Mar 24, 2025

Have a problem with how exhaustive test file compares result - will need to update MPFRWrapper to support Acospi and compare results correctly in libc/test/src/math/acospif16_test.cpp. Other than that (and running ./clang-format on the C++ files in the PR), should be ok. If you have corrections/suggestions about the implementation, please feel free to leave feedback.

@lntue lntue requested review from overmighty and lntue March 25, 2025 14:15
@overmighty
Copy link
Member

@amemov Are you still working on this? The CI build failed due to this error:

/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-clshd-1/llvm-project/github-pull-requests/libc/utils/MPFRWrapper/MPCommon.cpp:75:3: error: use of undeclared identifier 'mpfr_acospi'

I assume this is once again due to the CI using an older version of MPFR. @lntue

@lntue
Copy link
Contributor

lntue commented Apr 2, 2025

@amemov Are you still working on this? The CI build failed due to this error:

/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-clshd-1/llvm-project/github-pull-requests/libc/utils/MPFRWrapper/MPCommon.cpp:75:3: error: use of undeclared identifier 'mpfr_acospi'

I assume this is once again due to the CI using an older version of MPFR. @lntue

We need to add a workaround for older versions of MPFR then. At least it is quite straightforward for acospi.

@amemov
Copy link
Contributor Author

amemov commented Apr 2, 2025

@amemov Are you still working on this? The CI build failed due to this error:

/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-clshd-1/llvm-project/github-pull-requests/libc/utils/MPFRWrapper/MPCommon.cpp:75:3: error: use of undeclared identifier 'mpfr_acospi'

I assume this is once again due to the CI using an older version of MPFR. @lntue

Yes, I working on both issues that are assigned to me in libc - acospif16 and rsqrtf16.

I almost finished the implementation of acospif16.cpp -- I think I did write the approximations and polynomials right, but had to put it on hold temporarily because of personal circumstances. Still need to test it though on my machine, and to do that I need to do what you mentioned here.

I will resume the work on it hopefully next week!

@amemov
Copy link
Contributor Author

amemov commented Apr 4, 2025

Not 10000% why it failed here. I checked the logs, and it says that all test files for acospif16 ran successfully.

@amemov amemov requested a review from lntue April 6, 2025 21:05
@lntue lntue removed flang:codegen flang:parser clang:openmp OpenMP related changes to Clang ClangIR Anything related to the ClangIR project labels Apr 7, 2025
@lntue lntue removed this from HLSL Support Apr 7, 2025
@lntue
Copy link
Contributor

lntue commented Apr 7, 2025

Can you sync the PR to resolve merge conflicts?

@lntue lntue added the c23 label Apr 7, 2025
@amemov amemov closed this Apr 7, 2025
@amemov amemov force-pushed the acospif16-for-c23 branch from aabe6c5 to 8fddef8 Compare April 7, 2025 14:07
lntue pushed a commit that referenced this pull request Apr 24, 2025
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Ankur-0429 pushed a commit to Ankur-0429/llvm-project that referenced this pull request May 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants