[libc-commits] [libc] [llvm] [libc][math][c23] Add sinbf16 function (PR #185441)

via libc-commits libc-commits at lists.llvm.org
Sat Oct 17 01:56:28 PDT 2026


https://github.com/Sukumarsawant updated https://github.com/llvm/llvm-project/pull/185441

>From e28b83001a578dbaefba31f6c80a0b6f719f4870 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 17 Oct 2026 11:41:53 +0530
Subject: [PATCH 1/6] rebase

---
 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 +
 libc/docs/headers/math/index.rst              |  2 +-
 libc/shared/math.h                            |  1 +
 libc/shared/math/sinbf16.h                    | 23 +++++
 libc/src/__support/math/CMakeLists.txt        | 16 ++++
 libc/src/__support/math/sinbf16.h             | 92 +++++++++++++++++++
 libc/src/math/CMakeLists.txt                  |  1 +
 libc/src/math/generic/CMakeLists.txt          | 10 ++
 libc/src/math/generic/sinbf16.cpp             | 17 ++++
 libc/src/math/sinbf16.h                       | 21 +++++
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  1 +
 libc/test/src/math/CMakeLists.txt             | 13 +++
 libc/test/src/math/sinbf16_test.cpp           | 45 +++++++++
 libc/test/src/math/smoke/CMakeLists.txt       | 13 +++
 libc/test/src/math/smoke/sinbf16_test.cpp     | 40 ++++++++
 .../llvm-project-overlay/libc/BUILD.bazel     | 23 +++++
 28 files changed, 330 insertions(+), 1 deletion(-)
 create mode 100644 libc/shared/math/sinbf16.h
 create mode 100644 libc/src/__support/math/sinbf16.h
 create mode 100644 libc/src/math/generic/sinbf16.cpp
 create mode 100644 libc/src/math/sinbf16.h
 create mode 100644 libc/test/src/math/sinbf16_test.cpp
 create mode 100644 libc/test/src/math/smoke/sinbf16_test.cpp

diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index a85fa19a92fccf..a05050ddb73d6c 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -876,6 +876,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
+  libc.src.math.sinbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
   libc.src.math.truncbf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 87fdcd04652303..c4a34493cc2966 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -887,6 +887,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
+  libc.src.math.sinbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
   libc.src.math.truncbf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 19397087f3e92c..7be9de867d9094 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -879,6 +879,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
+  libc.src.math.sinbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
   libc.src.math.truncbf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 5600b82ad3410b..f3f8d4e73f61f0 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -698,6 +698,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
+  libc.src.math.sinbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
   libc.src.math.truncbf16
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index e3fc845d2ed3af..378d3bc90a3d57 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -334,6 +334,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
+  libc.src.math.sinbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
   libc.src.math.truncbf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index 2eaef608e9a7ac..73f72adc989357 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -747,6 +747,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
+  libc.src.math.sinbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
   libc.src.math.truncbf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index 79d0c733ec3c01..d0725a4a85a013 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -763,6 +763,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
+  libc.src.math.sinbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
   libc.src.math.truncbf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index b2c8d62a65f4e1..bed76330c32944 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1111,6 +1111,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
+  libc.src.math.sinbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
   libc.src.math.truncbf16
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 023d298170c245..690911840d3407 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -712,6 +712,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
+  libc.src.math.sinbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
   libc.src.math.truncbf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 3bdc686c0e4ce4..4f66ccc096adc0 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1185,6 +1185,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
+  libc.src.math.sinbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
   libc.src.math.truncbf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 23a588f65d7435..42c349cf3b4ee5 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1207,6 +1207,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
+  libc.src.math.sinbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
   libc.src.math.truncbf16
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 1712b2ad26fda4..2457f4347e4cd1 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -424,6 +424,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
+  libc.src.math.sinbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
   libc.src.math.truncbf16
diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index 575c7bd1d1a10a..3bce46bcc798ce 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -343,7 +343,7 @@ Higher Math Functions
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | rsqrt     | |check|          |                 |                        | |check|              |                        |                        | 7.12.7.9               | F.10.4.9                   |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| sin       | |check|          | |check|         |                        | |check|              |                        |                        | 7.12.4.6               | F.10.1.6                   |
+| sin       | |check|          | |check|         |                        | |check|              |                        | |check|                | 7.12.4.6               | F.10.1.6                   |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | sincos    | |check|          | |check|         |                        |                      |                        |                        |                        |                            |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/shared/math.h b/libc/shared/math.h
index eb72bebcb53bfa..93a1269c11d98a 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -463,6 +463,7 @@
 #include "math/setpayloadsigf16.h"
 #include "math/setpayloadsigl.h"
 #include "math/sin.h"
+#include "math/sinbf16.h"
 #include "math/sincos.h"
 #include "math/sincosf.h"
 #include "math/sinf.h"
diff --git a/libc/shared/math/sinbf16.h b/libc/shared/math/sinbf16.h
new file mode 100644
index 00000000000000..f85a4795d51702
--- /dev/null
+++ b/libc/shared/math/sinbf16.h
@@ -0,0 +1,23 @@
+//===-- Shared sinbf16 function ---------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_MATH_SINBF16_H
+#define LLVM_LIBC_SHARED_MATH_SINBF16_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/sinbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::sinbf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_SINBF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index baac96e76d5435..8d5d2399d85ba9 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -5625,6 +5625,22 @@ add_header_library(
     libc.src.__support.macros.optimization
 )
 
