[libc-commits] [libc] [libc][math][c++23] Add exp2bf16 math function (PR #230213)

via libc-commits libc-commits at lists.llvm.org
Fri Oct 9 09:49:19 PDT 2026


https://github.com/un-pixelated updated https://github.com/llvm/llvm-project/pull/230213

>From a4849758d59524262ef2656cfdd70f8c2cba2f65 Mon Sep 17 00:00:00 2001
From: pixl <216277949+un-pixelated at users.noreply.github.com>
Date: Thu, 8 Oct 2026 22:35:10 +0530
Subject: [PATCH 1/8] [libc][math] Start exp2bf16 implementation


>From 01422936a653d608c83eff7a7b19173ac10e58f7 Mon Sep 17 00:00:00 2001
From: pixl <216277949+un-pixelated at users.noreply.github.com>
Date: Fri, 9 Oct 2026 00:22:57 +0530
Subject: [PATCH 2/8] [libc][math] Register exp2bf16 entrypoint for
 bf16-supporting targets

---
 libc/config/baremetal/aarch64/entrypoints.txt | 1 +
 libc/config/baremetal/arm/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/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 +
 12 files changed, 12 insertions(+)

diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index 114534ce61ae6e..cb341c2d8b0e30 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -828,6 +828,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.canonicalizebf16
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
+  libc.src.math.exp2bf16
   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 c8148815c34e34..65e36c110d696f 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -839,6 +839,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.canonicalizebf16
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
+  libc.src.math.exp2bf16
   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 622d6156cff11d..caa5f3c559cc6c 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -831,6 +831,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.canonicalizebf16
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
+  libc.src.math.exp2bf16
   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 d702b2c5a65497..5600b82ad3410b 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -650,6 +650,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.canonicalizebf16
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
+  libc.src.math.exp2bf16
   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 716ab572f8d3cd..e3fc845d2ed3af 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -285,6 +285,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.canonicalizebf16
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
+  libc.src.math.exp2bf16
   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 5c5e5891da8227..2eaef608e9a7ac 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -699,6 +699,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.canonicalizebf16
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
+  libc.src.math.exp2bf16
   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 afae725484a891..79d0c733ec3c01 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -715,6 +715,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.canonicalizebf16
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
+  libc.src.math.exp2bf16
   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 b8a9a5cff2040f..fb7a850b8eb13d 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1058,6 +1058,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.canonicalizebf16
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
+  libc.src.math.exp2bf16
   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 ac66f1f5184842..5f99af243fcfad 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -659,6 +659,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.canonicalizebf16
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
+  libc.src.math.exp2bf16
   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 0728bd41a40f9d..a7d813cacc95bf 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1132,6 +1132,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.canonicalizebf16
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
+  libc.src.math.exp2bf16
   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 2df3fb0dfb3636..ec58d0e145f758 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1154,6 +1154,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.canonicalizebf16
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
+  libc.src.math.exp2bf16
   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 3a48b49e169b3a..1712b2ad26fda4 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -376,6 +376,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.canonicalizebf16
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
+  libc.src.math.exp2bf16
   libc.src.math.expbf16
   libc.src.math.fabsbf16
   libc.src.math.fdimbf16

>From 6ed7de038c48671fffc26f77e74b5c731e237e65 Mon Sep 17 00:00:00 2001
From: pixl <216277949+un-pixelated at users.noreply.github.com>
Date: Fri, 9 Oct 2026 00:19:10 +0530
Subject: [PATCH 3/8] [libc][math] Add exp2bf16 core implementation

---
 libc/src/__support/math/CMakeLists.txt | 20 +++++++++++++++++++
 libc/src/__support/math/exp2bf16.h     | 27 ++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
 create mode 100644 libc/src/__support/math/exp2bf16.h

diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index ef33e3f8ada7b0..61cc05b0a5bf82 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -4295,6 +4295,26 @@ add_header_library(
     libc.src.__support.macros.optimization
 )
 
+add_header_library(
+  exp2bf16
+  HDRS
+    exp2bf16.h
+  DEPENDS
+    libc.hdr.errno_macros
+    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.nearest_integer
+    libc.src.__support.FPUtil.polyeval
+    libc.src.__support.FPUtil.rounding_mode
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+    libc.src.__support.macros.optimization
+)
+
 add_header_library(
   exp2f16
   HDRS
diff --git a/libc/src/__support/math/exp2bf16.h b/libc/src/__support/math/exp2bf16.h
new file mode 100644
index 00000000000000..f4ea624615fe9b
--- /dev/null
+++ b/libc/src/__support/math/exp2bf16.h
@@ -0,0 +1,27 @@
+//===-- Implementation header for exp2bf16 ----------------------*- 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_EXP2BF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_EXP2BF16_H
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE bfloat16 exp2bf16(bfloat16 x) {
+  // TODO: implement
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXP2BF16_H

>From f8dbd37e63b7874b701e071f2a83ee5afd0ef459 Mon Sep 17 00:00:00 2001
From: pixl <216277949+un-pixelated at users.noreply.github.com>
Date: Fri, 9 Oct 2026 00:19:21 +0530
Subject: [PATCH 4/8] [libc][math] Wire up exp2bf16 entrypoint

---
 libc/shared/math/exp2bf16.h          | 23 +++++++++++++++++++++++
 libc/src/math/CMakeLists.txt         |  1 +
 libc/src/math/exp2bf16.h             | 21 +++++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt | 10 ++++++++++
 libc/src/math/generic/exp2bf16.cpp   | 18 ++++++++++++++++++
 5 files changed, 73 insertions(+)
 create mode 100644 libc/shared/math/exp2bf16.h
 create mode 100644 libc/src/math/exp2bf16.h
 create mode 100644 libc/src/math/generic/exp2bf16.cpp

diff --git a/libc/shared/math/exp2bf16.h b/libc/shared/math/exp2bf16.h
new file mode 100644
index 00000000000000..5b9172e674415f
--- /dev/null
+++ b/libc/shared/math/exp2bf16.h
@@ -0,0 +1,23 @@
+//===-- Shared exp2bf16 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_EXP2BF16_H
+#define LLVM_LIBC_SHARED_MATH_EXP2BF16_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/exp2bf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::exp2bf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_EXP2BF16_H
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 2c7568f0b63618..4f1dd95a6993da 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -154,6 +154,7 @@ add_math_entrypoint_object(expf16)
 add_math_entrypoint_object(expbf16)
 
 add_math_entrypoint_object(exp2)
+add_math_entrypoint_object(exp2bf16)
 add_math_entrypoint_object(exp2f)
 add_math_entrypoint_object(exp2f16)
 
diff --git a/libc/src/math/exp2bf16.h b/libc/src/math/exp2bf16.h
new file mode 100644
index 00000000000000..2eaa61acfdd5b3
--- /dev/null
+++ b/libc/src/math/exp2bf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for exp2bf16 ----------------------*- 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_EXP2BF16_H
+#define LLVM_LIBC_SRC_MATH_EXP2BF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 exp2bf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_EXP2BF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 28b8e954530ddf..17f586036396f1 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1284,6 +1284,16 @@ add_entrypoint_object(
     libc.src.__support.math.exp2f
 )
 
+add_entrypoint_object(
+  exp2bf16
+  SRCS
+    exp2bf16.cpp
+  HDRS
+    ../exp2bf16.h
+  DEPENDS
+    libc.src.__support.math.exp2bf16
+)
+
 add_entrypoint_object(
   exp2f16
   SRCS
diff --git a/libc/src/math/generic/exp2bf16.cpp b/libc/src/math/generic/exp2bf16.cpp
new file mode 100644
index 00000000000000..e5c476162f9674
--- /dev/null
+++ b/libc/src/math/generic/exp2bf16.cpp
@@ -0,0 +1,18 @@
+//===-- BFloat16 2^x function ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/math/exp2bf16.h"
+#include "src/math/exp2bf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, exp2bf16, (bfloat16 x)) {
+  return math::exp2bf16(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL

>From ef3e6a573a845b2e7e7f3a49a2f042bf7fcc7565 Mon Sep 17 00:00:00 2001
From: pixl <216277949+un-pixelated at users.noreply.github.com>
Date: Fri, 9 Oct 2026 00:19:28 +0530
Subject: [PATCH 5/8] [libc][math] Add exp2bf16 test stubs

---
 libc/test/src/math/CMakeLists.txt          | 12 ++++++++++++
 libc/test/src/math/exp2bf16_test.cpp       |  9 +++++++++
 libc/test/src/math/smoke/CMakeLists.txt    | 14 ++++++++++++++
 libc/test/src/math/smoke/exp2bf16_test.cpp |  9 +++++++++
 4 files changed, 44 insertions(+)
 create mode 100644 libc/test/src/math/exp2bf16_test.cpp
 create mode 100644 libc/test/src/math/smoke/exp2bf16_test.cpp

diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 92d599fba947e5..97facaf909097f 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -1398,6 +1398,18 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  exp2bf16_test
+  NEED_MPFR
+  SUITE
+    libc-math-unittests
+  SRCS
+    exp2bf16_test.cpp
+  DEPENDS
+    libc.src.math.exp2bf16
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_fp_unittest(
   exp2f16_test
   NEED_MPFR
diff --git a/libc/test/src/math/exp2bf16_test.cpp b/libc/test/src/math/exp2bf16_test.cpp
new file mode 100644
index 00000000000000..6c4be3afce6d2f
--- /dev/null
+++ b/libc/test/src/math/exp2bf16_test.cpp
@@ -0,0 +1,9 @@
+//===-- Exhaustive test for exp2bf16 --------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// TODO: implement
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index fe9fafdfab9a3f..cf9ca1fa39780e 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -1499,6 +1499,20 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  exp2bf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    exp2bf16_test.cpp
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.fenv_macros
+    libc.src.math.exp2bf16
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.cast
+)
+
 add_fp_unittest(
   exp2f16_test
   SUITE
diff --git a/libc/test/src/math/smoke/exp2bf16_test.cpp b/libc/test/src/math/smoke/exp2bf16_test.cpp
new file mode 100644
index 00000000000000..0417c53672af52
--- /dev/null
+++ b/libc/test/src/math/smoke/exp2bf16_test.cpp
@@ -0,0 +1,9 @@
+//===-- Unittests for exp2bf16 --------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// TODO: implement

>From 2345d5c7f2843f2fa4b3245bdc7ce9abafe85a83 Mon Sep 17 00:00:00 2001
From: pixl <216277949+un-pixelated at users.noreply.github.com>
Date: Fri, 9 Oct 2026 21:46:29 +0530
Subject: [PATCH 6/8] Update CMakeLists.txt Remove cast.h because bfloat16
 already ships with its cast function

---
 libc/test/src/math/smoke/CMakeLists.txt | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index cf9ca1fa39780e..6f1650fcffa7fd 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -1510,7 +1510,6 @@ add_fp_unittest(
     libc.hdr.fenv_macros
     libc.src.math.exp2bf16
     libc.src.__support.FPUtil.bfloat16
-    libc.src.__support.FPUtil.cast
 )
 
 add_fp_unittest(

>From afead3bfb592a12e77fd19f7a94c074e1ec8a00a Mon Sep 17 00:00:00 2001
From: pixl <216277949+un-pixelated at users.noreply.github.com>
Date: Fri, 9 Oct 2026 21:46:38 +0530
Subject: [PATCH 7/8] Add smoke test

---
 libc/test/src/math/smoke/exp2bf16_test.cpp | 55 +++++++++++++++++++++-
 1 file changed, 54 insertions(+), 1 deletion(-)

diff --git a/libc/test/src/math/smoke/exp2bf16_test.cpp b/libc/test/src/math/smoke/exp2bf16_test.cpp
index 0417c53672af52..7aeaf25a2d61ed 100644
--- a/libc/test/src/math/smoke/exp2bf16_test.cpp
+++ b/libc/test/src/math/smoke/exp2bf16_test.cpp
@@ -6,4 +6,57 @@
 //
 //===----------------------------------------------------------------------===//
 
-// TODO: implement
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/exp2bf16.h"
+#include "test/UnitTest/FEnvSafeTest.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+class LlvmLibcExp2bf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+  DECLARE_SPECIAL_CONSTANTS(bfloat16)
+
+public:
+  void test_special_numbers() {
+    EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::exp2bf16(aNaN));
+    EXPECT_MATH_ERRNO(0);
+
+    EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::exp2bf16(sNaN),
+                                FE_INVALID);
+    EXPECT_MATH_ERRNO(0);
+
+    EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::exp2bf16(inf));
+    EXPECT_MATH_ERRNO(0);
+
+    EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::exp2bf16(neg_inf));
+    EXPECT_MATH_ERRNO(0);
+
+    EXPECT_FP_EQ_ALL_ROUNDING(bfloat16(1.0f), LIBC_NAMESPACE::exp2bf16(zero));
+    EXPECT_MATH_ERRNO(0);
+
+    EXPECT_FP_EQ_ALL_ROUNDING(bfloat16(1.0f),
+                              LIBC_NAMESPACE::exp2bf16(neg_zero));
+    EXPECT_MATH_ERRNO(0);
+
+    EXPECT_FP_EQ_WITH_EXCEPTION(
+        inf, LIBC_NAMESPACE::exp2bf16(max_normal), FE_OVERFLOW);
+    EXPECT_MATH_ERRNO(ERANGE);
+
+    EXPECT_FP_EQ_WITH_EXCEPTION(
+        inf, LIBC_NAMESPACE::exp2bf16(bfloat16(128.0f)), FE_OVERFLOW);
+    EXPECT_MATH_ERRNO(ERANGE);
+
+    EXPECT_FP_EQ_WITH_EXCEPTION(
+        zero, LIBC_NAMESPACE::exp2bf16(neg_max_normal),
+        FE_UNDERFLOW | FE_INEXACT);
+    EXPECT_MATH_ERRNO(ERANGE);
+
+    EXPECT_FP_EQ_WITH_EXCEPTION(
+        zero, LIBC_NAMESPACE::exp2bf16(bfloat16(-134.0f)),
+        FE_UNDERFLOW | FE_INEXACT);
+    EXPECT_MATH_ERRNO(ERANGE);
+  }
+};
+
+TEST_F(LlvmLibcExp2bf16Test, SpecialNumbers) { test_special_numbers(); }

>From 44209dd3fb50396cd38b1ebecbef1daad2cf845d Mon Sep 17 00:00:00 2001
From: pixl <216277949+un-pixelated at users.noreply.github.com>
Date: Fri, 9 Oct 2026 22:01:58 +0530
Subject: [PATCH 8/8] Add exhaustive tests

---
 libc/test/src/math/exp2bf16_test.cpp | 34 +++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)

diff --git a/libc/test/src/math/exp2bf16_test.cpp b/libc/test/src/math/exp2bf16_test.cpp
index 6c4be3afce6d2f..81098699da0fcf 100644
--- a/libc/test/src/math/exp2bf16_test.cpp
+++ b/libc/test/src/math/exp2bf16_test.cpp
@@ -6,4 +6,36 @@
 //
 //===----------------------------------------------------------------------===//
 
-// TODO: implement
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/exp2bf16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcExp2bf16Test = 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(LlvmLibcExp2bf16Test, PositiveRange) {
+  for (uint16_t v = POS_START; v <= POS_STOP; v++) {
+    bfloat16 x = FPBits(v).get_val();
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp2, x,
+                                   LIBC_NAMESPACE::exp2bf16(x), 0.5);
+  }
+}
+
+TEST_F(LlvmLibcExp2bf16Test, NegativeRange) {
+  for (uint16_t v = NEG_START; v <= NEG_STOP; v++) {
+    bfloat16 x = FPBits(v).get_val();
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp2, x,
+                                   LIBC_NAMESPACE::exp2bf16(x), 0.5);
+  }
+}



More information about the libc-commits mailing list