[libc-commits] [libc] [libc][math] Add erfbf16 implementation (PR #246449)

via libc-commits libc-commits at lists.llvm.org
Thu Nov 26 08:53:24 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: harshasr60

<details>
<summary>Changes</summary>

Add `erfbf16` support to LLVM libc using a degree-11 polynomial approximation for BFloat16 error function computation.

Handle NaNs, infinities, signed zeros, and all four IEEE floating-point rounding modes, including floating-point exceptions.

Integrate `erfbf16` into LLVM libc with CMake configuration, platform entrypoints, and shared math support.

Parent issue: #<!-- -->177259 

---

Patch is 24.62 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/246449.diff


28 Files Affected:

- (modified) libc/config/baremetal/aarch64/entrypoints.txt (+1) 
- (modified) libc/config/baremetal/arm/entrypoints.txt (+1) 
- (modified) libc/config/baremetal/hexagon/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/freebsd/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/shared/math.h (+1) 
- (added) libc/shared/math/erfbf16.h (+23) 
- (modified) libc/src/__support/math/CMakeLists.txt (+14) 
- (added) libc/src/__support/math/erfbf16.h (+99) 
- (modified) libc/src/math/CMakeLists.txt (+1) 
- (added) libc/src/math/erfbf16.h (+13) 
- (modified) libc/src/math/generic/CMakeLists.txt (+10) 
- (added) libc/src/math/generic/erfbf16.cpp (+19) 
- (modified) libc/test/shared/CMakeLists.txt (+1) 
- (modified) libc/test/shared/shared_math_test.cpp (+4) 
- (modified) libc/test/src/math/CMakeLists.txt (+13) 
- (added) libc/test/src/math/erfbf16_test.cpp (+41) 
- (modified) libc/test/src/math/smoke/CMakeLists.txt (+16) 
- (added) libc/test/src/math/smoke/erfbf16_test.cpp (+160) 


``````````diff
diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index e3d5c8c0a68399..bcb5b989553762 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -854,6 +854,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.copysignbf16
   libc.src.math.coshbf16
   libc.src.math.exp2bf16
+  libc.src.math.erfbf16
   libc.src.math.expbf16
   libc.src.math.fabsbf16
   libc.src.math.fdimbf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index fc937dcc7670c0..3ce56bd2a23663 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -865,6 +865,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.copysignbf16
   libc.src.math.coshbf16
   libc.src.math.exp2bf16
+  libc.src.math.erfbf16
   libc.src.math.expbf16
   libc.src.math.fabsbf16
   libc.src.math.fdimbf16
diff --git a/libc/config/baremetal/hexagon/entrypoints.txt b/libc/config/baremetal/hexagon/entrypoints.txt
index b83f4d11379c4d..fb36c44b40b6bb 100644
--- a/libc/config/baremetal/hexagon/entrypoints.txt
+++ b/libc/config/baremetal/hexagon/entrypoints.txt
@@ -854,6 +854,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
   libc.src.math.coshbf16
+  libc.src.math.erfbf16
   libc.src.math.expbf16
   libc.src.math.fabsbf16
   libc.src.math.fdimbf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index ae01bdda48a8ed..f31d58f7342944 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -857,6 +857,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.copysignbf16
   libc.src.math.coshbf16
   libc.src.math.exp2bf16
+  libc.src.math.erfbf16
   libc.src.math.expbf16
   libc.src.math.fabsbf16
   libc.src.math.fdimbf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index b0fa44ccbd6df0..34fba53c020eef 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -675,6 +675,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.copysignbf16
   libc.src.math.coshbf16
   libc.src.math.exp2bf16
+  libc.src.math.erfbf16
   libc.src.math.expbf16
   libc.src.math.fabsbf16
   libc.src.math.fdimbf16
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index a85ea90f123e79..694631be495551 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -319,6 +319,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.copysignbf16
   libc.src.math.coshbf16
   libc.src.math.exp2bf16
+  libc.src.math.erfbf16
   libc.src.math.expbf16
   libc.src.math.fabsbf16
   libc.src.math.fdimbf16
diff --git a/libc/config/freebsd/x86_64/entrypoints.txt b/libc/config/freebsd/x86_64/entrypoints.txt
index 862a421c473d36..6946002248a503 100644
--- a/libc/config/freebsd/x86_64/entrypoints.txt
+++ b/libc/config/freebsd/x86_64/entrypoints.txt
@@ -607,6 +607,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
   libc.src.math.coshbf16
+  libc.src.math.erfbf16
   libc.src.math.expbf16
   libc.src.math.fabsbf16
   libc.src.math.fdimbf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index d2b1050abf66b6..ee2745c7ea600d 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -744,6 +744,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.copysignbf16
   libc.src.math.coshbf16
   libc.src.math.exp2bf16
+  libc.src.math.erfbf16
   libc.src.math.expbf16
   libc.src.math.fabsbf16
   libc.src.math.fdimbf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index 8dba78c330a5f5..77dc2f6b2c65d8 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -760,6 +760,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.copysignbf16
   libc.src.math.coshbf16
   libc.src.math.exp2bf16
+  libc.src.math.erfbf16
   libc.src.math.expbf16
   libc.src.math.fabsbf16
   libc.src.math.fdimbf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 99678f528cda26..c9c0f049a9a035 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1136,6 +1136,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.copysignbf16
   libc.src.math.coshbf16
   libc.src.math.exp2bf16
+  libc.src.math.erfbf16
   libc.src.math.expbf16
   libc.src.math.fabsbf16
   libc.src.math.fdimbf16
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 7a9e287839f9a7..327b7003b41087 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -739,6 +739,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.copysignbf16
   libc.src.math.coshbf16
   libc.src.math.exp2bf16
+  libc.src.math.erfbf16
   libc.src.math.expbf16
   libc.src.math.fabsbf16
   libc.src.math.fdimbf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index d1232ae7d11013..a5f8b9cb042e31 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1238,6 +1238,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.copysignbf16
   libc.src.math.coshbf16
   libc.src.math.exp2bf16
+  libc.src.math.erfbf16
   libc.src.math.expbf16
   libc.src.math.fabsbf16
   libc.src.math.fdimbf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 28d2d4881dbefd..a65e1f5311af9a 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1238,6 +1238,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.copysignbf16
   libc.src.math.coshbf16
   libc.src.math.exp2bf16
+  libc.src.math.erfbf16
   libc.src.math.expbf16
   libc.src.math.fabsbf16
   libc.src.math.fdimbf16
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 7046934c029380..488eb9a11d154a 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -418,6 +418,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.copysignbf16
   libc.src.math.coshbf16
   libc.src.math.exp2bf16
+  libc.src.math.erfbf16
   libc.src.math.expbf16
   libc.src.math.fabsbf16
   libc.src.math.fdimbf16
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 503c78bc1bd3e2..1767774ce030b6 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -108,6 +108,7 @@
 #include "math/erfcf16.h"
 #include "math/erff.h"
 #include "math/erff16.h"
+#include "math/erfbf16.h"
 #include "math/exp.h"
 #include "math/exp10.h"
 #include "math/exp10f.h"
diff --git a/libc/shared/math/erfbf16.h b/libc/shared/math/erfbf16.h
new file mode 100644
index 00000000000000..ed8cae352bd254
--- /dev/null
+++ b/libc/shared/math/erfbf16.h
@@ -0,0 +1,23 @@
+//===-- Shared erfbf16 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_ERFBF16_H
+#define LLVM_LIBC_SHARED_MATH_ERFBF16_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/erfbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::erfbf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ERFBF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 9a312dd0fded88..d8dc9be9deb697 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -3270,6 +3270,20 @@ add_header_library(
     libc.src.__support.macros.optimization
 )
 
