Skip to content

[libc][math][c++23] Add bfloat16 support in LLVM libc #144463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: main
Choose a base branch
from

Conversation

krishna2803
Copy link
Contributor

This PR enables support for BFloat16 type in LLVM libc along with support for testing BFloat16 functions via MPFR.

cc @overmighty @lntue

krishna2803 and others added 22 commits April 27, 2025 11:35
Signed-off-by: krishna2803 <kpandey81930@gmail.com>
Signed-off-by: krishna2803 <kpandey81930@gmail.com>
Signed-off-by: krishna2803 <kpandey81930@gmail.com>
…_type

Signed-off-by: krishna2803 <kpandey81930@gmail.com>
…n type

Signed-off-by: krishna2803 <kpandey81930@gmail.com>
Signed-off-by: krishna2803 <kpandey81930@gmail.com>
Signed-off-by: krishna2803 <kpandey81930@gmail.com>
Signed-off-by: krishna2803 <kpandey81930@gmail.com>
Signed-off-by: krishna2803 <kpandey81930@gmail.com>
Signed-off-by: krishna2803 <kpandey81930@gmail.com>
Signed-off-by: krishna2803 <kpandey81930@gmail.com>
Signed-off-by: krishna2803 <kpandey81930@gmail.com>
…d headers

Signed-off-by: krishna2803 <kpandey81930@gmail.com>
Signed-off-by: krishna2803 <kpandey81930@gmail.com>
Signed-off-by: krishna2803 <kpandey81930@gmail.com>
Signed-off-by: krishna2803 <kpandey81930@gmail.com>
…lization for MPFRNumber

Signed-off-by: krishna2803 <kpandey81930@gmail.com>
Signed-off-by: krishna2803 <kpandey81930@gmail.com>
Signed-off-by: krishna2803 <kpandey81930@gmail.com>
Signed-off-by: krishna2803 <kpandey81930@gmail.com>
Signed-off-by: krishna2803 <kpandey81930@gmail.com>
Copy link

github-actions bot commented Jun 17, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff HEAD~1 HEAD --extensions h,cpp -- libc/cmake/modules/compiler_features/check_bfloat16.cpp libc/src/__support/FPUtil/bfloat16.h libc/test/src/__support/FPUtil/bfloat16_test.cpp libc/src/__support/CPP/type_traits/is_floating_point.h libc/src/__support/FPUtil/FPBits.h libc/src/__support/FPUtil/cast.h libc/src/__support/FPUtil/dyadic_float.h libc/src/__support/macros/properties/types.h libc/utils/MPFRWrapper/MPCommon.cpp libc/utils/MPFRWrapper/MPCommon.h
View the diff from clang-format here.
diff --git a/libc/test/src/__support/FPUtil/bfloat16_test.cpp b/libc/test/src/__support/FPUtil/bfloat16_test.cpp
index 3bfabf3fd..14f06c305 100644
--- a/libc/test/src/__support/FPUtil/bfloat16_test.cpp
+++ b/libc/test/src/__support/FPUtil/bfloat16_test.cpp
@@ -24,7 +24,6 @@ static constexpr uint16_t POS_STOP = 0x7f80U;
 static constexpr uint16_t NEG_START = 0x8000U;
 static constexpr uint16_t NEG_STOP = 0xff80;
 
-
 using MPFRNumber = LIBC_NAMESPACE::testing::mpfr::MPFRNumber;
 
 // TODO: better naming?
@@ -32,7 +31,7 @@ TEST_F(LlvmLibcBfloat16ExhaustiveTest, PositiveRange) {
   for (uint16_t bits = POS_START; bits <= POS_STOP; bits++) {
     bfloat16 bf16_num{bits};
     MPFRNumber mpfr_num{bf16_num};
-    
+
     // bfloat16 to float
     float mpfr_float = mpfr_num.as<float>();
     ASSERT_FP_EQ(mpfr_float, bf16_num.as_float());
@@ -49,7 +48,7 @@ TEST_F(LlvmLibcBfloat16ExhaustiveTest, NegativeRange) {
   for (uint16_t bits = NEG_START; bits <= NEG_STOP; bits++) {
     bfloat16 bf16_num{bits};
     MPFRNumber mpfr_num{bf16_num};
-    
+
     // bfloat16 to float
     float mpfr_float = mpfr_num.as<float>();
     ASSERT_FP_EQ(mpfr_float, bf16_num.as_float());

Signed-off-by: krishna2803 <kpandey81930@gmail.com>
@lntue lntue requested review from overmighty and lntue June 17, 2025 03:27
@@ -15,6 +15,7 @@ set(
"fixed_point"
"cfloat16"
"cfloat128"
"bfloat16"
Copy link
Contributor

Choose a reason for hiding this comment

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

You can leave the cmake detection changes to other PR:

  CheckCompilerFeatures.cmake
  check_bfloat16.cpp
  entrypoints.txt

}
}

constexpr bool operator==(const BFloat16 other) const {
Copy link
Contributor

Choose a reason for hiding this comment

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

we should implement floating point equality of the same type in FPBits, and it should follow IEEE 754 rules for equality (aka not bit identical in some cases).

Copy link
Contributor

@lntue lntue left a comment

Choose a reason for hiding this comment

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

Add unit tests to make sure the class and the MPFR utility work.

Signed-off-by: krishna2803 <kpandey81930@gmail.com>
Signed-off-by: krishna2803 <kpandey81930@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants