Skip to content

Conversation

krishna2803
Copy link
Contributor

This PR adds the following basic math functions for BFloat16 type along with the tests:

  • scalbnbf16
  • scalblnbf16

…f16 math functions

Signed-off-by: Krishna Pandey <kpandey81930@gmail.com>
Signed-off-by: Krishna Pandey <kpandey81930@gmail.com>
…signaling}bf16 math functions

Signed-off-by: Krishna Pandey <kpandey81930@gmail.com>
Signed-off-by: Krishna Pandey <kpandey81930@gmail.com>
Signed-off-by: Krishna Pandey <kpandey81930@gmail.com>
…th functions

Signed-off-by: Krishna Pandey <kpandey81930@gmail.com>
Signed-off-by: Krishna Pandey <kpandey81930@gmail.com>
Signed-off-by: Krishna Pandey <kpandey81930@gmail.com>
Signed-off-by: Krishna Pandey <kpandey81930@gmail.com>
Signed-off-by: Krishna Pandey <kpandey81930@gmail.com>
Signed-off-by: Krishna Pandey <kpandey81930@gmail.com>
Signed-off-by: Krishna Pandey <kpandey81930@gmail.com>
Signed-off-by: Krishna Pandey <kpandey81930@gmail.com>
Signed-off-by: Krishna Pandey <kpandey81930@gmail.com>
@llvmbot
Copy link
Member

llvmbot commented Aug 27, 2025

@llvm/pr-subscribers-libc

@llvm/pr-subscribers-backend-risc-v

Author: Krishna Pandey (krishna2803)

Changes

This PR adds the following basic math functions for BFloat16 type along with the tests:

  • scalbnbf16
  • scalblnbf16

Full diff: https://github.com/llvm/llvm-project/pull/155569.diff

22 Files Affected:

  • (modified) libc/config/baremetal/aarch64/entrypoints.txt (+2)
  • (modified) libc/config/baremetal/arm/entrypoints.txt (+2)
  • (modified) libc/config/baremetal/riscv/entrypoints.txt (+2)
  • (modified) libc/config/darwin/aarch64/entrypoints.txt (+2)
  • (modified) libc/config/darwin/x86_64/entrypoints.txt (+2)
  • (modified) libc/config/gpu/amdgpu/entrypoints.txt (+2)
  • (modified) libc/config/gpu/nvptx/entrypoints.txt (+2)
  • (modified) libc/config/linux/aarch64/entrypoints.txt (+2)
  • (modified) libc/config/linux/arm/entrypoints.txt (+2)
  • (modified) libc/config/linux/riscv/entrypoints.txt (+2)
  • (modified) libc/config/linux/x86_64/entrypoints.txt (+2)
  • (modified) libc/config/windows/entrypoints.txt (+2)
  • (modified) libc/docs/headers/math/index.rst (+2-2)
  • (modified) libc/src/math/CMakeLists.txt (+2)
  • (modified) libc/src/math/generic/CMakeLists.txt (+28)
  • (added) libc/src/math/generic/scalblnbf16.cpp (+21)
  • (added) libc/src/math/generic/scalbnbf16.cpp (+21)
  • (added) libc/src/math/scalblnbf16.h (+21)
  • (added) libc/src/math/scalbnbf16.h (+21)
  • (modified) libc/test/src/math/smoke/CMakeLists.txt (+32)
  • (added) libc/test/src/math/smoke/scalblnbf16_test.cpp (+14)
  • (added) libc/test/src/math/smoke/scalbnbf16_test.cpp (+14)
diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index ff3a6bfa8a2fc..637be4f19d5b7 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -814,6 +814,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.rintbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.scalblnbf16
+  libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index f8916d71f86aa..19aedb0a8677d 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -817,6 +817,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.rintbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.scalblnbf16
+  libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 8a72fa6692869..cfbb598cbe4bf 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -817,6 +817,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.rintbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.scalblnbf16
+  libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 796c0d3c8a4ff..26e97d8993039 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -647,6 +647,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.rintbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.scalblnbf16
+  libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index fa1d8def8936c..0556a6f7a4715 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -290,6 +290,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.rintbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.scalblnbf16
+  libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index 49c41411cb5d2..8817972480aac 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -673,6 +673,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.rintbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.scalblnbf16
+  libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index 06a2c9addb7fb..db762ed5c74d6 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -675,6 +675,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.rintbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.scalblnbf16
+  libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index feea7ff69c166..0a2ebfa3f8720 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -902,6 +902,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.rintbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.scalblnbf16
+  libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 8e92fac863c6b..ea1c5c0558272 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -517,6 +517,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.rintbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.scalblnbf16
+  libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index fb08cfe7bd3f8..d5180b77c3af0 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -920,6 +920,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.rintbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.scalblnbf16
+  libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 37af75454daa3..c64db2cc3548f 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -953,6 +953,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.rintbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.scalblnbf16
+  libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index c09e6b259fa42..9f825bdf657e9 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -363,6 +363,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.rintbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.scalblnbf16
+  libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index 89624460c3af5..a707b37894afc 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -233,9 +233,9 @@ Basic Operations
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | roundeven        | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.9.8               | F.10.6.8                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| scalbln          | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.6.19              | F.10.3.19                  |
+| scalbln          | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.6.19              | F.10.3.19                  |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| scalbn           | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.6.19              | F.10.3.19                  |
+| scalbn           | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.6.19              | F.10.3.19                  |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | setpayload       | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | F.10.13.2              | N/A                        |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 1edb517421e28..4b325f773b35f 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -518,12 +518,14 @@ add_math_entrypoint_object(scalblnf)
 add_math_entrypoint_object(scalblnl)
 add_math_entrypoint_object(scalblnf16)
 add_math_entrypoint_object(scalblnf128)
+add_math_entrypoint_object(scalblnbf16)
 
 add_math_entrypoint_object(scalbn)
 add_math_entrypoint_object(scalbnf)
 add_math_entrypoint_object(scalbnl)
 add_math_entrypoint_object(scalbnf16)
 add_math_entrypoint_object(scalbnf128)
+add_math_entrypoint_object(scalbnbf16)
 
 add_math_entrypoint_object(setpayload)
 add_math_entrypoint_object(setpayloadf)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 904f8d2eb4704..b1fcbf356777a 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4646,6 +4646,20 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.manipulation_functions
 )
 
+add_entrypoint_object(
+  scalblnbf16
+  SRCS
+    scalblnbf16.cpp
+  HDRS
+    ../scalblnbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.manipulation_functions
+)
+
 add_entrypoint_object(
   scalbn
   SRCS
@@ -4703,6 +4717,20 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.manipulation_functions
 )
 
