Skip to content

Conversation

krishna2803
Copy link
Contributor

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

  • getpayloadbf16
  • setpayloadbf16
  • setpayloadsigbf16

Signed-off-by: Krishna Pandey <[email protected]>
Signed-off-by: Krishna Pandey <[email protected]>
Signed-off-by: Krishna Pandey <[email protected]>
Signed-off-by: Krishna Pandey <[email protected]>
@llvmbot
Copy link
Member

llvmbot commented Aug 17, 2025

@llvm/pr-subscribers-backend-amdgpu
@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:

  • getpayloadbf16
  • setpayloadbf16
  • setpayloadsigbf16

Patch is 33.42 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/153994.diff

29 Files Affected:

  • (modified) libc/config/baremetal/aarch64/entrypoints.txt (+3)
  • (modified) libc/config/baremetal/arm/entrypoints.txt (+3)
  • (modified) libc/config/baremetal/riscv/entrypoints.txt (+3)
  • (modified) libc/config/darwin/aarch64/entrypoints.txt (+3)
  • (modified) libc/config/darwin/x86_64/entrypoints.txt (+3)
  • (modified) libc/config/gpu/amdgpu/entrypoints.txt (+3)
  • (modified) libc/config/gpu/nvptx/entrypoints.txt (+3)
  • (modified) libc/config/linux/aarch64/entrypoints.txt (+3)
  • (modified) libc/config/linux/arm/entrypoints.txt (+3)
  • (modified) libc/config/linux/riscv/entrypoints.txt (+3)
  • (modified) libc/config/linux/x86_64/entrypoints.txt (+3)
  • (modified) libc/config/windows/entrypoints.txt (+3)
  • (modified) libc/docs/headers/math/index.rst (+3-3)
  • (modified) libc/src/__support/FPUtil/BasicOperations.h (+4-1)
  • (modified) libc/src/math/CMakeLists.txt (+3)
  • (modified) libc/src/math/generic/CMakeLists.txt (+42)
  • (added) libc/src/math/generic/getpayloadbf16.cpp (+21)
  • (added) libc/src/math/generic/setpayloadbf16.cpp (+21)
  • (added) libc/src/math/generic/setpayloadsigbf16.cpp (+21)
  • (added) libc/src/math/getpayloadbf16.h (+21)
  • (added) libc/src/math/setpayloadbf16.h (+21)
  • (added) libc/src/math/setpayloadsigbf16.h (+21)
  • (modified) libc/test/src/math/smoke/CMakeLists.txt (+38)
  • (modified) libc/test/src/math/smoke/GetPayloadTest.h (+44-17)
  • (modified) libc/test/src/math/smoke/SetPayloadSigTest.h (+25-9)
  • (modified) libc/test/src/math/smoke/SetPayloadTest.h (+25-9)
  • (added) libc/test/src/math/smoke/getpayloadbf16_test.cpp (+14)
  • (added) libc/test/src/math/smoke/setpayloadbf16_test.cpp (+14)
  • (added) libc/test/src/math/smoke/setpayloadsigbf16_test.cpp (+14)
diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index 26ee82d99192f..5f72793beecb4 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -787,12 +787,15 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.getpayloadbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.setpayloadbf16
+  libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 00025d324fc2c..244dd2e87b1fd 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -790,12 +790,15 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.getpayloadbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.setpayloadbf16
+  libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index c0ab0cf903c41..32980c4a1d222 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -790,12 +790,15 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.getpayloadbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.setpayloadbf16
+  libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index cd81756770ed4..3684713474cbc 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -620,12 +620,15 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.getpayloadbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.setpayloadbf16
+  libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index 3aa54e027a42d..7482a80c37d33 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -263,12 +263,15 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.getpayloadbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.setpayloadbf16
+  libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index 3b42c0fd71547..a88c74be2997a 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -646,12 +646,15 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.getpayloadbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.setpayloadbf16
+  libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index b569327409849..08e6c7dc34b07 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -647,12 +647,15 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.getpayloadbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.setpayloadbf16
+  libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 4058e7155f34a..ea7e2065b2216 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -874,12 +874,15 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.getpayloadbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.setpayloadbf16
+  libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 9002bd12d6733..8f21976e9e88e 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -490,12 +490,15 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.getpayloadbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.setpayloadbf16
+  libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 77d5bae188a3c..69d7519118198 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -893,12 +893,15 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.getpayloadbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.setpayloadbf16
+  libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index b811fe79c174d..75dcfd6382f1b 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -925,12 +925,15 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.getpayloadbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.setpayloadbf16
+  libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index ec2fde253e898..541d9fee1adf5 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -336,12 +336,15 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_numbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
+  libc.src.math.getpayloadbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.setpayloadbf16
+  libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index 591295659c3d4..d8921b985b1f2 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -187,7 +187,7 @@ Basic Operations
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | fsub             | N/A              | |check|         | |check|                | N/A                  | |check|\*              | N/A                    | 7.12.14.2              | F.10.11                    |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| getpayload       | |check|          | |check|         | |check|                | |check|              | |check|                |                        | F.10.13.1              | N/A                        |
+| getpayload       | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | F.10.13.1              | N/A                        |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | ilogb            | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.6.8               | F.10.3.8                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
@@ -237,9 +237,9 @@ Basic Operations
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | scalbn           | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.6.19              | F.10.3.19                  |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| setpayload       | |check|          | |check|         | |check|                | |check|              | |check|                |                        | F.10.13.2              | N/A                        |
+| setpayload       | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | F.10.13.2              | N/A                        |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| setpayloadsig    | |check|          | |check|         | |check|                | |check|              | |check|                |                        | F.10.13.3              | N/A                        |
+| setpayloadsig    | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | F.10.13.3              | N/A                        |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | totalorder       | |check|          | |check|         | |check|                | |check|              | |check|                |                        | F.10.12.1              | N/A                        |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/src/__support/FPUtil/BasicOperations.h b/libc/src/__support/FPUtil/BasicOperations.h
index 2357b053b60b8..994237ba8492e 100644
--- a/libc/src/__support/FPUtil/BasicOperations.h
+++ b/libc/src/__support/FPUtil/BasicOperations.h
@@ -354,7 +354,10 @@ LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> getpayload(T x) {
 
     return static_cast<T>(payload_dfloat);
   } else {
-    return static_cast<T>(payload);
+    if constexpr (cpp::is_same_v<T, bfloat16>)
+      return T(static_cast<int>(payload));
+    else
+      return static_cast<T>(payload);
   }
 }
 
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 3843247c4fa5b..023829b21996f 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -330,6 +330,7 @@ add_math_entrypoint_object(getpayloadf)
 add_math_entrypoint_object(getpayloadl)
 add_math_entrypoint_object(getpayloadf16)
 add_math_entrypoint_object(getpayloadf128)
+add_math_entrypoint_object(getpayloadbf16)
 
 add_math_entrypoint_object(hypot)
 add_math_entrypoint_object(hypotf)
@@ -507,12 +508,14 @@ add_math_entrypoint_object(setpayloadf)
 add_math_entrypoint_object(setpayloadl)
 add_math_entrypoint_object(setpayloadf16)
 add_math_entrypoint_object(setpayloadf128)
+add_math_entrypoint_object(setpayloadbf16)
 
 add_math_entrypoint_object(setpayloadsig)
 add_math_entrypoint_object(setpayloadsigf)
 add_math_entrypoint_object(setpayloadsigl)
 add_math_entrypoint_object(setpayloadsigf16)
 add_math_entrypoint_object(setpayloadsigf128)
+add_math_entrypoint_object(setpayloadsigbf16)
 
 add_math_entrypoint_object(sincos)
 add_math_entrypoint_object(sincosf)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 822da74d7597e..38ba34ae521ea 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4609,6 +4609,20 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.basic_operations
 )
 
+add_entrypoint_object(
+  getpayloadbf16
+  SRCS
+    getpayloadbf16.cpp
+  HDRS
+    ../getpayloadbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
 add_entrypoint_object(
   setpayload
   SRCS
@@ -4661,6 +4675,20 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.basic_operations
 )
 
+add_entrypoint_object(
+  setpayloadbf16
+  SRCS
+    setpayloadbf16.cpp
+  HDRS
+    ../setpayloadbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
 add_entrypoint_object(
   setpayloadsig
   SRCS
@@ -4713,6 +4741,20 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.basic_operations
 )
 
+add_entrypoint_object(
+  setpayloadsigbf16
+  SRCS
+    setpayloadsigbf16.cpp
+  HDRS
+    ../setpayloadsigbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
 add_entrypoint_object(
   f16add
   SRCS
diff --git a/libc/src/math/generic/getpayloadbf16.cpp b/libc/src/math/generic/getpayloadbf16.cpp
new file mode 100644
index 0000000000000..544ed0a2f5c9d
--- /dev/null
+++ b/libc/src/math/generic/getpayloadbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of getpayloadbf16 function -------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/getpayloadbf16.h"
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, getpayloadbf16, (const bfloat16 *x)) {
+  return fputil::getpayload(*x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/setpayloadbf16.cpp b/libc/src/math/generic/setpayloadbf16.cpp
new file mode 100644
index 0000000000000..49f9b9cabd6a2
--- /dev/null
+++ b/libc/src/math/generic/setpayloadbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of setpayloadbf16 function -------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/setpayloadbf16.h"
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, setpayloadbf16, (bfloat16 * res, bfloat16 pl)) {
+  return static_cast<int>(fputil::setpayload</*IsSignaling=*/false>(*res, pl));
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/setpayloadsigbf16.cpp b/libc/src/math/generic/setpayloadsigbf16.cpp
new file mode 100644
index 0000000000000..7a2b7c7dc66dd
--- /dev/null
+++ b/libc/src/math/generic/setpayloadsigbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of setpayloadsigbf16 function ----------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/setpayloadsigbf16.h"
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, setpayloadsigbf16, (bfloat16 * res, bfloat16 pl)) {
+  return static_cast<int>(fputil::setpayload</*IsSignaling=*/true>(*res, pl));
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/getpayloadbf16.h b/libc/src/math/getpayloadbf16.h
new file mode 100644
index 0000000000000..e4767f0728139
--- /dev/null
+++ b/libc/src/math/getpayloadbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for getpayloadbf16 ----------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_GETPAYLOADBF16_H
+#define LLVM_LIBC_SRC_MATH_GETPAYLOADBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 getpayloadb...
[truncated]

@overmighty overmighty requested review from overmighty and lntue August 18, 2025 21:14
Comment on lines 357 to 360
return static_cast<T>(payload);
if constexpr (cpp::is_same_v<T, bfloat16>)
return T(static_cast<int>(payload));
else
return static_cast<T>(payload);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have the BFloat16(uint16_t bits) constructor again? Removing it makes this change unnecessary and libc-math-smoke-tests and libc.test.src.__support.FPUtil.bfloat16_test.__unit__ still pass.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added it in case we might need to construct bfloat16 directly using uint16 values to be able to set its bits directly, but I think it can be removed as we don't use it anywhere. I will run all tests locally to confirm this.

EXPECT_FP_EQ(T(0x123.0p+0), funcWrapper(func, neg_qnan_123));
EXPECT_FP_EQ(T(0x123.0p+0), funcWrapper(func, snan_123));
EXPECT_FP_EQ(T(0x123.0p+0), funcWrapper(func, neg_snan_123));
if constexpr (FPBits::FRACTION_LEN - 1 >= 6) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: it's a bit weird that you guard both 6-bit and 5-bit payload test cases with a single FRACTION_LEN - 1 >= 6 check but you guard 7-bit and 9-bit payload test cases with separate FRACTION_LEN - 1 >= 7 and >= 9 checks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added another if for 5 bits in b310e151

Base automatically changed from users/krishna2803/nextfnbf16 to main August 22, 2025 16:31
@krishna2803 krishna2803 merged commit de6bd15 into main Aug 22, 2025
20 checks passed
@krishna2803 krishna2803 deleted the users/krishna2803/payloadbf16 branch August 22, 2025 19:32
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