+add_header_library(
+  sinbf16
+  HDRS
+    sinbf16.h
+  DEPENDS
+    .sincosf_utils
+    libc.hdr.errno_macros
+    libc.hdr.fenv_macros
+    libc.src.__support.FPUtil.cast
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.multiply_add
+    libc.src.__support.macros.optimization
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_header_library(
   sinf_double_eval
   HDRS
diff --git a/libc/src/__support/math/sinbf16.h b/libc/src/__support/math/sinbf16.h
new file mode 100644
index 00000000000000..de67d9ab575827
--- /dev/null
+++ b/libc/src/__support/math/sinbf16.h
@@ -0,0 +1,92 @@
+//===-- Implementation of sinbf16(x) function -----------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_SINBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_SINBF16_H
+
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "sincosf_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/macros/optimization.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE bfloat16 sinbf16(bfloat16 x) {
+  using namespace sincosf_utils_internal;
+
+  using FPBits = fputil::FPBits<bfloat16>;
+  FPBits xbits(x);
+
+  uint16_t x_u = xbits.uintval();
+  uint16_t x_abs = x_u & 0x7fff;
+  float xf = x;
+
+  if (x_abs < 0x7f80) {
+
+    int rounding = fputil::quick_get_round();
+    if (LIBC_UNLIKELY(x_abs <= 0x3e12)) {
+      // sin(+/-0) = +/-0
+      if (LIBC_UNLIKELY(x_abs == 0U))
+        return x;
+
+      // When x > 0, and rounding upward, sin(x) == x.
+      // When x < 0, and rounding downward, sin(x) == x.
+      if ((rounding == FE_UPWARD && xbits.is_pos()) ||
+          (rounding == FE_DOWNWARD && xbits.is_neg()))
+        return x;
+
+      // When x < 0, and rounding upward, sin(x) == (x - 1ULP)
+      if (rounding == FE_UPWARD && xbits.is_neg()) {
+        x_u--;
+        return FPBits(x_u).get_val();
+      }
+    }
+    double xd = static_cast<double>(xf);
+    uint32_t x_abs_d = fputil::FPBits<float>(xf).uintval() & 0x7fffffff;
+    double sin_k, cos_k, sin_y, cosm1_y;
+
+    sincosf_eval(xd, x_abs_d, sin_k, cos_k, sin_y, cosm1_y);
+    // using sin(a + b) = sin(a)*cos(b) + cos(a)*sin(b)
+    //  sin(x) = sin_k*cos_y + cos_k*sin_y
+    //  but cosm1_y = cos_y - 1 --> cos_y = cosm1_y + 1
+    //  sin(x) = sin_k*cosm1_y + sin_k + cos_k * sin_y
+
+    if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0))
+      return FPBits::zero(xbits.sign()).get_val();
+    return fputil::cast<bfloat16>(fputil::multiply_add(
+        sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k)));
+  }
+
+  // nan
+  if (xbits.is_nan()) {
+    if (xbits.is_signaling_nan()) {
+      fputil::raise_except_if_required(FE_INVALID);
+      return FPBits::quiet_nan().get_val();
+    }
+    return x;
+  }
+
+  // +/- inf
+  fputil::set_errno_if_required(EDOM);
+  fputil::raise_except_if_required(FE_INVALID);
+
+  return x + FPBits::quiet_nan().get_val();
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SINBF16_H
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 4f1dd95a6993da..4fbe654af08fa7 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -575,6 +575,7 @@ add_math_entrypoint_object(sincos)
 add_math_entrypoint_object(sincosf)
 
 add_math_entrypoint_object(sin)