+add_entrypoint_object(
+  scalbnbf16
+  SRCS
+    scalbnbf16.cpp
+  HDRS
+    ../scalblnbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.manipulation_functions
+)
+
 add_entrypoint_object(
   fmaf
   SRCS
diff --git a/libc/src/math/generic/scalblnbf16.cpp b/libc/src/math/generic/scalblnbf16.cpp
new file mode 100644
index 0000000000000..cda8e126efacb
--- /dev/null
+++ b/libc/src/math/generic/scalblnbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of scalblnbf16 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/scalblnbf16.h"
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, scalblnbf16, (bfloat16 x, long n)) {
+  return fputil::ldexp(x, n);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/scalbnbf16.cpp b/libc/src/math/generic/scalbnbf16.cpp
new file mode 100644
index 0000000000000..87833742d59ac
--- /dev/null
+++ b/libc/src/math/generic/scalbnbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of scalbnbf16 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/scalbnbf16.h"
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, scalbnbf16, (bfloat16 x, int n)) {
+  return fputil::ldexp(x, n);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/scalblnbf16.h b/libc/src/math/scalblnbf16.h
new file mode 100644
index 0000000000000..10aad05fc6b1f
--- /dev/null
+++ b/libc/src/math/scalblnbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for scalblnbf16 -------------------*- 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_SCALBLNBF16_H
+#define LLVM_LIBC_SRC_MATH_SCALBLNBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 scalblnbf16(bfloat16 x, long n);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_SCALBLNBF16_H
diff --git a/libc/src/math/scalbnbf16.h b/libc/src/math/scalbnbf16.h
new file mode 100644
index 0000000000000..67efa338fe630
--- /dev/null
+++ b/libc/src/math/scalbnbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for scalbnbf16 --------------------*- 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_SCALBNBF16_H
+#define LLVM_LIBC_SRC_MATH_SCALBNBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 scalbnbf16(bfloat16 x, int n);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_SCALBNBF16_H
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 166802fd9f9cc..226ef2ee71866 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -4881,6 +4881,22 @@ add_fp_unittest(
     libc.src.__support.FPUtil.normal_float
 )
 
+add_fp_unittest(
+  scalblnbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    scalblnbf16_test.cpp
+  HDRS
+    ScalbnTest.h
+  DEPENDS
+    libc.src.math.scalblnbf16
+    libc.src.__support.CPP.limits
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.normal_float
+)
+
 add_fp_unittest(
   scalbn_test
   SUITE
@@ -4956,6 +4972,22 @@ add_fp_unittest(
     libc.src.__support.FPUtil.normal_float
 )
 
+add_fp_unittest(
+  scalbnbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    scalbnbf16_test.cpp
+  HDRS
+    ScalbnTest.h
+  DEPENDS
+    libc.src.math.scalbnbf16
+    libc.src.__support.CPP.limits
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.normal_float
+)
+
 add_fp_unittest(
   erff_test
   SUITE
diff --git a/libc/test/src/math/smoke/scalblnbf16_test.cpp b/libc/test/src/math/smoke/scalblnbf16_test.cpp
new file mode 100644
index 0000000000000..c6fc3aac5e744
--- /dev/null
+++ b/libc/test/src/math/smoke/scalblnbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for scalblnbf16 -----------------------------------------===//
+//
+// 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 "ScalbnTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/scalblnbf16.h"
+
+LIST_SCALBN_TESTS(bfloat16, long, LIBC_NAMESPACE::scalblnbf16)
diff --git a/libc/test/src/math/smoke/scalbnbf16_test.cpp b/libc/test/src/math/smoke/scalbnbf16_test.cpp
new file mode 100644
index 0000000000000..855fd75e6e8f9
--- /dev/null
+++ b/libc/test/src/math/smoke/scalbnbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for scalbnbf16 ------------------------------------------===//
+//
+// 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 "ScalbnTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/scalbnbf16.h"
+
+LIST_SCALBN_TESTS(bfloat16, int, LIBC_NAMESPACE::scalbnbf16)

@llvmbot
Copy link
Member

llvmbot commented Aug 27, 2025

@llvm/pr-subscribers-backend-amdgpu

Author: Krishna Pandey (krishna2803)

Changes

This PR adds the following basic math functions for BFloat16 type along with the tests:

  • scalbnbf16
  • scalblnbf16

Full diff: https://github.com/llvm/llvm-project/pull/155569.diff

22 Files Affected:

  • (modified) libc/config/baremetal/aarch64/entrypoints.txt (+2)
  • (modified) libc/config/baremetal/arm/entrypoints.txt (+2)
  • (modified) libc/config/baremetal/riscv/entrypoints.txt (+2)
  • (modified) libc/config/darwin/aarch64/entrypoints.txt (+2)
  • (modified) libc/config/darwin/x86_64/entrypoints.txt (+2)
  • (modified) libc/config/gpu/amdgpu/entrypoints.txt (+2)
  • (modified) libc/config/gpu/nvptx/entrypoints.txt (+2)
  • (modified) libc/config/linux/aarch64/entrypoints.txt (+2)
  • (modified) libc/config/linux/arm/entrypoints.txt (+2)
  • (modified) libc/config/linux/riscv/entrypoints.txt (+2)
  • (modified) libc/config/linux/x86_64/entrypoints.txt (+2)
  • (modified) libc/config/windows/entrypoints.txt (+2)
  • (modified) libc/docs/headers/math/index.rst (+2-2)
  • (modified) libc/src/math/CMakeLists.txt (+2)
  • (modified) libc/src/math/generic/CMakeLists.txt (+28)
  • (added) libc/src/math/generic/scalblnbf16.cpp (+21)
  • (added) libc/src/math/generic/scalbnbf16.cpp (+21)
  • (added) libc/src/math/scalblnbf16.h (+21)
  • (added) libc/src/math/scalbnbf16.h (+21)
  • (modified) libc/test/src/math/smoke/CMakeLists.txt (+32)
  • (added) libc/test/src/math/smoke/scalblnbf16_test.cpp (+14)
  • (added) libc/test/src/math/smoke/scalbnbf16_test.cpp (+14)
diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index ff3a6bfa8a2fc..637be4f19d5b7 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -814,6 +814,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.rintbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.scalblnbf16
+  libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index f8916d71f86aa..19aedb0a8677d 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -817,6 +817,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.rintbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.scalblnbf16
+  libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 8a72fa6692869..cfbb598cbe4bf 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -817,6 +817,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.rintbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.scalblnbf16
+  libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 796c0d3c8a4ff..26e97d8993039 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -647,6 +647,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.rintbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.scalblnbf16
+  libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index fa1d8def8936c..0556a6f7a4715 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -290,6 +290,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.rintbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.scalblnbf16
+  libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index 49c41411cb5d2..8817972480aac 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -673,6 +673,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.rintbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.scalblnbf16
+  libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index 06a2c9addb7fb..db762ed5c74d6 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -675,6 +675,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.rintbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.scalblnbf16
+  libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index feea7ff69c166..0a2ebfa3f8720 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -902,6 +902,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.rintbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.scalblnbf16
+  libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 8e92fac863c6b..ea1c5c0558272 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -517,6 +517,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.rintbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.scalblnbf16
+  libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index fb08cfe7bd3f8..d5180b77c3af0 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -920,6 +920,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.rintbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.scalblnbf16
+  libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 37af75454daa3..c64db2cc3548f 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -953,6 +953,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.rintbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.scalblnbf16
+  libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index c09e6b259fa42..9f825bdf657e9 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -363,6 +363,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.rintbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.scalblnbf16
+  libc.src.math.scalbnbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index 89624460c3af5..a707b37894afc 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -233,9 +233,9 @@ Basic Operations
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | roundeven        | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.9.8               | F.10.6.8                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| scalbln          | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.6.19              | F.10.3.19                  |
+| scalbln          | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.6.19              | F.10.3.19                  |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| scalbn           | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.6.19              | F.10.3.19                  |
+| scalbn           | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.6.19              | F.10.3.19                  |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | setpayload       | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | F.10.13.2              | N/A                        |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 1edb517421e28..4b325f773b35f 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -518,12 +518,14 @@ add_math_entrypoint_object(scalblnf)
 add_math_entrypoint_object(scalblnl)
 add_math_entrypoint_object(scalblnf16)
 add_math_entrypoint_object(scalblnf128)
+add_math_entrypoint_object(scalblnbf16)
 
 add_math_entrypoint_object(scalbn)
 add_math_entrypoint_object(scalbnf)
 add_math_entrypoint_object(scalbnl)
 add_math_entrypoint_object(scalbnf16)
 add_math_entrypoint_object(scalbnf128)
+add_math_entrypoint_object(scalbnbf16)
 
 add_math_entrypoint_object(setpayload)
 add_math_entrypoint_object(setpayloadf)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 904f8d2eb4704..b1fcbf356777a 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4646,6 +4646,20 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.manipulation_functions
 )
 
+add_entrypoint_object(
+  scalblnbf16
+  SRCS
+    scalblnbf16.cpp
+  HDRS
+    ../scalblnbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.manipulation_functions
+)
+
 add_entrypoint_object(
   scalbn
   SRCS
@@ -4703,6 +4717,20 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.manipulation_functions
 )
 
+add_entrypoint_object(
+  scalbnbf16
+  SRCS
+    scalbnbf16.cpp
+  HDRS
+    ../scalblnbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.manipulation_functions
+)
+
 add_entrypoint_object(
   fmaf
   SRCS
diff --git a/libc/src/math/generic/scalblnbf16.cpp b/libc/src/math/generic/scalblnbf16.cpp
new file mode 100644
index 0000000000000..cda8e126efacb
--- /dev/null
+++ b/libc/src/math/generic/scalblnbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of scalblnbf16 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/scalblnbf16.h"
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, scalblnbf16, (bfloat16 x, long n)) {
+  return fputil::ldexp(x, n);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/scalbnbf16.cpp b/libc/src/math/generic/scalbnbf16.cpp
new file mode 100644
index 0000000000000..87833742d59ac
--- /dev/null
+++ b/libc/src/math/generic/scalbnbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of scalbnbf16 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/scalbnbf16.h"
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, scalbnbf16, (bfloat16 x, int n)) {
+  return fputil::ldexp(x, n);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/scalblnbf16.h b/libc/src/math/scalblnbf16.h
new file mode 100644
index 0000000000000..10aad05fc6b1f
--- /dev/null
+++ b/libc/src/math/scalblnbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for scalblnbf16 -------------------*- 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_SCALBLNBF16_H
+#define LLVM_LIBC_SRC_MATH_SCALBLNBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 scalblnbf16(bfloat16 x, long n);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_SCALBLNBF16_H
diff --git a/libc/src/math/scalbnbf16.h b/libc/src/math/scalbnbf16.h
new file mode 100644
index 0000000000000..67efa338fe630
--- /dev/null
+++ b/libc/src/math/scalbnbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for scalbnbf16 --------------------*- 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_SCALBNBF16_H
+#define LLVM_LIBC_SRC_MATH_SCALBNBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 scalbnbf16(bfloat16 x, int n);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_SCALBNBF16_H
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 166802fd9f9cc..226ef2ee71866 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -4881,6 +4881,22 @@ add_fp_unittest(
     libc.src.__support.FPUtil.normal_float
 )
 
+add_fp_unittest(
+  scalblnbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    scalblnbf16_test.cpp
+  HDRS
+    ScalbnTest.h
+  DEPENDS
+    libc.src.math.scalblnbf16
+    libc.src.__support.CPP.limits
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.normal_float
+)
+
 add_fp_unittest(
   scalbn_test
   SUITE
@@ -4956,6 +4972,22 @@ add_fp_unittest(
     libc.src.__support.FPUtil.normal_float
 )
 
+add_fp_unittest(
+  scalbnbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    scalbnbf16_test.cpp
+  HDRS
+    ScalbnTest.h
+  DEPENDS
+    libc.src.math.scalbnbf16
+    libc.src.__support.CPP.limits
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.normal_float
+)
+
 add_fp_unittest(
   erff_test
   SUITE
diff --git a/libc/test/src/math/smoke/scalblnbf16_test.cpp b/libc/test/src/math/smoke/scalblnbf16_test.cpp
new file mode 100644
index 0000000000000..c6fc3aac5e744
--- /dev/null
+++ b/libc/test/src/math/smoke/scalblnbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for scalblnbf16 -----------------------------------------===//
+//
+// 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 "ScalbnTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/scalblnbf16.h"
+
+LIST_SCALBN_TESTS(bfloat16, long, LIBC_NAMESPACE::scalblnbf16)
diff --git a/libc/test/src/math/smoke/scalbnbf16_test.cpp b/libc/test/src/math/smoke/scalbnbf16_test.cpp
new file mode 100644
index 0000000000000..855fd75e6e8f9
--- /dev/null
+++ b/libc/test/src/math/smoke/scalbnbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for scalbnbf16 ------------------------------------------===//
+//
+// 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 "ScalbnTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/scalbnbf16.h"
+
+LIST_SCALBN_TESTS(bfloat16, int, LIBC_NAMESPACE::scalbnbf16)

Signed-off-by: Krishna Pandey <kpandey81930@gmail.com>
Base automatically changed from users/krishna2803/totalorderbf16 to main August 28, 2025 14:07
@krishna2803 krishna2803 merged commit a985be6 into main Aug 28, 2025
20 checks passed
@krishna2803 krishna2803 deleted the users/krishna2803/scalbnbf16 branch August 28, 2025 14:59
rupprecht added a commit that referenced this pull request Aug 28, 2025
These are redefined/shadowed by the `if constexpr (FPBits::FRACTION_LEN
- 1 >= 5)` case below.

Added by #155569
rupprecht added a commit that referenced this pull request Aug 28, 2025
These are redefined/shadowed by the if constexpr (FPBits::FRACTION_LEN -
1 >= 5) case below.

Added by #155569. I missed
these ones in #155894.
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Aug 28, 2025
These are redefined/shadowed by the if constexpr (FPBits::FRACTION_LEN -
1 >= 5) case below.

Added by llvm/llvm-project#155569. I missed
these ones in #155894.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants