[libc-commits] [libc] [llvm] [libc][math] Add `expm1bf16` math function (PR #246689)
Arnav Kapoor via libc-commits
libc-commits at lists.llvm.org
Sat Nov 28 12:21:41 PST 2026
https://github.com/arnavk23 updated https://github.com/llvm/llvm-project/pull/246689
>From b1631f9cf10a939544b8d55df079fc5f4fb17cb0 Mon Sep 17 00:00:00 2001
From: Arnav Kapoor <kapoorarnav43 at gmail.com>
Date: Fri, 27 Nov 2026 21:22:09 +0530
Subject: [PATCH 1/3] [libc][math] Add expm1bf16 math function
Implement expm1bf16 as a header-only function in src/__support/math.
- |x| < 2^-3: evaluate x + x^2 * Q(x) in double, where Q is a degree-3
Sollya fit of (expm1(x) - x)/x^2. x^2 is exact in double for every
bfloat16 input, so the final fma rounds correctly in all rounding
modes even when x^2 * Q(x) is far below the last bit of x.
- Otherwise: reuse the expbf16 range reduction and evaluate
exp(hi + mid) * exp(lo) - 1 with an fma.
- x >= 89 overflows; x <= -6.25 rounds -1 + 2^-12 to give the correctly
rounded result in every mode.
- Four hard-to-round inputs are handled with an ExceptValues table.
The EXP_HI/EXP_MID tables are moved from expbf16.h into a new
expbf16_utils.h so both functions share them.
Tested exhaustively against MPFR in all four rounding modes.
Fixes #246678
---
libc/config/baremetal/aarch64/entrypoints.txt | 1 +
libc/config/baremetal/arm/entrypoints.txt | 1 +
libc/config/baremetal/hexagon/entrypoints.txt | 1 +
libc/config/baremetal/riscv/entrypoints.txt | 1 +
libc/config/darwin/aarch64/entrypoints.txt | 1 +
libc/config/darwin/x86_64/entrypoints.txt | 1 +
libc/config/freebsd/x86_64/entrypoints.txt | 1 +
libc/config/gpu/amdgpu/entrypoints.txt | 1 +
libc/config/gpu/nvptx/entrypoints.txt | 1 +
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/arm/entrypoints.txt | 1 +
libc/config/linux/riscv/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/config/windows/entrypoints.txt | 1 +
libc/shared/math.h | 1 +
libc/shared/math/expm1bf16.h | 23 +++
libc/src/__support/math/CMakeLists.txt | 30 ++++
libc/src/__support/math/expbf16.h | 31 +---
libc/src/__support/math/expbf16_utils.h | 53 +++++++
libc/src/__support/math/expm1bf16.h | 141 ++++++++++++++++++
libc/src/math/CMakeLists.txt | 1 +
libc/src/math/expm1bf16.h | 21 +++
libc/src/math/generic/CMakeLists.txt | 10 ++
libc/src/math/generic/expm1bf16.cpp | 18 +++
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
libc/test/src/math/CMakeLists.txt | 12 ++
libc/test/src/math/expm1bf16_test.cpp | 39 +++++
libc/test/src/math/smoke/CMakeLists.txt | 14 ++
libc/test/src/math/smoke/expm1bf16_test.cpp | 74 +++++++++
.../llvm-project-overlay/libc/BUILD.bazel | 1 +
.../libc/src/__support/math/BUILD.bazel | 30 ++++
.../libc/src/math/BUILD.bazel | 7 +
.../libc/test/src/math/BUILD.bazel | 7 +
.../libc/test/src/math/smoke/BUILD.bazel | 8 +
35 files changed, 507 insertions(+), 30 deletions(-)
create mode 100644 libc/shared/math/expm1bf16.h
create mode 100644 libc/src/__support/math/expbf16_utils.h
create mode 100644 libc/src/__support/math/expm1bf16.h
create mode 100644 libc/src/math/expm1bf16.h
create mode 100644 libc/src/math/generic/expm1bf16.cpp
create mode 100644 libc/test/src/math/expm1bf16_test.cpp
create mode 100644 libc/test/src/math/smoke/expm1bf16_test.cpp
diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index e3d5c8c0a68399..d2ce570aa8480d 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -855,6 +855,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.coshbf16
libc.src.math.exp2bf16
libc.src.math.expbf16
+ libc.src.math.expm1bf16
libc.src.math.fabsbf16
libc.src.math.fdimbf16
libc.src.math.floorbf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index fc937dcc7670c0..884b956c0ae0ed 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -866,6 +866,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.coshbf16
libc.src.math.exp2bf16
libc.src.math.expbf16
+ libc.src.math.expm1bf16
libc.src.math.fabsbf16
libc.src.math.fdimbf16
libc.src.math.floorbf16
diff --git a/libc/config/baremetal/hexagon/entrypoints.txt b/libc/config/baremetal/hexagon/entrypoints.txt
index b83f4d11379c4d..9f863e74ca966e 100644
--- a/libc/config/baremetal/hexagon/entrypoints.txt
+++ b/libc/config/baremetal/hexagon/entrypoints.txt
@@ -855,6 +855,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.copysignbf16
libc.src.math.coshbf16
libc.src.math.expbf16
+ libc.src.math.expm1bf16
libc.src.math.fabsbf16
libc.src.math.fdimbf16
libc.src.math.floorbf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index ae01bdda48a8ed..2c6e923f6c68e7 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -858,6 +858,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.coshbf16
libc.src.math.exp2bf16
libc.src.math.expbf16
+ libc.src.math.expm1bf16
libc.src.math.fabsbf16
libc.src.math.fdimbf16
libc.src.math.floorbf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index b0fa44ccbd6df0..7becd1dd63233e 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -676,6 +676,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.coshbf16
libc.src.math.exp2bf16
libc.src.math.expbf16
+ libc.src.math.expm1bf16
libc.src.math.fabsbf16
libc.src.math.fdimbf16
libc.src.math.floorbf16
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index a85ea90f123e79..c3f89aea529c55 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -320,6 +320,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.coshbf16
libc.src.math.exp2bf16
libc.src.math.expbf16
+ libc.src.math.expm1bf16
libc.src.math.fabsbf16
libc.src.math.fdimbf16
libc.src.math.floorbf16
diff --git a/libc/config/freebsd/x86_64/entrypoints.txt b/libc/config/freebsd/x86_64/entrypoints.txt
index 862a421c473d36..37d8b6589b239c 100644
--- a/libc/config/freebsd/x86_64/entrypoints.txt
+++ b/libc/config/freebsd/x86_64/entrypoints.txt
@@ -608,6 +608,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.copysignbf16
libc.src.math.coshbf16
libc.src.math.expbf16
+ libc.src.math.expm1bf16
libc.src.math.fabsbf16
libc.src.math.fdimbf16
libc.src.math.floorbf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index d2b1050abf66b6..1e9e516da232ff 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -745,6 +745,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.coshbf16
libc.src.math.exp2bf16
libc.src.math.expbf16
+ libc.src.math.expm1bf16
libc.src.math.fabsbf16
libc.src.math.fdimbf16
libc.src.math.floorbf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index 8dba78c330a5f5..a0ae612f876fbc 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -761,6 +761,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.coshbf16
libc.src.math.exp2bf16
libc.src.math.expbf16
+ libc.src.math.expm1bf16
libc.src.math.fabsbf16
libc.src.math.fdimbf16
libc.src.math.floorbf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 99678f528cda26..adf6e0b022a861 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1137,6 +1137,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.coshbf16
libc.src.math.exp2bf16
libc.src.math.expbf16
+ libc.src.math.expm1bf16
libc.src.math.fabsbf16
libc.src.math.fdimbf16
libc.src.math.floorbf16
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 7a9e287839f9a7..a497457f2e1776 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -740,6 +740,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.coshbf16
libc.src.math.exp2bf16
libc.src.math.expbf16
+ libc.src.math.expm1bf16
libc.src.math.fabsbf16
libc.src.math.fdimbf16
libc.src.math.floorbf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index d1232ae7d11013..2b5839c8b8503e 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1239,6 +1239,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.coshbf16
libc.src.math.exp2bf16
libc.src.math.expbf16
+ libc.src.math.expm1bf16
libc.src.math.fabsbf16
libc.src.math.fdimbf16
libc.src.math.floorbf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 28d2d4881dbefd..cc8dd45bcbf496 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1239,6 +1239,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.coshbf16
libc.src.math.exp2bf16
libc.src.math.expbf16
+ libc.src.math.expm1bf16
libc.src.math.fabsbf16
libc.src.math.fdimbf16
libc.src.math.floorbf16
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 7046934c029380..b24eecf66aaf32 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -419,6 +419,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.coshbf16
libc.src.math.exp2bf16
libc.src.math.expbf16
+ libc.src.math.expm1bf16
libc.src.math.fabsbf16
libc.src.math.fdimbf16
libc.src.math.floorbf16
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 503c78bc1bd3e2..0ac01245780bc3 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -124,6 +124,7 @@
#include "math/expf.h"
#include "math/expf16.h"
#include "math/expm1.h"
+#include "math/expm1bf16.h"
#include "math/expm1f.h"
#include "math/expm1f16.h"
#include "math/f16add.h"
diff --git a/libc/shared/math/expm1bf16.h b/libc/shared/math/expm1bf16.h
new file mode 100644
index 00000000000000..686833da10869f
--- /dev/null
+++ b/libc/shared/math/expm1bf16.h
@@ -0,0 +1,23 @@
+//===-- Shared expm1bf16 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_EXPM1BF16_H
+#define LLVM_LIBC_SHARED_MATH_EXPM1BF16_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/expm1bf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::expm1bf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_EXPM1BF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index c1db38835a508b..cce2764fa1238d 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -3433,11 +3433,21 @@ add_header_library(
libc.src.__support.math.exp10_float16_constants
)
+add_header_library(
+ expbf16_utils
+ HDRS
+ expbf16_utils.h
+ DEPENDS
+ libc.src.__support.macros.attributes
+ libc.src.__support.macros.config
+)
+
add_header_library(
expbf16
HDRS
expbf16.h
DEPENDS
+ .expbf16_utils
libc.hdr.errno_macros
libc.hdr.fenv_macros
libc.src.__support.CPP.array
@@ -4890,6 +4900,26 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ expm1bf16
+ HDRS
+ expm1bf16.h
+ DEPENDS
+ .expbf16_utils
+ libc.hdr.fenv_macros
+ libc.src.__support.FPUtil.bfloat16
+ 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.nearest_integer
+ libc.src.__support.FPUtil.polyeval
+ libc.src.__support.FPUtil.rounding_mode
+ libc.src.__support.macros.config
+ libc.src.__support.macros.optimization
+)
+
add_header_library(
fsqrtf128
HDRS
diff --git a/libc/src/__support/math/expbf16.h b/libc/src/__support/math/expbf16.h
index 6f2f5704bf49c7..1e01fbd05f67ea 100644
--- a/libc/src/__support/math/expbf16.h
+++ b/libc/src/__support/math/expbf16.h
@@ -22,41 +22,12 @@
#include "src/__support/FPUtil/rounding_mode.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/optimization.h"
+#include "src/__support/math/expbf16_utils.h"
namespace LIBC_NAMESPACE_DECL {
namespace math {
-namespace expbf16_internal {
-
-// Generated by Sollya with the following commands:
-// > display = hexadecimal;
-// > for i from -96 to 88 by 8 do print(i, round(exp(i), SG, RN) @ "f,");
-LIBC_INLINE_VAR constexpr float EXP_HI[24] = {
- 0x1.6a4p-139f, 0x1.07b71p-127f, 0x1.7fd974p-116f, 0x1.175afp-104f,
- 0x1.969d48p-93f, 0x1.27ec46p-81f, 0x1.aebabap-70f, 0x1.397924p-58f,
- 0x1.c8465p-47f, 0x1.4c1078p-35f, 0x1.e355bcp-24f, 0x1.5fc21p-12f,
- 0x1p0f, 0x1.749ea8p11f, 0x1.0f2ebep23f, 0x1.8ab7fcp34f,
- 0x1.1f43fcp46f, 0x1.a220d4p57f, 0x1.304d6ap69f, 0x1.baed16p80f,
- 0x1.425982p92f, 0x1.d531d8p103f, 0x1.55779cp115f, 0x1.f1056ep126f,
-};
-
-// Generated by Sollya with the following commands:
-// > display = hexadecimal;
-// > for i from 0 to 7.75 by 0.25 do print(round(exp(i), SG, RN) @ "f,");
-LIBC_INLINE_VAR constexpr float EXP_MID[32] = {
- 0x1p0f, 0x1.48b5e4p0f, 0x1.a61298p0f, 0x1.0ef9dcp1f,
- 0x1.5bf0a8p1f, 0x1.bec38ep1f, 0x1.1ed3fep2f, 0x1.704b6ap2f,
- 0x1.d8e64cp2f, 0x1.2f9b88p3f, 0x1.85d6fep3f, 0x1.f4907p3f,
- 0x1.415e5cp4f, 0x1.9ca53cp4f, 0x1.08ec72p5f, 0x1.542b2ep5f,
- 0x1.b4c902p5f, 0x1.186bf2p6f, 0x1.68118ap6f, 0x1.ce564ep6f,
- 0x1.28d38ap7f, 0x1.7d21eep7f, 0x1.e96244p7f, 0x1.3a30dp8f,
- 0x1.936dc6p8f, 0x1.0301a4p9f, 0x1.4c9222p9f, 0x1.ab0786p9f,
- 0x1.122886p10f, 0x1.6006b6p10f, 0x1.c402b6p10f, 0x1.223252p11f,
-};
-
-} // namespace expbf16_internal
-
LIBC_INLINE bfloat16 expbf16(bfloat16 x) {
using FPBits = fputil::FPBits<bfloat16>;
FPBits x_bits(x);
diff --git a/libc/src/__support/math/expbf16_utils.h b/libc/src/__support/math/expbf16_utils.h
new file mode 100644
index 00000000000000..e98eeb02b3293b
--- /dev/null
+++ b/libc/src/__support/math/expbf16_utils.h
@@ -0,0 +1,53 @@
+//===-- Shared tables for bfloat16 exp functions ----------------*- 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_EXPBF16_UTILS_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_EXPBF16_UTILS_H
+
+#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+namespace expbf16_internal {
+
+// Generated by Sollya with the following commands:
+// > display = hexadecimal;
+// > for i from -96 to 88 by 8 do print(i, round(exp(i), SG, RN) @ "f,");
+LIBC_INLINE_VAR constexpr float EXP_HI[24] = {
+ 0x1.6a4p-139f, 0x1.07b71p-127f, 0x1.7fd974p-116f, 0x1.175afp-104f,
+ 0x1.969d48p-93f, 0x1.27ec46p-81f, 0x1.aebabap-70f, 0x1.397924p-58f,
+ 0x1.c8465p-47f, 0x1.4c1078p-35f, 0x1.e355bcp-24f, 0x1.5fc21p-12f,
+ 0x1p0f, 0x1.749ea8p11f, 0x1.0f2ebep23f, 0x1.8ab7fcp34f,
+ 0x1.1f43fcp46f, 0x1.a220d4p57f, 0x1.304d6ap69f, 0x1.baed16p80f,
+ 0x1.425982p92f, 0x1.d531d8p103f, 0x1.55779cp115f, 0x1.f1056ep126f,
+};
+
+// Generated by Sollya with the following commands:
+// > display = hexadecimal;
+// > for i from 0 to 7.75 by 0.25 do print(round(exp(i), SG, RN) @ "f,");
+LIBC_INLINE_VAR constexpr float EXP_MID[32] = {
+ 0x1p0f, 0x1.48b5e4p0f, 0x1.a61298p0f, 0x1.0ef9dcp1f,
+ 0x1.5bf0a8p1f, 0x1.bec38ep1f, 0x1.1ed3fep2f, 0x1.704b6ap2f,
+ 0x1.d8e64cp2f, 0x1.2f9b88p3f, 0x1.85d6fep3f, 0x1.f4907p3f,
+ 0x1.415e5cp4f, 0x1.9ca53cp4f, 0x1.08ec72p5f, 0x1.542b2ep5f,
+ 0x1.b4c902p5f, 0x1.186bf2p6f, 0x1.68118ap6f, 0x1.ce564ep6f,
+ 0x1.28d38ap7f, 0x1.7d21eep7f, 0x1.e96244p7f, 0x1.3a30dp8f,
+ 0x1.936dc6p8f, 0x1.0301a4p9f, 0x1.4c9222p9f, 0x1.ab0786p9f,
+ 0x1.122886p10f, 0x1.6006b6p10f, 0x1.c402b6p10f, 0x1.223252p11f,
+};
+
+} // namespace expbf16_internal
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXPBF16_UTILS_H
diff --git a/libc/src/__support/math/expm1bf16.h b/libc/src/__support/math/expm1bf16.h
new file mode 100644
index 00000000000000..dbec650af25e62
--- /dev/null
+++ b/libc/src/__support/math/expm1bf16.h
@@ -0,0 +1,141 @@
+//===-- Implementation header for expm1bf16 ---------------------*- 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_EXPM1BF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_EXPM1BF16_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/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/FPUtil/polyeval.h"
+#include "src/__support/FPUtil/rounding_mode.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+#include "src/__support/math/expbf16_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE bfloat16 expm1bf16(bfloat16 x) {
+ using FPBits = fputil::FPBits<bfloat16>;
+ FPBits x_bits(x);
+
+ uint16_t x_u = x_bits.uintval();
+ uint16_t x_abs = x_u & 0x7fffU;
+
+ // When |x| < 2^-3, or x >= 89, or x <= -6.25, or x is NaN.
+ if (LIBC_UNLIKELY(x_abs < 0x3e00U || x_u >= 0xc0c8U ||
+ (x_bits.is_pos() && x_abs >= 0x42b2U))) {
+ // expm1(NaN) = NaN
+ if (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;
+ }
+
+ // expm1(+/-0) = +/-0
+ if (x_bits.is_zero())
+ return x;
+
+ // When x >= 89, expm1(x) overflows.
+ if (x_bits.is_pos() && x_abs >= 0x42b2U) {
+ // expm1(+inf) = +inf
+ if (x_bits.is_inf())
+ return x;
+ return fputil::set_overflow<bfloat16>();
+ }
+
+ // When x <= -6.25, exp(x) < 2^-9 and round(expm1(x), BF16, RN) = -1.
+ if (x_u >= 0xc0c8U) {
+ // expm1(-inf) = -1
+ if (x_bits.is_inf())
+ return FPBits::one(Sign::NEG).get_val();
+ // -1 < expm1(x) < -1 + 2^-9, and -1 + 2^-12 is in the same rounding
+ // interval in every rounding mode, so rounding it from float gives the
+ // correctly rounded result and raises FE_INEXACT.
+ return fputil::cast<bfloat16>(-1.0f + 0x1.0p-12f);
+ }
+
+ // When 0 < |x| < 2^-3, evaluate expm1(x) = x + x^2 * Q(x) in double with a
+ // single fma at the end. x^2 is exact in double for every bfloat16 x, so
+ // the x^2 * Q(x) term never vanishes, and the fma rounds x + x^2 * Q(x) in
+ // the current rounding mode. This keeps directed rounding correct even
+ // when x^2 * Q(x) is far below the last bit of x.
+ double xd = static_cast<double>(static_cast<float>(x));
+ // Generated by Sollya with:
+ // > display = hexadecimal;
+ // > Q = fpminimax((expm1(x) - x)/x^2, 3, [|D...|], [-2^-3, 2^-3]);
+ // Relative error: 2^-23.49.
+ double q = fputil::polyeval(xd, 0x1.fffffd281841p-2, 0x1.555557c59dc16p-3,
+ 0x1.5582d91e85f05p-5, 0x1.1119bbdb5fb18p-7);
+ return fputil::cast<bfloat16>(fputil::multiply_add(xd * xd, q, xd));
+ }
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+ constexpr fputil::ExceptValues<bfloat16, 4> EXPM1BF16_EXCEPTS = {{
+ // (input, RZ output, RU offset, RD offset, RN offset)
+ // x = 0x3EBF (0.373046875)
+ // MPFR: RU=0x3EE8, RD=0x3EE7, RZ=0x3EE7, RN=0x3EE8
+ {0x3EBFU, 0x3EE7U, 1U, 0U, 1U},
+ // x = 0x419D (19.625)
+ // MPFR: RU=0x4DA0, RD=0x4D9F, RZ=0x4D9F, RN=0x4D9F
+ {0x419DU, 0x4D9FU, 1U, 0U, 0U},
+ // x = 0x41F9 (31.125)
+ // MPFR: RU=0x55F0, RD=0x55EF, RZ=0x55EF, RN=0x55F0
+ {0x41F9U, 0x55EFU, 1U, 0U, 1U},
+ // x = 0xBE66 (-0.224609375)
+ // MPFR: RU=0xBE4D, RD=0xBE4E, RZ=0xBE4D, RN=0xBE4E
+ {0xBE66U, 0xBE4DU, 0U, 1U, 1U},
+ }};
+
+ if (auto r = EXPM1BF16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
+ return r.value();
+#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+ // For 2^-3 <= |x| and -6.25 < x < 89, use the same range reduction as
+ // expbf16:
+ // x = hi + mid + lo, where hi + mid = round(4 * x) / 4 and |lo| <= 2^-3,
+ // exp(x) = exp(hi) * exp(mid) * exp(lo),
+ // then expm1(x) = exp(hi + mid) * exp(lo) - 1. For |x| >= 2^-3, the
+ // subtraction cancels at most 3 bits of the float result, which leaves
+ // enough precision for a bfloat16 result.
+ float xf = static_cast<float>(x);
+ float kf = fputil::nearest_integer(xf * 4.0f);
+ int x_hi_mid = static_cast<int>(kf);
+ int x_hi = x_hi_mid >> 5;
+ int x_mid = x_hi_mid & 0b11111;
+ // lo = x - (hi + mid) = round(x * 4) / (-4) + x
+ float lo = fputil::sub_multiply(xf, kf, 0.25f);
+
+ float exp_hi_mid =
+ expbf16_internal::EXP_HI[x_hi + 12] * expbf16_internal::EXP_MID[x_mid];
+
+ // Same degree-3 polynomial as expbf16 for exp(lo):
+ // > display = hexadecimal;
+ // > P = fpminimax(expm1(x)/x, 2, [|SG...|], [-2^-7, 2^-7]);
+ // > 1 + x * P;
+ float exp_lo =
+ fputil::polyeval(lo, 0x1p+0f, 0x1p+0f, 0x1.00004p-1f, 0x1.555578p-3f);
+
+ return fputil::cast<bfloat16>(
+ fputil::multiply_add(exp_hi_mid, exp_lo, -1.0f));
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXPM1BF16_H
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index aa2c720f47df16..b9ad9104b98c22 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -176,6 +176,7 @@ add_math_entrypoint_object(exp10m1f16)
add_math_entrypoint_object(expm1)
add_math_entrypoint_object(expm1f)
add_math_entrypoint_object(expm1f16)
+add_math_entrypoint_object(expm1bf16)
add_math_entrypoint_object(f16add)
add_math_entrypoint_object(f16addf)
diff --git a/libc/src/math/expm1bf16.h b/libc/src/math/expm1bf16.h
new file mode 100644
index 00000000000000..89de74de4380f5
--- /dev/null
+++ b/libc/src/math/expm1bf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for expm1bf16 ---------------------*- 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_EXPM1BF16_H
+#define LLVM_LIBC_SRC_MATH_EXPM1BF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 expm1bf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_EXPM1BF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 0c495701e97886..48f86bef58b7d0 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1438,6 +1438,16 @@ add_entrypoint_object(
libc.src.__support.math.expm1f16
)
+add_entrypoint_object(
+ expm1bf16
+ SRCS
+ expm1bf16.cpp
+ HDRS
+ ../expm1bf16.h
+ DEPENDS
+ libc.src.__support.math.expm1bf16
+)
+
add_entrypoint_object(
powf
SRCS
diff --git a/libc/src/math/generic/expm1bf16.cpp b/libc/src/math/generic/expm1bf16.cpp
new file mode 100644
index 00000000000000..a8995d200a4e0b
--- /dev/null
+++ b/libc/src/math/generic/expm1bf16.cpp
@@ -0,0 +1,18 @@
+//===-- BFloat16 e^x - 1 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/expm1bf16.h"
+#include "src/__support/math/expm1bf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, expm1bf16, (bfloat16 x)) {
+ return math::expm1bf16(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index be3df9fa6e6ab6..5b96f34ce97085 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -147,6 +147,7 @@ else()
libc.src.__support.math.expm1
libc.src.__support.math.expm1f
libc.src.__support.math.expm1f16
+ libc.src.__support.math.expm1bf16
libc.src.__support.math.exp10
libc.src.__support.math.exp10f
libc.src.__support.math.exp10f16
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 65a61537662372..cd26e6a2e7866e 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -868,6 +868,7 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
EXPECT_FP_EQ(bfloat16(1.0), LIBC_NAMESPACE::shared::exp2bf16(bfloat16(0.0)));
EXPECT_FP_EQ(bfloat16(1.0), LIBC_NAMESPACE::shared::expbf16(bfloat16(0.0)));
+ EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::expm1bf16(bfloat16(0.0)));
EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::log_bf16(bfloat16(1.0)));
EXPECT_FP_EQ(bfloat16(1.0),
LIBC_NAMESPACE::shared::log10p1bf16(bfloat16(9.0)));
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 2a7ef8e0e186d8..133e47aa8d32ea 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -2485,6 +2485,18 @@ add_fp_unittest(
libc.src.math.expm1f16
)
+add_fp_unittest(
+ expm1bf16_test
+ NEED_MPFR
+ SUITE
+ libc-math-tests
+ SRCS
+ expm1bf16_test.cpp
+ DEPENDS
+ libc.src.math.expm1bf16
+ libc.src.__support.FPUtil.bfloat16
+)
+
add_fp_unittest(
log_test
NEED_MPFR
diff --git a/libc/test/src/math/expm1bf16_test.cpp b/libc/test/src/math/expm1bf16_test.cpp
new file mode 100644
index 00000000000000..fee78ac1400209
--- /dev/null
+++ b/libc/test/src/math/expm1bf16_test.cpp
@@ -0,0 +1,39 @@
+//===-- Exhaustive test for expm1bf16 -------------------------------------===//
+//
+// 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/expm1bf16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcExpm1Bf16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+static constexpr uint16_t POS_START = 0x0000U;
+static constexpr uint16_t POS_STOP = 0x7f80U;
+
+static constexpr uint16_t NEG_START = 0x8000U;
+static constexpr uint16_t NEG_STOP = 0xff80U;
+
+TEST_F(LlvmLibcExpm1Bf16Test, PositiveRange) {
+ for (uint16_t v = POS_START; v <= POS_STOP; ++v) {
+ bfloat16 x = FPBits(v).get_val();
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Expm1, x,
+ LIBC_NAMESPACE::expm1bf16(x), 0.5);
+ }
+}
+
+TEST_F(LlvmLibcExpm1Bf16Test, NegativeRange) {
+ for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) {
+ bfloat16 x = FPBits(v).get_val();
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Expm1, x,
+ LIBC_NAMESPACE::expm1bf16(x), 0.5);
+ }
+}
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index fe398e4b1367aa..085120f5599941 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -4727,6 +4727,20 @@ add_fp_unittest(
libc.src.__support.FPUtil.cast
)
+add_fp_unittest(
+ expm1bf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ expm1bf16_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.fenv_macros
+ libc.src.math.expm1bf16
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.cast
+)
+
add_fp_unittest(
log_test
SUITE
diff --git a/libc/test/src/math/smoke/expm1bf16_test.cpp b/libc/test/src/math/smoke/expm1bf16_test.cpp
new file mode 100644
index 00000000000000..e0a3caeffb2c04
--- /dev/null
+++ b/libc/test/src/math/smoke/expm1bf16_test.cpp
@@ -0,0 +1,74 @@
+//===-- Unittests for expm1bf16 -------------------------------------------===//
+//
+// 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 "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/math/expm1bf16.h"
+#include "test/UnitTest/FEnvSafeTest.h"
+#include "test/UnitTest/FPMatcher.h"
+
+class LlvmLibcExpm1bf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+ DECLARE_SPECIAL_CONSTANTS(bfloat16)
+public:
+ void test_special_numbers() {
+ EXPECT_FP_IS_NAN(LIBC_NAMESPACE::expm1bf16(aNaN));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_IS_NAN_WITH_EXCEPTION(LIBC_NAMESPACE::expm1bf16(sNaN),
+ FE_INVALID);
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::expm1bf16(inf));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(LIBC_NAMESPACE::fputil::cast<bfloat16>(-1.0f),
+ LIBC_NAMESPACE::expm1bf16(neg_inf));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::expm1bf16(zero));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(neg_zero, LIBC_NAMESPACE::expm1bf16(neg_zero));
+ EXPECT_MATH_ERRNO(0);
+ }
+
+ void test_overflow() {
+ EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::expm1bf16(max_normal),
+ FE_OVERFLOW);
+ EXPECT_MATH_ERRNO(ERANGE);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(
+ inf,
+ LIBC_NAMESPACE::expm1bf16(
+ LIBC_NAMESPACE::fputil::cast<bfloat16>(89.0f)),
+ FE_OVERFLOW);
+ EXPECT_MATH_ERRNO(ERANGE);
+ }
+
+ void test_result_near_minus_one() {
+ EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(
+ LIBC_NAMESPACE::fputil::cast<bfloat16>(-1.0f),
+ LIBC_NAMESPACE::expm1bf16(neg_max_normal), FE_INEXACT);
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(
+ LIBC_NAMESPACE::fputil::cast<bfloat16>(-1.0f),
+ LIBC_NAMESPACE::expm1bf16(
+ LIBC_NAMESPACE::fputil::cast<bfloat16>(-6.25f)),
+ FE_INEXACT);
+ EXPECT_MATH_ERRNO(0);
+ }
+};
+
+TEST_F(LlvmLibcExpm1bf16Test, SpecialNumbers) { test_special_numbers(); }
+TEST_F(LlvmLibcExpm1bf16Test, Overflow) { test_overflow(); }
+TEST_F(LlvmLibcExpm1bf16Test, ResultNearMinusOne) {
+ test_result_near_minus_one();
+}
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index f68c6522d16772..d1d3ea39ea186b 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -644,6 +644,7 @@ alias(
"expm1",
"expm1f",
"expm1f16",
+ "expm1bf16",
"f16add",
"f16addf",
"f16addf128",
diff --git a/utils/bazel/llvm-project-overlay/libc/src/__support/math/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/src/__support/math/BUILD.bazel
index c725e95998ab8b..c2719ae56990aa 100644
--- a/utils/bazel/llvm-project-overlay/libc/src/__support/math/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/src/__support/math/BUILD.bazel
@@ -3662,10 +3662,20 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "expbf16_utils",
+ hdrs = ["expbf16_utils.h"],
+ deps = [
+ "//libc/src/__support/macros:attributes",
+ "//libc/src/__support/macros:config",
+ ],
+)
+
libc_support_library(
name = "expbf16",
hdrs = ["expbf16.h"],
deps = [
+ ":expbf16_utils",
"//libc/hdr:errno_macros",
"//libc/hdr:fenv_macros",
"//libc/src/__support/CPP:array",
@@ -5818,6 +5828,26 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "expm1bf16",
+ hdrs = ["expm1bf16.h"],
+ deps = [
+ ":expbf16_utils",
+ "//libc/hdr:fenv_macros",
+ "//libc/src/__support/FPUtil:bfloat16",
+ "//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:nearest_integer",
+ "//libc/src/__support/FPUtil:polyeval",
+ "//libc/src/__support/FPUtil:rounding_mode",
+ "//libc/src/__support/macros:config",
+ "//libc/src/__support/macros:optimization",
+ ],
+)
+
libc_support_library(
name = "expm1f16",
hdrs = ["expm1f16.h"],
diff --git a/utils/bazel/llvm-project-overlay/libc/src/math/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/src/math/BUILD.bazel
index ed405ad161f95c..079e2595fd4a53 100644
--- a/utils/bazel/llvm-project-overlay/libc/src/math/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/src/math/BUILD.bazel
@@ -976,6 +976,13 @@ libc_math_function(
additional_deps = ["//libc/src/__support/math:expm1f"],
)
+libc_math_function(
+ name = "expm1bf16",
+ additional_deps = [
+ "//libc/src/__support/math:expm1bf16",
+ ],
+)
+
libc_math_function(
name = "expm1f16",
additional_deps = [
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/math/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/math/BUILD.bazel
index 333862f4596d08..b89b648b40c846 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/math/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/math/BUILD.bazel
@@ -378,6 +378,13 @@ math_mpfr_test(name = "expm1f")
math_mpfr_test(name = "expm1f16")
+math_mpfr_test(
+ name = "expm1bf16",
+ deps = [
+ "//libc/src/__support/FPUtil:bfloat16",
+ ],
+)
+
math_mpfr_test(
name = "fabs",
hdrs = ["FAbsTest.h"],
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel
index ce56ed23fdf071..4cb90d359da5de 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel
@@ -2706,6 +2706,14 @@ math_test(
math_test(name = "expm1f16")
+math_test(
+ name = "expm1bf16",
+ deps = [
+ "//libc/src/__support/FPUtil:bfloat16",
+ "//libc/src/__support/FPUtil:cast",
+ ],
+)
+
math_test(
name = "issignaling",
hdrs = ["IsSignalingTest.h"],
>From 540ae069296c7c2dece825f8e73c06f931202642 Mon Sep 17 00:00:00 2001
From: Arnav Kapoor <kapoorarnav43 at gmail.com>
Date: Sat, 28 Nov 2026 02:23:34 +0530
Subject: [PATCH 2/3] Adding new header
Added documentation comments for the expm1bf16 function.
---
libc/shared/math/expm1bf16.h | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/libc/shared/math/expm1bf16.h b/libc/shared/math/expm1bf16.h
index 686833da10869f..f80a076ce90f74 100644
--- a/libc/shared/math/expm1bf16.h
+++ b/libc/shared/math/expm1bf16.h
@@ -1,10 +1,17 @@
-//===-- Shared expm1bf16 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
//
//===----------------------------------------------------------------------===//
+///
+/// \expm1bf16.h
+/// This file contains the declaration of the Instruction class, which is the
+/// base class for all of the VM instructions.
+///
+//===----------------------------------------------------------------------===//
+
#ifndef LLVM_LIBC_SHARED_MATH_EXPM1BF16_H
#define LLVM_LIBC_SHARED_MATH_EXPM1BF16_H
>From a4d0b2e1bc68b28571225271f24d7ff50035452b Mon Sep 17 00:00:00 2001
From: Arnav Kapoor <kapoorarnav43 at gmail.com>
Date: Sun, 29 Nov 2026 01:50:02 +0530
Subject: [PATCH 3/3] Use new LLVM file headers and drop duplicate Bazel dep
Convert the file headers of all new files to the style in the LLVM
Coding Standards, and fix the header of shared/math/expm1bf16.h, which
still had the example's text.
The smoke test's Bazel rule listed FPUtil:cast, which math_test already
adds, so Bazel rejected the package.
---
libc/shared/math/expm1bf16.h | 6 ++----
libc/src/__support/math/expbf16_utils.h | 7 ++++++-
libc/src/__support/math/expm1bf16.h | 7 ++++++-
libc/src/math/expm1bf16.h | 7 ++++++-
libc/src/math/generic/expm1bf16.cpp | 7 ++++++-
libc/test/src/math/expm1bf16_test.cpp | 7 ++++++-
libc/test/src/math/smoke/expm1bf16_test.cpp | 7 ++++++-
.../libc/test/src/math/smoke/BUILD.bazel | 1 -
8 files changed, 38 insertions(+), 11 deletions(-)
diff --git a/libc/shared/math/expm1bf16.h b/libc/shared/math/expm1bf16.h
index f80a076ce90f74..580dce29d57029 100644
--- a/libc/shared/math/expm1bf16.h
+++ b/libc/shared/math/expm1bf16.h
@@ -6,13 +6,11 @@
//
//===----------------------------------------------------------------------===//
///
-/// \expm1bf16.h
-/// This file contains the declaration of the Instruction class, which is the
-/// base class for all of the VM instructions.
+/// \file
+/// Shared expm1bf16 function.
///
//===----------------------------------------------------------------------===//
-
#ifndef LLVM_LIBC_SHARED_MATH_EXPM1BF16_H
#define LLVM_LIBC_SHARED_MATH_EXPM1BF16_H
diff --git a/libc/src/__support/math/expbf16_utils.h b/libc/src/__support/math/expbf16_utils.h
index e98eeb02b3293b..806a3167014189 100644
--- a/libc/src/__support/math/expbf16_utils.h
+++ b/libc/src/__support/math/expbf16_utils.h
@@ -1,10 +1,15 @@
-//===-- Shared tables for bfloat16 exp functions ----------------*- 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
//
//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Shared tables for bfloat16 exp functions.
+///
+//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXPBF16_UTILS_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_EXPBF16_UTILS_H
diff --git a/libc/src/__support/math/expm1bf16.h b/libc/src/__support/math/expm1bf16.h
index dbec650af25e62..b0066529ee2635 100644
--- a/libc/src/__support/math/expm1bf16.h
+++ b/libc/src/__support/math/expm1bf16.h
@@ -1,10 +1,15 @@
-//===-- Implementation header for expm1bf16 ---------------------*- 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
//
//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for expm1bf16.
+///
+//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXPM1BF16_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_EXPM1BF16_H
diff --git a/libc/src/math/expm1bf16.h b/libc/src/math/expm1bf16.h
index 89de74de4380f5..8e739ddd355ebc 100644
--- a/libc/src/math/expm1bf16.h
+++ b/libc/src/math/expm1bf16.h
@@ -1,10 +1,15 @@
-//===-- Implementation header for expm1bf16 ---------------------*- 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
//
//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for expm1bf16.
+///
+//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC_MATH_EXPM1BF16_H
#define LLVM_LIBC_SRC_MATH_EXPM1BF16_H
diff --git a/libc/src/math/generic/expm1bf16.cpp b/libc/src/math/generic/expm1bf16.cpp
index a8995d200a4e0b..cac53792d3d821 100644
--- a/libc/src/math/generic/expm1bf16.cpp
+++ b/libc/src/math/generic/expm1bf16.cpp
@@ -1,10 +1,15 @@
-//===-- BFloat16 e^x - 1 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
//
//===----------------------------------------------------------------------===//
+///
+/// \file
+/// BFloat16 e^x - 1 function.
+///
+//===----------------------------------------------------------------------===//
#include "src/math/expm1bf16.h"
#include "src/__support/math/expm1bf16.h"
diff --git a/libc/test/src/math/expm1bf16_test.cpp b/libc/test/src/math/expm1bf16_test.cpp
index fee78ac1400209..19178d06851593 100644
--- a/libc/test/src/math/expm1bf16_test.cpp
+++ b/libc/test/src/math/expm1bf16_test.cpp
@@ -1,10 +1,15 @@
-//===-- Exhaustive test for expm1bf16 -------------------------------------===//
+//===----------------------------------------------------------------------===//
//
// 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
+/// Exhaustive test for expm1bf16.
+///
+//===----------------------------------------------------------------------===//
#include "src/__support/FPUtil/bfloat16.h"
#include "src/math/expm1bf16.h"
diff --git a/libc/test/src/math/smoke/expm1bf16_test.cpp b/libc/test/src/math/smoke/expm1bf16_test.cpp
index e0a3caeffb2c04..dbda068e224af1 100644
--- a/libc/test/src/math/smoke/expm1bf16_test.cpp
+++ b/libc/test/src/math/smoke/expm1bf16_test.cpp
@@ -1,10 +1,15 @@
-//===-- Unittests for expm1bf16 -------------------------------------------===//
+//===----------------------------------------------------------------------===//
//
// 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
+/// Unittests for expm1bf16.
+///
+//===----------------------------------------------------------------------===//
#include "hdr/errno_macros.h"
#include "hdr/fenv_macros.h"
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel
index 4cb90d359da5de..f8b3ed588edbad 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel
@@ -2710,7 +2710,6 @@ math_test(
name = "expm1bf16",
deps = [
"//libc/src/__support/FPUtil:bfloat16",
- "//libc/src/__support/FPUtil:cast",
],
)
More information about the libc-commits
mailing list