[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:35:08 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-amdgpu
@llvm/pr-subscribers-libc
Author: Zorojuro (Sukumarsawant)
<details>
<summary>Changes</summary>
This PR intends to add atanpi math function for bfloat16 type, along with exhaustive tests for it.
---
Patch is 22.52 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/189151.diff
28 Files Affected:
- (modified) libc/config/baremetal/aarch64/entrypoints.txt (+1)
- (modified) libc/config/baremetal/arm/entrypoints.txt (+1)
- (modified) libc/config/baremetal/riscv/entrypoints.txt (+1)
- (modified) libc/config/darwin/aarch64/entrypoints.txt (+1)
- (modified) libc/config/darwin/x86_64/entrypoints.txt (+1)
- (modified) libc/config/gpu/amdgpu/entrypoints.txt (+1)
- (modified) libc/config/gpu/nvptx/entrypoints.txt (+1)
- (modified) libc/config/linux/aarch64/entrypoints.txt (+1)
- (modified) libc/config/linux/arm/entrypoints.txt (+1)
- (modified) libc/config/linux/riscv/entrypoints.txt (+1)
- (modified) libc/config/linux/x86_64/entrypoints.txt (+1)
- (modified) libc/config/windows/entrypoints.txt (+1)
- (modified) libc/docs/headers/math/index.rst (+1-1)
- (modified) libc/shared/math.h (+1)
- (added) libc/shared/math/atanpibf16.h (+23)
- (modified) libc/src/__support/math/CMakeLists.txt (+16-1)
- (added) libc/src/__support/math/atanpibf16.h (+104)
- (modified) libc/src/math/CMakeLists.txt (+1)
- (added) libc/src/math/atanpibf16.h (+21)
- (modified) libc/src/math/generic/CMakeLists.txt (+10)
- (added) libc/src/math/generic/atanpibf16.cpp (+18)
- (modified) libc/test/shared/CMakeLists.txt (+1)
- (modified) libc/test/shared/shared_math_test.cpp (+1)
- (modified) libc/test/src/math/CMakeLists.txt (+13)
- (added) libc/test/src/math/atanpibf16_test.cpp (+45)
- (modified) libc/test/src/math/smoke/CMakeLists.txt (+13)
- (added) libc/test/src/math/smoke/atanpibf16_test.cpp (+40)
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+21)
``````````diff
diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index 84600efc5e0d1..e1fbcdbb352a9 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -790,6 +790,7 @@ endif()
list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
libc.src.math.atanbf16
+ libc.src.math.atanpibf16
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 902f6ed44179b..3c536b1d89073 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -800,6 +800,7 @@ endif()
list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
libc.src.math.atanbf16
+ libc.src.math.atanpibf16
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 30f9bccd22214..e5c6fc203bd40 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -796,6 +796,7 @@ endif()
list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
libc.src.math.atanbf16
+ libc.src.math.atanpibf16
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 4d27e994323fd..1b090d9dd58aa 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -609,6 +609,7 @@ endif()
list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
libc.src.math.atanbf16
+ libc.src.math.atanpibf16
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index cad973f1ac069..3d5f5af62c0ba 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -235,6 +235,7 @@ set(TARGET_LIBM_ENTRYPOINTS
list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
libc.src.math.atanbf16
+ libc.src.math.atanpibf16
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index 7aaf3470dceab..ed7299127ac0f 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -624,6 +624,7 @@ endif()
list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
libc.src.math.atanbf16
+ libc.src.math.atanpibf16
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index 96c0902d729dc..4eecdb0844e6c 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -626,6 +626,7 @@ endif()
list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
libc.src.math.atanbf16
+ libc.src.math.atanpibf16
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index d6569e59a3e5f..3eb45fc134966 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -870,6 +870,7 @@ endif()
list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
libc.src.math.atanbf16
+ libc.src.math.atanpibf16
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 9fd45329ad0f9..e42a57e7fc3eb 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -476,6 +476,7 @@ set(TARGET_LIBM_ENTRYPOINTS
list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
libc.src.math.atanbf16
+ libc.src.math.atanpibf16
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index fdfce36b824f0..a4666f38c8210 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -891,6 +891,7 @@ endif()
list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
libc.src.math.atanbf16
+ libc.src.math.atanpibf16
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 8cceaccc314dc..f26e0cafbc112 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -946,6 +946,7 @@ endif()
list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
libc.src.math.atanbf16
+ libc.src.math.atanpibf16
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index ee8072a3465e5..cee7ca319ee52 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -322,6 +322,7 @@ set(TARGET_LIBM_ENTRYPOINTS
list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
libc.src.math.atanbf16
+ libc.src.math.atanpibf16
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index 202ef034b8ace..1556982cf48b2 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -279,7 +279,7 @@ Higher Math Functions
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| atanh | |check| | | | |check| | | | 7.12.5.3 | F.10.2.3 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| atanpi | | | | |check| | | | 7.12.4.10 | F.10.1.10 |
+| atanpi | | | | |check| | | |check| | 7.12.4.10 | F.10.1.10 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| cbrt | |check| | |check| | | | | |check| | 7.12.7.1 | F.10.4.1 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 6add9628cabdd..bd6e0e9820355 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -36,6 +36,7 @@
#include "math/atanhf.h"
#include "math/atanhf16.h"
#include "math/atanpif16.h"
+#include "math/atanpibf16.h"
#include "math/bf16add.h"
#include "math/bf16addf.h"
#include "math/bf16addf128.h"
diff --git a/libc/shared/math/atanpibf16.h b/libc/shared/math/atanpibf16.h
new file mode 100644
index 0000000000000..69c55accda3cc
--- /dev/null
+++ b/libc/shared/math/atanpibf16.h
@@ -0,0 +1,23 @@
+//===-- Shared atanpibf16 function ------------------------------*- 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_SHARED_MATH_ATANPIBF16_H
+#define LLVM_LIBC_SHARED_MATH_ATANPIBF16_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atanpibf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atanpibf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATANPIBF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index c3ffa2f79dd3e..67cb6e4368aff 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -304,7 +304,6 @@ add_header_library(
libc.src.__support.FPUtil.sqrt
libc.src.__support.macros.optimization
libc.src.__support.FPUtil.bfloat16
-
)
add_header_library(
@@ -364,6 +363,22 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atanpibf16
+ HDRS
+ atanpibf16.h
+ DEPENDS
+ libc.hdr.fenv_macros
+ libc.src.__support.FPUtil.cast
+ 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.FPUtil.bfloat16
+)
+
add_header_library(
atanpif16
HDRS
diff --git a/libc/src/__support/math/atanpibf16.h b/libc/src/__support/math/atanpibf16.h
new file mode 100644
index 0000000000000..01b2ad915e959
--- /dev/null
+++ b/libc/src/__support/math/atanpibf16.h
@@ -0,0 +1,104 @@
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_ATANPIBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANPIBF16_H
+
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.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 {
+
+LIBC_INLINE bfloat16 atanpibf16(bfloat16 x) {
+
+ using FPBits = fputil::FPBits<bfloat16>;
+ FPBits xbits(x);
+
+ uint16_t x_u = xbits.uintval();
+ uint16_t x_abs = x_u & 0x7fff;
+ bool x_sign = x_u >> 15;
+ float sign = (x_sign ? -1.0f : 1.0f);
+
+ // Taylor series for atan
+ // atan(x) = [x - x^3/3 + x^5/5 - x^7/7 ...]
+ // = x * [1 - x^2/3 + x^4/5 - x^6/7...]
+ // = x * P(x^2)
+ //
+ // atan(x) = x * P(x^2)
+ //
+ // since atanpi(x) = atan(x)/pi
+ // atanpi(x) = x * poly(x^2)/pi
+ // where [poly(x^2)/pi] <= atan(x)/(x*pi)
+ //
+ // 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]);
+ //
+ // 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);
+};
+
+ float xf = x;
+ float x_sq = xf * xf ;
+
+ // Case 1: |x| <= 1
+ if (x_abs <= 0x3f80) {
+ // atanpibf16(±0) = ±0
+ if (LIBC_UNLIKELY(x_abs == 0))
+ return x;
+ // atanpibf16(±1) = ±0.25
+ if (LIBC_UNLIKELY(x_abs == 0x3f80))
+ return fputil::cast<bfloat16>(sign * 0.25f);
+
+
+ float result = atanpi_eval(x_sq);
+ return fputil::cast<bfloat16>(xf *result );
+ }
+
+ // Case 2: |x| > 1 ( uses range reduction )
+ 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);
+
+ // atan(x) = sign(x) * (pi/2 - atan(1/|x|))
+ // atan(x)/pi = sign(x) * ((pi/2)/pi) - ((atan(1/|x|))/pi))
+ // atanpi(x) = sign(x) * ((0.5) - atanpi(1/|x|))
+ // Since 1/|x| < 1, we can use the same polynomial.
+ float x_inv_sq = 1.0f / x_sq;
+ float x_inv = fputil::sqrt<float>(x_inv_sq);
+
+ float result = atanpi_eval(x_inv_sq);
+ float atan_inv = (x_inv * result);
+ return fputil::cast<bfloat16>(sign * (0.5 - atan_inv));
+ }
+
+ // Case 3: |x| is ±inf or NaN
+ if (xbits.is_nan()) {
+ if (xbits.is_signaling_nan()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+ return x;
+ }
+ // atanpibf16( ±inf )/pi = ±((pi/2)/pi) = ± 1/2
+ return fputil::cast<bfloat16>(sign * 0.5f);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ATANPIBF16_H
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 5bdb8d99eedeb..7d88e0cfeb77d 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -77,6 +77,7 @@ add_math_entrypoint_object(atanh)
add_math_entrypoint_object(atanhf)
add_math_entrypoint_object(atanhf16)
+add_math_entrypoint_object(atanpibf16)
add_math_entrypoint_object(atanpif16)
add_math_entrypoint_object(canonicalize)
diff --git a/libc/src/math/atanpibf16.h b/libc/src/math/atanpibf16.h
new file mode 100644
index 0000000000000..0ae3e4d371fea
--- /dev/null
+++ b/libc/src/math/atanpibf16.h
@@ -0,0 +1,21 @@
+//===-- 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.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_ATANPIBF16_H
+#define LLVM_LIBC_SRC_MATH_ATANPIBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 atanpibf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_ATANPIBF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 40d21ffaaa524..69a4ac973b36e 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4058,6 +4058,16 @@ add_entrypoint_object(
libc.src.__support.math.atanhf16
)
+add_entrypoint_object(
+ atanpibf16
+ SRCS
+ atanpibf16.cpp
+ HDRS
+ ../atanpibf16.h
+ DEPENDS
+ libc.src.__support.math.atanpibf16
+)
+
add_entrypoint_object(
atanpif16
SRCS
diff --git a/libc/src/math/generic/atanpibf16.cpp b/libc/src/math/generic/atanpibf16.cpp
new file mode 100644
index 0000000000000..35830810c5c75
--- /dev/null
+++ b/libc/src/math/generic/atanpibf16.cpp
@@ -0,0 +1,18 @@
+//===-- Bfloat16 atanpi 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/atanpibf16.h"
+#include "src/__support/math/atanpibf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, atanpibf16, (bfloat16 x)) {
+ return math::atanpibf16(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 9ea5d1bd359c7..29fa1216d4953 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -32,6 +32,7 @@ add_fp_unittest(
libc.src.__support.math.atanf16
libc.src.__support.math.atanhf
libc.src.__support.math.atanhf16
+ libc.src.__support.math.atanpibf16
libc.src.__support.math.atanpif16
libc.src.__support.math.bf16add
libc.src.__support.math.bf16addf
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 5afb982dd57ed..4c3fa3531797b 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -423,6 +423,7 @@ 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(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/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index ceed946e2386e..028e65bf6402c 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -2389,6 +2389,19 @@ add_fp_unittest(
libc.src.math.atanhf16
)
+add_fp_unittest(
+ atanpibf16_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ atanpibf16_test.cpp
+ DEPENDS
+ libc.src.math.atanpibf16
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.bfloat16
+)
+
add_fp_unittest(
atanpif16_test
NEED_MPFR
diff --git a/libc/test/src/math/atanpibf16_test.cpp b/libc/test/src/math/atanpibf16_test.cpp
new file mode 100644
index 0000000000000..2b4586fc6f3fe
--- /dev/null
+++ b/libc/test/src/math/atanpibf16_test.cpp
@@ -0,0 +1,45 @@
+//===-- 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.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/atanpibf16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcAtanpiBf16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+// Normal range: [+0, +int]
+static constexpr uint16_t POS_START = 0x0000U;
+static constexpr uint16_t POS_STOP = 0x7f80U;
+
+// Normal range: [-0, -int]
+static constexpr uint16_t NEG_START = 0x8000U;
+static constexpr uint16_t NEG_STOP = 0xff80U;
+
+TEST_F(LlvmLibcAtanpiBf16Test, NormalPositiveRange) {
+ for (uint16_t v1 = POS_START; v1 <= POS_STOP; v1++) {
+
+ bfloat16 x = FPBits(v1).get_val();
+
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atanpi, x,
+ LIBC_NAMESPACE::atanpibf16(x), 0.5);
+ }
+}
+
+TEST_F(LlvmLibcAtanpiBf16Test, NormalNegativeRange) {
+ for (uint16_t v1 = NEG_START; v1 <= NEG_STOP; v1++) {
+
+ bfloat16 x = FPBits(v1).get_val();
+
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atanpi, x,
+ LIBC_NAMESPACE::atanpibf16(x), 0.5);
+ }
+}
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 9d97c90cfaeb9..89a22812e47d7 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -4710,6 +4710,19 @@ add_fp_unittest(
libc.src.__support.FPUtil.cast
)
+add_fp_unittest(
+ atanpibf16_test
+ SUITE
+ libc-math-unittests
+ SRCS
+ atanpibf16_test.cpp
+ DEPENDS
+ libc.src.math.atanpibf16
+ libc.hdr.errno_macros
+ libc.hdr.fenv_macros
+ libc.src.__support.FPUtil.bfloat16
+)
+
add_fp_unittest(
atanpif16_test
NEED_MPFR
diff --git a/libc/test/src/math/smoke/atanpibf16_test.cpp b/libc/test/src/math/smoke/atanpibf16_test.cpp
new file mode 100644
index 0000000000000..ee8b26789d132
--- /dev/null
+++ b/libc/test/src/math/smoke/atanpibf16_test.cpp
@@ -0,0 +1,40 @@
+//===-- Unittests for atanpibf16 ------------------------------------------===//
+//
+//...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/189151
More information about the libc-commits
mailing list