+add_math_entrypoint_object(sinbf16)
 add_math_entrypoint_object(sinf)
 add_math_entrypoint_object(sinf16)
 add_math_entrypoint_object(sinpif)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 17f586036396f1..90be0c9a7a7aef 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -341,6 +341,16 @@ add_entrypoint_object(
     libc.src.__support.math.sin_integer_eval
 )
 
+add_entrypoint_object(
+  sinbf16
+  SRCS
+    sinbf16.cpp
+  HDRS
+    ../sinbf16.h
+  DEPENDS
+    libc.src.__support.math.sinbf16
+)
+
 add_entrypoint_object(
   sinf
   SRCS
diff --git a/libc/src/math/generic/sinbf16.cpp b/libc/src/math/generic/sinbf16.cpp
new file mode 100644
index 00000000000000..44f30352ead0e1
--- /dev/null
+++ b/libc/src/math/generic/sinbf16.cpp
@@ -0,0 +1,17 @@
+//===-- Implementation for sinbf16 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/sinbf16.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/math/sinbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, sinbf16, (bfloat16 x)) { return math::sinbf16(x); }
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/sinbf16.h b/libc/src/math/sinbf16.h
new file mode 100644
index 00000000000000..ae754400168cd8
--- /dev/null
+++ b/libc/src/math/sinbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for sinbf16 -----------------------*- 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_SINBF16_H
+#define LLVM_LIBC_SRC_MATH_SINBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 sinbf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_SINBF16_H
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 74eda7cdaf065e..92486fc9124475 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -468,6 +468,7 @@ else()
       libc.src.__support.math.setpayloadsigl
       libc.src.__support.math.sqrtf16
       libc.src.__support.math.sin
+      libc.src.__support.math.sinbf16
       libc.src.__support.math.sincos
       libc.src.__support.math.sincosf
       libc.src.__support.math.sinf
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 5b6663fa6494f6..d30ad8af955e77 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -799,6 +799,7 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
                LIBC_NAMESPACE::shared::hypotbf16(bfloat16(4.0), bfloat16(3.0)));
 
   EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::logbbf16(bfloat16(1.0f)));
