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

via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 9 08:16:51 PDT 2026


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

>From 1c0921e5510b0c1c541eb9379b2102ae39588e3c Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 7 Mar 2026 18:59:34 +0530
Subject: [PATCH 1/4] feat: Implementation and tests for sinbf16

entrypoints added

test added

switch

raw implenatation

nit

cmake

t1

t2

exhaustive + case

err

err

err

feat: Implementation and tests for sinbf16
---
 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             | 95 +++++++++++++++++++
 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         |  2 +
 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     | 41 ++++++++
 .../llvm-project-overlay/libc/BUILD.bazel     | 21 ++++
 28 files changed, 333 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 278e1f5afde70..36ab9ef8b6035 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -842,6 +842,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.truncbf16
   libc.src.math.totalorderbf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 7bc92e84b7fa4..c2c71027d9d6c 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -845,6 +845,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.truncbf16
   libc.src.math.totalorderbf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 29be47dfbfd53..87c1fff875f5c 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -840,6 +840,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.truncbf16
   libc.src.math.totalorderbf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index b4a1ee70764cb..ce0b5b99d3baf 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -656,6 +656,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.truncbf16
   libc.src.math.totalorderbf16
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index e899bf97ea3f6..4e1904e836b71 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -296,6 +296,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.truncbf16
   libc.src.math.totalorderbf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index a65b6f0274fd8..7c37e49db5079 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -681,6 +681,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.truncbf16
   libc.src.math.totalorderbf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index fee0038c88cc0..a9724989d2a17 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -683,6 +683,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.truncbf16
   libc.src.math.totalorderbf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 960958b7cf90a..139e23ced2bfc 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -912,6 +912,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.truncbf16
   libc.src.math.totalorderbf16
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index e76bf81670ce5..7f6e856ed3904 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -525,6 +525,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.truncbf16
   libc.src.math.totalorderbf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index a69d1a1b0a642..1d192bd687c15 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -933,6 +933,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.truncbf16
   libc.src.math.totalorderbf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 4103ee0331a23..6e9bde8c63a19 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -983,6 +983,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.truncbf16
   libc.src.math.totalorderbf16
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index f777fc6c94bc2..3cabdea505cfa 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -371,6 +371,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.truncbf16
   libc.src.math.totalorderbf16
diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index 7ea54fb4d8263..94579d9d9c759 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -349,7 +349,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 ede0ebd5371ac..e8c1c702d2359 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -221,6 +221,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 0000000000000..f85a4795d5170
--- /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 0df8262cfb5f2..2cabb250ab7c4 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -2766,6 +2766,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
   HDRS
diff --git a/libc/src/__support/math/sinbf16.h b/libc/src/__support/math/sinbf16.h
new file mode 100644
index 0000000000000..60a4fa36105ee
--- /dev/null
+++ b/libc/src/__support/math/sinbf16.h
@@ -0,0 +1,95 @@
+//===-- 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) {
+    //  +/-0
+    if (LIBC_UNLIKELY(x_abs == 0)) {
+      return x;
+    }
+    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*cok_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 e617950368994..f54a953a6fb2e 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -554,6 +554,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 418cf85b84a20..f0c7e1e64dbf7 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -343,6 +343,16 @@ add_entrypoint_object(
     libc.src.__support.math.sin
 )
 
+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 0000000000000..44f30352ead0e
--- /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 0000000000000..ae754400168cd
--- /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 c90e5687d8c33..f24e433d7b8d4 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -220,6 +220,7 @@ add_fp_unittest(
     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 17045ce5edfdb..100135bdf424b 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -434,6 +434,8 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
   EXPECT_FP_EQ(bfloat16(-1.0),
                LIBC_NAMESPACE::shared::getpayloadbf16(&getpayloadbf16_x));
 
+  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,
                                                       bfloat16(0.0)));
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 73b5ebf5a856e..ca6d7664bfbb7 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -94,6 +94,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 0000000000000..5c3d9138e2b6e
--- /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 3d52873c40bb8..636d5378ab54c 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 0000000000000..935cb0eaf5c35
--- /dev/null
+++ b/libc/test/src/math/smoke/sinbf16_test.cpp
@@ -0,0 +1,41 @@
+//===-- 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 31a229d94a187..80855cab7fc48 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -5339,6 +5339,20 @@ 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_fputil_sqrt",
+        ":__support_macros_optimization",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_sinf",
     hdrs = ["src/__support/math/sinf.h"],
@@ -5943,6 +5957,13 @@ libc_math_function(
     additional_deps = [":__support_math_asin"],
 )
 
