[libc-commits] [libc] [libc][math][c++23] Add {modf, remainder, remquo}bf16 math functions (PR #154652)

Krishna Pandey via libc-commits libc-commits at lists.llvm.org
Mon Aug 25 13:07:38 PDT 2025


https://github.com/krishna2803 updated https://github.com/llvm/llvm-project/pull/154652

>From b58785d627db072eee8cc8f06adc57268d75a8d1 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Sun, 17 Aug 2025 05:03:27 +0530
Subject: [PATCH 01/29] feat: implement {,u}fromfp{,x}bf16 math functions

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/src/math/CMakeLists.txt           |  4 ++
 libc/src/math/fromfpbf16.h             | 21 ++++++++++
 libc/src/math/fromfpxbf16.h            | 21 ++++++++++
 libc/src/math/generic/CMakeLists.txt   | 56 ++++++++++++++++++++++++++
 libc/src/math/generic/fromfpbf16.cpp   | 22 ++++++++++
 libc/src/math/generic/fromfpxbf16.cpp  | 22 ++++++++++
 libc/src/math/generic/ufromfpbf16.cpp  | 22 ++++++++++
 libc/src/math/generic/ufromfpxbf16.cpp | 22 ++++++++++
 libc/src/math/ufromfpbf16.h            | 21 ++++++++++
 libc/src/math/ufromfpxbf16.h           | 21 ++++++++++
 10 files changed, 232 insertions(+)
 create mode 100644 libc/src/math/fromfpbf16.h
 create mode 100644 libc/src/math/fromfpxbf16.h
 create mode 100644 libc/src/math/generic/fromfpbf16.cpp
 create mode 100644 libc/src/math/generic/fromfpxbf16.cpp
 create mode 100644 libc/src/math/generic/ufromfpbf16.cpp
 create mode 100644 libc/src/math/generic/ufromfpxbf16.cpp
 create mode 100644 libc/src/math/ufromfpbf16.h
 create mode 100644 libc/src/math/ufromfpxbf16.h

diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 8db5901afa9c0..f94120272ce66 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -312,12 +312,14 @@ add_math_entrypoint_object(fromfpf)
 add_math_entrypoint_object(fromfpl)
 add_math_entrypoint_object(fromfpf16)
 add_math_entrypoint_object(fromfpf128)
+add_math_entrypoint_object(fromfpbf16)
 
 add_math_entrypoint_object(fromfpx)
 add_math_entrypoint_object(fromfpxf)
 add_math_entrypoint_object(fromfpxl)
 add_math_entrypoint_object(fromfpxf16)
 add_math_entrypoint_object(fromfpxf128)
+add_math_entrypoint_object(fromfpxbf16)
 
 add_math_entrypoint_object(fsub)
 add_math_entrypoint_object(fsubl)
@@ -567,12 +569,14 @@ add_math_entrypoint_object(ufromfpf)
 add_math_entrypoint_object(ufromfpl)
 add_math_entrypoint_object(ufromfpf16)
 add_math_entrypoint_object(ufromfpf128)
+add_math_entrypoint_object(ufromfpbf16)
 
 add_math_entrypoint_object(ufromfpx)
 add_math_entrypoint_object(ufromfpxf)
 add_math_entrypoint_object(ufromfpxl)
 add_math_entrypoint_object(ufromfpxf16)
 add_math_entrypoint_object(ufromfpxf128)
+add_math_entrypoint_object(ufromfpxbf16)
 
 add_math_entrypoint_object(bf16add)
 add_math_entrypoint_object(bf16addf)
diff --git a/libc/src/math/fromfpbf16.h b/libc/src/math/fromfpbf16.h
new file mode 100644
index 0000000000000..bff991c6c1ed5
--- /dev/null
+++ b/libc/src/math/fromfpbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for fromfpbf16 --------------------*- 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_FROMFPBF16_H
+#define LLVM_LIBC_SRC_MATH_FROMFPBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 fromfpbf16(bfloat16 x, int rnd, unsigned int width);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_FROMFPBF16_H
diff --git a/libc/src/math/fromfpxbf16.h b/libc/src/math/fromfpxbf16.h
new file mode 100644
index 0000000000000..e40d975617ef2
--- /dev/null
+++ b/libc/src/math/fromfpxbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for fromfpxbf16 -------------------*- 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_FROMFPXBF16_H
+#define LLVM_LIBC_SRC_MATH_FROMFPXBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 fromfpxbf16(bfloat16 x, int rnd, unsigned int width);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_FROMFPXBF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 65e4839268443..1608bed87cb6b 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3745,6 +3745,20 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.nearest_integer_operations
 )
 
