[libc-commits] [libc] 337ac53 - [libc][math][c23] Add sinbf16 function (#185441)

via libc-commits libc-commits at lists.llvm.org
Sat Nov 7 09:40:15 PST 2026


Author: Zorojuro
Date: 2026-11-07T12:40:09-05:00
New Revision: 337ac53883243b3bca603f00e3c0e9c3b1f5f528

URL: https://github.com/llvm/llvm-project/commit/337ac53883243b3bca603f00e3c0e9c3b1f5f528
DIFF: https://github.com/llvm/llvm-project/commit/337ac53883243b3bca603f00e3c0e9c3b1f5f528.diff

LOG: [libc][math][c23] Add sinbf16 function (#185441)

This PR intends to add sinbf16 higher math function for bfloat16 type .

( The new header comments are updated using gemini flash 3.8 (low) )

Added: 
    libc/shared/math/sinbf16.h
    libc/src/__support/math/sinbf16.h
    libc/src/math/generic/sinbf16.cpp
    libc/src/math/sinbf16.h
    libc/test/src/math/sinbf16_test.cpp
    libc/test/src/math/smoke/sinbf16_test.cpp

Modified: 
    libc/config/baremetal/aarch64/entrypoints.txt
    libc/config/baremetal/arm/entrypoints.txt
    libc/config/baremetal/hexagon/entrypoints.txt
    libc/config/baremetal/riscv/entrypoints.txt
    libc/config/darwin/aarch64/entrypoints.txt
    libc/config/darwin/x86_64/entrypoints.txt
    libc/config/freebsd/x86_64/entrypoints.txt
    libc/config/gpu/amdgpu/entrypoints.txt
    libc/config/gpu/nvptx/entrypoints.txt
    libc/config/linux/aarch64/entrypoints.txt
    libc/config/linux/arm/entrypoints.txt
    libc/config/linux/riscv/entrypoints.txt
    libc/config/linux/x86_64/entrypoints.txt
    libc/config/windows/entrypoints.txt
    libc/docs/headers/math/index.md
    libc/shared/math.h
    libc/src/__support/math/CMakeLists.txt
    libc/src/math/CMakeLists.txt
    libc/src/math/generic/CMakeLists.txt
    libc/test/shared/CMakeLists.txt
    libc/test/shared/shared_math_test.cpp
    libc/test/src/math/CMakeLists.txt
    libc/test/src/math/smoke/CMakeLists.txt
    utils/bazel/llvm-project-overlay/libc/BUILD.bazel
    utils/bazel/llvm-project-overlay/libc/test/src/math/BUILD.bazel
    utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel

Removed: 
    


################################################################################
diff  --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index bd46d678013fd0..8bb90fcc20ea13 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -899,6 +899,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.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16

diff  --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 0cf179de3daa9c..a1c1606fb75406 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -910,6 +910,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.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16

diff  --git a/libc/config/baremetal/hexagon/entrypoints.txt b/libc/config/baremetal/hexagon/entrypoints.txt
index 44e287ec3b62e6..f10de569db06f6 100644
--- a/libc/config/baremetal/hexagon/entrypoints.txt
+++ b/libc/config/baremetal/hexagon/entrypoints.txt
@@ -899,6 +899,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.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16

diff  --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 380ee84c219e9d..f6ecc79ccd5275 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -902,6 +902,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.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16

diff  --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 78cffd1f6b4fee..d5dacf834d6d3e 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -721,6 +721,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.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16

diff  --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index a5f5cacb0f8638..fb7bba25bf7a77 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -359,6 +359,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.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16

diff  --git a/libc/config/freebsd/x86_64/entrypoints.txt b/libc/config/freebsd/x86_64/entrypoints.txt
index 7ee9f58c04f72e..28c3ca6411f7f6 100644
--- a/libc/config/freebsd/x86_64/entrypoints.txt
+++ b/libc/config/freebsd/x86_64/entrypoints.txt
@@ -653,6 +653,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.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16

diff  --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index ffa65e07f6e558..b28c80501c9ec4 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -783,6 +783,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.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16

diff  --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index 7d35aecf54e44a..2e9b333d58d60c 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -799,6 +799,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.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16

diff  --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 2f22842dde7cec..8ba560989af6af 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1145,6 +1145,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.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16

diff  --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 49162a88046b97..5e04635e298e77 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -751,6 +751,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.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16

diff  --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 2087af04a93888..56813aaf66a91f 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1225,6 +1225,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.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index a5aea75c9b1c22..25b24740400b97 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1247,6 +1247,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.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16

diff  --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index cebd511414bcb6..c0c46291515b81 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -460,6 +460,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadf128
   libc.src.math.setpayloadsigbf16
   libc.src.math.setpayloadsigf128
+  libc.src.math.sinbf16
   libc.src.math.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16

diff  --git a/libc/docs/headers/math/index.md b/libc/docs/headers/math/index.md
index e82e308351dce4..d6e0f647594537 100644
--- a/libc/docs/headers/math/index.md
+++ b/libc/docs/headers/math/index.md
@@ -209,7 +209,7 @@ log
 | powr      |                   |                  |                   |                         |                       |                         |                         | 7.12.7.7               | F.10.4.7                   |
 | rootn     |                   |                  |                   |                         |                       |                         |                         | 7.12.7.8               | F.10.4.8                   |
 | 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 }}      |                   |                         |                       |                         |                         |                        |                            |
 | sinh      | {{ check }}       | {{ check }}      |                   |                         | {{ check }}           |                         | {{ check }}             | 7.12.5.5               | F.10.2.5                   |
 | sinpi     | {{ check }}       |                  |                   |                         | {{ check }}           |                         |                         | 7.12.4.13              | F.10.1.13                  |