+libc_math_function(
+    name = "sinbf16",
+    additional_deps = [
+        ":__support_math_sinbf16",
+    ],
+)
+
 libc_math_function(
     name = "asinf",
     additional_deps = [":__support_math_asinf"],

>From c894d3c2dfc5c95f68757816fc69815bb668dc91 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Mon, 9 Mar 2026 20:16:43 +0530
Subject: [PATCH 2/4] chore: nit

chore: nit

chore: nit
---
 libc/src/__support/math/sinbf16.h                 |  7 ++-----
 libc/test/src/math/smoke/sinbf16_test.cpp         |  4 ++--
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 14 +++++++-------
 3 files changed, 11 insertions(+), 14 deletions(-)

diff --git a/libc/src/__support/math/sinbf16.h b/libc/src/__support/math/sinbf16.h
index 60a4fa36105ee..cb0b3590d8f88 100644
--- a/libc/src/__support/math/sinbf16.h
+++ b/libc/src/__support/math/sinbf16.h
@@ -34,10 +34,7 @@ LIBC_INLINE bfloat16 sinbf16(bfloat16 x) {
   float xf = x;
 
   if (x_abs < 0x7f80) {
-    //  +/-0
-    if (LIBC_UNLIKELY(x_abs == 0)) {
-      return x;
-    }
+
     int rounding = fputil::quick_get_round();
     if (LIBC_UNLIKELY(x_abs <= 0x3e12)) {
       // sin(+/-0) = +/-0
@@ -81,7 +78,7 @@ LIBC_INLINE bfloat16 sinbf16(bfloat16 x) {
     return x;
   }
 
-  // inf
+  // +/- inf
   fputil::set_errno_if_required(EDOM);
   fputil::raise_except_if_required(FE_INVALID);
 
diff --git a/libc/test/src/math/smoke/sinbf16_test.cpp b/libc/test/src/math/smoke/sinbf16_test.cpp
index 935cb0eaf5c35..0d32bab81d97d 100644
--- a/libc/test/src/math/smoke/sinbf16_test.cpp
+++ b/libc/test/src/math/smoke/sinbf16_test.cpp
@@ -13,7 +13,7 @@
 #include "test/UnitTest/FPMatcher.h"
 #include "test/UnitTest/Test.h"
 
-class LlvmLibcsinBf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+class LlvmLibcSinBf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
   DECLARE_SPECIAL_CONSTANTS(bfloat16)
 public:
   void test_special_numbers() {
@@ -38,4 +38,4 @@ class LlvmLibcsinBf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
 
   }
 };
-TEST_F(LlvmLibcsinBf16Test, SpecialNumbers) { test_special_numbers(); }
+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 80855cab7fc48..5bc5d7f637a54 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -5957,13 +5957,6 @@ libc_math_function(
     additional_deps = [":__support_math_asin"],
 )
 
-libc_math_function(
-    name = "sinbf16",
-    additional_deps = [
-        ":__support_math_sinbf16",
-    ],
-)
-
 libc_math_function(
     name = "asinf",
     additional_deps = [":__support_math_asinf"],
@@ -7858,6 +7851,13 @@ libc_math_function(
     additional_deps = [":__support_math_sin"],
 )
 
+libc_math_function(
+    name = "sinbf16",
+    additional_deps = [
+        ":__support_math_sinbf16",
+    ],
+)
+
 libc_math_function(
     name = "sinf",
     additional_deps = [":__support_math_sinf"],

>From f94e6b3c2ddcdfdadf65264fe27449baf9521418 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Mon, 9 Mar 2026 20:40:27 +0530
Subject: [PATCH 3/4] chore: bazel

---
 libc/src/__support/math/sinbf16.h                 | 2 +-
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 3 +++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/libc/src/__support/math/sinbf16.h b/libc/src/__support/math/sinbf16.h
index cb0b3590d8f88..de67d9ab57582 100644
--- a/libc/src/__support/math/sinbf16.h
+++ b/libc/src/__support/math/sinbf16.h
@@ -59,7 +59,7 @@ LIBC_INLINE bfloat16 sinbf16(bfloat16 x) {
 
     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*cok_y + cos_k*sin_y
+    //  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
 
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 5bc5d7f637a54..c278f132637fa 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -5350,6 +5350,9 @@ libc_support_library(
         ":__support_fputil_multiply_add",
         ":__support_fputil_sqrt",
         ":__support_macros_optimization",
+        ":__support_sincosf_utils",
+        ":hdr_errno_macros",
+        ":hdr_fenv_macros",
     ],
 )
 

>From 28bdd3825847a0d065cb13899016173e8d579cf5 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Mon, 9 Mar 2026 20:46:24 +0530
Subject: [PATCH 4/4] chore: nit

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

diff --git a/libc/test/src/math/smoke/sinbf16_test.cpp b/libc/test/src/math/smoke/sinbf16_test.cpp
index 0d32bab81d97d..4f2cb201340e2 100644
--- a/libc/test/src/math/smoke/sinbf16_test.cpp
+++ b/libc/test/src/math/smoke/sinbf16_test.cpp
@@ -35,7 +35,6 @@ class LlvmLibcSinBf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
 
     EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::sinbf16(neg_inf));
     EXPECT_MATH_ERRNO(EDOM);
-
   }
 };
 TEST_F(LlvmLibcSinBf16Test, SpecialNumbers) { test_special_numbers(); }



More information about the llvm-commits mailing list