+add_entrypoint_object(
+  fromfpbf16
+  SRCS
+    fromfpbf16.cpp
+  HDRS
+    ../fromfpbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.nearest_integer_operations
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
 add_entrypoint_object(
   fromfpx
   SRCS
@@ -3797,6 +3811,20 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.nearest_integer_operations
 )
 
+add_entrypoint_object(
+  fromfpxbf16
+  SRCS
+    fromfpxbf16.cpp
+  HDRS
+    ../fromfpxbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.nearest_integer_operations
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
 add_entrypoint_object(
   ufromfp
   SRCS
@@ -3849,6 +3877,20 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.nearest_integer_operations
 )
 
+add_entrypoint_object(
+  ufromfpbf16
+  SRCS
+    ufromfpbf16.cpp
+  HDRS
+    ../ufromfpbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.nearest_integer_operations
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
 add_entrypoint_object(
   ufromfpx
   SRCS
@@ -3901,6 +3943,20 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.nearest_integer_operations
 )
 
+add_entrypoint_object(
+  ufromfpxbf16
+  SRCS
+    ufromfpxbf16.cpp
+  HDRS
+    ../ufromfpxbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.nearest_integer_operations
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
 add_entrypoint_object(
   coshf
   SRCS
diff --git a/libc/src/math/generic/fromfpbf16.cpp b/libc/src/math/generic/fromfpbf16.cpp
new file mode 100644
index 0000000000000..db1b8f18f6810
--- /dev/null
+++ b/libc/src/math/generic/fromfpbf16.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of fromfpbf16 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/fromfpbf16.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, fromfpbf16,
+                   (bfloat16 x, int rnd, unsigned int width)) {
+  return fputil::fromfp</*IsSigned=*/true>(x, rnd, width);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fromfpxbf16.cpp b/libc/src/math/generic/fromfpxbf16.cpp
new file mode 100644
index 0000000000000..8c16c411265ab
--- /dev/null
+++ b/libc/src/math/generic/fromfpxbf16.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of fromfpxbf16 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/fromfpxbf16.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, fromfpxbf16,
+                   (bfloat16 x, int rnd, unsigned int width)) {
+  return fputil::fromfpx</*IsSigned=*/true>(x, rnd, width);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/ufromfpbf16.cpp b/libc/src/math/generic/ufromfpbf16.cpp
new file mode 100644
index 0000000000000..336771b695d97
--- /dev/null
+++ b/libc/src/math/generic/ufromfpbf16.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of ufromfpbf16 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/ufromfpbf16.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, ufromfpbf16,
+                   (bfloat16 x, int rnd, unsigned int width)) {
+  return fputil::fromfp</*IsSigned=*/false>(x, rnd, width);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/ufromfpxbf16.cpp b/libc/src/math/generic/ufromfpxbf16.cpp
new file mode 100644
index 0000000000000..ac9cf44470a8e
--- /dev/null
+++ b/libc/src/math/generic/ufromfpxbf16.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of ufromfpxbf16 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/ufromfpxbf16.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, ufromfpxbf16,
+                   (bfloat16 x, int rnd, unsigned int width)) {
+  return fputil::fromfpx</*IsSigned=*/false>(x, rnd, width);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/ufromfpbf16.h b/libc/src/math/ufromfpbf16.h
new file mode 100644
index 0000000000000..1fd876a15e600
--- /dev/null
+++ b/libc/src/math/ufromfpbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for ufromfpbf16 -------------------*- 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_UFROMFPBF16_H
+#define LLVM_LIBC_SRC_MATH_UFROMFPBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 ufromfpbf16(bfloat16 x, int rnd, unsigned int width);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_UFROMFPBF16_H
diff --git a/libc/src/math/ufromfpxbf16.h b/libc/src/math/ufromfpxbf16.h
new file mode 100644
index 0000000000000..ec63744c8045b
--- /dev/null
+++ b/libc/src/math/ufromfpxbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for ufromfpxbf16 ------------------*- 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_UFROMFPXBF16_H
+#define LLVM_LIBC_SRC_MATH_UFROMFPXBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 ufromfpxbf16(bfloat16 x, int rnd, unsigned int width);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_UFROMFPXBF16_H

>From 0a2b5278c817a3b75c61a2a655a50aba76e050b3 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Sun, 17 Aug 2025 05:03:49 +0530
Subject: [PATCH 02/29] chore: add tests for {,u}fromfp{,x}bf16 math functions

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/test/src/math/smoke/CMakeLists.txt       | 53 ++++++++++++++++++
 libc/test/src/math/smoke/FromfpTest.h         | 56 +++++++++----------
 libc/test/src/math/smoke/FromfpxTest.h        | 48 ++++++++--------
 libc/test/src/math/smoke/UfromfpTest.h        | 28 +++++-----
 libc/test/src/math/smoke/UfromfpxTest.h       | 24 ++++----
 libc/test/src/math/smoke/fromfpbf16_test.cpp  | 14 +++++
 libc/test/src/math/smoke/fromfpxbf16_test.cpp | 14 +++++
 libc/test/src/math/smoke/ufromfpbf16_test.cpp | 14 +++++
 .../test/src/math/smoke/ufromfpxbf16_test.cpp | 14 +++++
 9 files changed, 187 insertions(+), 78 deletions(-)
 create mode 100644 libc/test/src/math/smoke/fromfpbf16_test.cpp
 create mode 100644 libc/test/src/math/smoke/fromfpxbf16_test.cpp
 create mode 100644 libc/test/src/math/smoke/ufromfpbf16_test.cpp
 create mode 100644 libc/test/src/math/smoke/ufromfpxbf16_test.cpp

diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 00881bd27f24e..694467e19d47c 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -1596,6 +1596,19 @@ add_fp_unittest(
     libc.src.math.fromfpf128
 )
 
+add_fp_unittest(
+  fromfpbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    fromfpbf16_test.cpp
+  HDRS
+    FromfpTest.h
+  DEPENDS
+    libc.src.math.fromfpbf16
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_fp_unittest(
   fromfpx_test
   SUITE
@@ -1656,6 +1669,20 @@ add_fp_unittest(
     libc.src.math.fromfpxf128
 )
 
+add_fp_unittest(
+  fromfpxbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    fromfpxbf16_test.cpp
+  HDRS
+    FromfpxTest.h
+  DEPENDS
+    libc.src.math.fromfpxbf16
+    libc.src.__support.FPUtil.bfloat16
+)
+
+
 add_fp_unittest(
   ufromfp_test
   SUITE
@@ -1716,6 +1743,19 @@ add_fp_unittest(
     libc.src.math.ufromfpf128
 )
 
+add_fp_unittest(
+  ufromfpbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    ufromfpbf16_test.cpp
+  HDRS
+    UfromfpTest.h
+  DEPENDS
+    libc.src.math.ufromfpbf16
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_fp_unittest(
   ufromfpx_test
   SUITE
@@ -1776,6 +1816,19 @@ add_fp_unittest(
     libc.src.math.ufromfpxf128
 )
 
+add_fp_unittest(
+  ufromfpxbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    ufromfpxbf16_test.cpp
+  HDRS
+    UfromfpxTest.h
+  DEPENDS
+    libc.src.math.ufromfpxbf16
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_fp_unittest(
   ilogb_test
   SUITE
diff --git a/libc/test/src/math/smoke/FromfpTest.h b/libc/test/src/math/smoke/FromfpTest.h
index 56205187d731c..d786dfdf8dc5a 100644
--- a/libc/test/src/math/smoke/FromfpTest.h
+++ b/libc/test/src/math/smoke/FromfpTest.h
@@ -85,10 +85,10 @@ class FromfpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_FP_EQ(T(-10.0), func(T(-10.32), FP_INT_UPWARD, 5U));
     EXPECT_FP_EQ(T(11.0), func(T(10.65), FP_INT_UPWARD, 5U));
     EXPECT_FP_EQ(T(-10.0), func(T(-10.65), FP_INT_UPWARD, 5U));
-    EXPECT_FP_EQ(T(124.0), func(T(123.38), FP_INT_UPWARD, 8U));
-    EXPECT_FP_EQ(T(-123.0), func(T(-123.38), FP_INT_UPWARD, 8U));
-    EXPECT_FP_EQ(T(124.0), func(T(123.96), FP_INT_UPWARD, 8U));
-    EXPECT_FP_EQ(T(-123.0), func(T(-123.96), FP_INT_UPWARD, 8U));
+    EXPECT_FP_EQ(T(64.0), func(T(63.25), FP_INT_UPWARD, 8U));
+    EXPECT_FP_EQ(T(-63.0), func(T(-63.25), FP_INT_UPWARD, 8U));
+    EXPECT_FP_EQ(T(64.0), func(T(63.75), FP_INT_UPWARD, 8U));
+    EXPECT_FP_EQ(T(-63.0), func(T(-63.75), FP_INT_UPWARD, 8U));
   }
 
   void testFractionsUpwardOutsideRange(FromfpFunc func) {
@@ -139,10 +139,10 @@ class FromfpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_FP_EQ(T(-11.0), func(T(-10.32), FP_INT_DOWNWARD, 5U));
     EXPECT_FP_EQ(T(10.0), func(T(10.65), FP_INT_DOWNWARD, 5U));
     EXPECT_FP_EQ(T(-11.0), func(T(-10.65), FP_INT_DOWNWARD, 5U));
-    EXPECT_FP_EQ(T(123.0), func(T(123.38), FP_INT_DOWNWARD, 8U));
-    EXPECT_FP_EQ(T(-124.0), func(T(-123.38), FP_INT_DOWNWARD, 8U));
-    EXPECT_FP_EQ(T(123.0), func(T(123.96), FP_INT_DOWNWARD, 8U));
-    EXPECT_FP_EQ(T(-124.0), func(T(-123.96), FP_INT_DOWNWARD, 8U));
+    EXPECT_FP_EQ(T(63.0), func(T(63.25), FP_INT_DOWNWARD, 8U));
+    EXPECT_FP_EQ(T(-64.0), func(T(-63.25), FP_INT_DOWNWARD, 8U));
+    EXPECT_FP_EQ(T(63.0), func(T(63.75), FP_INT_DOWNWARD, 8U));
+    EXPECT_FP_EQ(T(-64.0), func(T(-63.75), FP_INT_DOWNWARD, 8U));
   }
 
   void testFractionsDownwardOutsideRange(FromfpFunc func) {
@@ -193,10 +193,10 @@ class FromfpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_FP_EQ(T(-10.0), func(T(-10.32), FP_INT_TOWARDZERO, 5U));
     EXPECT_FP_EQ(T(10.0), func(T(10.65), FP_INT_TOWARDZERO, 5U));
     EXPECT_FP_EQ(T(-10.0), func(T(-10.65), FP_INT_TOWARDZERO, 5U));
-    EXPECT_FP_EQ(T(123.0), func(T(123.38), FP_INT_TOWARDZERO, 8U));
-    EXPECT_FP_EQ(T(-123.0), func(T(-123.38), FP_INT_TOWARDZERO, 8U));
-    EXPECT_FP_EQ(T(123.0), func(T(123.96), FP_INT_TOWARDZERO, 8U));
-    EXPECT_FP_EQ(T(-123.0), func(T(-123.96), FP_INT_TOWARDZERO, 8U));
+    EXPECT_FP_EQ(T(63.0), func(T(63.25), FP_INT_TOWARDZERO, 8U));
+    EXPECT_FP_EQ(T(-63.0), func(T(-63.25), FP_INT_TOWARDZERO, 8U));
+    EXPECT_FP_EQ(T(63.0), func(T(63.75), FP_INT_TOWARDZERO, 8U));
+    EXPECT_FP_EQ(T(-63.0), func(T(-63.75), FP_INT_TOWARDZERO, 8U));
   }
 
   void testFractionsTowardZeroOutsideRange(FromfpFunc func) {
@@ -241,10 +241,10 @@ class FromfpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_FP_EQ(T(-10.0), func(T(-10.32), FP_INT_TONEARESTFROMZERO, 5U));
     EXPECT_FP_EQ(T(11.0), func(T(10.65), FP_INT_TONEARESTFROMZERO, 5U));
     EXPECT_FP_EQ(T(-11.0), func(T(-10.65), FP_INT_TONEARESTFROMZERO, 5U));
-    EXPECT_FP_EQ(T(123.0), func(T(123.38), FP_INT_TONEARESTFROMZERO, 8U));
-    EXPECT_FP_EQ(T(-123.0), func(T(-123.38), FP_INT_TONEARESTFROMZERO, 8U));
-    EXPECT_FP_EQ(T(124.0), func(T(123.96), FP_INT_TONEARESTFROMZERO, 8U));
-    EXPECT_FP_EQ(T(-124.0), func(T(-123.96), FP_INT_TONEARESTFROMZERO, 8U));
+    EXPECT_FP_EQ(T(63.0), func(T(63.25), FP_INT_TONEARESTFROMZERO, 8U));
+    EXPECT_FP_EQ(T(-63.0), func(T(-63.25), FP_INT_TONEARESTFROMZERO, 8U));
+    EXPECT_FP_EQ(T(64.0), func(T(63.75), FP_INT_TONEARESTFROMZERO, 8U));
+    EXPECT_FP_EQ(T(-64.0), func(T(-63.75), FP_INT_TONEARESTFROMZERO, 8U));
   }
 
   void testFractionsToNearestFromZeroOutsideRange(FromfpFunc func) {
@@ -297,10 +297,10 @@ class FromfpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_FP_EQ(T(-10.0), func(T(-10.32), FP_INT_TONEAREST, 5U));
     EXPECT_FP_EQ(T(11.0), func(T(10.65), FP_INT_TONEAREST, 5U));
     EXPECT_FP_EQ(T(-11.0), func(T(-10.65), FP_INT_TONEAREST, 5U));
-    EXPECT_FP_EQ(T(123.0), func(T(123.38), FP_INT_TONEAREST, 8U));
-    EXPECT_FP_EQ(T(-123.0), func(T(-123.38), FP_INT_TONEAREST, 8U));
-    EXPECT_FP_EQ(T(124.0), func(T(123.96), FP_INT_TONEAREST, 8U));
-    EXPECT_FP_EQ(T(-124.0), func(T(-123.96), FP_INT_TONEAREST, 8U));
+    EXPECT_FP_EQ(T(63.0), func(T(63.25), FP_INT_TONEAREST, 8U));
+    EXPECT_FP_EQ(T(-63.0), func(T(-63.25), FP_INT_TONEAREST, 8U));
+    EXPECT_FP_EQ(T(64.0), func(T(63.75), FP_INT_TONEAREST, 8U));
+    EXPECT_FP_EQ(T(-64.0), func(T(-63.75), FP_INT_TONEAREST, 8U));
 
     EXPECT_FP_EQ(T(2.0), func(T(2.3), FP_INT_TONEAREST, 3U));
     EXPECT_FP_EQ(T(-2.0), func(T(-2.3), FP_INT_TONEAREST, 2U));
@@ -391,14 +391,14 @@ class FromfpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_FP_EQ(T(11.0), func(T(10.65), UNKNOWN_MATH_ROUNDING_DIRECTION, 5U));
     EXPECT_FP_EQ(T(-11.0),
                  func(T(-10.65), UNKNOWN_MATH_ROUNDING_DIRECTION, 5U));
-    EXPECT_FP_EQ(T(123.0),
-                 func(T(123.38), UNKNOWN_MATH_ROUNDING_DIRECTION, 8U));
-    EXPECT_FP_EQ(T(-123.0),
-                 func(T(-123.38), UNKNOWN_MATH_ROUNDING_DIRECTION, 8U));
-    EXPECT_FP_EQ(T(124.0),
-                 func(T(123.96), UNKNOWN_MATH_ROUNDING_DIRECTION, 8U));
-    EXPECT_FP_EQ(T(-124.0),
-                 func(T(-123.96), UNKNOWN_MATH_ROUNDING_DIRECTION, 8U));
+    EXPECT_FP_EQ(T(63.0),
+                 func(T(63.25), UNKNOWN_MATH_ROUNDING_DIRECTION, 8U));
+    EXPECT_FP_EQ(T(-63.0),
+                 func(T(-63.25), UNKNOWN_MATH_ROUNDING_DIRECTION, 8U));
+    EXPECT_FP_EQ(T(64.0),
+                 func(T(63.75), UNKNOWN_MATH_ROUNDING_DIRECTION, 8U));
+    EXPECT_FP_EQ(T(-64.0),
+                 func(T(-63.75), UNKNOWN_MATH_ROUNDING_DIRECTION, 8U));
 
     EXPECT_FP_EQ(T(2.0), func(T(2.3), UNKNOWN_MATH_ROUNDING_DIRECTION, 3U));
     EXPECT_FP_EQ(T(-2.0), func(T(-2.3), UNKNOWN_MATH_ROUNDING_DIRECTION, 2U));
diff --git a/libc/test/src/math/smoke/FromfpxTest.h b/libc/test/src/math/smoke/FromfpxTest.h
index a175a66e21b64..cb4c98b10685b 100644
--- a/libc/test/src/math/smoke/FromfpxTest.h
+++ b/libc/test/src/math/smoke/FromfpxTest.h
@@ -101,13 +101,13 @@ class FromfpxTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
                                 FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(T(-10.0), func(T(-10.65), FP_INT_UPWARD, 5U),
                                 FE_INEXACT);
-    EXPECT_FP_EQ_WITH_EXCEPTION(T(124.0), func(T(123.38), FP_INT_UPWARD, 8U),
+    EXPECT_FP_EQ_WITH_EXCEPTION(T(64.0), func(T(63.25), FP_INT_UPWARD, 8U),
                                 FE_INEXACT);
-    EXPECT_FP_EQ_WITH_EXCEPTION(T(-123.0), func(T(-123.38), FP_INT_UPWARD, 8U),
+    EXPECT_FP_EQ_WITH_EXCEPTION(T(-63.0), func(T(-63.25), FP_INT_UPWARD, 8U),
                                 FE_INEXACT);
-    EXPECT_FP_EQ_WITH_EXCEPTION(T(124.0), func(T(123.96), FP_INT_UPWARD, 8U),
+    EXPECT_FP_EQ_WITH_EXCEPTION(T(64.0), func(T(63.75), FP_INT_UPWARD, 8U),
                                 FE_INEXACT);
-    EXPECT_FP_EQ_WITH_EXCEPTION(T(-123.0), func(T(-123.96), FP_INT_UPWARD, 8U),
+    EXPECT_FP_EQ_WITH_EXCEPTION(T(-63.0), func(T(-63.75), FP_INT_UPWARD, 8U),
                                 FE_INEXACT);
   }
 
@@ -175,14 +175,14 @@ class FromfpxTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
                                 FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(T(-11.0), func(T(-10.65), FP_INT_DOWNWARD, 5U),
                                 FE_INEXACT);
-    EXPECT_FP_EQ_WITH_EXCEPTION(T(123.0), func(T(123.38), FP_INT_DOWNWARD, 8U),
+    EXPECT_FP_EQ_WITH_EXCEPTION(T(63.0), func(T(63.25), FP_INT_DOWNWARD, 8U),
                                 FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(
-        T(-124.0), func(T(-123.38), FP_INT_DOWNWARD, 8U), FE_INEXACT);
-    EXPECT_FP_EQ_WITH_EXCEPTION(T(123.0), func(T(123.96), FP_INT_DOWNWARD, 8U),
+        T(-64.0), func(T(-63.25), FP_INT_DOWNWARD, 8U), FE_INEXACT);
+    EXPECT_FP_EQ_WITH_EXCEPTION(T(63.0), func(T(63.75), FP_INT_DOWNWARD, 8U),
                                 FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(
-        T(-124.0), func(T(-123.96), FP_INT_DOWNWARD, 8U), FE_INEXACT);
+        T(-64.0), func(T(-63.75), FP_INT_DOWNWARD, 8U), FE_INEXACT);
   }
 
   void testFractionsDownwardOutsideRange(FromfpxFunc func) {
@@ -250,13 +250,13 @@ class FromfpxTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_FP_EQ_WITH_EXCEPTION(
         T(-10.0), func(T(-10.65), FP_INT_TOWARDZERO, 5U), FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(
-        T(123.0), func(T(123.38), FP_INT_TOWARDZERO, 8U), FE_INEXACT);
+        T(63.0), func(T(63.25), FP_INT_TOWARDZERO, 8U), FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(
-        T(-123.0), func(T(-123.38), FP_INT_TOWARDZERO, 8U), FE_INEXACT);
+        T(-63.0), func(T(-63.25), FP_INT_TOWARDZERO, 8U), FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(
-        T(123.0), func(T(123.96), FP_INT_TOWARDZERO, 8U), FE_INEXACT);
+        T(63.0), func(T(63.75), FP_INT_TOWARDZERO, 8U), FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(
-        T(-123.0), func(T(-123.96), FP_INT_TOWARDZERO, 8U), FE_INEXACT);
+        T(-63.0), func(T(-63.75), FP_INT_TOWARDZERO, 8U), FE_INEXACT);
   }
 
   void testFractionsTowardZeroOutsideRange(FromfpxFunc func) {
@@ -318,13 +318,13 @@ class FromfpxTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_FP_EQ_WITH_EXCEPTION(
         T(-11.0), func(T(-10.65), FP_INT_TONEARESTFROMZERO, 5U), FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(
-        T(123.0), func(T(123.38), FP_INT_TONEARESTFROMZERO, 8U), FE_INEXACT);
+        T(63.0), func(T(63.25), FP_INT_TONEARESTFROMZERO, 8U), FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(
-        T(-123.0), func(T(-123.38), FP_INT_TONEARESTFROMZERO, 8U), FE_INEXACT);
+        T(-63.0), func(T(-63.25), FP_INT_TONEARESTFROMZERO, 8U), FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(
-        T(124.0), func(T(123.96), FP_INT_TONEARESTFROMZERO, 8U), FE_INEXACT);
+        T(64.0), func(T(63.75), FP_INT_TONEARESTFROMZERO, 8U), FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(
-        T(-124.0), func(T(-123.96), FP_INT_TONEARESTFROMZERO, 8U), FE_INEXACT);
+        T(-64.0), func(T(-63.75), FP_INT_TONEARESTFROMZERO, 8U), FE_INEXACT);
   }
 
   void testFractionsToNearestFromZeroOutsideRange(FromfpxFunc func) {
@@ -393,14 +393,14 @@ class FromfpxTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
                                 FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(T(-11.0), func(T(-10.65), FP_INT_TONEAREST, 5U),
                                 FE_INEXACT);
-    EXPECT_FP_EQ_WITH_EXCEPTION(T(123.0), func(T(123.38), FP_INT_TONEAREST, 8U),
+    EXPECT_FP_EQ_WITH_EXCEPTION(T(63.0), func(T(63.25), FP_INT_TONEAREST, 8U),
                                 FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(
-        T(-123.0), func(T(-123.38), FP_INT_TONEAREST, 8U), FE_INEXACT);
-    EXPECT_FP_EQ_WITH_EXCEPTION(T(124.0), func(T(123.96), FP_INT_TONEAREST, 8U),
+        T(-63.0), func(T(-63.25), FP_INT_TONEAREST, 8U), FE_INEXACT);
+    EXPECT_FP_EQ_WITH_EXCEPTION(T(64.0), func(T(63.75), FP_INT_TONEAREST, 8U),
                                 FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(
-        T(-124.0), func(T(-123.96), FP_INT_TONEAREST, 8U), FE_INEXACT);
+        T(-64.0), func(T(-63.75), FP_INT_TONEAREST, 8U), FE_INEXACT);
 
     EXPECT_FP_EQ_WITH_EXCEPTION(T(2.0), func(T(2.3), FP_INT_TONEAREST, 3U),
                                 FE_INEXACT);
@@ -530,16 +530,16 @@ class FromfpxTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
         T(-11.0), func(T(-10.65), UNKNOWN_MATH_ROUNDING_DIRECTION, 5U),
         FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(
-        T(123.0), func(T(123.38), UNKNOWN_MATH_ROUNDING_DIRECTION, 8U),
+        T(63.0), func(T(63.25), UNKNOWN_MATH_ROUNDING_DIRECTION, 8U),
         FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(
-        T(-123.0), func(T(-123.38), UNKNOWN_MATH_ROUNDING_DIRECTION, 8U),
+        T(-63.0), func(T(-63.25), UNKNOWN_MATH_ROUNDING_DIRECTION, 8U),
         FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(
-        T(124.0), func(T(123.96), UNKNOWN_MATH_ROUNDING_DIRECTION, 8U),
+        T(64.0), func(T(63.75), UNKNOWN_MATH_ROUNDING_DIRECTION, 8U),
         FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(
-        T(-124.0), func(T(-123.96), UNKNOWN_MATH_ROUNDING_DIRECTION, 8U),
+        T(-64.0), func(T(-63.75), UNKNOWN_MATH_ROUNDING_DIRECTION, 8U),
         FE_INEXACT);
 
     EXPECT_FP_EQ_WITH_EXCEPTION(
diff --git a/libc/test/src/math/smoke/UfromfpTest.h b/libc/test/src/math/smoke/UfromfpTest.h
index 84c9f805193ed..237500af84bff 100644
--- a/libc/test/src/math/smoke/UfromfpTest.h
+++ b/libc/test/src/math/smoke/UfromfpTest.h
@@ -76,8 +76,8 @@ class UfromfpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_FP_EQ(T(2.0), func(T(1.75), FP_INT_UPWARD, 2U));
     EXPECT_FP_EQ(T(11.0), func(T(10.32), FP_INT_UPWARD, 4U));
     EXPECT_FP_EQ(T(11.0), func(T(10.65), FP_INT_UPWARD, 4U));
-    EXPECT_FP_EQ(T(124.0), func(T(123.38), FP_INT_UPWARD, 7U));
-    EXPECT_FP_EQ(T(124.0), func(T(123.96), FP_INT_UPWARD, 7U));
+    EXPECT_FP_EQ(T(64.0), func(T(63.25), FP_INT_UPWARD, 7U));
+    EXPECT_FP_EQ(T(64.0), func(T(63.75), FP_INT_UPWARD, 7U));
   }
 
   void testFractionsUpwardOutsideRange(UfromfpFunc func) {
@@ -120,8 +120,8 @@ class UfromfpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_FP_EQ(T(1.0), func(T(1.75), FP_INT_DOWNWARD, 1U));
     EXPECT_FP_EQ(T(10.0), func(T(10.32), FP_INT_DOWNWARD, 4U));
     EXPECT_FP_EQ(T(10.0), func(T(10.65), FP_INT_DOWNWARD, 4U));
-    EXPECT_FP_EQ(T(123.0), func(T(123.38), FP_INT_DOWNWARD, 7U));
-    EXPECT_FP_EQ(T(123.0), func(T(123.96), FP_INT_DOWNWARD, 7U));
+    EXPECT_FP_EQ(T(63.0), func(T(63.25), FP_INT_DOWNWARD, 7U));
+    EXPECT_FP_EQ(T(63.0), func(T(63.75), FP_INT_DOWNWARD, 7U));
   }
 
   void testFractionsDownwardOutsideRange(UfromfpFunc func) {
@@ -167,8 +167,8 @@ class UfromfpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_FP_EQ(T(1.0), func(T(1.75), FP_INT_TOWARDZERO, 1U));
     EXPECT_FP_EQ(T(10.0), func(T(10.32), FP_INT_TOWARDZERO, 4U));
     EXPECT_FP_EQ(T(10.0), func(T(10.65), FP_INT_TOWARDZERO, 4U));
-    EXPECT_FP_EQ(T(123.0), func(T(123.38), FP_INT_TOWARDZERO, 7U));
-    EXPECT_FP_EQ(T(123.0), func(T(123.96), FP_INT_TOWARDZERO, 7U));
+    EXPECT_FP_EQ(T(63.0), func(T(63.25), FP_INT_TOWARDZERO, 7U));
+    EXPECT_FP_EQ(T(63.0), func(T(63.75), FP_INT_TOWARDZERO, 7U));
   }
 
   void testFractionsTowardZeroOutsideRange(UfromfpFunc func) {
@@ -206,8 +206,8 @@ class UfromfpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_FP_EQ(T(2.0), func(T(1.75), FP_INT_TONEARESTFROMZERO, 2U));
     EXPECT_FP_EQ(T(10.0), func(T(10.32), FP_INT_TONEARESTFROMZERO, 4U));
     EXPECT_FP_EQ(T(11.0), func(T(10.65), FP_INT_TONEARESTFROMZERO, 4U));
-    EXPECT_FP_EQ(T(123.0), func(T(123.38), FP_INT_TONEARESTFROMZERO, 7U));
-    EXPECT_FP_EQ(T(124.0), func(T(123.96), FP_INT_TONEARESTFROMZERO, 7U));
+    EXPECT_FP_EQ(T(63.0), func(T(63.25), FP_INT_TONEARESTFROMZERO, 7U));
+    EXPECT_FP_EQ(T(64.0), func(T(63.75), FP_INT_TONEARESTFROMZERO, 7U));
   }
 
   void testFractionsToNearestFromZeroOutsideRange(UfromfpFunc func) {
@@ -254,8 +254,8 @@ class UfromfpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_FP_EQ(T(2.0), func(T(1.75), FP_INT_TONEAREST, 2U));
     EXPECT_FP_EQ(T(10.0), func(T(10.32), FP_INT_TONEAREST, 4U));
     EXPECT_FP_EQ(T(11.0), func(T(10.65), FP_INT_TONEAREST, 4U));
-    EXPECT_FP_EQ(T(123.0), func(T(123.38), FP_INT_TONEAREST, 7U));
-    EXPECT_FP_EQ(T(124.0), func(T(123.96), FP_INT_TONEAREST, 7U));
+    EXPECT_FP_EQ(T(63.0), func(T(63.25), FP_INT_TONEAREST, 7U));
+    EXPECT_FP_EQ(T(64.0), func(T(63.75), FP_INT_TONEAREST, 7U));
 
     EXPECT_FP_EQ(T(2.0), func(T(2.3), FP_INT_TONEAREST, 2U));
     EXPECT_FP_EQ(T(2.0), func(T(2.5), FP_INT_TONEAREST, 2U));
@@ -332,10 +332,10 @@ class UfromfpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_FP_EQ(T(2.0), func(T(1.75), UNKNOWN_MATH_ROUNDING_DIRECTION, 2U));
     EXPECT_FP_EQ(T(10.0), func(T(10.32), UNKNOWN_MATH_ROUNDING_DIRECTION, 4U));
     EXPECT_FP_EQ(T(11.0), func(T(10.65), UNKNOWN_MATH_ROUNDING_DIRECTION, 4U));
-    EXPECT_FP_EQ(T(123.0),
-                 func(T(123.38), UNKNOWN_MATH_ROUNDING_DIRECTION, 7U));
-    EXPECT_FP_EQ(T(124.0),
-                 func(T(123.96), UNKNOWN_MATH_ROUNDING_DIRECTION, 7U));
+    EXPECT_FP_EQ(T(63.0),
+                 func(T(63.25), UNKNOWN_MATH_ROUNDING_DIRECTION, 7U));
+    EXPECT_FP_EQ(T(64.0),
+                 func(T(63.75), UNKNOWN_MATH_ROUNDING_DIRECTION, 7U));
 
     EXPECT_FP_EQ(T(2.0), func(T(2.3), UNKNOWN_MATH_ROUNDING_DIRECTION, 2U));
     EXPECT_FP_EQ(T(2.0), func(T(2.5), UNKNOWN_MATH_ROUNDING_DIRECTION, 2U));
diff --git a/libc/test/src/math/smoke/UfromfpxTest.h b/libc/test/src/math/smoke/UfromfpxTest.h
index 5916492d79187..e39e09b71f825 100644
--- a/libc/test/src/math/smoke/UfromfpxTest.h
+++ b/libc/test/src/math/smoke/UfromfpxTest.h
@@ -87,9 +87,9 @@ class UfromfpxTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
                                 FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(T(11.0), func(T(10.65), FP_INT_UPWARD, 4U),
                                 FE_INEXACT);
-    EXPECT_FP_EQ_WITH_EXCEPTION(T(124.0), func(T(123.38), FP_INT_UPWARD, 7U),
+    EXPECT_FP_EQ_WITH_EXCEPTION(T(64.0), func(T(63.25), FP_INT_UPWARD, 7U),
                                 FE_INEXACT);
-    EXPECT_FP_EQ_WITH_EXCEPTION(T(124.0), func(T(123.96), FP_INT_UPWARD, 7U),
+    EXPECT_FP_EQ_WITH_EXCEPTION(T(64.0), func(T(63.75), FP_INT_UPWARD, 7U),
                                 FE_INEXACT);
   }
 
@@ -141,9 +141,9 @@ class UfromfpxTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
                                 FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(T(10.0), func(T(10.65), FP_INT_DOWNWARD, 4U),
                                 FE_INEXACT);
-    EXPECT_FP_EQ_WITH_EXCEPTION(T(123.0), func(T(123.38), FP_INT_DOWNWARD, 7U),
+    EXPECT_FP_EQ_WITH_EXCEPTION(T(63.0), func(T(63.25), FP_INT_DOWNWARD, 7U),
                                 FE_INEXACT);
-    EXPECT_FP_EQ_WITH_EXCEPTION(T(123.0), func(T(123.96), FP_INT_DOWNWARD, 7U),
+    EXPECT_FP_EQ_WITH_EXCEPTION(T(63.0), func(T(63.75), FP_INT_DOWNWARD, 7U),
                                 FE_INEXACT);
   }
 
@@ -202,9 +202,9 @@ class UfromfpxTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_FP_EQ_WITH_EXCEPTION(T(10.0), func(T(10.65), FP_INT_TOWARDZERO, 4U),
                                 FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(
-        T(123.0), func(T(123.38), FP_INT_TOWARDZERO, 7U), FE_INEXACT);
+        T(63.0), func(T(63.25), FP_INT_TOWARDZERO, 7U), FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(
-        T(123.0), func(T(123.96), FP_INT_TOWARDZERO, 7U), FE_INEXACT);
+        T(63.0), func(T(63.75), FP_INT_TOWARDZERO, 7U), FE_INEXACT);
   }
 
   void testFractionsTowardZeroOutsideRange(UfromfpxFunc func) {
@@ -252,9 +252,9 @@ class UfromfpxTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_FP_EQ_WITH_EXCEPTION(
         T(11.0), func(T(10.65), FP_INT_TONEARESTFROMZERO, 4U), FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(
-        T(123.0), func(T(123.38), FP_INT_TONEARESTFROMZERO, 7U), FE_INEXACT);
+        T(63.0), func(T(63.25), FP_INT_TONEARESTFROMZERO, 7U), FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(
-        T(124.0), func(T(123.96), FP_INT_TONEARESTFROMZERO, 7U), FE_INEXACT);
+        T(64.0), func(T(63.75), FP_INT_TONEARESTFROMZERO, 7U), FE_INEXACT);
   }
 
   void testFractionsToNearestFromZeroOutsideRange(UfromfpxFunc func) {
@@ -311,9 +311,9 @@ class UfromfpxTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
                                 FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(T(11.0), func(T(10.65), FP_INT_TONEAREST, 4U),
                                 FE_INEXACT);
-    EXPECT_FP_EQ_WITH_EXCEPTION(T(123.0), func(T(123.38), FP_INT_TONEAREST, 7U),
+    EXPECT_FP_EQ_WITH_EXCEPTION(T(63.0), func(T(63.25), FP_INT_TONEAREST, 7U),
                                 FE_INEXACT);
-    EXPECT_FP_EQ_WITH_EXCEPTION(T(124.0), func(T(123.96), FP_INT_TONEAREST, 7U),
+    EXPECT_FP_EQ_WITH_EXCEPTION(T(64.0), func(T(63.75), FP_INT_TONEAREST, 7U),
                                 FE_INEXACT);
 
     EXPECT_FP_EQ_WITH_EXCEPTION(T(2.0), func(T(2.3), FP_INT_TONEAREST, 2U),
@@ -414,10 +414,10 @@ class UfromfpxTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
         T(11.0), func(T(10.65), UNKNOWN_MATH_ROUNDING_DIRECTION, 4U),
         FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(
-        T(123.0), func(T(123.38), UNKNOWN_MATH_ROUNDING_DIRECTION, 7U),
+        T(63.0), func(T(63.25), UNKNOWN_MATH_ROUNDING_DIRECTION, 7U),
         FE_INEXACT);
     EXPECT_FP_EQ_WITH_EXCEPTION(
-        T(124.0), func(T(123.96), UNKNOWN_MATH_ROUNDING_DIRECTION, 7U),
+        T(64.0), func(T(63.75), UNKNOWN_MATH_ROUNDING_DIRECTION, 7U),
         FE_INEXACT);
 
     EXPECT_FP_EQ_WITH_EXCEPTION(
diff --git a/libc/test/src/math/smoke/fromfpbf16_test.cpp b/libc/test/src/math/smoke/fromfpbf16_test.cpp
new file mode 100644
index 0000000000000..45d70b2a05c4f
--- /dev/null
+++ b/libc/test/src/math/smoke/fromfpbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for fromfpbf16 ------------------------------------------===//
+//
+// 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 "FromfpTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/fromfpbf16.h"
+
+LIST_FROMFP_TESTS(bfloat16, LIBC_NAMESPACE::fromfpbf16)
diff --git a/libc/test/src/math/smoke/fromfpxbf16_test.cpp b/libc/test/src/math/smoke/fromfpxbf16_test.cpp
new file mode 100644
index 0000000000000..7630b7e887356
--- /dev/null
+++ b/libc/test/src/math/smoke/fromfpxbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for fromfpxbf16 -----------------------------------------===//
+//
+// 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 "FromfpxTest.h"
+
+#include "src/math/fromfpxbf16.h"
+#include "src/__support/FPUtil/bfloat16.h"
+
+LIST_FROMFPX_TESTS(bfloat16, LIBC_NAMESPACE::fromfpxbf16)
diff --git a/libc/test/src/math/smoke/ufromfpbf16_test.cpp b/libc/test/src/math/smoke/ufromfpbf16_test.cpp
new file mode 100644
index 0000000000000..52189bbfa8997
--- /dev/null
+++ b/libc/test/src/math/smoke/ufromfpbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for ufromfpbf16 -----------------------------------------===//
+//
+// 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 "UfromfpTest.h"
+
+#include "src/math/ufromfpbf16.h"
+#include "src/__support/FPUtil/bfloat16.h"
+
+LIST_UFROMFP_TESTS(bfloat16, LIBC_NAMESPACE::ufromfpbf16)
diff --git a/libc/test/src/math/smoke/ufromfpxbf16_test.cpp b/libc/test/src/math/smoke/ufromfpxbf16_test.cpp
new file mode 100644
index 0000000000000..71d25c718f53b
--- /dev/null
+++ b/libc/test/src/math/smoke/ufromfpxbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for ufromfpxbf16 ----------------------------------------===//
+//
+// 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 "UfromfpxTest.h"
+
+#include "src/math/ufromfpxbf16.h"
+#include "src/__support/FPUtil/bfloat16.h"
+
+LIST_UFROMFPX_TESTS(bfloat16, LIBC_NAMESPACE::ufromfpxbf16)

>From baaf0e03e1cbe4fead76f50a45c20bb5e976d3b2 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Sun, 17 Aug 2025 05:06:00 +0530
Subject: [PATCH 03/29] chore: update entrypoints

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/config/baremetal/aarch64/entrypoints.txt | 4 ++++
 libc/config/baremetal/arm/entrypoints.txt     | 4 ++++
 libc/config/baremetal/riscv/entrypoints.txt   | 4 ++++
 libc/config/darwin/aarch64/entrypoints.txt    | 4 ++++
 libc/config/darwin/x86_64/entrypoints.txt     | 5 ++++-
 libc/config/gpu/amdgpu/entrypoints.txt        | 4 ++++
 libc/config/gpu/nvptx/entrypoints.txt         | 4 ++++
 libc/config/linux/aarch64/entrypoints.txt     | 4 ++++
 libc/config/linux/arm/entrypoints.txt         | 4 ++++
 libc/config/linux/riscv/entrypoints.txt       | 4 ++++
 libc/config/linux/x86_64/entrypoints.txt      | 4 ++++
 libc/config/windows/entrypoints.txt           | 4 ++++
 12 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index 00fd4675e7f09..c2710429b3cfd 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -785,9 +785,13 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.fromfpbf16
+  libc.src.math.fromfpxbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.truncbf16
+  libc.src.math.ufromfpbf16
+  libc.src.math.ufromfpxbf16
 )
 
 if(LIBC_TYPES_HAS_FLOAT128)
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 9a7800c0f9b86..6e5389d542f80 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -788,9 +788,13 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.fromfpbf16
+  libc.src.math.fromfpxbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.truncbf16
+  libc.src.math.ufromfpbf16
+  libc.src.math.ufromfpxbf16
 )
 
 if(LIBC_TYPES_HAS_FLOAT128)
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 34b400bdc52c5..76da24d2cc876 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -788,9 +788,13 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.fromfpbf16
+  libc.src.math.fromfpxbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.truncbf16
+  libc.src.math.ufromfpbf16
+  libc.src.math.ufromfpxbf16
 )
 
 if(LIBC_TYPES_HAS_FLOAT128)
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index d45ec3589c97e..5bfd6c863fabb 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -618,9 +618,13 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.fromfpbf16
+  libc.src.math.fromfpxbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.truncbf16
+  libc.src.math.ufromfpbf16
+  libc.src.math.ufromfpxbf16
 )
 
 if(LIBC_TYPES_HAS_FLOAT128)
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index 1e12e9e167b73..27d214d06cd0b 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -261,10 +261,13 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
-
+  libc.src.math.fromfpbf16
+  libc.src.math.fromfpxbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.truncbf16
+  libc.src.math.ufromfpbf16
+  libc.src.math.ufromfpxbf16
 )
 
 set(TARGET_LLVMLIBC_ENTRYPOINTS
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index 4b6f3337036aa..bc75956738e5a 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -644,9 +644,13 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.fromfpbf16
+  libc.src.math.fromfpxbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.truncbf16
+  libc.src.math.ufromfpbf16
+  libc.src.math.ufromfpxbf16
 )
 
 set(TARGET_LLVMLIBC_ENTRYPOINTS
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index d24cc740d4234..c9688284df16c 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -645,9 +645,13 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.fromfpbf16
+  libc.src.math.fromfpxbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.truncbf16
+  libc.src.math.ufromfpbf16
+  libc.src.math.ufromfpxbf16
 )
 
 set(TARGET_LLVMLIBC_ENTRYPOINTS
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index e71dc2ee0d02f..e42bbb14ec119 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -872,9 +872,13 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.fromfpbf16
+  libc.src.math.fromfpxbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.truncbf16
+  libc.src.math.ufromfpbf16
+  libc.src.math.ufromfpxbf16
 )
 
 if(LIBC_TYPES_HAS_FLOAT128)
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index ec01030c77d4f..60fc953c2b9f4 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -488,9 +488,13 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.fromfpbf16
+  libc.src.math.fromfpxbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.truncbf16
+  libc.src.math.ufromfpbf16
+  libc.src.math.ufromfpxbf16
 )
 
 set(TARGET_LLVMLIBC_ENTRYPOINTS
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 54ea983d64839..1a03683d72e61 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -891,9 +891,13 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.fromfpbf16
+  libc.src.math.fromfpxbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.truncbf16
+  libc.src.math.ufromfpbf16
+  libc.src.math.ufromfpxbf16
 )
 
 if(LIBC_TYPES_HAS_FLOAT128)
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 1ee10e6d3cade..9b4946b883626 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -923,9 +923,13 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.fromfpbf16
+  libc.src.math.fromfpxbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.truncbf16
+  libc.src.math.ufromfpbf16
+  libc.src.math.ufromfpxbf16
 )
 
 if(LIBC_TYPES_HAS_FLOAT128)
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 37a2ee286010a..b97e893567038 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -334,9 +334,13 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.fromfpbf16
+  libc.src.math.fromfpxbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.truncbf16
+  libc.src.math.ufromfpbf16
+  libc.src.math.ufromfpxbf16
 )
 
 set(TARGET_LLVMLIBC_ENTRYPOINTS

>From 733f4f17cc9d6b1ad4f71e04cb7ce142196c0e3d Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Sun, 17 Aug 2025 05:07:21 +0530
Subject: [PATCH 04/29] chore: update docs

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/docs/headers/math/index.rst | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index add34d0e877fd..754c1a55cf449 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -181,9 +181,9 @@ Basic Operations
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | frexp            | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.6.7               | F.10.3.7                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| fromfp           | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.9.10              | F.10.6.10                  |
+| fromfp           | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.9.10              | F.10.6.10                  |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| fromfpx          | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.9.11              | F.10.6.11                  |
+| fromfpx          | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.9.11              | F.10.6.11                  |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | fsub             | N/A              | |check|         | |check|                | N/A                  | |check|\*              | N/A                    | 7.12.14.2              | F.10.11                    |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
@@ -247,9 +247,9 @@ Basic Operations
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | trunc            | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.9.9               | F.10.6.9                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| ufromfp          | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.9.10              | F.10.6.10                  |
+| ufromfp          | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.9.10              | F.10.6.10                  |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| ufromfpx         | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.9.11              | F.10.6.11                  |
+| ufromfpx         | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.9.11              | F.10.6.11                  |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 
 Higher Math Functions

>From 4a80312010e3adaff8873a40831e74d393a1d71c Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Sun, 17 Aug 2025 05:59:39 +0530
Subject: [PATCH 05/29] feat: implement next{after,down,toward,up}bf16 math
 functions

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/src/math/CMakeLists.txt             |  4 ++
 libc/src/math/generic/CMakeLists.txt     | 56 ++++++++++++++++++++++++
 libc/src/math/generic/nextafterbf16.cpp  | 21 +++++++++
 libc/src/math/generic/nextdownbf16.cpp   | 21 +++++++++
 libc/src/math/generic/nexttowardbf16.cpp | 22 ++++++++++
 libc/src/math/generic/nextupbf16.cpp     | 21 +++++++++
 libc/src/math/nextafterbf16.h            | 21 +++++++++
 libc/src/math/nextdownbf16.h             | 21 +++++++++
 libc/src/math/nexttowardbf16.h           | 21 +++++++++
 libc/src/math/nextupbf16.h               | 21 +++++++++
 10 files changed, 229 insertions(+)
 create mode 100644 libc/src/math/generic/nextafterbf16.cpp
 create mode 100644 libc/src/math/generic/nextdownbf16.cpp
 create mode 100644 libc/src/math/generic/nexttowardbf16.cpp
 create mode 100644 libc/src/math/generic/nextupbf16.cpp
 create mode 100644 libc/src/math/nextafterbf16.h
 create mode 100644 libc/src/math/nextdownbf16.h
 create mode 100644 libc/src/math/nexttowardbf16.h
 create mode 100644 libc/src/math/nextupbf16.h

diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index f94120272ce66..3843247c4fa5b 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -431,23 +431,27 @@ add_math_entrypoint_object(nextafterf)
 add_math_entrypoint_object(nextafterl)
 add_math_entrypoint_object(nextafterf16)
 add_math_entrypoint_object(nextafterf128)
+add_math_entrypoint_object(nextafterbf16)
 
 add_math_entrypoint_object(nexttoward)
 add_math_entrypoint_object(nexttowardf)
 add_math_entrypoint_object(nexttowardl)
 add_math_entrypoint_object(nexttowardf16)
+add_math_entrypoint_object(nexttowardbf16)
 
 add_math_entrypoint_object(nextdown)
 add_math_entrypoint_object(nextdownf)
 add_math_entrypoint_object(nextdownl)
 add_math_entrypoint_object(nextdownf16)
 add_math_entrypoint_object(nextdownf128)
+add_math_entrypoint_object(nextdownbf16)
 
 add_math_entrypoint_object(nextup)
 add_math_entrypoint_object(nextupf)
 add_math_entrypoint_object(nextupl)
 add_math_entrypoint_object(nextupf16)
 add_math_entrypoint_object(nextupf128)
+add_math_entrypoint_object(nextupbf16)
 
 add_math_entrypoint_object(pow)
 add_math_entrypoint_object(powf)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 1608bed87cb6b..822da74d7597e 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3496,6 +3496,20 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.manipulation_functions
 )
 
+add_entrypoint_object(
+  nextafterbf16
+  SRCS
+    nextafterbf16.cpp
+  HDRS
+    ../nextafterbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
 add_entrypoint_object(
   nexttoward
   SRCS
@@ -3537,6 +3551,20 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.manipulation_functions
 )
 
+add_entrypoint_object(
+  nexttowardbf16
+  SRCS
+    nexttowardbf16.cpp
+  HDRS
+    ../nexttowardbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
 add_entrypoint_object(
   nextdown
   SRCS
@@ -3589,6 +3617,20 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.manipulation_functions
 )
 
+add_entrypoint_object(
+  nextdownbf16
+  SRCS
+    nextdownbf16.cpp
+  HDRS
+    ../nextdownbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
 add_entrypoint_object(
   nextup
   SRCS
@@ -3641,6 +3683,20 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.manipulation_functions
 )
 
+add_entrypoint_object(
+  nextupbf16
+  SRCS
+    nextupbf16.cpp
+  HDRS
+    ../nextupbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
 add_entrypoint_object(
   fmod
   SRCS
diff --git a/libc/src/math/generic/nextafterbf16.cpp b/libc/src/math/generic/nextafterbf16.cpp
new file mode 100644
index 0000000000000..e21a2dcb3d664
--- /dev/null
+++ b/libc/src/math/generic/nextafterbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of nextafterbf16 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/nextafterbf16.h"
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, nextafterbf16, (bfloat16 x, bfloat16 y)) {
+  return fputil::nextafter(x, y);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/nextdownbf16.cpp b/libc/src/math/generic/nextdownbf16.cpp
new file mode 100644
index 0000000000000..2115df95ff072
--- /dev/null
+++ b/libc/src/math/generic/nextdownbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of nextdownbf16 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/nextdownbf16.h"
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, nextdownbf16, (bfloat16 x)) {
+  return fputil::nextupdown</*IsDown=*/true>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/nexttowardbf16.cpp b/libc/src/math/generic/nexttowardbf16.cpp
new file mode 100644
index 0000000000000..3deab87575939
--- /dev/null
+++ b/libc/src/math/generic/nexttowardbf16.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of nexttowardbf16 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/nexttowardbf16.h"
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, nexttowardbf16, (bfloat16 x, long double y)) {
+  // nextafter<T, U> where T != U is nexttoward
+  return fputil::nextafter(x, y);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/nextupbf16.cpp b/libc/src/math/generic/nextupbf16.cpp
new file mode 100644
index 0000000000000..147ce37477d9f
--- /dev/null
+++ b/libc/src/math/generic/nextupbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of nextupbf16 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/nextupbf16.h"
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, nextupbf16, (bfloat16 x)) {
+  return fputil::nextupdown</*IsDown=*/false>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/nextafterbf16.h b/libc/src/math/nextafterbf16.h
new file mode 100644
index 0000000000000..f962c7c51e5cd
--- /dev/null
+++ b/libc/src/math/nextafterbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for nextafterbf16 -----------------*- 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_NEXTAFTERBF16_H
+#define LLVM_LIBC_SRC_MATH_NEXTAFTERBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 nextafterbf16(bfloat16 x, bfloat16 y);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_NEXTAFTERBF16_H
diff --git a/libc/src/math/nextdownbf16.h b/libc/src/math/nextdownbf16.h
new file mode 100644
index 0000000000000..36b0cd7cb9626
--- /dev/null
+++ b/libc/src/math/nextdownbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for nextdownbf16 ------------------*- 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_NEXTDOWNBF16_H
+#define LLVM_LIBC_SRC_MATH_NEXTDOWNBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 nextdownbf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_NEXTDOWNBF16_H
diff --git a/libc/src/math/nexttowardbf16.h b/libc/src/math/nexttowardbf16.h
new file mode 100644
index 0000000000000..930abf858ef51
--- /dev/null
+++ b/libc/src/math/nexttowardbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for nexttowardbf16 ----------------*- 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_NEXTTOWARDBF16_H
+#define LLVM_LIBC_SRC_MATH_NEXTTOWARDBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 nexttowardbf16(bfloat16 x, long double y);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_NEXTTOWARDBF16_H
diff --git a/libc/src/math/nextupbf16.h b/libc/src/math/nextupbf16.h
new file mode 100644
index 0000000000000..872de84338d95
--- /dev/null
+++ b/libc/src/math/nextupbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for nextupbf16 --------------------*- 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_NEXTUPBF16_H
+#define LLVM_LIBC_SRC_MATH_NEXTUPBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 nextupbf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_NEXTUPBF16_H

>From 446e63718e21f90dbfea74dcbc699c1f4c08334c Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Sun, 17 Aug 2025 06:00:01 +0530
Subject: [PATCH 06/29] chore: add smoke tests for
 next{after,down,toward,up}bf16 math functions

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/test/src/math/smoke/CMakeLists.txt       | 52 +++++++++++++++++++
 libc/test/src/math/smoke/NextAfterTest.h      | 43 ++++++++-------
 libc/test/src/math/smoke/NextTowardTest.h     |  4 +-
 .../src/math/smoke/nextafterbf16_test.cpp     | 14 +++++
 .../test/src/math/smoke/nextdownbf16_test.cpp | 14 +++++
 .../src/math/smoke/nexttowardbf16_test.cpp    | 14 +++++
 libc/test/src/math/smoke/nextupbf16_test.cpp  | 14 +++++
 7 files changed, 133 insertions(+), 22 deletions(-)
 create mode 100644 libc/test/src/math/smoke/nextafterbf16_test.cpp
 create mode 100644 libc/test/src/math/smoke/nextdownbf16_test.cpp
 create mode 100644 libc/test/src/math/smoke/nexttowardbf16_test.cpp
 create mode 100644 libc/test/src/math/smoke/nextupbf16_test.cpp

diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 694467e19d47c..40481a711bade 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3660,6 +3660,19 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  nextafterbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    nextafterbf16_test.cpp
+  HDRS
+    NextUpTest.h
+  DEPENDS
+    libc.src.math.nextafterbf16
+    libc.src.__support.FPUtil.bfloat16
+)
+
 # FIXME: These tests are currently spurious for the GPU.
 if(NOT LIBC_TARGET_OS_IS_GPU)
   add_fp_unittest(
@@ -3727,6 +3740,19 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  nexttowardbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    nexttowardbf16_test.cpp
+  HDRS
+    NextUpTest.h
+  DEPENDS
+    libc.src.math.nexttowardbf16
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_fp_unittest(
   nextdown_test
   SUITE
@@ -3787,6 +3813,19 @@ add_fp_unittest(
     libc.src.math.nextdownf128
 )
 
+add_fp_unittest(
+  nextdownbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    nextdownbf16_test.cpp
+  HDRS
+    NextUpTest.h
+  DEPENDS
+    libc.src.math.nextdownbf16
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_fp_unittest(
   nextup_test
   SUITE
@@ -3847,6 +3886,19 @@ add_fp_unittest(
     libc.src.math.nextupf128
 )
 
+add_fp_unittest(
+  nextupbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    nextupbf16_test.cpp
+  HDRS
+    NextUpTest.h
+  DEPENDS
+    libc.src.math.nextupbf16
+    libc.src.__support.FPUtil.bfloat16
+)
+
 # TODO(lntue): The current implementation of fputil::general::fma<float> is only
 # correctly rounded for the default rounding mode round-to-nearest tie-to-even.
 add_fp_unittest(
diff --git a/libc/test/src/math/smoke/NextAfterTest.h b/libc/test/src/math/smoke/NextAfterTest.h
index be27c9f188a72..b7e59f7cd8cfd 100644
--- a/libc/test/src/math/smoke/NextAfterTest.h
+++ b/libc/test/src/math/smoke/NextAfterTest.h
@@ -12,6 +12,7 @@
 #include "src/__support/CPP/bit.h"
 #include "src/__support/FPUtil/FEnvImpl.h"
 #include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/sign.h"
 #include "test/UnitTest/FEnvSafeTest.h"
 #include "test/UnitTest/FPMatcher.h"
 #include "test/UnitTest/Test.h"
@@ -42,6 +43,8 @@ class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
   const T neg_inf = FPBits::inf(Sign::NEG).get_val();
   const T zero = FPBits::zero(Sign::POS).get_val();
   const T neg_zero = FPBits::zero(Sign::NEG).get_val();
+  const T one = FPBits::one(Sign::POS).get_val();
+  const T neg_one = FPBits::one(Sign::NEG).get_val();
   const T nan = FPBits::quiet_nan().get_val();
 
   static constexpr StorageType min_subnormal =
@@ -55,8 +58,8 @@ class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
   typedef T (*NextAfterFunc)(T, T);
 
   void testNaN(NextAfterFunc func) {
-    ASSERT_FP_EQ(func(nan, 0), nan);
-    ASSERT_FP_EQ(func(0, nan), nan);
+    ASSERT_FP_EQ(func(nan, zero), nan);
+    ASSERT_FP_EQ(func(zero, nan), nan);
   }
 
   void testBoundaries(NextAfterFunc func) {
@@ -65,68 +68,68 @@ class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
 
     // 'from' is zero|neg_zero.
     T x = zero;
-    T result = func(x, T(1));
+    T result = func(x, one);
     StorageType expected_bits = 1;
     T expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
     ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
 
-    result = func(x, T(-1));
+    result = func(x, neg_one);
     expected_bits = FPBits::SIGN_MASK + 1;
     expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
     ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
 
     x = neg_zero;
-    result = func(x, 1);
+    result = func(x, one);
     expected_bits = 1;
     expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
     ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
 
-    result = func(x, -1);
+    result = func(x, neg_one);
     expected_bits = FPBits::SIGN_MASK + 1;
     expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
     ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
 
     // 'from' is max subnormal value.
     x = LIBC_NAMESPACE::cpp::bit_cast<T>(max_subnormal);
-    result = func(x, 1);
+    result = func(x, one);
     expected = LIBC_NAMESPACE::cpp::bit_cast<T>(min_normal);
     ASSERT_FP_EQ(result, expected);
 
-    result = func(x, 0);
+    result = func(x, zero);
     expected_bits = max_subnormal - 1;
     expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
     ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
 
     x = -x;
 
-    result = func(x, -1);
+    result = func(x, neg_one);
     expected_bits = FPBits::SIGN_MASK + min_normal;
     expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
     ASSERT_FP_EQ(result, expected);
 
-    result = func(x, 0);
+    result = func(x, zero);
     expected_bits = FPBits::SIGN_MASK + max_subnormal - 1;
     expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
     ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
 
     // 'from' is min subnormal value.
     x = LIBC_NAMESPACE::cpp::bit_cast<T>(min_subnormal);
-    result = func(x, 1);
+    result = func(x, one);
     expected_bits = min_subnormal + 1;
     expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
     ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
-    ASSERT_FP_EQ_WITH_UNDERFLOW(func(x, 0), zero);
+    ASSERT_FP_EQ_WITH_UNDERFLOW(func(x, zero), zero);
 
     x = -x;
-    result = func(x, -1);
+    result = func(x, neg_one);
     expected_bits = FPBits::SIGN_MASK + min_subnormal + 1;
     expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
     ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
-    ASSERT_FP_EQ_WITH_UNDERFLOW(func(x, 0), T(-0.0));
+    ASSERT_FP_EQ_WITH_UNDERFLOW(func(x, zero), neg_zero);
 
     // 'from' is min normal.
     x = LIBC_NAMESPACE::cpp::bit_cast<T>(min_normal);
-    result = func(x, 0);
+    result = func(x, zero);
     expected_bits = max_subnormal;
     expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
     ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
@@ -137,7 +140,7 @@ class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     ASSERT_FP_EQ(result, expected);
 
     x = -x;
-    result = func(x, 0);
+    result = func(x, zero);
     expected_bits = FPBits::SIGN_MASK + max_subnormal;
     expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
     ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
@@ -157,14 +160,14 @@ class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
 
     // 'from' is infinity.
     x = inf;
-    result = func(x, 0);
+    result = func(x, zero);
     expected_bits = max_normal;
     expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
     ASSERT_FP_EQ(result, expected);
     ASSERT_FP_EQ(func(x, inf), inf);
 
     x = neg_inf;
-    result = func(x, 0);
+    result = func(x, zero);
     expected_bits = FPBits::SIGN_MASK + max_normal;
     expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
     ASSERT_FP_EQ(result, expected);
@@ -172,7 +175,7 @@ class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
 
     // 'from' is a power of 2.
     x = T(32.0);
-    result = func(x, 0);
+    result = func(x, zero);
     FPBits x_bits = FPBits(x);
     FPBits result_bits = FPBits(result);
     ASSERT_EQ(result_bits.get_biased_exponent(),
@@ -187,7 +190,7 @@ class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
 
     x = -x;
 
-    result = func(x, 0);
+    result = func(x, zero);
     result_bits = FPBits(result);
     ASSERT_EQ(result_bits.get_biased_exponent(),
               uint16_t(x_bits.get_biased_exponent() - 1));
diff --git a/libc/test/src/math/smoke/NextTowardTest.h b/libc/test/src/math/smoke/NextTowardTest.h
index d2f352cdcbdf7..43e71c6a2d8f6 100644
--- a/libc/test/src/math/smoke/NextTowardTest.h
+++ b/libc/test/src/math/smoke/NextTowardTest.h
@@ -62,8 +62,8 @@ class NextTowardTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
   typedef T (*NextTowardFunc)(T, long double);
 
   void testNaN(NextTowardFunc func) {
-    ASSERT_FP_EQ(func(nan, 0), nan);
-    ASSERT_FP_EQ(func(0, to_nan), nan);
+    ASSERT_FP_EQ(func(nan, to_zero), nan);
+    ASSERT_FP_EQ(func(zero, to_nan), nan);
   }
 
   void testBoundaries(NextTowardFunc func) {
diff --git a/libc/test/src/math/smoke/nextafterbf16_test.cpp b/libc/test/src/math/smoke/nextafterbf16_test.cpp
new file mode 100644
index 0000000000000..b73a3cbef0b74
--- /dev/null
+++ b/libc/test/src/math/smoke/nextafterbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for nextafterbf16 ---------------------------------------===//
+//
+// 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 "NextAfterTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/nextafterbf16.h"
+
+LIST_NEXTAFTER_TESTS(bfloat16, LIBC_NAMESPACE::nextafterbf16)
diff --git a/libc/test/src/math/smoke/nextdownbf16_test.cpp b/libc/test/src/math/smoke/nextdownbf16_test.cpp
new file mode 100644
index 0000000000000..300206461e2de
--- /dev/null
+++ b/libc/test/src/math/smoke/nextdownbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for nextdownbf16 ----------------------------------------===//
+//
+// 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 "NextDownTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/nextdownbf16.h"
+
+LIST_NEXTDOWN_TESTS(bfloat16, LIBC_NAMESPACE::nextdownbf16)
diff --git a/libc/test/src/math/smoke/nexttowardbf16_test.cpp b/libc/test/src/math/smoke/nexttowardbf16_test.cpp
new file mode 100644
index 0000000000000..c1f35fcb96ef6
--- /dev/null
+++ b/libc/test/src/math/smoke/nexttowardbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for nexttowardbf16 --------------------------------------===//
+//
+// 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 "NextTowardTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/nexttowardbf16.h"
+
+LIST_NEXTTOWARD_TESTS(bfloat16, LIBC_NAMESPACE::nexttowardbf16)
diff --git a/libc/test/src/math/smoke/nextupbf16_test.cpp b/libc/test/src/math/smoke/nextupbf16_test.cpp
new file mode 100644
index 0000000000000..49ad2f70b12a8
--- /dev/null
+++ b/libc/test/src/math/smoke/nextupbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for nextupbf16 ------------------------------------------===//
+//
+// 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 "NextUpTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/nextupbf16.h"
+
+LIST_NEXTUP_TESTS(bfloat16, LIBC_NAMESPACE::nextupbf16)

>From a98b9281d87906165b1b7ade5591335dcba0efc5 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Sun, 17 Aug 2025 06:00:15 +0530
Subject: [PATCH 07/29] chore: update entrypoints

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/config/baremetal/aarch64/entrypoints.txt | 4 ++++
 libc/config/baremetal/arm/entrypoints.txt     | 4 ++++
 libc/config/baremetal/riscv/entrypoints.txt   | 4 ++++
 libc/config/darwin/aarch64/entrypoints.txt    | 4 ++++
 libc/config/darwin/x86_64/entrypoints.txt     | 4 ++++
 libc/config/gpu/amdgpu/entrypoints.txt        | 4 ++++
 libc/config/gpu/nvptx/entrypoints.txt         | 4 ++++
 libc/config/linux/aarch64/entrypoints.txt     | 4 ++++
 libc/config/linux/arm/entrypoints.txt         | 4 ++++
 libc/config/linux/riscv/entrypoints.txt       | 4 ++++
 libc/config/linux/x86_64/entrypoints.txt      | 4 ++++
 libc/config/windows/entrypoints.txt           | 4 ++++
 12 files changed, 48 insertions(+)

diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index c2710429b3cfd..26ee82d99192f 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -787,6 +787,10 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.nextafterbf16
+  libc.src.math.nextdownbf16
+  libc.src.math.nexttowardbf16
+  libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.truncbf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 6e5389d542f80..00025d324fc2c 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -790,6 +790,10 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.nextafterbf16
+  libc.src.math.nextdownbf16
+  libc.src.math.nexttowardbf16
+  libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.truncbf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 76da24d2cc876..c0ab0cf903c41 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -790,6 +790,10 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.nextafterbf16
+  libc.src.math.nextdownbf16
+  libc.src.math.nexttowardbf16
+  libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.truncbf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 5bfd6c863fabb..cd81756770ed4 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -620,6 +620,10 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.nextafterbf16
+  libc.src.math.nextdownbf16
+  libc.src.math.nexttowardbf16
+  libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.truncbf16
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index 27d214d06cd0b..3aa54e027a42d 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -263,6 +263,10 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.nextafterbf16
+  libc.src.math.nextdownbf16
+  libc.src.math.nexttowardbf16
+  libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.truncbf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index bc75956738e5a..3b42c0fd71547 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -646,6 +646,10 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.nextafterbf16
+  libc.src.math.nextdownbf16
+  libc.src.math.nexttowardbf16
+  libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.truncbf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index c9688284df16c..b569327409849 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -647,6 +647,10 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.nextafterbf16
+  libc.src.math.nextdownbf16
+  libc.src.math.nexttowardbf16
+  libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.truncbf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index e42bbb14ec119..4058e7155f34a 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -874,6 +874,10 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.nextafterbf16
+  libc.src.math.nextdownbf16
+  libc.src.math.nexttowardbf16
+  libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.truncbf16
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 60fc953c2b9f4..9002bd12d6733 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -490,6 +490,10 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.nextafterbf16
+  libc.src.math.nextdownbf16
+  libc.src.math.nexttowardbf16
+  libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.truncbf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 1a03683d72e61..77d5bae188a3c 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -893,6 +893,10 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.nextafterbf16
+  libc.src.math.nextdownbf16
+  libc.src.math.nexttowardbf16
+  libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.truncbf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 9b4946b883626..b811fe79c174d 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -925,6 +925,10 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.nextafterbf16
+  libc.src.math.nextdownbf16
+  libc.src.math.nexttowardbf16
+  libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.truncbf16
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index b97e893567038..ec2fde253e898 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -336,6 +336,10 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.nextafterbf16
+  libc.src.math.nextdownbf16
+  libc.src.math.nexttowardbf16
+  libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.truncbf16

>From 12f1923f5e1e3681a203f546aeefaaa38ea3fe58 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Sun, 17 Aug 2025 06:00:38 +0530
Subject: [PATCH 08/29] docs: add next{after,down,toward,up}bf16 math functions

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/docs/headers/math/index.rst | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index 754c1a55cf449..591295659c3d4 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -215,13 +215,13 @@ Basic Operations
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | nearbyint        | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.9.3               | F.10.6.3                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| nextafter        | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.11.3              | F.10.8.3                   |
+| nextafter        | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.11.3              | F.10.8.3                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| nextdown         | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.11.6              | F.10.8.6                   |
+| nextdown         | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.11.6              | F.10.8.6                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| nexttoward       | |check|          | |check|         | |check|                | |check|              | N/A                    |                        | 7.12.11.4              | F.10.8.4                   |
+| nexttoward       | |check|          | |check|         | |check|                | |check|              | N/A                    | |check|                | 7.12.11.4              | F.10.8.4                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| nextup           | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.11.5              | F.10.8.5                   |
+| nextup           | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.11.5              | F.10.8.5                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | remainder        | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.10.2              | F.10.7.2                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+

>From 6f8d84d83beff959e838c70057f2e4bb29f0bc4c Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Sun, 17 Aug 2025 08:09:15 +0530
Subject: [PATCH 09/29] feat: implement {get,set}payloadbf16 and
 setpayloadsigbf16 math functions

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/src/math/CMakeLists.txt                |  3 ++
 libc/src/math/generic/CMakeLists.txt        | 42 +++++++++++++++++++++
 libc/src/math/generic/getpayloadbf16.cpp    | 21 +++++++++++
 libc/src/math/generic/setpayloadbf16.cpp    | 21 +++++++++++
 libc/src/math/generic/setpayloadsigbf16.cpp | 21 +++++++++++
 libc/src/math/getpayloadbf16.h              | 21 +++++++++++
 libc/src/math/setpayloadbf16.h              | 21 +++++++++++
 libc/src/math/setpayloadsigbf16.h           | 21 +++++++++++
 8 files changed, 171 insertions(+)
 create mode 100644 libc/src/math/generic/getpayloadbf16.cpp
 create mode 100644 libc/src/math/generic/setpayloadbf16.cpp
 create mode 100644 libc/src/math/generic/setpayloadsigbf16.cpp
 create mode 100644 libc/src/math/getpayloadbf16.h
 create mode 100644 libc/src/math/setpayloadbf16.h
 create mode 100644 libc/src/math/setpayloadsigbf16.h

diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 3843247c4fa5b..023829b21996f 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -330,6 +330,7 @@ add_math_entrypoint_object(getpayloadf)
 add_math_entrypoint_object(getpayloadl)
 add_math_entrypoint_object(getpayloadf16)
 add_math_entrypoint_object(getpayloadf128)
+add_math_entrypoint_object(getpayloadbf16)
 
 add_math_entrypoint_object(hypot)
 add_math_entrypoint_object(hypotf)
@@ -507,12 +508,14 @@ add_math_entrypoint_object(setpayloadf)
 add_math_entrypoint_object(setpayloadl)
 add_math_entrypoint_object(setpayloadf16)
 add_math_entrypoint_object(setpayloadf128)
+add_math_entrypoint_object(setpayloadbf16)
 
 add_math_entrypoint_object(setpayloadsig)
 add_math_entrypoint_object(setpayloadsigf)
 add_math_entrypoint_object(setpayloadsigl)
 add_math_entrypoint_object(setpayloadsigf16)
 add_math_entrypoint_object(setpayloadsigf128)
+add_math_entrypoint_object(setpayloadsigbf16)
 
 add_math_entrypoint_object(sincos)
 add_math_entrypoint_object(sincosf)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 822da74d7597e..38ba34ae521ea 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4609,6 +4609,20 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.basic_operations
 )
 
+add_entrypoint_object(
+  getpayloadbf16
+  SRCS
+    getpayloadbf16.cpp
+  HDRS
+    ../getpayloadbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
 add_entrypoint_object(
   setpayload
   SRCS
@@ -4661,6 +4675,20 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.basic_operations
 )
 
+add_entrypoint_object(
+  setpayloadbf16
+  SRCS
+    setpayloadbf16.cpp
+  HDRS
+    ../setpayloadbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
 add_entrypoint_object(
   setpayloadsig
   SRCS
@@ -4713,6 +4741,20 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.basic_operations
 )
 
+add_entrypoint_object(
+  setpayloadsigbf16
+  SRCS
+    setpayloadsigbf16.cpp
+  HDRS
+    ../setpayloadsigbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
 add_entrypoint_object(
   f16add
   SRCS
diff --git a/libc/src/math/generic/getpayloadbf16.cpp b/libc/src/math/generic/getpayloadbf16.cpp
new file mode 100644
index 0000000000000..544ed0a2f5c9d
--- /dev/null
+++ b/libc/src/math/generic/getpayloadbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of getpayloadbf16 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/getpayloadbf16.h"
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, getpayloadbf16, (const bfloat16 *x)) {
+  return fputil::getpayload(*x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/setpayloadbf16.cpp b/libc/src/math/generic/setpayloadbf16.cpp
new file mode 100644
index 0000000000000..49f9b9cabd6a2
--- /dev/null
+++ b/libc/src/math/generic/setpayloadbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of setpayloadbf16 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/setpayloadbf16.h"
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, setpayloadbf16, (bfloat16 * res, bfloat16 pl)) {
+  return static_cast<int>(fputil::setpayload</*IsSignaling=*/false>(*res, pl));
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/setpayloadsigbf16.cpp b/libc/src/math/generic/setpayloadsigbf16.cpp
new file mode 100644
index 0000000000000..7a2b7c7dc66dd
--- /dev/null
+++ b/libc/src/math/generic/setpayloadsigbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of setpayloadsigbf16 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/setpayloadsigbf16.h"
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, setpayloadsigbf16, (bfloat16 * res, bfloat16 pl)) {
+  return static_cast<int>(fputil::setpayload</*IsSignaling=*/true>(*res, pl));
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/getpayloadbf16.h b/libc/src/math/getpayloadbf16.h
new file mode 100644
index 0000000000000..e4767f0728139
--- /dev/null
+++ b/libc/src/math/getpayloadbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for getpayloadbf16 ----------------*- 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_GETPAYLOADBF16_H
+#define LLVM_LIBC_SRC_MATH_GETPAYLOADBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 getpayloadbf16(const bfloat16 *x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_GETPAYLOADBF16_H
diff --git a/libc/src/math/setpayloadbf16.h b/libc/src/math/setpayloadbf16.h
new file mode 100644
index 0000000000000..e3a60b2f2e021
--- /dev/null
+++ b/libc/src/math/setpayloadbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for setpayloadbf16 ----------------*- 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_SETPAYLOADBF16_H
+#define LLVM_LIBC_SRC_MATH_SETPAYLOADBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int setpayloadbf16(bfloat16 *res, bfloat16 pl);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_SETPAYLOADBF16_H
diff --git a/libc/src/math/setpayloadsigbf16.h b/libc/src/math/setpayloadsigbf16.h
new file mode 100644
index 0000000000000..5baba95b45758
--- /dev/null
+++ b/libc/src/math/setpayloadsigbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for setpayloadsigbf16 -------------*- 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_SETPAYLOADSIGBF16_H
+#define LLVM_LIBC_SRC_MATH_SETPAYLOADSIGBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int setpayloadsigbf16(bfloat16 *res, bfloat16 pl);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_SETPAYLOADSIGBF16_H

>From c8f43800a59a3777c74545484bb337523b9b2a26 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Sun, 17 Aug 2025 08:09:51 +0530
Subject: [PATCH 10/29] chore: implement smoke tests for {get,set}payloadbf16
 and setpayloadsigbf16 math functions

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/test/src/math/smoke/CMakeLists.txt       | 38 ++++++++++++
 libc/test/src/math/smoke/GetPayloadTest.h     | 61 +++++++++++++------
 libc/test/src/math/smoke/SetPayloadSigTest.h  | 34 ++++++++---
 libc/test/src/math/smoke/SetPayloadTest.h     | 34 ++++++++---
 .../src/math/smoke/getpayloadbf16_test.cpp    | 14 +++++
 .../src/math/smoke/setpayloadbf16_test.cpp    | 14 +++++
 .../src/math/smoke/setpayloadsigbf16_test.cpp | 14 +++++
 7 files changed, 174 insertions(+), 35 deletions(-)
 create mode 100644 libc/test/src/math/smoke/getpayloadbf16_test.cpp
 create mode 100644 libc/test/src/math/smoke/setpayloadbf16_test.cpp
 create mode 100644 libc/test/src/math/smoke/setpayloadsigbf16_test.cpp

diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 40481a711bade..8c257664dd8d7 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -4853,6 +4853,19 @@ add_fp_unittest(
     libc.src.math.getpayloadf128
 )
 
+add_fp_unittest(
+  getpayloadbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    getpayloadbf16_test.cpp
+  HDRS
+    GetPayloadTest.h
+  DEPENDS
+    libc.src.math.getpayloadbf16
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_fp_unittest(
   setpayload_test
   SUITE
@@ -4913,6 +4926,19 @@ add_fp_unittest(
     libc.src.math.setpayloadf128
 )
 
+add_fp_unittest(
+  setpayloadbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    setpayloadbf16_test.cpp
+  HDRS
+    SetPayloadTest.h
+  DEPENDS
+    libc.src.math.setpayloadbf16
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_fp_unittest(
   setpayloadsig_test
   SUITE
@@ -4973,6 +4999,18 @@ add_fp_unittest(
     libc.src.math.setpayloadsigf128
 )
 
+add_fp_unittest(
+  setpayloadsigbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    setpayloadsigbf16_test.cpp
+  HDRS
+    SetPayloadTest.h
+  DEPENDS
+    libc.src.math.setpayloadsigbf16
+    libc.src.__support.FPUtil.bfloat16
+)
 
 add_fp_unittest(
   f16add_test
diff --git a/libc/test/src/math/smoke/GetPayloadTest.h b/libc/test/src/math/smoke/GetPayloadTest.h
index 1b1bf4f5d56ff..486ea42bf50c1 100644
--- a/libc/test/src/math/smoke/GetPayloadTest.h
+++ b/libc/test/src/math/smoke/GetPayloadTest.h
@@ -51,23 +51,50 @@ class GetPayloadTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_FP_EQ(default_snan_payload, funcWrapper(func, sNaN));
     EXPECT_FP_EQ(default_snan_payload, funcWrapper(func, neg_sNaN));
 
-    T qnan_42 = FPBits::quiet_nan(Sign::POS, 0x42).get_val();
-    T neg_qnan_42 = FPBits::quiet_nan(Sign::NEG, 0x42).get_val();
-    T snan_42 = FPBits::signaling_nan(Sign::POS, 0x42).get_val();
-    T neg_snan_42 = FPBits::signaling_nan(Sign::NEG, 0x42).get_val();
-    EXPECT_FP_EQ(T(0x42.0p+0), funcWrapper(func, qnan_42));
-    EXPECT_FP_EQ(T(0x42.0p+0), funcWrapper(func, neg_qnan_42));
-    EXPECT_FP_EQ(T(0x42.0p+0), funcWrapper(func, snan_42));
-    EXPECT_FP_EQ(T(0x42.0p+0), funcWrapper(func, neg_snan_42));
-
-    T qnan_123 = FPBits::quiet_nan(Sign::POS, 0x123).get_val();
-    T neg_qnan_123 = FPBits::quiet_nan(Sign::NEG, 0x123).get_val();
-    T snan_123 = FPBits::signaling_nan(Sign::POS, 0x123).get_val();
-    T neg_snan_123 = FPBits::signaling_nan(Sign::NEG, 0x123).get_val();
-    EXPECT_FP_EQ(T(0x123.0p+0), funcWrapper(func, qnan_123));
-    EXPECT_FP_EQ(T(0x123.0p+0), funcWrapper(func, neg_qnan_123));
-    EXPECT_FP_EQ(T(0x123.0p+0), funcWrapper(func, snan_123));
-    EXPECT_FP_EQ(T(0x123.0p+0), funcWrapper(func, neg_snan_123));
+    if constexpr (FPBits::FRACTION_LEN - 1 >= 6) {
+      // [S] [E..E] [QM..M] -> number of M bits should be at least 6
+      // 0x31 = 0b110001 = 6 bits
+      T qnan_31 = FPBits::quiet_nan(Sign::POS, 0x31).get_val();
+      T neg_qnan_31 = FPBits::quiet_nan(Sign::NEG, 0x31).get_val();
+      T snan_31 = FPBits::signaling_nan(Sign::POS, 0x31).get_val();
+      T neg_snan_31 = FPBits::signaling_nan(Sign::NEG, 0x31).get_val();
+      EXPECT_FP_EQ(T(0x31.0p+0), funcWrapper(func, qnan_31));
+      EXPECT_FP_EQ(T(0x31.0p+0), funcWrapper(func, neg_qnan_31));
+      EXPECT_FP_EQ(T(0x31.0p+0), funcWrapper(func, snan_31));
+      EXPECT_FP_EQ(T(0x31.0p+0), funcWrapper(func, neg_snan_31));
+
+      // 0x15 = 0b10101 = 5 bits
+      T qnan_15 = FPBits::quiet_nan(Sign::POS, 0x15).get_val();
+      T neg_qnan_15 = FPBits::quiet_nan(Sign::NEG, 0x15).get_val();
+      T snan_15 = FPBits::signaling_nan(Sign::POS, 0x15).get_val();
+      T neg_snan_15 = FPBits::signaling_nan(Sign::NEG, 0x15).get_val();
+      EXPECT_FP_EQ(T(0x15.0p+0), funcWrapper(func, qnan_15));
+      EXPECT_FP_EQ(T(0x15.0p+0), funcWrapper(func, neg_qnan_15));
+      EXPECT_FP_EQ(T(0x15.0p+0), funcWrapper(func, snan_15));
+      EXPECT_FP_EQ(T(0x15.0p+0), funcWrapper(func, neg_snan_15));
+    }
+
+    if constexpr (FPBits::FRACTION_LEN - 1 >= 7) {
+      T qnan_42 = FPBits::quiet_nan(Sign::POS, 0x42).get_val();
+      T neg_qnan_42 = FPBits::quiet_nan(Sign::NEG, 0x42).get_val();
+      T snan_42 = FPBits::signaling_nan(Sign::POS, 0x42).get_val();
+      T neg_snan_42 = FPBits::signaling_nan(Sign::NEG, 0x42).get_val();
+      EXPECT_FP_EQ(T(0x42.0p+0), funcWrapper(func, qnan_42));
+      EXPECT_FP_EQ(T(0x42.0p+0), funcWrapper(func, neg_qnan_42));
+      EXPECT_FP_EQ(T(0x42.0p+0), funcWrapper(func, snan_42));
+      EXPECT_FP_EQ(T(0x42.0p+0), funcWrapper(func, neg_snan_42));
+    }
+
+    if constexpr (FPBits::FRACTION_LEN - 1 >= 9) {
+      T qnan_123 = FPBits::quiet_nan(Sign::POS, 0x123).get_val();
+      T neg_qnan_123 = FPBits::quiet_nan(Sign::NEG, 0x123).get_val();
+      T snan_123 = FPBits::signaling_nan(Sign::POS, 0x123).get_val();
+      T neg_snan_123 = FPBits::signaling_nan(Sign::NEG, 0x123).get_val();
+      EXPECT_FP_EQ(T(0x123.0p+0), funcWrapper(func, qnan_123));
+      EXPECT_FP_EQ(T(0x123.0p+0), funcWrapper(func, neg_qnan_123));
+      EXPECT_FP_EQ(T(0x123.0p+0), funcWrapper(func, snan_123));
+      EXPECT_FP_EQ(T(0x123.0p+0), funcWrapper(func, neg_snan_123));
+    }
   }
 };
 
diff --git a/libc/test/src/math/smoke/SetPayloadSigTest.h b/libc/test/src/math/smoke/SetPayloadSigTest.h
index f480479618a08..7b26c98b7ee35 100644
--- a/libc/test/src/math/smoke/SetPayloadSigTest.h
+++ b/libc/test/src/math/smoke/SetPayloadSigTest.h
@@ -54,15 +54,31 @@ class SetPayloadSigTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_EQ(FPBits::signaling_nan(Sign::POS, 1).uintval(),
               FPBits(res).uintval());
 
-    EXPECT_EQ(0, func(&res, T(0x42.0p+0)));
-    EXPECT_TRUE(FPBits(res).is_signaling_nan());
-    EXPECT_EQ(FPBits::signaling_nan(Sign::POS, 0x42).uintval(),
-              FPBits(res).uintval());
-
-    EXPECT_EQ(0, func(&res, T(0x123.0p+0)));
-    EXPECT_TRUE(FPBits(res).is_signaling_nan());
-    EXPECT_EQ(FPBits::signaling_nan(Sign::POS, 0x123).uintval(),
-              FPBits(res).uintval());
+    if constexpr (FPBits::FRACTION_LEN - 1 >= 6) {
+      EXPECT_EQ(0, func(&res, T(0x31.0p+0)));
+      EXPECT_TRUE(FPBits(res).is_signaling_nan());
+      EXPECT_EQ(FPBits::signaling_nan(Sign::POS, 0x31).uintval(),
+                FPBits(res).uintval());
+
+      EXPECT_EQ(0, func(&res, T(0x15.0p+0)));
+      EXPECT_TRUE(FPBits(res).is_signaling_nan());
+      EXPECT_EQ(FPBits::signaling_nan(Sign::POS, 0x15).uintval(),
+                FPBits(res).uintval());
+    }
+
+    if constexpr (FPBits::FRACTION_LEN - 1 >= 7) {
+      EXPECT_EQ(0, func(&res, T(0x42.0p+0)));
+      EXPECT_TRUE(FPBits(res).is_signaling_nan());
+      EXPECT_EQ(FPBits::signaling_nan(Sign::POS, 0x42).uintval(),
+                FPBits(res).uintval());
+    }
+
+    if constexpr (FPBits::FRACTION_LEN - 1 >= 9) {
+      EXPECT_EQ(0, func(&res, T(0x123.0p+0)));
+      EXPECT_TRUE(FPBits(res).is_signaling_nan());
+      EXPECT_EQ(FPBits::signaling_nan(Sign::POS, 0x123).uintval(),
+                FPBits(res).uintval());
+    }
 
     FPBits nan_payload_bits = FPBits::one();
     nan_payload_bits.set_biased_exponent(FPBits::FRACTION_LEN - 2 +
diff --git a/libc/test/src/math/smoke/SetPayloadTest.h b/libc/test/src/math/smoke/SetPayloadTest.h
index 9ede5678fef1b..6aff47ab60a99 100644
--- a/libc/test/src/math/smoke/SetPayloadTest.h
+++ b/libc/test/src/math/smoke/SetPayloadTest.h
@@ -54,15 +54,31 @@ class SetPayloadTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_TRUE(FPBits(res).is_quiet_nan());
     EXPECT_EQ(FPBits::quiet_nan(Sign::POS, 1).uintval(), FPBits(res).uintval());
 
-    EXPECT_EQ(0, func(&res, T(0x42.0p+0)));
-    EXPECT_TRUE(FPBits(res).is_quiet_nan());
-    EXPECT_EQ(FPBits::quiet_nan(Sign::POS, 0x42).uintval(),
-              FPBits(res).uintval());
-
-    EXPECT_EQ(0, func(&res, T(0x123.0p+0)));
-    EXPECT_TRUE(FPBits(res).is_quiet_nan());
-    EXPECT_EQ(FPBits::quiet_nan(Sign::POS, 0x123).uintval(),
-              FPBits(res).uintval());
+    if constexpr (FPBits::FRACTION_LEN - 1 >= 6) {
+      EXPECT_EQ(0, func(&res, T(0x31.0p+0)));
+      EXPECT_TRUE(FPBits(res).is_quiet_nan());
+      EXPECT_EQ(FPBits::quiet_nan(Sign::POS, 0x31).uintval(),
+                FPBits(res).uintval());
+
+      EXPECT_EQ(0, func(&res, T(0x15.0p+0)));
+      EXPECT_TRUE(FPBits(res).is_quiet_nan());
+      EXPECT_EQ(FPBits::quiet_nan(Sign::POS, 0x15).uintval(),
+                FPBits(res).uintval());
+    }
+
+    if constexpr (FPBits::FRACTION_LEN - 1 >= 7) {
+      EXPECT_EQ(0, func(&res, T(0x42.0p+0)));
+      EXPECT_TRUE(FPBits(res).is_quiet_nan());
+      EXPECT_EQ(FPBits::quiet_nan(Sign::POS, 0x42).uintval(),
+                FPBits(res).uintval());
+    }
+
+    if constexpr (FPBits::FRACTION_LEN - 1 >= 9) {
+      EXPECT_EQ(0, func(&res, T(0x123.0p+0)));
+      EXPECT_TRUE(FPBits(res).is_quiet_nan());
+      EXPECT_EQ(FPBits::quiet_nan(Sign::POS, 0x123).uintval(),
+                FPBits(res).uintval());
+    }
 
     // The following code is creating a NaN payload manually to prevent a
     // conversion from BigInt to float128.
diff --git a/libc/test/src/math/smoke/getpayloadbf16_test.cpp b/libc/test/src/math/smoke/getpayloadbf16_test.cpp
new file mode 100644
index 0000000000000..51a5fad88cd97
--- /dev/null
+++ b/libc/test/src/math/smoke/getpayloadbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for getpayloadbf16 --------------------------------------===//
+//
+// 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 "GetPayloadTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/getpayloadbf16.h"
+
+LIST_GETPAYLOAD_TESTS(bfloat16, LIBC_NAMESPACE::getpayloadbf16)
diff --git a/libc/test/src/math/smoke/setpayloadbf16_test.cpp b/libc/test/src/math/smoke/setpayloadbf16_test.cpp
new file mode 100644
index 0000000000000..198b454ed78b0
--- /dev/null
+++ b/libc/test/src/math/smoke/setpayloadbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for setpayloadbf16 --------------------------------------===//
+//
+// 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 "SetPayloadTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/setpayloadbf16.h"
+
+LIST_SETPAYLOAD_TESTS(bfloat16, LIBC_NAMESPACE::setpayloadbf16)
diff --git a/libc/test/src/math/smoke/setpayloadsigbf16_test.cpp b/libc/test/src/math/smoke/setpayloadsigbf16_test.cpp
new file mode 100644
index 0000000000000..aa6a3e9dcbf2f
--- /dev/null
+++ b/libc/test/src/math/smoke/setpayloadsigbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for setpayloadsigbf16 -----------------------------------===//
+//
+// 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 "SetPayloadSigTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/setpayloadsigbf16.h"
+
+LIST_SETPAYLOADSIG_TESTS(bfloat16, LIBC_NAMESPACE::setpayloadsigbf16)

>From 8ea93545572ba5a69d468e21d00b2be69d4c857c Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Sun, 17 Aug 2025 08:11:23 +0530
Subject: [PATCH 11/29] fix: add correct static_cast for fputil::getpload for
 bfloat16

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/src/__support/FPUtil/BasicOperations.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/libc/src/__support/FPUtil/BasicOperations.h b/libc/src/__support/FPUtil/BasicOperations.h
index 2357b053b60b8..994237ba8492e 100644
--- a/libc/src/__support/FPUtil/BasicOperations.h
+++ b/libc/src/__support/FPUtil/BasicOperations.h
@@ -354,7 +354,10 @@ LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> getpayload(T x) {
 
     return static_cast<T>(payload_dfloat);
   } else {
-    return static_cast<T>(payload);
+    if constexpr (cpp::is_same_v<T, bfloat16>)
+      return T(static_cast<int>(payload));
+    else
+      return static_cast<T>(payload);
   }
 }
 

>From fc2b6ebb84a27f07541d1922724b06cd9efe09cc Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Sun, 17 Aug 2025 08:12:28 +0530
Subject: [PATCH 12/29] chore: update entrypoints

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/config/baremetal/aarch64/entrypoints.txt | 3 +++
 libc/config/baremetal/arm/entrypoints.txt     | 3 +++
 libc/config/baremetal/riscv/entrypoints.txt   | 3 +++
 libc/config/darwin/aarch64/entrypoints.txt    | 3 +++
 libc/config/darwin/x86_64/entrypoints.txt     | 3 +++
 libc/config/gpu/amdgpu/entrypoints.txt        | 3 +++
 libc/config/gpu/nvptx/entrypoints.txt         | 3 +++
 libc/config/linux/aarch64/entrypoints.txt     | 3 +++
 libc/config/linux/arm/entrypoints.txt         | 3 +++
 libc/config/linux/riscv/entrypoints.txt       | 3 +++
 libc/config/linux/x86_64/entrypoints.txt      | 3 +++
 libc/config/windows/entrypoints.txt           | 3 +++
 12 files changed, 36 insertions(+)

diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index 26ee82d99192f..5f72793beecb4 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -787,12 +787,15 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.getpayloadbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.setpayloadbf16
+  libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 00025d324fc2c..244dd2e87b1fd 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -790,12 +790,15 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.getpayloadbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.setpayloadbf16
+  libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index c0ab0cf903c41..32980c4a1d222 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -790,12 +790,15 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.getpayloadbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.setpayloadbf16
+  libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index cd81756770ed4..3684713474cbc 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -620,12 +620,15 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.getpayloadbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.setpayloadbf16
+  libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index 3aa54e027a42d..7482a80c37d33 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -263,12 +263,15 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.getpayloadbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.setpayloadbf16
+  libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index 3b42c0fd71547..a88c74be2997a 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -646,12 +646,15 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.getpayloadbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.setpayloadbf16
+  libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index b569327409849..08e6c7dc34b07 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -647,12 +647,15 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.getpayloadbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.setpayloadbf16
+  libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 4058e7155f34a..ea7e2065b2216 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -874,12 +874,15 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.getpayloadbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.setpayloadbf16
+  libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 9002bd12d6733..8f21976e9e88e 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -490,12 +490,15 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.getpayloadbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.setpayloadbf16
+  libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 77d5bae188a3c..69d7519118198 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -893,12 +893,15 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.getpayloadbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.setpayloadbf16
+  libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index b811fe79c174d..75dcfd6382f1b 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -925,12 +925,15 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.getpayloadbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.setpayloadbf16
+  libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index ec2fde253e898..541d9fee1adf5 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -336,12 +336,15 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.getpayloadbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.setpayloadbf16
+  libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16

>From 70e4463400df96054520b1dd53a52860d96be929 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Sun, 17 Aug 2025 08:12:59 +0530
Subject: [PATCH 13/29] docs: add {get,set}payloadbf16 and setpayloadsigbf16
 math functions

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/docs/headers/math/index.rst | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index 591295659c3d4..d8921b985b1f2 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -187,7 +187,7 @@ Basic Operations
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | fsub             | N/A              | |check|         | |check|                | N/A                  | |check|\*              | N/A                    | 7.12.14.2              | F.10.11                    |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| getpayload       | |check|          | |check|         | |check|                | |check|              | |check|                |                        | F.10.13.1              | N/A                        |
+| getpayload       | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | F.10.13.1              | N/A                        |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | ilogb            | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.6.8               | F.10.3.8                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
@@ -237,9 +237,9 @@ Basic Operations
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | scalbn           | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.6.19              | F.10.3.19                  |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| setpayload       | |check|          | |check|         | |check|                | |check|              | |check|                |                        | F.10.13.2              | N/A                        |
+| setpayload       | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | F.10.13.2              | N/A                        |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| setpayloadsig    | |check|          | |check|         | |check|                | |check|              | |check|                |                        | F.10.13.3              | N/A                        |
+| setpayloadsig    | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | F.10.13.3              | N/A                        |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | totalorder       | |check|          | |check|         | |check|                | |check|              | |check|                |                        | F.10.12.1              | N/A                        |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+

>From ea9625e60eb47e205c8acb8d9f4cabcd1959c8e6 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Sun, 17 Aug 2025 08:42:17 +0530
Subject: [PATCH 14/29] feat: implement nanbf16 math function

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/src/math/CMakeLists.txt         |  1 +
 libc/src/math/generic/CMakeLists.txt | 16 ++++++++++++++++
 libc/src/math/generic/nanbf16.cpp    | 25 +++++++++++++++++++++++++
 libc/src/math/nanbf16.h              | 21 +++++++++++++++++++++
 4 files changed, 63 insertions(+)
 create mode 100644 libc/src/math/generic/nanbf16.cpp
 create mode 100644 libc/src/math/nanbf16.h

diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 023829b21996f..87a341bb6267b 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -420,6 +420,7 @@ add_math_entrypoint_object(nanf)
 add_math_entrypoint_object(nanl)
 add_math_entrypoint_object(nanf16)
 add_math_entrypoint_object(nanf128)
+add_math_entrypoint_object(nanbf16)
 
 add_math_entrypoint_object(nearbyint)
 add_math_entrypoint_object(nearbyintf)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 38ba34ae521ea..e34e3f04ed8bd 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3444,6 +3444,22 @@ add_entrypoint_object(
     libc.src.errno.errno
 )
 
+add_entrypoint_object(
+  nanbf16
+  SRCS
+    nanbf16.cpp
+  HDRS
+    ../nanbf16.h
+  DEPENDS
+    libc.src.errno.errno
+    libc.src.__support.common
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.libc_errno
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+    libc.src.__support.str_to_float
+)
+
 add_entrypoint_object(
   nextafter
   SRCS
diff --git a/libc/src/math/generic/nanbf16.cpp b/libc/src/math/generic/nanbf16.cpp
new file mode 100644
index 0000000000000..678dd6aed9ee3
--- /dev/null
+++ b/libc/src/math/generic/nanbf16.cpp
@@ -0,0 +1,25 @@
+//===-- Implementation of nanbf16 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/nanbf16.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/str_to_float.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, nanbf16, (const char *arg)) {
+  auto result = internal::strtonan<bfloat16>(arg);
+  if (result.has_error())
+    libc_errno = result.error;
+  return result.value;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/nanbf16.h b/libc/src/math/nanbf16.h
new file mode 100644
index 0000000000000..1551044677768
--- /dev/null
+++ b/libc/src/math/nanbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for nanbf16 -----------------------*- 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_NANBF16_H
+#define LLVM_LIBC_SRC_MATH_NANBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 nanbf16(const char *arg);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_NANBF16_H

>From ae0e6165f51e45721fead6ba7af0adb29345b263 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Sun, 17 Aug 2025 08:42:41 +0530
Subject: [PATCH 15/29] chore: add smoke tests for nanbf16 math function

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/test/src/math/smoke/CMakeLists.txt   | 16 +++++++
 libc/test/src/math/smoke/nanbf16_test.cpp | 55 +++++++++++++++++++++++
 2 files changed, 71 insertions(+)
 create mode 100644 libc/test/src/math/smoke/nanbf16_test.cpp

diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 8c257664dd8d7..c26bd84e2ca9d 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3515,6 +3515,22 @@ add_fp_unittest(
   UNIT_TEST_ONLY
 )
 
+add_fp_unittest(
+  nanbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    nanbf16_test.cpp
+  DEPENDS
+    libc.hdr.signal_macros
+    libc.src.math.nanbf16
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.fp_bits
+  # FIXME: The nan tests currently have death tests, which aren't supported for
+  # hermetic tests.
+  UNIT_TEST_ONLY
+)
+
 add_fp_unittest(
   nearbyint_test
   SUITE
diff --git a/libc/test/src/math/smoke/nanbf16_test.cpp b/libc/test/src/math/smoke/nanbf16_test.cpp
new file mode 100644
index 0000000000000..4154f6ed637c8
--- /dev/null
+++ b/libc/test/src/math/smoke/nanbf16_test.cpp
@@ -0,0 +1,55 @@
+//===-- Unittests for nanbf16 ---------------------------------------------===//
+//
+// 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/signal_macros.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/nanbf16.h"
+#include "test/UnitTest/FEnvSafeTest.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+class LlvmLibcNanf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+public:
+  using StorageType = LIBC_NAMESPACE::fputil::FPBits<bfloat16>::StorageType;
+
+  void run_test(const char *input_str, StorageType bits) {
+    bfloat16 result = LIBC_NAMESPACE::nanbf16(input_str);
+    auto actual_fp = LIBC_NAMESPACE::fputil::FPBits<bfloat16>(result);
+    auto expected_fp = LIBC_NAMESPACE::fputil::FPBits<bfloat16>(bits);
+    EXPECT_EQ(actual_fp.uintval(), expected_fp.uintval());
+  }
+};
+
+TEST_F(LlvmLibcNanf16Test, NCharSeq) {
+  run_test("", 0x7fc0);
+
+  // 0x7fc0 + 0x1f (31) = 0x7cdf
+  run_test("31", 0x7fdf);
+
+  // 0x7fc0 + 0x15 = 0x7fd5
+  run_test("0x15", 0x7fd5);
+
+  run_test("1a", 0x7fc0);
+  run_test("1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_",
+           0x7fc0);
+  run_test("10000000000000000000000000000", 0x7fc0);
+}
+
+TEST_F(LlvmLibcNanf16Test, RandomString) {
+  run_test(" 1234", 0x7fc0);
+  run_test("-1234", 0x7fc0);
+  run_test("asd&f", 0x7fc0);
+  run_test("123 ", 0x7fc0);
+}
+
+#if defined(LIBC_ADD_NULL_CHECKS)
+TEST_F(LlvmLibcNanf16Test, InvalidInput) {
+  EXPECT_DEATH([] { LIBC_NAMESPACE::nanbf16(nullptr); }, WITH_SIGNAL(-1));
+}
+#endif // LIBC_ADD_NULL_CHECKS

>From 27364c5fe27d1e06a5ef07c11319f09025fc5c23 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Sun, 17 Aug 2025 08:42:52 +0530
Subject: [PATCH 16/29] chore: update entrypoints

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 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 5f72793beecb4..c4ec03fc146e8 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -788,6 +788,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
+  libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 244dd2e87b1fd..d9e173a71e47a 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -791,6 +791,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
+  libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 32980c4a1d222..9b0cf14037fae 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -791,6 +791,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
+  libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 3684713474cbc..2a634852a2ed0 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -621,6 +621,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
+  libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index 7482a80c37d33..6a18d557f86fb 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -264,6 +264,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
+  libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index a88c74be2997a..008f7c7bd642c 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -647,6 +647,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
+  libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index 08e6c7dc34b07..ae434117eb8e2 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -648,6 +648,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
+  libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index ea7e2065b2216..88438247643f6 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -875,6 +875,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
+  libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 8f21976e9e88e..865392a918624 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -491,6 +491,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
+  libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 69d7519118198..207608c431a9b 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -894,6 +894,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
+  libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 75dcfd6382f1b..44f5f2d03db0a 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -926,6 +926,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
+  libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 541d9fee1adf5..32a6a578aae13 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -337,6 +337,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
+  libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16

>From 250cfeaa32868029be40d44f235bacbdf1bb472e Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Sun, 17 Aug 2025 08:43:03 +0530
Subject: [PATCH 17/29] docs: add nanbf16

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/docs/headers/math/index.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index d8921b985b1f2..9efbce06920b5 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -211,7 +211,7 @@ Basic Operations
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | modf             | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.6.18              | F.10.3.18                  |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| nan              | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.11.2              | F.10.8.2                   |
+| nan              | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.11.2              | F.10.8.2                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | nearbyint        | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.9.3               | F.10.6.3                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+

>From 832bbc6f4743015ef1af5e9d4a9aea4b94e73374 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Tue, 19 Aug 2025 22:13:40 +0530
Subject: [PATCH 18/29] feat: implement {frexp,ilogb,ldexp,llogb,logb}bf16 math
 functions

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/src/math/CMakeLists.txt         |  5 ++
 libc/src/math/frexpbf16.h            | 21 +++++++++
 libc/src/math/generic/CMakeLists.txt | 70 ++++++++++++++++++++++++++++
 libc/src/math/generic/frexpbf16.cpp  | 21 +++++++++
 libc/src/math/generic/ilogbbf16.cpp  | 21 +++++++++
 libc/src/math/generic/ldexpbf16.cpp  | 21 +++++++++
 libc/src/math/generic/llogbbf16.cpp  | 21 +++++++++
 libc/src/math/generic/logbbf16.cpp   | 19 ++++++++
 libc/src/math/ilogbbf16.h            | 21 +++++++++
 libc/src/math/ldexpbf16.h            | 21 +++++++++
 libc/src/math/llogbbf16.h            | 21 +++++++++
 libc/src/math/logbbf16.h             | 21 +++++++++
 12 files changed, 283 insertions(+)
 create mode 100644 libc/src/math/frexpbf16.h
 create mode 100644 libc/src/math/generic/frexpbf16.cpp
 create mode 100644 libc/src/math/generic/ilogbbf16.cpp
 create mode 100644 libc/src/math/generic/ldexpbf16.cpp
 create mode 100644 libc/src/math/generic/llogbbf16.cpp
 create mode 100644 libc/src/math/generic/logbbf16.cpp
 create mode 100644 libc/src/math/ilogbbf16.h
 create mode 100644 libc/src/math/ldexpbf16.h
 create mode 100644 libc/src/math/llogbbf16.h
 create mode 100644 libc/src/math/logbbf16.h

diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 87a341bb6267b..b1d76c6008cf5 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -306,6 +306,7 @@ add_math_entrypoint_object(frexpf)
 add_math_entrypoint_object(frexpl)
 add_math_entrypoint_object(frexpf16)
 add_math_entrypoint_object(frexpf128)
+add_math_entrypoint_object(frexpbf16)
 
 add_math_entrypoint_object(fromfp)
 add_math_entrypoint_object(fromfpf)
@@ -341,6 +342,7 @@ add_math_entrypoint_object(ilogbf)
 add_math_entrypoint_object(ilogbl)
 add_math_entrypoint_object(ilogbf16)
 add_math_entrypoint_object(ilogbf128)
+add_math_entrypoint_object(ilogbbf16)
 
 add_math_entrypoint_object(isnan)
 add_math_entrypoint_object(isnanf)
@@ -357,12 +359,14 @@ add_math_entrypoint_object(llogbf)
 add_math_entrypoint_object(llogbl)
 add_math_entrypoint_object(llogbf16)
 add_math_entrypoint_object(llogbf128)
+add_math_entrypoint_object(llogbbf16)
 
 add_math_entrypoint_object(ldexp)
 add_math_entrypoint_object(ldexpf)
 add_math_entrypoint_object(ldexpl)
 add_math_entrypoint_object(ldexpf16)
 add_math_entrypoint_object(ldexpf128)
+add_math_entrypoint_object(ldexpbf16)
 
 add_math_entrypoint_object(log10)
 add_math_entrypoint_object(log10f)
@@ -384,6 +388,7 @@ add_math_entrypoint_object(logbf)
 add_math_entrypoint_object(logbl)
 add_math_entrypoint_object(logbf16)
 add_math_entrypoint_object(logbf128)
+add_math_entrypoint_object(logbbf16)
 
 add_math_entrypoint_object(llrint)
 add_math_entrypoint_object(llrintf)
diff --git a/libc/src/math/frexpbf16.h b/libc/src/math/frexpbf16.h
new file mode 100644
index 0000000000000..1e9bba16ea6c4
--- /dev/null
+++ b/libc/src/math/frexpbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for frexpbf16 ---------------------*- 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_FREXPFB16_H
+#define LLVM_LIBC_SRC_MATH_FREXPFB16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 frexpbf16(bfloat16 x, int *exp);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_FREXPFB16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index e34e3f04ed8bd..3dbc54a819722 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1732,6 +1732,20 @@ add_entrypoint_object(
     libc.src.__support.math.frexpf128
 )
 
+add_entrypoint_object(
+  frexpbf16
+  SRCS
+    frexpbf16.cpp
+  HDRS
+    ../frexpbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
 add_entrypoint_object(
   ilogb
   SRCS
@@ -1784,6 +1798,20 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.manipulation_functions
 )
 
+add_entrypoint_object(
+  ilogbbf16
+  SRCS
+    ilogbbf16.cpp
+  HDRS
+    ../ilogbbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
 add_entrypoint_object(
   llogb
   SRCS
@@ -1836,6 +1864,20 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.manipulation_functions
 )
 
+add_entrypoint_object(
+  llogbbf16
+  SRCS
+    llogbbf16.cpp
+  HDRS
+    ../llogbbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
 add_entrypoint_object(
   ldexp
   SRCS
@@ -1886,6 +1928,20 @@ add_entrypoint_object(
     libc.src.__support.math.ldexpf128
 )
 
+add_entrypoint_object(
+  ldexpbf16
+  SRCS
+    ldexpbf16.cpp
+  HDRS
+    ../ldexpbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
 add_object_library(
   common_constants
   HDRS
@@ -2158,6 +2214,20 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.manipulation_functions
 )
 
+add_entrypoint_object(
+  logbbf16
+  SRCS
+    logbbf16.cpp
+  HDRS
+    ../logbbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
 add_entrypoint_object(
   modf
   SRCS
diff --git a/libc/src/math/generic/frexpbf16.cpp b/libc/src/math/generic/frexpbf16.cpp
new file mode 100644
index 0000000000000..004f64f282f7d
--- /dev/null
+++ b/libc/src/math/generic/frexpbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of frexpbf16 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/frexpbf16.h"
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, frexpbf16, (bfloat16 x, int *exp)) {
+  return fputil::frexp(x, *exp);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/ilogbbf16.cpp b/libc/src/math/generic/ilogbbf16.cpp
new file mode 100644
index 0000000000000..6811139c6dcfd
--- /dev/null
+++ b/libc/src/math/generic/ilogbbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of ilogbbf16 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/ilogbbf16.h"
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, ilogbbf16, (bfloat16 x)) {
+  return fputil::intlogb<int>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/ldexpbf16.cpp b/libc/src/math/generic/ldexpbf16.cpp
new file mode 100644
index 0000000000000..42a5039e81bc0
--- /dev/null
+++ b/libc/src/math/generic/ldexpbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of ldexpbf16 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/ldexpbf16.h"
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, ldexpbf16, (bfloat16 x, int exp)) {
+  return fputil::ldexp(x, exp);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/llogbbf16.cpp b/libc/src/math/generic/llogbbf16.cpp
new file mode 100644
index 0000000000000..74c27620053da
--- /dev/null
+++ b/libc/src/math/generic/llogbbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of llogbbf16 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/llogbbf16.h"
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(long, llogbbf16, (bfloat16 x)) {
+  return fputil::intlogb<long>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/logbbf16.cpp b/libc/src/math/generic/logbbf16.cpp
new file mode 100644
index 0000000000000..5a43ddfafde75
--- /dev/null
+++ b/libc/src/math/generic/logbbf16.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of logbbf16 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/logbbf16.h"
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, logbbf16, (bfloat16 x)) { return fputil::logb(x); }
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/ilogbbf16.h b/libc/src/math/ilogbbf16.h
new file mode 100644
index 0000000000000..da2384b6d62f6
--- /dev/null
+++ b/libc/src/math/ilogbbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for ilogbbf16 ---------------------*- 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_ILOGBBF16_H
+#define LLVM_LIBC_SRC_MATH_ILOGBBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int ilogbbf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_ILOGBBF16_H
diff --git a/libc/src/math/ldexpbf16.h b/libc/src/math/ldexpbf16.h
new file mode 100644
index 0000000000000..7436d8dc5c149
--- /dev/null
+++ b/libc/src/math/ldexpbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for ldexpbf16 ---------------------*- 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_LDEXPBF16_H
+#define LLVM_LIBC_SRC_MATH_LDEXPBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 ldexpbf16(bfloat16 x, int exp);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_LDEXPBF16_H
diff --git a/libc/src/math/llogbbf16.h b/libc/src/math/llogbbf16.h
new file mode 100644
index 0000000000000..13f0570585d82
--- /dev/null
+++ b/libc/src/math/llogbbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for llogbbf16 ---------------------*- 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_LLOGBBF16_H
+#define LLVM_LIBC_SRC_MATH_LLOGBBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+long llogbbf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_LLOGBBF16_H
diff --git a/libc/src/math/logbbf16.h b/libc/src/math/logbbf16.h
new file mode 100644
index 0000000000000..2c0d77e9df8af
--- /dev/null
+++ b/libc/src/math/logbbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for logbbf16 ----------------------*- 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_LOGBBF16_H
+#define LLVM_LIBC_SRC_MATH_LOGBBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 logbbf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_LOGBBF16_H

>From a08faab58f32221951ae0af6091a2ac3ddc672d2 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Tue, 19 Aug 2025 22:14:10 +0530
Subject: [PATCH 19/29] chore: add smoke tests for
 {frexp,ilogb,ldexp,llogb,logb}bf16 math functions

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/test/src/math/smoke/CMakeLists.txt     | 76 +++++++++++++++++++++
 libc/test/src/math/smoke/frexpbf16_test.cpp | 14 ++++
 libc/test/src/math/smoke/ilogbbf16_test.cpp | 14 ++++
 libc/test/src/math/smoke/ldexpbf16_test.cpp | 14 ++++
 libc/test/src/math/smoke/llogbbf16_test.cpp | 14 ++++
 libc/test/src/math/smoke/logbbf16_test.cpp  | 14 ++++
 6 files changed, 146 insertions(+)
 create mode 100644 libc/test/src/math/smoke/frexpbf16_test.cpp
 create mode 100644 libc/test/src/math/smoke/ilogbbf16_test.cpp
 create mode 100644 libc/test/src/math/smoke/ldexpbf16_test.cpp
 create mode 100644 libc/test/src/math/smoke/llogbbf16_test.cpp
 create mode 100644 libc/test/src/math/smoke/logbbf16_test.cpp

diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index c26bd84e2ca9d..b71d47568309b 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -1536,6 +1536,19 @@ add_fp_unittest(
     libc.src.math.frexpf128
 )
 
+add_fp_unittest(
+  frexpbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    frexpbf16_test.cpp
+  HDRS
+    FrexpTest.h
+  DEPENDS
+    libc.src.math.frexpbf16
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_fp_unittest(
   fromfp_test
   SUITE
@@ -1904,6 +1917,22 @@ add_fp_unittest(
     libc.src.__support.FPUtil.manipulation_functions
 )
 
+add_fp_unittest(
+  ilogbbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    ilogbbf16_test.cpp
+  HDRS
+    ILogbTest.h
+  DEPENDS
+    libc.src.math.ilogbbf16
+    libc.src.__support.CPP.algorithm
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.manipulation_functions
+)
+
 add_fp_unittest(
   issignaling_test
   SUITE
@@ -2039,6 +2068,22 @@ add_fp_unittest(
     libc.src.__support.FPUtil.manipulation_functions
 )
 
+add_fp_unittest(
+  llogbbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    llogbbf16_test.cpp
+  HDRS
+    ILogbTest.h
+  DEPENDS
+    libc.src.math.llogbbf16
+    libc.src.__support.CPP.algorithm
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.manipulation_functions
+)
+
 add_fp_unittest(
   ldexp_test
   SUITE
@@ -2114,6 +2159,22 @@ add_fp_unittest(
     libc.src.__support.FPUtil.normal_float
 )
 
+add_fp_unittest(
+  ldexpbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    ldexpbf16_test.cpp
+  HDRS
+    LdExpTest.h
+  DEPENDS
+    libc.src.math.ldexpbf16
+    libc.src.__support.CPP.limits
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.normal_float
+)
+
 add_fp_unittest(
   logb_test
   SUITE
@@ -2184,6 +2245,21 @@ add_fp_unittest(
     libc.src.__support.FPUtil.manipulation_functions
 )
 
+add_fp_unittest(
+  logbbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    logbbf16_test.cpp
+  HDRS
+    LogbTest.h
+  DEPENDS
+    libc.src.math.logbbf16
+    libc.src.__support.CPP.algorithm
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.manipulation_functions
+)
+
 add_fp_unittest(
   modf_test
   SUITE
diff --git a/libc/test/src/math/smoke/frexpbf16_test.cpp b/libc/test/src/math/smoke/frexpbf16_test.cpp
new file mode 100644
index 0000000000000..3c80edbb433ba
--- /dev/null
+++ b/libc/test/src/math/smoke/frexpbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for frexpbf16 -------------------------------------------===//
+//
+// 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 "FrexpTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/frexpbf16.h"
+
+LIST_FREXP_TESTS(bfloat16, LIBC_NAMESPACE::frexpbf16);
diff --git a/libc/test/src/math/smoke/ilogbbf16_test.cpp b/libc/test/src/math/smoke/ilogbbf16_test.cpp
new file mode 100644
index 0000000000000..d9fa03a2e50cb
--- /dev/null
+++ b/libc/test/src/math/smoke/ilogbbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for ilogbbf16 -------------------------------------------===//
+//
+// 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 "ILogbTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/ilogbbf16.h"
+
+LIST_INTLOGB_TESTS(int, bfloat16, LIBC_NAMESPACE::ilogbbf16);
diff --git a/libc/test/src/math/smoke/ldexpbf16_test.cpp b/libc/test/src/math/smoke/ldexpbf16_test.cpp
new file mode 100644
index 0000000000000..2e57eb8460c9e
--- /dev/null
+++ b/libc/test/src/math/smoke/ldexpbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for ldexpbf16 -------------------------------------------===//
+//
+// 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 "LdExpTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/ldexpbf16.h"
+
+LIST_LDEXP_TESTS(bfloat16, LIBC_NAMESPACE::ldexpbf16);
diff --git a/libc/test/src/math/smoke/llogbbf16_test.cpp b/libc/test/src/math/smoke/llogbbf16_test.cpp
new file mode 100644
index 0000000000000..4454fbfb0644b
--- /dev/null
+++ b/libc/test/src/math/smoke/llogbbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for llogbbf16 -------------------------------------------===//
+//
+// 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 "ILogbTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/llogbbf16.h"
+
+LIST_INTLOGB_TESTS(long, bfloat16, LIBC_NAMESPACE::llogbbf16);
diff --git a/libc/test/src/math/smoke/logbbf16_test.cpp b/libc/test/src/math/smoke/logbbf16_test.cpp
new file mode 100644
index 0000000000000..4ceec3d6984dd
--- /dev/null
+++ b/libc/test/src/math/smoke/logbbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for logbbf16 --------------------------------------------===//
+//
+// 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 "LogbTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/logbbf16.h"
+
+LIST_LOGB_TESTS(bfloat16, LIBC_NAMESPACE::logbbf16)

>From 5fb2a5c94516a3964308f8b2320d98c2b8e9796d Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Tue, 19 Aug 2025 22:16:15 +0530
Subject: [PATCH 20/29] chore: update entrypoints

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/config/baremetal/aarch64/entrypoints.txt | 5 +++++
 libc/config/baremetal/arm/entrypoints.txt     | 5 +++++
 libc/config/baremetal/riscv/entrypoints.txt   | 5 +++++
 libc/config/darwin/aarch64/entrypoints.txt    | 5 +++++
 libc/config/darwin/x86_64/entrypoints.txt     | 5 +++++
 libc/config/gpu/amdgpu/entrypoints.txt        | 5 +++++
 libc/config/gpu/nvptx/entrypoints.txt         | 5 +++++
 libc/config/linux/aarch64/entrypoints.txt     | 5 +++++
 libc/config/linux/arm/entrypoints.txt         | 5 +++++
 libc/config/linux/riscv/entrypoints.txt       | 5 +++++
 libc/config/linux/x86_64/entrypoints.txt      | 5 +++++
 libc/config/windows/entrypoints.txt           | 5 +++++
 12 files changed, 60 insertions(+)

diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index c4ec03fc146e8..10d5b3a591d0d 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -785,9 +785,14 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.frexpbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
+  libc.src.math.ilogbbf16
+  libc.src.math.ldexpbf16
+  libc.src.math.llogbbf16
+  libc.src.math.logbbf16
   libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index d9e173a71e47a..785b84163701f 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -788,9 +788,14 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.frexpbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
+  libc.src.math.ilogbbf16
+  libc.src.math.ldexpbf16
+  libc.src.math.llogbbf16
+  libc.src.math.logbbf16
   libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 9b0cf14037fae..2038f3e4952cf 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -788,9 +788,14 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.frexpbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
+  libc.src.math.ilogbbf16
+  libc.src.math.ldexpbf16
+  libc.src.math.llogbbf16
+  libc.src.math.logbbf16
   libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 2a634852a2ed0..707e8b1d43658 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -618,9 +618,14 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.frexpbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
+  libc.src.math.ilogbbf16
+  libc.src.math.ldexpbf16
+  libc.src.math.llogbbf16
+  libc.src.math.logbbf16
   libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index 6a18d557f86fb..db3b305d376b0 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -261,9 +261,14 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.frexpbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
+  libc.src.math.ilogbbf16
+  libc.src.math.ldexpbf16
+  libc.src.math.llogbbf16
+  libc.src.math.logbbf16
   libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index 008f7c7bd642c..76bda91f3b229 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -644,9 +644,14 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.frexpbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
+  libc.src.math.ilogbbf16
+  libc.src.math.ldexpbf16
+  libc.src.math.llogbbf16
+  libc.src.math.logbbf16
   libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index ae434117eb8e2..cae9d593b056d 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -645,9 +645,14 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.frexpbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
+  libc.src.math.ilogbbf16
+  libc.src.math.ldexpbf16
+  libc.src.math.llogbbf16
+  libc.src.math.logbbf16
   libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 88438247643f6..18853de2c13c1 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -872,9 +872,14 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.frexpbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
+  libc.src.math.ilogbbf16
+  libc.src.math.ldexpbf16
+  libc.src.math.llogbbf16
+  libc.src.math.logbbf16
   libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 865392a918624..150e34a1fb195 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -488,9 +488,14 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.frexpbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
+  libc.src.math.ilogbbf16
+  libc.src.math.ldexpbf16
+  libc.src.math.llogbbf16
+  libc.src.math.logbbf16
   libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 207608c431a9b..937ec4253299a 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -891,9 +891,14 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.frexpbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
+  libc.src.math.ilogbbf16
+  libc.src.math.ldexpbf16
+  libc.src.math.llogbbf16
+  libc.src.math.logbbf16
   libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 44f5f2d03db0a..23871c406d236 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -923,9 +923,14 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.frexpbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
+  libc.src.math.ilogbbf16
+  libc.src.math.ldexpbf16
+  libc.src.math.llogbbf16
+  libc.src.math.logbbf16
   libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 32a6a578aae13..cc7c655280213 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -334,9 +334,14 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.frexpbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
+  libc.src.math.ilogbbf16
+  libc.src.math.ldexpbf16
+  libc.src.math.llogbbf16
+  libc.src.math.logbbf16
   libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16

>From 5f0ab92aca48dce84062940924d6327195e17f64 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Wed, 20 Aug 2025 03:08:21 +0530
Subject: [PATCH 21/29] fix: infinite recursion and tests

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/src/__support/FPUtil/bfloat16.h | 2 ++
 libc/test/src/math/smoke/LdExpTest.h | 7 ++++---
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/libc/src/__support/FPUtil/bfloat16.h b/libc/src/__support/FPUtil/bfloat16.h
index 3fab2b80317de..89d06a06f2dcf 100644
--- a/libc/src/__support/FPUtil/bfloat16.h
+++ b/libc/src/__support/FPUtil/bfloat16.h
@@ -48,6 +48,8 @@ struct BFloat16 {
           xd(sign, 0, value);
       bits = xd.template as<bfloat16, /*ShouldSignalExceptions=*/true>().bits;
 
+    } else if constexpr (cpp::is_convertible_v<T, BFloat16>) {
+      bits = value.operator BFloat16().bits;
     } else {
       bits = fputil::cast<bfloat16>(static_cast<float>(value)).bits;
     }
diff --git a/libc/test/src/math/smoke/LdExpTest.h b/libc/test/src/math/smoke/LdExpTest.h
index 8de70ad161ad6..ac3affdcd680b 100644
--- a/libc/test/src/math/smoke/LdExpTest.h
+++ b/libc/test/src/math/smoke/LdExpTest.h
@@ -135,8 +135,8 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     // Normal which trigger mantissa overflow.
     T x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS + 1,
                       StorageType(2) * NormalFloat::ONE - StorageType(1));
-    ASSERT_FP_EQ(func(x, -1), x / 2);
-    ASSERT_FP_EQ(func(-x, -1), -x / 2);
+    ASSERT_FP_EQ(func(x, -1), T(x / 2));
+    ASSERT_FP_EQ(func(-x, -1), -T(x / 2));
 
     // Start with a normal number high exponent but pass a very low number for
     // exp. The result should be a subnormal number.
@@ -154,7 +154,8 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
 
     // Start with a subnormal number but pass a very high number for exponent.
     // The result should not be infinity.
-    x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS + 1, NormalFloat::ONE >> 10);
+    x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS + 1,
+                    NormalFloat::ONE >> FPBits::FRACTION_LEN);
     exp = FPBits::MAX_BIASED_EXPONENT + 5;
     ASSERT_FALSE(FPBits(func(x, exp)).is_inf());
     // But if the exp is large enough to oversome than the normalization shift,

>From 49dc65d6fea894b444191a844f5ed819a7d7dd73 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Wed, 20 Aug 2025 03:10:07 +0530
Subject: [PATCH 22/29] docs: add {frexp,ilogb,ldexp,llogb,logb}bf16 math
 functions

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/docs/headers/math/index.rst | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index 9efbce06920b5..5049ab65d00af 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -179,7 +179,7 @@ Basic Operations
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | fmul             | N/A              | |check|         | |check|                | N/A                  | |check|\*              | N/A                    | 7.12.14.3              | F.10.11                    |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| frexp            | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.6.7               | F.10.3.7                   |
+| frexp            | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.6.7               | F.10.3.7                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | fromfp           | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.9.10              | F.10.6.10                  |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
@@ -189,21 +189,21 @@ Basic Operations
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | getpayload       | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | F.10.13.1              | N/A                        |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| ilogb            | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.6.8               | F.10.3.8                   |
+| ilogb            | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.6.8               | F.10.3.8                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | iscanonical      | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.3.2               | N/A                        |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | issignaling      | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.3.8               | N/A                        |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| ldexp            | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.6.9               | F.10.3.9                   |
+| ldexp            | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.6.9               | F.10.3.9                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| llogb            | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.6.10              | F.10.3.10                  |
+| llogb            | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.6.10              | F.10.3.10                  |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | llrint           | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.9.5               | F.10.6.5                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | llround          | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.9.7               | F.10.6.7                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| logb             | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.6.17              | F.10.3.17                  |
+| logb             | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.6.17              | F.10.3.17                  |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | lrint            | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.9.5               | F.10.6.5                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+

>From fc5e2152b4c57238ed424138ca27de1635146ef5 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Wed, 20 Aug 2025 03:43:58 +0530
Subject: [PATCH 23/29] feat: implement {modf,remainder,remquo}bf16 math
 functions

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/src/math/CMakeLists.txt            |  6 ++-
 libc/src/math/generic/CMakeLists.txt    | 58 +++++++++++++++++++++++++
 libc/src/math/generic/modfbf16.cpp      | 21 +++++++++
 libc/src/math/generic/remainderbf16.cpp | 22 ++++++++++
 libc/src/math/generic/remquobf16.cpp    | 21 +++++++++
 libc/src/math/modfbf16.h                | 21 +++++++++
 libc/src/math/remainderbf16.h           | 21 +++++++++
 libc/src/math/remquobf16.h              | 21 +++++++++
 8 files changed, 190 insertions(+), 1 deletion(-)
 create mode 100644 libc/src/math/generic/modfbf16.cpp
 create mode 100644 libc/src/math/generic/remainderbf16.cpp
 create mode 100644 libc/src/math/generic/remquobf16.cpp
 create mode 100644 libc/src/math/modfbf16.h
 create mode 100644 libc/src/math/remainderbf16.h
 create mode 100644 libc/src/math/remquobf16.h

diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index b1d76c6008cf5..6cc221178df94 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -300,6 +300,7 @@ add_math_entrypoint_object(fmodf)
 add_math_entrypoint_object(fmodl)
 add_math_entrypoint_object(fmodf16)
 add_math_entrypoint_object(fmodf128)
+add_math_entrypoint_object(fmodbf16)
 
 add_math_entrypoint_object(frexp)
 add_math_entrypoint_object(frexpf)
@@ -419,6 +420,7 @@ add_math_entrypoint_object(modff)
 add_math_entrypoint_object(modfl)
 add_math_entrypoint_object(modff16)
 add_math_entrypoint_object(modff128)
+add_math_entrypoint_object(modfbf16)
 
 add_math_entrypoint_object(nan)
 add_math_entrypoint_object(nanf)
@@ -470,12 +472,14 @@ add_math_entrypoint_object(remainderf)
 add_math_entrypoint_object(remainderl)
 add_math_entrypoint_object(remainderf16)
 add_math_entrypoint_object(remainderf128)
+add_math_entrypoint_object(remainderbf16)
 
 add_math_entrypoint_object(remquo)
 add_math_entrypoint_object(remquof)
-add_math_entrypoint_object(remquof128)
 add_math_entrypoint_object(remquol)
 add_math_entrypoint_object(remquof16)
+add_math_entrypoint_object(remquof128)
+add_math_entrypoint_object(remquobf16)
 
 add_math_entrypoint_object(rint)
 add_math_entrypoint_object(rintf)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 3dbc54a819722..46de2e09d3ba9 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2280,6 +2280,21 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.manipulation_functions
 )
 
+add_entrypoint_object(
+  modfbf16
+  SRCS
+    modfbf16.cpp
+  HDRS
+    ../modfbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
+
 add_entrypoint_object(
   fmin
   SRCS
@@ -3184,6 +3199,21 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.division_and_remainder_operations
 )
 
+add_entrypoint_object(
+  remquobf16
+  SRCS
+    remquobf16.cpp
+  HDRS
+    ../remquobf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.division_and_remainder_operations
+    libc.src.__support.macros.properties.types
+    libc.src.__support.macros.config
+)
+
+
 add_entrypoint_object(
   remainderf
   SRCS
@@ -3236,6 +3266,20 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.division_and_remainder_operations
 )
 
+add_entrypoint_object(
+  remainderbf16
+  SRCS
+    remainderbf16.cpp
+  HDRS
+    ../remainderbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.division_and_remainder_operations
+    libc.src.__support.macros.properties.types
+    libc.src.__support.macros.config
+)
+
 add_entrypoint_object(
   hypotf
   SRCS
@@ -3835,6 +3879,20 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.generic.fmod
 )
 
+add_entrypoint_object(
+  fmodbf16
+  SRCS
+    fmodbf16.cpp
+  HDRS
+    ../fmodbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.generic.fmod
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
 add_entrypoint_object(
   fromfp
   SRCS
diff --git a/libc/src/math/generic/modfbf16.cpp b/libc/src/math/generic/modfbf16.cpp
new file mode 100644
index 0000000000000..09458f6a2db40
--- /dev/null
+++ b/libc/src/math/generic/modfbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of modfbf16 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/modfbf16.h"
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, modfbf16, (bfloat16 x, bfloat16 *iptr)) {
+  return fputil::modf(x, *iptr);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/remainderbf16.cpp b/libc/src/math/generic/remainderbf16.cpp
new file mode 100644
index 0000000000000..e70726a6cbc55
--- /dev/null
+++ b/libc/src/math/generic/remainderbf16.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of remainderbf16 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/remainderbf16.h"
+#include "src/__support/FPUtil/DivisionAndRemainderOperations.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, remainderbf16, (bfloat16 x, bfloat16 y)) {
+  int quotient;
+  return fputil::remquo(x, y, quotient);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/remquobf16.cpp b/libc/src/math/generic/remquobf16.cpp
new file mode 100644
index 0000000000000..e1b13f8deb19a
--- /dev/null
+++ b/libc/src/math/generic/remquobf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of remquobf16 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/remquobf16.h"
+#include "src/__support/FPUtil/DivisionAndRemainderOperations.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, remquobf16, (bfloat16 x, bfloat16 y, int *exp)) {
+  return fputil::remquo(x, y, *exp);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/modfbf16.h b/libc/src/math/modfbf16.h
new file mode 100644
index 0000000000000..df05b6c5b0aad
--- /dev/null
+++ b/libc/src/math/modfbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for modfbf16 ----------------------*- 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_MODFBF16_H
+#define LLVM_LIBC_SRC_MATH_MODFBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 modfbf16(bfloat16 x, bfloat16 *iptr);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_MODFBF16_H
diff --git a/libc/src/math/remainderbf16.h b/libc/src/math/remainderbf16.h
new file mode 100644
index 0000000000000..a1a2eaa3a0efb
--- /dev/null
+++ b/libc/src/math/remainderbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for remainderbf16 -----------------*- 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_REMAINDERBF16_H
+#define LLVM_LIBC_SRC_MATH_REMAINDERBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 remainderbf16(bfloat16 x, bfloat16 y);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_REMAINDERBF16_H
diff --git a/libc/src/math/remquobf16.h b/libc/src/math/remquobf16.h
new file mode 100644
index 0000000000000..909db17dec58a
--- /dev/null
+++ b/libc/src/math/remquobf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for remquobf16 --------------------*- 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_REMQUOBF16_H
+#define LLVM_LIBC_SRC_MATH_REMQUOBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 remquobf16(bfloat16 x, bfloat16 y, int *exp);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_REMQUOBF16_H

>From d0e8c830dbbe7712412dfcec15b369f4757770c8 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Wed, 20 Aug 2025 03:44:13 +0530
Subject: [PATCH 24/29] chore: add smoke tests for {modf,remainder,remquo}bf16
 math functions

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/test/src/math/smoke/CMakeLists.txt      | 30 ++++++++++++++++++++
 libc/test/src/math/smoke/ModfTest.h          | 10 +++++--
 libc/test/src/math/smoke/modfbf16_test.cpp   | 14 +++++++++
 libc/test/src/math/smoke/remquobf16_test.cpp | 14 +++++++++
 4 files changed, 66 insertions(+), 2 deletions(-)
 create mode 100644 libc/test/src/math/smoke/modfbf16_test.cpp
 create mode 100644 libc/test/src/math/smoke/remquobf16_test.cpp

diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index b71d47568309b..ba8776f8d449a 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -2335,6 +2335,22 @@ add_fp_unittest(
     libc.src.__support.FPUtil.nearest_integer_operations
 )
 
+add_fp_unittest(
+  modfbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    modfbf16_test.cpp
+  HDRS
+    ModfTest.h
+  DEPENDS
+    libc.src.math.modfbf16
+    libc.src.__support.CPP.algorithm
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.nearest_integer_operations
+)
+
 add_fp_unittest(
   fdimf_test
   SUITE
@@ -3475,6 +3491,20 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  remquobf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    remquobf16_test.cpp
+  HDRS
+    RemQuoTest.h
+  DEPENDS
+    libc.src.math.remquobf16
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.fp_bits
+)
+
 add_fp_unittest(
   hypotf_test
   SUITE
diff --git a/libc/test/src/math/smoke/ModfTest.h b/libc/test/src/math/smoke/ModfTest.h
index 24cfb1152c2e5..71f1c6bf4be28 100644
--- a/libc/test/src/math/smoke/ModfTest.h
+++ b/libc/test/src/math/smoke/ModfTest.h
@@ -76,10 +76,16 @@ class ModfTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_FP_EQ(T(-0.75), func(T(-10.75), &integral));
     EXPECT_FP_EQ(integral, T(-10.0));
 
-    EXPECT_FP_EQ(T(0.125), func(T(100.125), &integral));
+    EXPECT_FP_EQ(T(0.125), func(T(31.125), &integral));
+    EXPECT_FP_EQ(integral, T(31.0));
+
+    EXPECT_FP_EQ(T(-0.125), func(T(-31.125), &integral));
+    EXPECT_FP_EQ(integral, T(-31.0));
+
+    EXPECT_FP_EQ(T(0.5), func(T(100.5), &integral));
     EXPECT_FP_EQ(integral, T(100.0));
 
-    EXPECT_FP_EQ(T(-0.125), func(T(-100.125), &integral));
+    EXPECT_FP_EQ(T(-0.5), func(T(-100.5), &integral));
     EXPECT_FP_EQ(integral, T(-100.0));
   }
 
diff --git a/libc/test/src/math/smoke/modfbf16_test.cpp b/libc/test/src/math/smoke/modfbf16_test.cpp
new file mode 100644
index 0000000000000..88ebed55222ca
--- /dev/null
+++ b/libc/test/src/math/smoke/modfbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for modfbf16 --------------------------------------------===//
+//
+// 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 "ModfTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/modfbf16.h"
+
+LIST_MODF_TESTS(bfloat16, LIBC_NAMESPACE::modfbf16)
diff --git a/libc/test/src/math/smoke/remquobf16_test.cpp b/libc/test/src/math/smoke/remquobf16_test.cpp
new file mode 100644
index 0000000000000..3f754bbd28a41
--- /dev/null
+++ b/libc/test/src/math/smoke/remquobf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for remquobf16 ------------------------------------------===//
+//
+// 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 "RemQuoTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/remquobf16.h"
+
+LIST_REMQUO_TESTS(bfloat16, LIBC_NAMESPACE::remquobf16)

>From 7711efd183b742886f46562260035c2cef776212 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Wed, 20 Aug 2025 03:46:08 +0530
Subject: [PATCH 25/29] chore: update entrypoints

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/config/baremetal/aarch64/entrypoints.txt | 3 +++
 libc/config/baremetal/arm/entrypoints.txt     | 3 +++
 libc/config/baremetal/riscv/entrypoints.txt   | 3 +++
 libc/config/darwin/aarch64/entrypoints.txt    | 3 +++
 libc/config/darwin/x86_64/entrypoints.txt     | 3 +++
 libc/config/gpu/amdgpu/entrypoints.txt        | 3 +++
 libc/config/gpu/nvptx/entrypoints.txt         | 3 +++
 libc/config/linux/aarch64/entrypoints.txt     | 3 +++
 libc/config/linux/arm/entrypoints.txt         | 3 +++
 libc/config/linux/riscv/entrypoints.txt       | 3 +++
 libc/config/linux/x86_64/entrypoints.txt      | 3 +++
 libc/config/windows/entrypoints.txt           | 3 +++
 12 files changed, 36 insertions(+)

diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index 10d5b3a591d0d..f6f2740e52fe3 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -793,11 +793,14 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ldexpbf16
   libc.src.math.llogbbf16
   libc.src.math.logbbf16
+  libc.src.math.modfbf16
   libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
+  libc.src.math.remainderbf16
+  libc.src.math.remquobf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.setpayloadbf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 785b84163701f..6f9ce31e4bb45 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -796,11 +796,14 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ldexpbf16
   libc.src.math.llogbbf16
   libc.src.math.logbbf16
+  libc.src.math.modfbf16
   libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
+  libc.src.math.remainderbf16
+  libc.src.math.remquobf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.setpayloadbf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 2038f3e4952cf..ddca368e7175c 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -796,11 +796,14 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ldexpbf16
   libc.src.math.llogbbf16
   libc.src.math.logbbf16
+  libc.src.math.modfbf16
   libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
+  libc.src.math.remainderbf16
+  libc.src.math.remquobf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.setpayloadbf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 707e8b1d43658..22b291ac6c2b7 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -626,11 +626,14 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ldexpbf16
   libc.src.math.llogbbf16
   libc.src.math.logbbf16
+  libc.src.math.modfbf16
   libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
+  libc.src.math.remainderbf16
+  libc.src.math.remquobf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.setpayloadbf16
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index db3b305d376b0..e69c24446403d 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -269,11 +269,14 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ldexpbf16
   libc.src.math.llogbbf16
   libc.src.math.logbbf16
+  libc.src.math.modfbf16
   libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
+  libc.src.math.remainderbf16
+  libc.src.math.remquobf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.setpayloadbf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index 76bda91f3b229..5630538547485 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -652,11 +652,14 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ldexpbf16
   libc.src.math.llogbbf16
   libc.src.math.logbbf16
+  libc.src.math.modfbf16
   libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
+  libc.src.math.remainderbf16
+  libc.src.math.remquobf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.setpayloadbf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index cae9d593b056d..c10cd8f454238 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -653,11 +653,14 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ldexpbf16
   libc.src.math.llogbbf16
   libc.src.math.logbbf16
+  libc.src.math.modfbf16
   libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
+  libc.src.math.remainderbf16
+  libc.src.math.remquobf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.setpayloadbf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 18853de2c13c1..ff4b3bdcbcdad 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -880,11 +880,14 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ldexpbf16
   libc.src.math.llogbbf16
   libc.src.math.logbbf16
+  libc.src.math.modfbf16
   libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
+  libc.src.math.remainderbf16
+  libc.src.math.remquobf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.setpayloadbf16
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 150e34a1fb195..8d81edf536b78 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -496,11 +496,14 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ldexpbf16
   libc.src.math.llogbbf16
   libc.src.math.logbbf16
+  libc.src.math.modfbf16
   libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
+  libc.src.math.remainderbf16
+  libc.src.math.remquobf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.setpayloadbf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 937ec4253299a..dbe7356510e2d 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -899,11 +899,14 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ldexpbf16
   libc.src.math.llogbbf16
   libc.src.math.logbbf16
+  libc.src.math.modfbf16
   libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
+  libc.src.math.remainderbf16
+  libc.src.math.remquobf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.setpayloadbf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 23871c406d236..a24af935fa6b2 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -931,11 +931,14 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ldexpbf16
   libc.src.math.llogbbf16
   libc.src.math.logbbf16
+  libc.src.math.modfbf16
   libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
+  libc.src.math.remainderbf16
+  libc.src.math.remquobf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.setpayloadbf16
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index cc7c655280213..06868e1792dd4 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -342,11 +342,14 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ldexpbf16
   libc.src.math.llogbbf16
   libc.src.math.logbbf16
+  libc.src.math.modfbf16
   libc.src.math.nanbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
+  libc.src.math.remainderbf16
+  libc.src.math.remquobf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
   libc.src.math.setpayloadbf16

>From e52a7fa92f22e770c7398a0e31965ea44b08ea29 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Wed, 20 Aug 2025 03:47:06 +0530
Subject: [PATCH 26/29] docs: add {modf,remainder,remquo}bf16 math functions

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/docs/headers/math/index.rst | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index 5049ab65d00af..d4326665ea851 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -209,7 +209,7 @@ Basic Operations
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | lround           | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.9.7               | F.10.6.7                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| modf             | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.6.18              | F.10.3.18                  |
+| modf             | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.6.18              | F.10.3.18                  |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | nan              | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.11.2              | F.10.8.2                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
@@ -223,9 +223,9 @@ Basic Operations
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | nextup           | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.11.5              | F.10.8.5                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| remainder        | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.10.2              | F.10.7.2                   |
+| remainder        | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.10.2              | F.10.7.2                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| remquo           | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.10.3              | F.10.7.3                   |
+| remquo           | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.10.3              | F.10.7.3                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | rint             | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.9.4               | F.10.6.4                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+

>From 25674ef3b1b4874ba1fc7bd040ec9a7461aef891 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Wed, 20 Aug 2025 20:06:54 +0530
Subject: [PATCH 27/29] chore: remove fmodbf16 from CMakeLists.txt

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/src/math/generic/CMakeLists.txt | 14 --------------
 1 file changed, 14 deletions(-)

diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 46de2e09d3ba9..dc9d101cc76c8 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3879,20 +3879,6 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.generic.fmod
 )
 
-add_entrypoint_object(
-  fmodbf16
-  SRCS
-    fmodbf16.cpp
-  HDRS
-    ../fmodbf16.h
-  DEPENDS
-    libc.src.__support.common
-    libc.src.__support.FPUtil.bfloat16
-    libc.src.__support.FPUtil.generic.fmod
-    libc.src.__support.macros.config
-    libc.src.__support.macros.properties.types
-)
-
 add_entrypoint_object(
   fromfp
   SRCS

>From 2bf7920d79c31551408e689f8c065eb92ff27726 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Thu, 21 Aug 2025 05:22:19 +0530
Subject: [PATCH 28/29] fix: add cpp::min in LdExpTest.h

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
 libc/test/src/math/smoke/CMakeLists.txt | 6 ++++++
 libc/test/src/math/smoke/LdExpTest.h    | 6 ++++--
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index b71d47568309b..a7135def118c4 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -2094,6 +2094,7 @@ add_fp_unittest(
     LdExpTest.h
   DEPENDS
     libc.src.math.ldexp
+    libc.src.__support.CPP.algorithm
     libc.src.__support.CPP.limits
     libc.src.__support.FPUtil.fp_bits
     libc.src.__support.FPUtil.normal_float
@@ -2109,6 +2110,7 @@ add_fp_unittest(
     LdExpTest.h
   DEPENDS
     libc.src.math.ldexpf
+    libc.src.__support.CPP.algorithm
     libc.src.__support.CPP.limits
     libc.src.__support.FPUtil.fp_bits
     libc.src.__support.FPUtil.normal_float
@@ -2124,6 +2126,7 @@ add_fp_unittest(
     LdExpTest.h
   DEPENDS
     libc.src.math.ldexpl
+    libc.src.__support.CPP.algorithm
     libc.src.__support.CPP.limits
     libc.src.__support.FPUtil.fp_bits
     libc.src.__support.FPUtil.normal_float
@@ -2139,6 +2142,7 @@ add_fp_unittest(
     LdExpTest.h
   DEPENDS
     libc.src.math.ldexpf16
+    libc.src.__support.CPP.algorithm
     libc.src.__support.CPP.limits
     libc.src.__support.FPUtil.fp_bits
     libc.src.__support.FPUtil.normal_float
@@ -2154,6 +2158,7 @@ add_fp_unittest(
     LdExpTest.h
   DEPENDS
     libc.src.math.ldexpf128
+    libc.src.__support.CPP.algorithm
     libc.src.__support.CPP.limits
     libc.src.__support.FPUtil.fp_bits
     libc.src.__support.FPUtil.normal_float
@@ -2169,6 +2174,7 @@ add_fp_unittest(
     LdExpTest.h
   DEPENDS
     libc.src.math.ldexpbf16
+    libc.src.__support.CPP.algorithm
     libc.src.__support.CPP.limits
     libc.src.__support.FPUtil.bfloat16
     libc.src.__support.FPUtil.fp_bits
diff --git a/libc/test/src/math/smoke/LdExpTest.h b/libc/test/src/math/smoke/LdExpTest.h
index ac3affdcd680b..d005f053c0c91 100644
--- a/libc/test/src/math/smoke/LdExpTest.h
+++ b/libc/test/src/math/smoke/LdExpTest.h
@@ -10,7 +10,8 @@
 #define LLVM_LIBC_TEST_SRC_MATH_LDEXPTEST_H
 
 #include "hdr/stdint_proxy.h"
-#include "src/__support/CPP/limits.h" // INT_MAX
+#include "src/__support/CPP/algorithm.h" // cpp::min
+#include "src/__support/CPP/limits.h"    // INT_MAX
 #include "src/__support/FPUtil/FPBits.h"
 #include "src/__support/FPUtil/NormalFloat.h"
 #include "test/UnitTest/FEnvSafeTest.h"
@@ -155,7 +156,8 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     // Start with a subnormal number but pass a very high number for exponent.
     // The result should not be infinity.
     x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS + 1,
-                    NormalFloat::ONE >> FPBits::FRACTION_LEN);
+                    NormalFloat::ONE >>
+                        LIBC_NAMESPACE::cpp::min(FPBits::FRACTION_LEN, 10));
     exp = FPBits::MAX_BIASED_EXPONENT + 5;
     ASSERT_FALSE(FPBits(func(x, exp)).is_inf());
     // But if the exp is large enough to oversome than the normalization shift,

>From 5a76e0227d58ac89594f5058982a10e0db48bfd8 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Mon, 25 Aug 2025 02:39:58 +0530
Subject: [PATCH 29/29] fix: nits

Co-authored-by: OverMighty <its.overmighty at gmail.com>
---
 libc/src/math/CMakeLists.txt         | 1 -
 libc/src/math/generic/CMakeLists.txt | 2 --
 2 files changed, 3 deletions(-)

diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 6cc221178df94..61fd525a06d25 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -300,7 +300,6 @@ add_math_entrypoint_object(fmodf)
 add_math_entrypoint_object(fmodl)
 add_math_entrypoint_object(fmodf16)
 add_math_entrypoint_object(fmodf128)
-add_math_entrypoint_object(fmodbf16)
 
 add_math_entrypoint_object(frexp)
 add_math_entrypoint_object(frexpf)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index dc9d101cc76c8..f79f91fcdb7d4 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2294,7 +2294,6 @@ add_entrypoint_object(
     libc.src.__support.macros.properties.types
 )
 
-
 add_entrypoint_object(
   fmin
   SRCS
@@ -3213,7 +3212,6 @@ add_entrypoint_object(
     libc.src.__support.macros.config
 )
 
-
 add_entrypoint_object(
   remainderf
   SRCS



More information about the libc-commits mailing list