diff  --git a/libc/shared/math.h b/libc/shared/math.h
index 348ef11f9f655a..f7616676b27c08 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -482,6 +482,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..71605acf635f95
--- /dev/null
+++ b/libc/shared/math/sinbf16.h
@@ -0,0 +1,29 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+#include "shared/libc_common.h"
+#include "src/__support/macros/config.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 7af8fba1f85a58..ee57d082456c33 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -5880,6 +5880,24 @@ 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.attributes
+    libc.src.__support.macros.config
+    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..219fb2b7fadcc3
--- /dev/null
+++ b/libc/src/__support/math/sinbf16.h
@@ -0,0 +1,102 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+#include "src/__support/math/sincosf_utils.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;
+
+  // |x| < inf (0x7f80)
+  if (x_abs < 0x7f80) {
+    // |x| <= 0x1.24p-3f (0x3e12)
+    if (LIBC_UNLIKELY(x_abs <= 0x3e12)) {
+      // sin(+/-0) = +/-0
+      if (LIBC_UNLIKELY(x_abs == 0))
+        return x;
+
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+      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()) ||
+          (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();
+      }
+#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+    }
+    double xd = static_cast<double>(xf);
+    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_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
+    //  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 6119fdb817f050..2a75aba26a2c7b 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -593,6 +593,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 e79e69f9bfc0b7..1d628e75ecd807 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -363,6 +363,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..b4846830947f1e
--- /dev/null
+++ b/libc/src/math/generic/sinbf16.cpp
@@ -0,0 +1,23 @@
+//===----------------------------------------------------------------------===//
+//
+// 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"
+#include "src/__support/macros/config.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..9a8aebadac0791
--- /dev/null
+++ b/libc/src/math/sinbf16.h
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+#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 18638523256cf3..e71040c591b56a 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -495,6 +495,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 9ddce72220c8dd..53cf9a94c39b29 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -856,6 +856,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 e87122875cfec3..ba0ab3a0fd8846 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-tests
+  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..5d5e09cf4d65af
--- /dev/null
+++ b/libc/test/src/math/sinbf16_test.cpp
@@ -0,0 +1,50 @@
+//===----------------------------------------------------------------------===//
+//
+// 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"
+#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 9bb2f0ccd5dce2..7f2b50feaacf78 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -47,6 +47,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..7f7214a2ea72e1
--- /dev/null
+++ b/libc/test/src/math/smoke/sinbf16_test.cpp
@@ -0,0 +1,45 @@
+//===----------------------------------------------------------------------===//
+//
+// 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"
+#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 e41bc10b25dff6..89c312fa4fe87b 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -12119,6 +12119,24 @@ 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_attributes",
+        ":__support_macros_config",
+        ":__support_macros_optimization",
+        ":__support_sincosf_utils",
+        ":hdr_errno_macros",
+        ":hdr_fenv_macros",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_sinf",
     hdrs = [
@@ -16494,6 +16512,13 @@ libc_math_function(
     ],
 )
 
+libc_math_function(
+    name = "sinbf16",
+    additional_deps = [
+        ":__support_math_sinbf16",
+    ],
+)
+
 libc_math_function(
     name = "sinf",
     additional_deps = [":__support_math_sinf"],

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 4e641c872edd2d..8a15664f1ab33a 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
@@ -1041,6 +1041,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 c43d3f272cfe06..2d4aa158ef13f3 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
@@ -2379,6 +2379,13 @@ math_test(
 
 math_test(name = "sin")
 
+math_test(
+    name = "sinbf16",
+    deps = [
+        "//libc:__support_fputil_bfloat16",
+    ],
+)
+
 math_test(name = "sinf")
 
 math_test(name = "sincos")


        


More information about the libc-commits mailing list