+  EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::sinbf16(bfloat16(0.0)));
 
   bfloat16 setpayloadbf16_res = bfloat16(0.0);
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::setpayloadbf16(&setpayloadbf16_res,
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 921dbf220eb372..c6c679791ae90d 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -83,6 +83,19 @@ add_fp_unittest(
     libc.src.math.daddl
 )
 
+add_fp_unittest(
+  sinbf16_test
+  NEED_MPFR
+  SUITE
+    libc-math-unittests
+  SRCS
+    sinbf16_test.cpp
+  DEPENDS
+    libc.src.math.sinbf16
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_fp_unittest(
   sinf_test
   NEED_MPFR
diff --git a/libc/test/src/math/sinbf16_test.cpp b/libc/test/src/math/sinbf16_test.cpp
new file mode 100644
index 00000000000000..5c3d9138e2b6ed
--- /dev/null
+++ b/libc/test/src/math/sinbf16_test.cpp
@@ -0,0 +1,45 @@
+//===-- Unittests for sinbf16 ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/sinbf16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcSinBf16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+// Range: [0, Inf]
+static constexpr uint16_t POS_START = 0x0000U;
+static constexpr uint16_t POS_STOP = 0x7f80U;
+
+// Range: [-Inf, 0]
+static constexpr uint16_t NEG_START = 0x8000U;
+static constexpr uint16_t NEG_STOP = 0xff80U;
+
+TEST_F(LlvmLibcSinBf16Test, PositiveRange) {
+
+  for (uint16_t v = POS_START; v <= POS_STOP; ++v) {
+    bfloat16 x = FPBits(v).get_val();
+
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sin, x,
+                                   LIBC_NAMESPACE::sinbf16(x), 0.5);
+  }
+}
+
+TEST_F(LlvmLibcSinBf16Test, NegativeRange) {
+
+  for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) {
+    bfloat16 x = FPBits(v).get_val();
+
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sin, x,
+                                   LIBC_NAMESPACE::sinbf16(x), 0.5);
+  }
+}
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index a1f85df34e4c7d..10019ea434f431 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -48,6 +48,19 @@ add_fp_unittest(
     libc.src.__support.FPUtil.cast
 )
 
