[libc-commits] [libc] [llvm] [libc][math][c23] Add atanpibf16 math function (PR #189151)

via libc-commits libc-commits at lists.llvm.org
Sat Mar 28 01:36:27 PDT 2026


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff origin/main HEAD --extensions cpp,h -- libc/shared/math/atanpibf16.h libc/src/__support/math/atanpibf16.h libc/src/math/atanpibf16.h libc/src/math/generic/atanpibf16.cpp libc/test/src/math/atanpibf16_test.cpp libc/test/src/math/smoke/atanpibf16_test.cpp libc/shared/math.h libc/test/shared/shared_math_test.cpp --diff_from_common_commit
``````````

:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 9ff35e539..62174c559 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -35,8 +35,8 @@
 #include "math/atanf16.h"
 #include "math/atanhf.h"
 #include "math/atanhf16.h"
-#include "math/atanpif16.h"
 #include "math/atanpibf16.h"
+#include "math/atanpif16.h"
 #include "math/bf16add.h"
 #include "math/bf16addf.h"
 #include "math/bf16addf128.h"
diff --git a/libc/src/__support/math/atanpibf16.h b/libc/src/__support/math/atanpibf16.h
index 01b2ad915..073925580 100644
--- a/libc/src/__support/math/atanpibf16.h
+++ b/libc/src/__support/math/atanpibf16.h
@@ -9,11 +9,11 @@
 #include "src/__support/FPUtil/FEnvImpl.h"
 #include "src/__support/FPUtil/FPBits.h"
 #include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/bfloat16.h"
 #include "src/__support/FPUtil/cast.h"
 #include "src/__support/FPUtil/multiply_add.h"
 #include "src/__support/FPUtil/sqrt.h"
 #include "src/__support/macros/optimization.h"
-#include "src/__support/FPUtil/bfloat16.h"
 
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
@@ -41,17 +41,20 @@ LIBC_INLINE bfloat16 atanpibf16(bfloat16 x) {
   //
   // Degree 12 polynomial of atanpi(x) generated using Sollya with command :
   // > display = hexadecimal ;
-  // > P = fpminimax(atan(x)/(x*pi), [|0, 2, 4, 6, 8, 10, 12|], [|SG,SG,SG,SG,SG,SG,SG|], [0, 1]);
+  // > P = fpminimax(atan(x)/(x*pi), [|0, 2, 4, 6, 8, 10, 12|],
+  // [|SG,SG,SG,SG,SG,SG,SG|], [0, 1]);
   //
   // relative error for the polynomial given by:
   // > dirtyinfnorm(atan(x)/(x*pi) - P(x^2), [0, 1]);
   // error - 0x1.db939p-23
-auto atanpi_eval = [](float x0) {
-    return fputil::polyeval(x0, 0x1.45f2f8p-2f, -0x1.b28236p-4f, 0x1.0333bp-4f, -0x1.5f9b92p-5f, 0x1.b520c4p-6f, -0x1.819ef4p-7f, 0x1.4789p-9f);
-};
+  auto atanpi_eval = [](float x0) {
+    return fputil::polyeval(x0, 0x1.45f2f8p-2f, -0x1.b28236p-4f, 0x1.0333bp-4f,
+                            -0x1.5f9b92p-5f, 0x1.b520c4p-6f, -0x1.819ef4p-7f,
+                            0x1.4789p-9f);
+  };
 
   float xf = x;
-  float x_sq = xf * xf ;
+  float x_sq = xf * xf;
 
   // Case 1: |x| <= 1
   if (x_abs <= 0x3f80) {
@@ -60,19 +63,18 @@ auto atanpi_eval = [](float x0) {
       return x;
     // atanpibf16(±1) = ±0.25
     if (LIBC_UNLIKELY(x_abs == 0x3f80))
-        return fputil::cast<bfloat16>(sign * 0.25f);
-
+      return fputil::cast<bfloat16>(sign * 0.25f);
 
-    float result = atanpi_eval(x_sq); 
-    return fputil::cast<bfloat16>(xf *result );
+    float result = atanpi_eval(x_sq);
+    return fputil::cast<bfloat16>(xf * result);
   }
 
   // Case 2: |x| > 1 ( uses range reduction )
-    if(x_abs < 0x7F80){
+  if (x_abs < 0x7F80) {
 
     // For Large x in bfloat16 the value is close to 0.5 but not exactly 0.5
-    if (LIBC_UNLIKELY(x_abs >= 0x43a3)) 
-    return fputil::cast<bfloat16>(sign * 0x1.fffffep-2f); 
+    if (LIBC_UNLIKELY(x_abs >= 0x43a3))
+      return fputil::cast<bfloat16>(sign * 0x1.fffffep-2f);
 
     // atan(x) = sign(x) * (pi/2 - atan(1/|x|))
     // atan(x)/pi = sign(x) * ((pi/2)/pi) - ((atan(1/|x|))/pi))
@@ -82,7 +84,7 @@ auto atanpi_eval = [](float x0) {
     float x_inv = fputil::sqrt<float>(x_inv_sq);
 
     float result = atanpi_eval(x_inv_sq);
-    float atan_inv = (x_inv *  result);
+    float atan_inv = (x_inv * result);
     return fputil::cast<bfloat16>(sign * (0.5 - atan_inv));
   }
 
diff --git a/libc/src/math/atanpibf16.h b/libc/src/math/atanpibf16.h
index 0ae3e4d37..004a8edd2 100644
--- a/libc/src/math/atanpibf16.h
+++ b/libc/src/math/atanpibf16.h
@@ -1,4 +1,5 @@
-//===-- Implementation header for atanpibf16 ---------------------*- C++ -*-===//
+//===-- Implementation header for atanpibf16 ---------------------*- C++
+//-*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 26e7e140d..f4881278d 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -424,7 +424,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
 TEST(LlvmLibcSharedMathTest, AllBFloat16) {
   using FPBits = LIBC_NAMESPACE::fputil::FPBits<bfloat16>;
   EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::atanbf16(bfloat16(0.0)));
-  EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::atanpibf16(bfloat16(0.0)));
+  EXPECT_FP_EQ(bfloat16(0.0),
+               LIBC_NAMESPACE::shared::atanpibf16(bfloat16(0.0)));
   EXPECT_FP_EQ(bfloat16(5.0), LIBC_NAMESPACE::shared::bf16add(2.0, 3.0));
   EXPECT_FP_EQ(bfloat16(2.0f), LIBC_NAMESPACE::shared::bf16divf(4.0f, 2.0f));
   EXPECT_FP_EQ(bfloat16(2.0), LIBC_NAMESPACE::shared::bf16divl(6.0L, 3.0L));
diff --git a/libc/test/src/math/atanpibf16_test.cpp b/libc/test/src/math/atanpibf16_test.cpp
index 2b4586fc6..4bd2ef4e2 100644
--- a/libc/test/src/math/atanpibf16_test.cpp
+++ b/libc/test/src/math/atanpibf16_test.cpp
@@ -1,4 +1,5 @@
-//===-- Exhaustive test for atanpibf16 -------------------------------------===//
+//===-- Exhaustive test for atanpibf16
+//-------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/libc/test/src/math/smoke/atanpibf16_test.cpp b/libc/test/src/math/smoke/atanpibf16_test.cpp
index ee8b26789..40687ec9e 100644
--- a/libc/test/src/math/smoke/atanpibf16_test.cpp
+++ b/libc/test/src/math/smoke/atanpibf16_test.cpp
@@ -31,8 +31,9 @@ public:
 
     EXPECT_FP_EQ_ALL_ROUNDING(bfloat16(0.5), LIBC_NAMESPACE::atanpibf16(inf));
     EXPECT_MATH_ERRNO(0);
-    
-    EXPECT_FP_EQ_ALL_ROUNDING(bfloat16(-0.5), LIBC_NAMESPACE::atanpibf16(neg_inf));
+
+    EXPECT_FP_EQ_ALL_ROUNDING(bfloat16(-0.5),
+                              LIBC_NAMESPACE::atanpibf16(neg_inf));
     EXPECT_MATH_ERRNO(0);
   }
 };

``````````

</details>


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


More information about the libc-commits mailing list