+add_header_library(
+  erfbf16
+  HDRS
+    erfbf16.h
+  DEPENDS
+    libc.hdr.fenv_macros
+    libc.src.__support.FPUtil.bfloat16
+    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.rounding_mode
+)
+
 add_header_library(
   exp_float_constants
   HDRS
diff --git a/libc/src/__support/math/erfbf16.h b/libc/src/__support/math/erfbf16.h
new file mode 100644
index 00000000000000..8a4be1d8c8ce7c
--- /dev/null
+++ b/libc/src/__support/math/erfbf16.h
@@ -0,0 +1,99 @@
+//===-- Implementation header for erfbf16 -----------------------*- 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___SUPPORT_MATH_ERFBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ERFBF16_H
+
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/rounding_mode.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+namespace erfbf16_internal {
+
+// Approximate F(z) = erf(sqrt(z)) / sqrt(z)
+// on the interval 0 <= z <= 5.
+// Coefficients are in descending order.
+LIBC_INLINE_VAR constexpr double COEFFS[12] = {
+    -0x1.30d6be1481948p-33, 0x1.7cf774adce86ep-28,  -0x1.d1591ee4c2994p-24,
+    0x1.7c231d88f04b8p-20,  -0x1.de87683f1fc4bp-17, 0x1.f41cbbaa3fd0bp-14,
+    -0x1.bf42adf1e3d24p-11, 0x1.5642bc032f199p-8,   -0x1.b829b41499428p-6,
+    0x1.ce2eebc4dd9eep-4,   -0x1.81274549391d9p-2,  0x1.20dd75029b655p+0,
+};
+
+} // namespace erfbf16_internal
+
+LIBC_INLINE bfloat16 erfbf16(bfloat16 x) {
+  using FPBits = fputil::FPBits<bfloat16>;
+
+  const FPBits x_bits(x);
+  const uint16_t x_abs = x_bits.uintval() & 0x7fffU;
+  const bool negative = x_bits.is_neg();
+
+  // Handle NaN.
+  if (LIBC_UNLIKELY(x_bits.is_nan())) {
+    if (x_bits.is_signaling_nan()) {
+      fputil::raise_except_if_required(FE_INVALID);
+      return FPBits::quiet_nan().get_val();
+    }
+    return x;
+  }
+
+  // erf(+/-inf) = +/-1.
+  if (LIBC_UNLIKELY(x_bits.is_inf()))
+    return FPBits(static_cast<uint16_t>(negative ? 0xbf80U : 0x3f80U))
+        .get_val();
+
+  // Preserve signed zero.
+  if (LIBC_UNLIKELY(x_abs == 0))
+    return x;
+
+  // Handle large finite values.
+  if (LIBC_UNLIKELY(x_abs >= 0x400dU)) {
+    int mode = fputil::get_round();
+
+    const uint16_t result_bits =
+        negative
+            ? ((mode == FE_UPWARD || mode == FE_TOWARDZERO) ? 0xbf7fU : 0xbf80U)
+            : ((mode == FE_DOWNWARD || mode == FE_TOWARDZERO) ? 0x3f7fU
+                                                              : 0x3f80U);
+
+    fputil::raise_except_if_required(FE_INEXACT);
+    return FPBits(result_bits).get_val();
+  }
+
+  // Evaluate erf(x) using a polynomial approximation.
+  const double ax = fputil::cast<double>(FPBits(x_abs).get_val());
+  const double z = ax * ax;
+
+  double p = erfbf16_internal::COEFFS[0];
+
+  for (int i = 1; i < 12; ++i)
+    p = fputil::multiply_add(p, z, erfbf16_internal::COEFFS[i]);
+
+  const double y = negative ? -(ax * p) : (ax * p);
+
+  const bfloat16 rounded = fputil::cast<bfloat16>(y);
+
+  fputil::raise_except_if_required(FE_INEXACT);
+
+  return rounded;
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ERFBF16_H
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index aa2c720f47df16..7f66b27fa09e17 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -150,6 +150,7 @@ add_math_entrypoint_object(dsubf128)
 add_math_entrypoint_object(erf)
 add_math_entrypoint_object(erff)
 add_math_entrypoint_object(erff16)
+add_math_entrypoint_object(erfbf16)
 
 add_math_entrypoint_object(erfcf16)
 
diff --git a/libc/src/math/erfbf16.h b/libc/src/math/erfbf16.h
new file mode 100644
index 00000000000000..4e35a56e09f613
--- /dev/null
+++ b/libc/src/math/erfbf16.h
@@ -0,0 +1,13 @@
+#ifndef LLVM_LIBC_SRC_MATH_ERFBF16_H
+#define LLVM_LIBC_SRC_MATH_ERFBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 erfbf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_ERFBF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 0c495701e97886..241f5d045fed17 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1248,6 +1248,16 @@ add_entrypoint_object(
     libc.src.__support.math.erff16
 )
 
+add_entrypoint_object(
+  erfbf16
+  SRCS
+    erfbf16.cpp
+  HDRS
+    ../erfbf16.h
+  DEPENDS
+    libc.src.__support.math.erfbf16
+)
+
 add_entrypoint_object(
   erfcf16
   SRCS
diff --git a/libc/src/math/generic/erfbf16.cpp b/libc/src/math/generic/erfbf16.cpp
new file mode 100644
index 00000000000000..e41ca7ae41c467
--- /dev/null
+++ b/libc/src/math/generic/erfbf16.cpp
@@ -0,0 +1,19 @@
+//===-- erfbf16.cpp -------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+/// \file
+/// This file contains the entrypoint implementation of erfbf16.
+//
+#include "src/math/erfbf16.h"
+#include "src/__support/math/erfbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, erfbf16, (bfloat16 x)) { return math::erfbf16(x); }
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index be3df9fa6e6ab6..dfa9ec1bfbb2e9 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -138,6 +138,7 @@ else()
       libc.src.__support.math.erfcf16
       libc.src.__support.math.erff
       libc.src.__support.math.erff16
+    libc.src.__support.math.erfbf16
       libc.src.__support.math.exp
       libc.src.__support.math.exp2
       libc.src.__support.math.exp2f
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 65a61537662372..65cdd5298dab3d 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -815,6 +815,10 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
 #endif // LIBC_TYPES_HAS_NATIVE_FLOAT128
 
 TEST(LlvmLibcSharedMathTest, AllBFloat16) {
+  EXPECT_FP_EQ(bfloat16(0.0),
+               LIBC_NAMESPACE::shared::erfbf16(bfloat16(0.0)));
+  EXPECT_FP_EQ(bfloat16(0.84375),
+               LIBC_NAMESPACE::shared::erfbf16(bfloat16(1.0)));
   using FPBits = LIBC_NAMESPACE::fputil::FPBits<bfloat16>;
   EXPECT_FP_EQ(bfloat16(0x1.921fb6p0f),
                LIBC_NAMESPACE::shared::acosbf16(bfloat16(0.0f)));
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 2a7ef8e0e186d8..4ed82a86a81934 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -3292,6 +3292,19 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  erfbf16_test
+  NEED_MPFR
+  SUITE
+    libc-math-tests
+  SRCS
+    erfbf16_test.cpp
+  DEPENDS
+    libc.src.math.erfbf16
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_fp_unittest(
   erfcf16_test
   NEED_MPFR
diff --git a/libc/test/src/math/erfbf16_test.cpp b/libc/test/src/math/erfbf16_test.cpp
new file mode 100644
index 00000000000000..969eff82cc26f3
--- /dev/null
+++ b/libc/test/src/math/erfbf16_test.cpp
@@ -0,0 +1,41 @@
+//===-- Exhaustive test for erfbf16 ---------------------------------------===//
+//
+// 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/erfbf16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcErfbf16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+// Range: [+0, +Inf].
+static constexpr uint16_t POS_START = 0x0000U;
+static constexpr uint16_t POS_STOP = 0x7f80U;
+
+// Range: [-0, -Inf].
+static constexpr uint16_t NEG_START = 0x8000U;
+static constexpr uint16_t NEG_STOP = 0xff80U;
+
+TEST_F(LlvmLibcErfbf16Test, PositiveRange) {
+  for (uint32_t bits = POS_START; bits <= POS_STOP; ++bits) {
+    bfloat16 x = FPBits(static_cast<uint16_t>(bits)).get_val();
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Erf, x,
+                                   LIBC_NAMESPACE::erfbf16(x), 0.5);
+  }
+}
+
+TEST_F(LlvmLibcErfbf16Test, NegativeRange) {
+  for (uint32_t bits = NEG_START; bits <= NEG_STOP; ++bits) {
+    bfloat16 x = FPBits(static_cast<uint16_t>(bits)).get_val();
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Erf, x,
+                                   LIBC_NAMESPACE::erfbf16(x), 0.5);
+  }
+}
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 754430326df0b8..2b35b31190fb2c 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -5723,6 +5723,22 @@ add_fp_unittest(
     libc.src.math.erff16
 )
 
+add_fp_unittest(
+  erfbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    erfbf16_test.cpp
+  DEPENDS
+    libc.hdr.fenv_macros
+    libc.src.math.erfbf16
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.fenv_impl
+    libc.hdr.errno_macros
+    libc.hdr.stdint_proxy
+)
+
 add_fp_unittest(
   erfcf16_test
   SUITE
diff --git a/libc/test/src/math/smoke/erfbf16_test.cpp b/libc/test/src/math/smoke/erfbf16_test.cpp
new file mode 100644
index 00000000000000..5f74f5ab5dae32
--- /dev/null
+++ b/libc/test/src/math/smoke/erfbf16_test.cpp
@@ -0,0 +1,160 @@
+//===-- erfbf16_test.cpp --------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+/// \file
+/// This file contains smoke tests for the erfbf16 function.
+//
+//===-- Smoke tests for erfbf16 -------------------------------------------===//
+//
+// 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 "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "hdr/stdint_proxy.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/erfbf16.h"
+#include "test/UnitTest/FEnvSafeTest.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcErfbf16SmokeTest = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
+using BFBits = LIBC_NAMESPACE::fputil::FPBits<bfloat16>;
+
+static bfloat16 from_bits(uint16_t bits) { return BFBits(bits).get_val(); }
+
+TEST_F(LlvmLibcErfbf16SmokeTest, ZerosAndInfinities) {
+  EXPECT_FP_EQ_ALL_ROUNDING(from_bits(0x0...
[truncated]

``````````

</details>


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


More information about the libc-commits mailing list