+add_fp_unittest(
+  sinbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    sinbf16_test.cpp
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.src.math.sinbf16
+    libc.hdr.fenv_macros
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_fp_unittest(
   sinf_test
   SUITE
diff --git a/libc/test/src/math/smoke/sinbf16_test.cpp b/libc/test/src/math/smoke/sinbf16_test.cpp
new file mode 100644
index 00000000000000..4f2cb201340e20
--- /dev/null
+++ b/libc/test/src/math/smoke/sinbf16_test.cpp
@@ -0,0 +1,40 @@
+//===-- Unittests for sinbf16 ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/sinbf16.h"
+#include "test/UnitTest/FEnvSafeTest.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+class LlvmLibcSinBf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+  DECLARE_SPECIAL_CONSTANTS(bfloat16)
+public:
+  void test_special_numbers() {
+    EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::sinbf16(aNaN));
+    EXPECT_MATH_ERRNO(0);
+
+    EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(
+        aNaN, LIBC_NAMESPACE::sinbf16(sNaN), FE_INVALID);
+    EXPECT_MATH_ERRNO(0);
+
+    EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::sinbf16(zero));
+    EXPECT_MATH_ERRNO(0);
+
+    EXPECT_FP_EQ_ALL_ROUNDING(neg_zero, LIBC_NAMESPACE::sinbf16(neg_zero));
+    EXPECT_MATH_ERRNO(0);
+
+    EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::sinbf16(inf));
+    EXPECT_MATH_ERRNO(EDOM);
+
+    EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::sinbf16(neg_inf));
+    EXPECT_MATH_ERRNO(EDOM);
+  }
+};
+TEST_F(LlvmLibcSinBf16Test, SpecialNumbers) { test_special_numbers(); }
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index f2df1ef9466183..5f2431454d7c81 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -11213,6 +11213,22 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_sinbf16",
+    hdrs = ["src/__support/math/sinbf16.h"],
+    deps = [
+        ":__support_fputil_bfloat16",
+        ":__support_fputil_cast",
+        ":__support_fputil_fenv_impl",
+        ":__support_fputil_fp_bits",
+        ":__support_fputil_multiply_add",
+        ":__support_macros_optimization",
+        ":__support_sincosf_utils",
+        ":hdr_errno_macros",
+        ":hdr_fenv_macros",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_sinf",
     hdrs = [
@@ -15219,6 +15235,13 @@ libc_math_function(
     ],
 )
 
+libc_math_function(
+    name = "sinbf16",
+    additional_deps = [
+        ":__support_math_sinbf16",
+    ],
+)
+
 libc_math_function(
     name = "sinf",
     additional_deps = [":__support_math_sinf"],

>From 733c4753143aa92f26f725c05060b8c4654b913e Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 27 Jun 2026 00:57:18 +0530
Subject: [PATCH 2/6] nits and add freebsd entrypoint

---
 libc/config/freebsd/x86_64/entrypoints.txt |  1 +
 libc/src/__support/math/sinbf16.h          | 11 +++++------
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/libc/config/freebsd/x86_64/entrypoints.txt b/libc/config/freebsd/x86_64/entrypoints.txt
index 8f6c178301c2f2..ad84d432c93a3d 100644
--- a/libc/config/freebsd/x86_64/entrypoints.txt
+++ b/libc/config/freebsd/x86_64/entrypoints.txt
@@ -630,6 +630,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
+  libc.src.math.sinbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
   libc.src.math.truncbf16
diff --git a/libc/src/__support/math/sinbf16.h b/libc/src/__support/math/sinbf16.h
index de67d9ab575827..cdbd29ce54e231 100644
--- a/libc/src/__support/math/sinbf16.h
+++ b/libc/src/__support/math/sinbf16.h
@@ -34,13 +34,12 @@ LIBC_INLINE bfloat16 sinbf16(bfloat16 x) {
   float xf = x;
 
   if (x_abs < 0x7f80) {
-
-    int rounding = fputil::quick_get_round();
     if (LIBC_UNLIKELY(x_abs <= 0x3e12)) {
       // sin(+/-0) = +/-0
-      if (LIBC_UNLIKELY(x_abs == 0U))
+      if (LIBC_UNLIKELY(x_abs == 0))
         return x;
 
+      int rounding = fputil::quick_get_round();
       // When x > 0, and rounding upward, sin(x) == x.
       // When x < 0, and rounding downward, sin(x) == x.
       if ((rounding == FE_UPWARD && xbits.is_pos()) ||
@@ -54,10 +53,10 @@ LIBC_INLINE bfloat16 sinbf16(bfloat16 x) {
       }
     }
     double xd = static_cast<double>(xf);
-    uint32_t x_abs_d = fputil::FPBits<float>(xf).uintval() & 0x7fffffff;
+    uint32_t x_abs_f = fputil::FPBits<float>(xf).uintval() & 0x7fffffff;
     double sin_k, cos_k, sin_y, cosm1_y;
 
-    sincosf_eval(xd, x_abs_d, sin_k, cos_k, sin_y, cosm1_y);
+    sincosf_eval(xd, x_abs_f, sin_k, cos_k, sin_y, cosm1_y);
     // using sin(a + b) = sin(a)*cos(b) + cos(a)*sin(b)
     //  sin(x) = sin_k*cos_y + cos_k*sin_y
     //  but cosm1_y = cos_y - 1 --> cos_y = cosm1_y + 1
@@ -69,7 +68,7 @@ LIBC_INLINE bfloat16 sinbf16(bfloat16 x) {
         sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k)));
   }
 
-  // nan
+  // NaN
   if (xbits.is_nan()) {
     if (xbits.is_signaling_nan()) {
       fputil::raise_except_if_required(FE_INVALID);

>From 1c460de1726947d7786494665ec42fabeae2a190 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 17 Oct 2026 12:10:58 +0530
Subject: [PATCH 3/6] nits

---
 libc/shared/math/sinbf16.h             | 1 +
 libc/src/__support/math/CMakeLists.txt | 2 ++
 libc/src/__support/math/sinbf16.h      | 2 ++
 libc/src/math/generic/sinbf16.cpp      | 3 ++-
 4 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/libc/shared/math/sinbf16.h b/libc/shared/math/sinbf16.h
index f85a4795d51702..4874866d1f12cc 100644
--- a/libc/shared/math/sinbf16.h
+++ b/libc/shared/math/sinbf16.h
@@ -10,6 +10,7 @@
 #define LLVM_LIBC_SHARED_MATH_SINBF16_H
 
 #include "shared/libc_common.h"
+#include "src/__support/macros/config.h"
 #include "src/__support/math/sinbf16.h"
 
 namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 8d5d2399d85ba9..dd5809f479a475 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -5637,6 +5637,8 @@ add_header_library(
     libc.src.__support.FPUtil.fenv_impl
     libc.src.__support.FPUtil.fp_bits
     libc.src.__support.FPUtil.multiply_add
+    libc.src.__support.macros.attributes
+    libc.src.__support.macros.config
     libc.src.__support.macros.optimization
     libc.src.__support.FPUtil.bfloat16
 )
diff --git a/libc/src/__support/math/sinbf16.h b/libc/src/__support/math/sinbf16.h
index cdbd29ce54e231..83993caf6e73ed 100644
--- a/libc/src/__support/math/sinbf16.h
+++ b/libc/src/__support/math/sinbf16.h
@@ -17,6 +17,8 @@
 #include "src/__support/FPUtil/bfloat16.h"
 #include "src/__support/FPUtil/cast.h"
 #include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/config.h"
 #include "src/__support/macros/optimization.h"
 
 namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/math/generic/sinbf16.cpp b/libc/src/math/generic/sinbf16.cpp
index 44f30352ead0e1..422299beec2ed0 100644
--- a/libc/src/math/generic/sinbf16.cpp
+++ b/libc/src/math/generic/sinbf16.cpp
@@ -7,7 +7,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/sinbf16.h"
-#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
 #include "src/__support/math/sinbf16.h"
 
 namespace LIBC_NAMESPACE_DECL {

>From 0dd9043643dcaafc7a338a412862064f915a0f74 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 17 Oct 2026 12:18:27 +0530
Subject: [PATCH 4/6] new header commments

---
 libc/shared/math/sinbf16.h                | 7 ++++++-
 libc/src/__support/math/sinbf16.h         | 7 ++++++-
 libc/src/math/generic/sinbf16.cpp         | 7 ++++++-
 libc/src/math/sinbf16.h                   | 7 ++++++-
 libc/test/src/math/sinbf16_test.cpp       | 7 ++++++-
 libc/test/src/math/smoke/sinbf16_test.cpp | 7 ++++++-
 6 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/libc/shared/math/sinbf16.h b/libc/shared/math/sinbf16.h
index 4874866d1f12cc..71605acf635f95 100644
--- a/libc/shared/math/sinbf16.h
+++ b/libc/shared/math/sinbf16.h
@@ -1,10 +1,15 @@
-//===-- Shared sinbf16 function ---------------------------------*- C++ -*-===//
+//===----------------------------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Shared header for the sinbf16 function.
+///
+//===----------------------------------------------------------------------===//
 
 #ifndef LLVM_LIBC_SHARED_MATH_SINBF16_H
 #define LLVM_LIBC_SHARED_MATH_SINBF16_H
diff --git a/libc/src/__support/math/sinbf16.h b/libc/src/__support/math/sinbf16.h
index 83993caf6e73ed..a01b642eff7145 100644
--- a/libc/src/__support/math/sinbf16.h
+++ b/libc/src/__support/math/sinbf16.h
@@ -1,10 +1,15 @@
-//===-- Implementation of sinbf16(x) function -----------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of the sinbf16 function.
+///
+//===----------------------------------------------------------------------===//
 
 #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_SINBF16_H
 #define LLVM_LIBC_SRC___SUPPORT_MATH_SINBF16_H
diff --git a/libc/src/math/generic/sinbf16.cpp b/libc/src/math/generic/sinbf16.cpp
index 422299beec2ed0..b4846830947f1e 100644
--- a/libc/src/math/generic/sinbf16.cpp
+++ b/libc/src/math/generic/sinbf16.cpp
@@ -1,10 +1,15 @@
-//===-- Implementation for sinbf16 function -------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Entrypoint definition for the sinbf16 function.
+///
+//===----------------------------------------------------------------------===//
 
 #include "src/math/sinbf16.h"
 #include "src/__support/common.h"
diff --git a/libc/src/math/sinbf16.h b/libc/src/math/sinbf16.h
index ae754400168cd8..9a8aebadac0791 100644
--- a/libc/src/math/sinbf16.h
+++ b/libc/src/math/sinbf16.h
@@ -1,10 +1,15 @@
-//===-- Implementation header for sinbf16 -----------------------*- C++ -*-===//
+//===----------------------------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Public declaration of the sinbf16 entrypoint.
+///
+//===----------------------------------------------------------------------===//
 
 #ifndef LLVM_LIBC_SRC_MATH_SINBF16_H
 #define LLVM_LIBC_SRC_MATH_SINBF16_H
diff --git a/libc/test/src/math/sinbf16_test.cpp b/libc/test/src/math/sinbf16_test.cpp
index 5c3d9138e2b6ed..5d5e09cf4d65af 100644
--- a/libc/test/src/math/sinbf16_test.cpp
+++ b/libc/test/src/math/sinbf16_test.cpp
@@ -1,10 +1,15 @@
-//===-- Unittests for sinbf16 ---------------------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unit tests for the sinbf16 function.
+///
+//===----------------------------------------------------------------------===//
 
 #include "src/__support/FPUtil/bfloat16.h"
 #include "src/math/sinbf16.h"
diff --git a/libc/test/src/math/smoke/sinbf16_test.cpp b/libc/test/src/math/smoke/sinbf16_test.cpp
index 4f2cb201340e20..7f7214a2ea72e1 100644
--- a/libc/test/src/math/smoke/sinbf16_test.cpp
+++ b/libc/test/src/math/smoke/sinbf16_test.cpp
@@ -1,10 +1,15 @@
-//===-- Unittests for sinbf16 ---------------------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Smoke tests for the sinbf16 function.
+///
+//===----------------------------------------------------------------------===//
 
 #include "hdr/errno_macros.h"
 #include "src/__support/FPUtil/bfloat16.h"

>From b74344d4f06b5c6c04aa42a7b14a7c37414b04a1 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 17 Oct 2026 14:06:30 +0530
Subject: [PATCH 5/6] bazel

---
 .../llvm-project-overlay/libc/test/src/math/BUILD.bazel    | 7 +++++++
 .../libc/test/src/math/smoke/BUILD.bazel                   | 7 +++++++
 2 files changed, 14 insertions(+)

diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/math/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/math/BUILD.bazel
index 3c6468dfd3b1e9..7b5166b85608b5 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/math/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/math/BUILD.bazel
@@ -780,6 +780,13 @@ math_mpfr_test(
 
 math_mpfr_test(name = "sin")
 
+math_mpfr_test(
+    name = "sinbf16",
+    deps = [
+        "//libc:__support_fputil_bfloat16",
+    ],
+)
+
 math_mpfr_test(
     name = "sinf",
     hdrs = ["sdcomp26094.h"],
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel
index b86930825f493d..9edf3aeb9ef380 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel
@@ -2135,6 +2135,13 @@ math_test(
 
 math_test(name = "sin")
 
+math_test(
+    name = "sinbf16",
+    deps = [
+        "//libc:__support_fputil_bfloat16",
+    ],
+)
+
 math_test(name = "sinf")
 
 math_test(name = "sincos")

>From 4ce42226e577f94055e2d123026bdbe2292025ed Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 17 Oct 2026 14:26:12 +0530
Subject: [PATCH 6/6] bazel missing deps

---
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 5f2431454d7c81..baa42735c3c70f 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -11222,6 +11222,8 @@ libc_support_library(
         ":__support_fputil_fenv_impl",
         ":__support_fputil_fp_bits",
         ":__support_fputil_multiply_add",
+        ":__support_macros_attributes",
+        ":__support_macros_config",
         ":__support_macros_optimization",
         ":__support_sincosf_utils",
         ":hdr_errno_macros",



More information about the libc-commits mailing list