[libc-commits] [libc] [libc][c++23][math] Add fabsbf16 math function (PR #148398)
Krishna Pandey via libc-commits
libc-commits at lists.llvm.org
Tue Jul 22 10:08:37 PDT 2025
https://github.com/krishna2803 updated https://github.com/llvm/llvm-project/pull/148398
>From e4135e32b39af9e7faa2c66df8f712aeeee5ef6b Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Wed, 4 Jun 2025 04:20:10 +0530
Subject: [PATCH 01/18] add: FPType::BFloat16
---
.../llvm-libc-macros/bfloat16-macros.h | 21 +++++++++++++++++++
1 file changed, 21 insertions(+)
create mode 100644 libc/include/llvm-libc-macros/bfloat16-macros.h
diff --git a/libc/include/llvm-libc-macros/bfloat16-macros.h b/libc/include/llvm-libc-macros/bfloat16-macros.h
new file mode 100644
index 0000000000000..5b52f73efbadb
--- /dev/null
+++ b/libc/include/llvm-libc-macros/bfloat16-macros.h
@@ -0,0 +1,21 @@
+//===-- Detection of __bf16 compiler builtin type -------------------------===//
+//
+// 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_MACROS_BFLOAT16_MACROS_H
+#define LLVM_LIBC_MACROS_BFLOAT16_MACROS_H
+
+#if ((defined(__clang__) && __clang_major__ > 17) || \
+ (defined(__GNUC__) && __GNUC__ > 13)) && \
+ !defined(__arm__) && !defined(_M_ARM) && !defined(__riscv) && \
+ !defined(_WIN32)
+
+#define LIBC_TYPES_HAS_BFLOAT16
+
+#endif // LIBC_TYPES_HAS_BFLOAT16
+
+#endif // LLVM_LIBC_MACROS_BFLOAT16_MACROS_H
>From 6cd97e43c0b7a1b64cc640f1e30eb8bf41898e9c Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Thu, 5 Jun 2025 05:02:18 +0530
Subject: [PATCH 02/18] chore: remove compiler version checks and add bfloat16
to is_floating_type
---
libc/include/llvm-libc-macros/bfloat16-macros.h | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/libc/include/llvm-libc-macros/bfloat16-macros.h b/libc/include/llvm-libc-macros/bfloat16-macros.h
index 5b52f73efbadb..466432b990a32 100644
--- a/libc/include/llvm-libc-macros/bfloat16-macros.h
+++ b/libc/include/llvm-libc-macros/bfloat16-macros.h
@@ -9,13 +9,15 @@
#ifndef LLVM_LIBC_MACROS_BFLOAT16_MACROS_H
#define LLVM_LIBC_MACROS_BFLOAT16_MACROS_H
-#if ((defined(__clang__) && __clang_major__ > 17) || \
- (defined(__GNUC__) && __GNUC__ > 13)) && \
- !defined(__arm__) && !defined(_M_ARM) && !defined(__riscv) && \
- !defined(_WIN32)
+// #if ((defined(__clang__) && __clang_major__ > 17) || \
+// (defined(__GNUC__) && __GNUC__ > 13)) && \
+// !defined(__arm__) && !defined(_M_ARM) && !defined(__riscv) && \
+// !defined(_WIN32)
#define LIBC_TYPES_HAS_BFLOAT16
-#endif // LIBC_TYPES_HAS_BFLOAT16
+struct BFloat16;
+
+// #endif // LIBC_TYPES_HAS_BFLOAT16
#endif // LLVM_LIBC_MACROS_BFLOAT16_MACROS_H
>From 0ddfaedba0b16576c3f1935cc92118a9bcca7cee Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Thu, 5 Jun 2025 07:07:19 +0530
Subject: [PATCH 03/18] feat: update MPCommon.h for bfloat16 type and change
__bf16 to our own type
---
libc/utils/MPFRWrapper/MPCommon.h | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/libc/utils/MPFRWrapper/MPCommon.h b/libc/utils/MPFRWrapper/MPCommon.h
index af03ff054e1a9..e19de5674d470 100644
--- a/libc/utils/MPFRWrapper/MPCommon.h
+++ b/libc/utils/MPFRWrapper/MPCommon.h
@@ -139,6 +139,19 @@ class MPFRNumber {
mpfr_set_d(value, x, mpfr_rounding);
}
+#ifdef LIBC_TYPES_HAS_BFLOAT16
+ template <typename XType,
+ cpp::enable_if_t<cpp::is_same_v<bfloat16, XType>, int> = 0>
+ explicit MPFRNumber(XType x,
+ unsigned int precision = 8,
+ RoundingMode rounding = RoundingMode::Nearest)
+ : mpfr_precision(precision),
+ mpfr_rounding(get_mpfr_rounding_mode(rounding)) {
+ mpfr_init2(value, mpfr_precision);
+ mpfr_set_flt(value, x.as_float(), mpfr_rounding);
+ }
+#endif
+
template <typename XType,
cpp::enable_if_t<cpp::is_same_v<long double, XType>, int> = 0>
explicit MPFRNumber(XType x,
>From 7aac686f4500d75515b46d2170c4d902858cc32d Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Fri, 6 Jun 2025 12:50:34 +0530
Subject: [PATCH 04/18] feat: add BFloat16 checks in compiler features
Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
libc/cmake/modules/CheckCompilerFeatures.cmake | 3 +++
libc/cmake/modules/compiler_features/check_bfloat16.cpp | 5 +++++
2 files changed, 8 insertions(+)
create mode 100644 libc/cmake/modules/compiler_features/check_bfloat16.cpp
diff --git a/libc/cmake/modules/CheckCompilerFeatures.cmake b/libc/cmake/modules/CheckCompilerFeatures.cmake
index a5ea66a5935b7..6db69bd5c4895 100644
--- a/libc/cmake/modules/CheckCompilerFeatures.cmake
+++ b/libc/cmake/modules/CheckCompilerFeatures.cmake
@@ -15,6 +15,7 @@ set(
"fixed_point"
"cfloat16"
"cfloat128"
+ "bfloat16"
)
# Making sure ALL_COMPILER_FEATURES is sorted.
@@ -116,6 +117,8 @@ foreach(feature IN LISTS ALL_COMPILER_FEATURES)
set(LIBC_TYPES_HAS_CFLOAT16 TRUE)
elseif(${feature} STREQUAL "cfloat128")
set(LIBC_TYPES_HAS_CFLOAT128 TRUE)
+ elseif(${feature} STREQUAL "bfloat16")
+ set(LIBC_TYPES_HAS_BFLOAT16 TRUE)
elseif(${feature} STREQUAL "builtin_ceil_floor_rint_trunc")
set(LIBC_COMPILER_HAS_BUILTIN_CEIL_FLOOR_RINT_TRUNC TRUE)
elseif(${feature} STREQUAL "builtin_fmax_fmin")
diff --git a/libc/cmake/modules/compiler_features/check_bfloat16.cpp b/libc/cmake/modules/compiler_features/check_bfloat16.cpp
new file mode 100644
index 0000000000000..1fcb73b01eb9c
--- /dev/null
+++ b/libc/cmake/modules/compiler_features/check_bfloat16.cpp
@@ -0,0 +1,5 @@
+#include "src/__support/macros/properties/types.h"
+
+#ifndef LIBC_TYPES_HAS_BFLOAT16
+#error unsupported
+#endif
>From e35655cffe94f021efda696d48c0ebaf84a7ff3e Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Mon, 9 Jun 2025 13:12:17 +0530
Subject: [PATCH 05/18] feat: add dummy bfloat16 -> float conversion function
Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
libc/src/math/fbfloat16.h | 22 ++++++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 14 ++++++++++++++
libc/src/math/generic/fbfloat16.cpp | 25 +++++++++++++++++++++++++
3 files changed, 61 insertions(+)
create mode 100644 libc/src/math/fbfloat16.h
create mode 100644 libc/src/math/generic/fbfloat16.cpp
diff --git a/libc/src/math/fbfloat16.h b/libc/src/math/fbfloat16.h
new file mode 100644
index 0000000000000..5b1b04b24a563
--- /dev/null
+++ b/libc/src/math/fbfloat16.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for fbfloat16 ---------------------*- 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_FBFLOAT16_H
+#define LLVM_LIBC_SRC_MATH_FBFLOAT16_H
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float fbfloat16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_FBFLOAT16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index fd1e6c0d648aa..2802c82ad9607 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5070,6 +5070,20 @@ add_entrypoint_object(
libc.src.__support.FPUtil.generic.mul
)
+# Bfloat16 Functions
+
+add_entrypoint_object(
+ fbfloat16
+ SRCS
+ fbfloat16.cpp
+ HDRS
+ ../fbfloat16.h
+ DEPENDS
+ libc.src.__support.macros.config
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.bfloat16
+)
+
add_header_library(
expxf16
HDRS
diff --git a/libc/src/math/generic/fbfloat16.cpp b/libc/src/math/generic/fbfloat16.cpp
new file mode 100644
index 0000000000000..d42a486354e64
--- /dev/null
+++ b/libc/src/math/generic/fbfloat16.cpp
@@ -0,0 +1,25 @@
+//===-- Implementation of fbfloat16 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/fbfloat16.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float, fbfloat16, (LIBC_NAMESPACE::bfloat16 x)) {
+ if (x.bits == 0)
+ return 0.0f;
+ else
+ return 1.0f;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
>From 79749034442e93444e4f69be4482829d5b18826f Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Mon, 9 Jun 2025 13:15:49 +0530
Subject: [PATCH 06/18] chore: add entrypoints for fbfloat16 function
Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
libc/config/linux/x86_64/entrypoints.txt | 7 +++++++
libc/src/math/CMakeLists.txt | 3 +++
2 files changed, 10 insertions(+)
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index edad30634b6da..df1922172c8ae 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -883,6 +883,13 @@ if(LIBC_TYPES_HAS_FLOAT128)
)
endif()
+if(LIBC_TYPES_HAS_BFLOAT16)
+ list(APPEND TARGET_LIBM_ENTRYPOINTS
+ # Bfloat16 Entrypoints
+ libc.src.math.fbfloat16
+ )
+endif()
+
if(LIBC_COMPILER_HAS_FIXED_POINT)
list(APPEND TARGET_LIBM_ENTRYPOINTS
# stdfix.h _Fract and _Accum entrypoints
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index b27b0d2b523f8..57b72d6e712f6 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -555,3 +555,6 @@ add_math_entrypoint_object(ufromfpxf)
add_math_entrypoint_object(ufromfpxl)
add_math_entrypoint_object(ufromfpxf16)
add_math_entrypoint_object(ufromfpxf128)
+
+# BFloat16 functions
+add_math_entrypoint_object(fbfloat16)
>From 8aacba633f888bdbbfd556b60aa3f0c0fe47ce57 Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Mon, 9 Jun 2025 18:32:40 +0530
Subject: [PATCH 07/18] chore: add dummy tests for fbfloat16
Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
libc/test/src/math/smoke/CMakeLists.txt | 13 ++++++++++++
libc/test/src/math/smoke/fbfloat16_test.cpp | 23 +++++++++++++++++++++
2 files changed, 36 insertions(+)
create mode 100644 libc/test/src/math/smoke/fbfloat16_test.cpp
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 599939cbec4b5..57fade182b128 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -5318,3 +5318,16 @@ add_fp_unittest(
libc.src.__support.macros.properties.os
libc.src.__support.macros.properties.types
)
+
+# Bfloat16 Smoke Tests
+
+add_fp_unittest(
+ fbfloat16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ fbfloat16_test.cpp
+ DEPENDS
+ libc.src.errno.errno
+ libc.src.math.fbfloat16
+)
diff --git a/libc/test/src/math/smoke/fbfloat16_test.cpp b/libc/test/src/math/smoke/fbfloat16_test.cpp
new file mode 100644
index 0000000000000..8f52fe36aae61
--- /dev/null
+++ b/libc/test/src/math/smoke/fbfloat16_test.cpp
@@ -0,0 +1,23 @@
+//===-- Unittests for fbfloat16 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/fbfloat16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+// TODO: change this to bfloat16
+using LlvmLibcFBfloat16Test = LIBC_NAMESPACE::testing::FPTest<float>;
+
+TEST_F(LlvmLibcFBfloat16Test, SpecialNumbers) {
+ ASSERT_EQ(1, 2);
+ // TODO: implement this!
+ // x = some bfloat number
+ // float y = x as float (using our ctor?)
+ // float z = mfpr(x) as float
+ // check y == z
+}
>From 78a77f63062b6852f6ab6e525b834bcf2fc74133 Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Mon, 9 Jun 2025 18:40:58 +0530
Subject: [PATCH 08/18] chore: update fbfloat16 dummy smoke tests
Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
libc/test/src/math/smoke/fbfloat16_test.cpp | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/libc/test/src/math/smoke/fbfloat16_test.cpp b/libc/test/src/math/smoke/fbfloat16_test.cpp
index 8f52fe36aae61..a63a7e05a8ba5 100644
--- a/libc/test/src/math/smoke/fbfloat16_test.cpp
+++ b/libc/test/src/math/smoke/fbfloat16_test.cpp
@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "src/math/fbfloat16.h"
+
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
@@ -14,7 +15,14 @@
using LlvmLibcFBfloat16Test = LIBC_NAMESPACE::testing::FPTest<float>;
TEST_F(LlvmLibcFBfloat16Test, SpecialNumbers) {
- ASSERT_EQ(1, 2);
+
+ LIBC_NAMESPACE::bfloat16 x{0.0f};
+ ASSERT_EQ(0, static_cast<int>(x.bits));
+
+
+ LIBC_NAMESPACE::bfloat16 y{1.0f};
+ ASSERT_EQ(1, static_cast<int>(y.bits));
+
// TODO: implement this!
// x = some bfloat number
// float y = x as float (using our ctor?)
>From ed3f8ac5f091d1ab11ea377414ef6c8a1686b87a Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Wed, 11 Jun 2025 20:20:44 +0530
Subject: [PATCH 09/18] fix: circular dependencies
---
.../llvm-libc-macros/bfloat16-macros.h | 23 -------------------
libc/src/math/generic/fbfloat16.cpp | 2 +-
libc/test/src/math/smoke/CMakeLists.txt | 1 -
libc/test/src/math/smoke/fbfloat16_test.cpp | 9 ++++----
4 files changed, 6 insertions(+), 29 deletions(-)
delete mode 100644 libc/include/llvm-libc-macros/bfloat16-macros.h
diff --git a/libc/include/llvm-libc-macros/bfloat16-macros.h b/libc/include/llvm-libc-macros/bfloat16-macros.h
deleted file mode 100644
index 466432b990a32..0000000000000
--- a/libc/include/llvm-libc-macros/bfloat16-macros.h
+++ /dev/null
@@ -1,23 +0,0 @@
-//===-- Detection of __bf16 compiler builtin type -------------------------===//
-//
-// 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_MACROS_BFLOAT16_MACROS_H
-#define LLVM_LIBC_MACROS_BFLOAT16_MACROS_H
-
-// #if ((defined(__clang__) && __clang_major__ > 17) || \
-// (defined(__GNUC__) && __GNUC__ > 13)) && \
-// !defined(__arm__) && !defined(_M_ARM) && !defined(__riscv) && \
-// !defined(_WIN32)
-
-#define LIBC_TYPES_HAS_BFLOAT16
-
-struct BFloat16;
-
-// #endif // LIBC_TYPES_HAS_BFLOAT16
-
-#endif // LLVM_LIBC_MACROS_BFLOAT16_MACROS_H
diff --git a/libc/src/math/generic/fbfloat16.cpp b/libc/src/math/generic/fbfloat16.cpp
index d42a486354e64..1ef183668a51a 100644
--- a/libc/src/math/generic/fbfloat16.cpp
+++ b/libc/src/math/generic/fbfloat16.cpp
@@ -15,7 +15,7 @@
namespace LIBC_NAMESPACE_DECL {
-LLVM_LIBC_FUNCTION(float, fbfloat16, (LIBC_NAMESPACE::bfloat16 x)) {
+LLVM_LIBC_FUNCTION(float, fbfloat16, (bfloat16 x)) {
if (x.bits == 0)
return 0.0f;
else
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 57fade182b128..4dce1cad2cf58 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -5328,6 +5328,5 @@ add_fp_unittest(
SRCS
fbfloat16_test.cpp
DEPENDS
- libc.src.errno.errno
libc.src.math.fbfloat16
)
diff --git a/libc/test/src/math/smoke/fbfloat16_test.cpp b/libc/test/src/math/smoke/fbfloat16_test.cpp
index a63a7e05a8ba5..2a7a2ea4fddf9 100644
--- a/libc/test/src/math/smoke/fbfloat16_test.cpp
+++ b/libc/test/src/math/smoke/fbfloat16_test.cpp
@@ -6,21 +6,22 @@
//
//===----------------------------------------------------------------------===//
-#include "src/math/fbfloat16.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/fbfloat16.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
// TODO: change this to bfloat16
-using LlvmLibcFBfloat16Test = LIBC_NAMESPACE::testing::FPTest<float>;
+using LlvmLibcFBfloat16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
TEST_F(LlvmLibcFBfloat16Test, SpecialNumbers) {
- LIBC_NAMESPACE::bfloat16 x{0.0f};
+ bfloat16 x{0.0f};
ASSERT_EQ(0, static_cast<int>(x.bits));
- LIBC_NAMESPACE::bfloat16 y{1.0f};
+ bfloat16 y{1.0f};
ASSERT_EQ(1, static_cast<int>(y.bits));
// TODO: implement this!
>From e9e089b375ea56190729edd52743842b5ce6cd6a Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Mon, 9 Jun 2025 21:10:18 +0530
Subject: [PATCH 10/18] refactor: remove `LIBC_TYPES_HAS_BFLOAT16` dependency
from sources and headers
---
libc/cmake/modules/compiler_features/check_bfloat16.cpp | 6 +-----
libc/test/src/math/smoke/fbfloat16_test.cpp | 1 -
libc/utils/MPFRWrapper/MPCommon.h | 2 --
3 files changed, 1 insertion(+), 8 deletions(-)
diff --git a/libc/cmake/modules/compiler_features/check_bfloat16.cpp b/libc/cmake/modules/compiler_features/check_bfloat16.cpp
index 1fcb73b01eb9c..fac0b901ac594 100644
--- a/libc/cmake/modules/compiler_features/check_bfloat16.cpp
+++ b/libc/cmake/modules/compiler_features/check_bfloat16.cpp
@@ -1,5 +1 @@
-#include "src/__support/macros/properties/types.h"
-
-#ifndef LIBC_TYPES_HAS_BFLOAT16
-#error unsupported
-#endif
+// intentionally left empty for CheckCompilerFeatures to work.
diff --git a/libc/test/src/math/smoke/fbfloat16_test.cpp b/libc/test/src/math/smoke/fbfloat16_test.cpp
index 2a7a2ea4fddf9..723922746eae2 100644
--- a/libc/test/src/math/smoke/fbfloat16_test.cpp
+++ b/libc/test/src/math/smoke/fbfloat16_test.cpp
@@ -12,7 +12,6 @@
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
-// TODO: change this to bfloat16
using LlvmLibcFBfloat16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
TEST_F(LlvmLibcFBfloat16Test, SpecialNumbers) {
diff --git a/libc/utils/MPFRWrapper/MPCommon.h b/libc/utils/MPFRWrapper/MPCommon.h
index e19de5674d470..0cabb63439927 100644
--- a/libc/utils/MPFRWrapper/MPCommon.h
+++ b/libc/utils/MPFRWrapper/MPCommon.h
@@ -139,7 +139,6 @@ class MPFRNumber {
mpfr_set_d(value, x, mpfr_rounding);
}
-#ifdef LIBC_TYPES_HAS_BFLOAT16
template <typename XType,
cpp::enable_if_t<cpp::is_same_v<bfloat16, XType>, int> = 0>
explicit MPFRNumber(XType x,
@@ -150,7 +149,6 @@ class MPFRNumber {
mpfr_init2(value, mpfr_precision);
mpfr_set_flt(value, x.as_float(), mpfr_rounding);
}
-#endif
template <typename XType,
cpp::enable_if_t<cpp::is_same_v<long double, XType>, int> = 0>
>From 8ded7ba17990096cfb27fb30e6dd90edb31729f0 Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Thu, 12 Jun 2025 22:34:15 +0530
Subject: [PATCH 11/18] chore: complete smoke tests for fbfloat16 function
Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
libc/test/src/math/smoke/fbfloat16_test.cpp | 25 ++++++++++-----------
1 file changed, 12 insertions(+), 13 deletions(-)
diff --git a/libc/test/src/math/smoke/fbfloat16_test.cpp b/libc/test/src/math/smoke/fbfloat16_test.cpp
index 723922746eae2..06624fbfd33d0 100644
--- a/libc/test/src/math/smoke/fbfloat16_test.cpp
+++ b/libc/test/src/math/smoke/fbfloat16_test.cpp
@@ -15,17 +15,16 @@
using LlvmLibcFBfloat16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
TEST_F(LlvmLibcFBfloat16Test, SpecialNumbers) {
-
- bfloat16 x{0.0f};
- ASSERT_EQ(0, static_cast<int>(x.bits));
-
-
- bfloat16 y{1.0f};
- ASSERT_EQ(1, static_cast<int>(y.bits));
-
- // TODO: implement this!
- // x = some bfloat number
- // float y = x as float (using our ctor?)
- // float z = mfpr(x) as float
- // check y == z
+ constexpr float SPECIAL_FLOATS[] = {
+ 0.0f, 1.0f, 2.0f, 4.5f, -1.0f, -0.5f, 3.140625f
+ };
+
+ constexpr uint16_t SPECIAL_BFLOAT16_BITS[] = {
+ 0, 0x3f80U, 0x4000U, 0x4090U, 0xbf80U, 0xbf00, 0x4049U
+ };
+
+ for (int i=0; i<7; i++) {
+ bfloat16 x{SPECIAL_FLOATS[i]};
+ ASSERT_EQ(SPECIAL_BFLOAT16_BITS[i], x.bits);
+ }
}
>From 823d84f1ce8b2a3b654b61591395d370ada9c906 Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Fri, 13 Jun 2025 05:09:24 +0530
Subject: [PATCH 12/18] refactor: rename fbfloat16 smoke test suite, refactor
bfloat16 specialization for MPFRNumber
Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
libc/test/src/math/smoke/fbfloat16_test.cpp | 4 ++--
libc/utils/MPFRWrapper/MPCommon.h | 23 +++++++++++----------
2 files changed, 14 insertions(+), 13 deletions(-)
diff --git a/libc/test/src/math/smoke/fbfloat16_test.cpp b/libc/test/src/math/smoke/fbfloat16_test.cpp
index 06624fbfd33d0..c4f0892e4772e 100644
--- a/libc/test/src/math/smoke/fbfloat16_test.cpp
+++ b/libc/test/src/math/smoke/fbfloat16_test.cpp
@@ -12,9 +12,9 @@
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
-using LlvmLibcFBfloat16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
+using LlvmLibcFBfloat16SmokeTest = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
-TEST_F(LlvmLibcFBfloat16Test, SpecialNumbers) {
+TEST_F(LlvmLibcFBfloat16SmokeTest, SpecialNumbers) {
constexpr float SPECIAL_FLOATS[] = {
0.0f, 1.0f, 2.0f, 4.5f, -1.0f, -0.5f, 3.140625f
};
diff --git a/libc/utils/MPFRWrapper/MPCommon.h b/libc/utils/MPFRWrapper/MPCommon.h
index 0cabb63439927..199363a5f2fba 100644
--- a/libc/utils/MPFRWrapper/MPCommon.h
+++ b/libc/utils/MPFRWrapper/MPCommon.h
@@ -139,17 +139,6 @@ class MPFRNumber {
mpfr_set_d(value, x, mpfr_rounding);
}
- template <typename XType,
- cpp::enable_if_t<cpp::is_same_v<bfloat16, XType>, int> = 0>
- explicit MPFRNumber(XType x,
- unsigned int precision = 8,
- RoundingMode rounding = RoundingMode::Nearest)
- : mpfr_precision(precision),
- mpfr_rounding(get_mpfr_rounding_mode(rounding)) {
- mpfr_init2(value, mpfr_precision);
- mpfr_set_flt(value, x.as_float(), mpfr_rounding);
- }
-
template <typename XType,
cpp::enable_if_t<cpp::is_same_v<long double, XType>, int> = 0>
explicit MPFRNumber(XType x,
@@ -185,6 +174,18 @@ class MPFRNumber {
mpfr_set_sj(value, x, mpfr_rounding);
}
+ // BFloat16
+ template <typename XType,
+ cpp::enable_if_t<cpp::is_same_v<bfloat16, XType>, int> = 0>
+ explicit MPFRNumber(XType x, unsigned int precision = 8,
+ RoundingMode rounding = RoundingMode::Nearest)
+ : mpfr_precision(precision),
+ mpfr_rounding(get_mpfr_rounding_mode(rounding)) {
+ mpfr_init2(value, mpfr_precision);
+ // BFloat16::as_float() requires no rounding
+ mpfr_set_flt(value, x.as_float(), mpfr_rounding);
+ }
+
MPFRNumber(const MPFRNumber &other);
MPFRNumber(const MPFRNumber &other, unsigned int precision);
MPFRNumber(const mpfr_t x, unsigned int precision, RoundingMode rounding);
>From 4e7a38e7de5982400fec6b9de3b021d07fcb3831 Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Fri, 13 Jun 2025 05:09:53 +0530
Subject: [PATCH 13/18] feat: add exhaustive tests for fbfloat16
---
.../src/math/exhaustive/fbfloat16_test.cpp | 45 +++++++++++++++++++
1 file changed, 45 insertions(+)
create mode 100644 libc/test/src/math/exhaustive/fbfloat16_test.cpp
diff --git a/libc/test/src/math/exhaustive/fbfloat16_test.cpp b/libc/test/src/math/exhaustive/fbfloat16_test.cpp
new file mode 100644
index 0000000000000..bb5e6f9854a54
--- /dev/null
+++ b/libc/test/src/math/exhaustive/fbfloat16_test.cpp
@@ -0,0 +1,45 @@
+//===-- Exhaustive tests for fbfloat16 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/__support/FPUtil/bfloat16.h"
+
+#include "utils/MPFRWrapper/MPCommon.h"
+
+#include "src/math/fbfloat16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcFBfloat16ExhaustiveTest =
+ LIBC_NAMESPACE::testing::FPTest<bfloat16>;
+
+// range: [0, inf]
+static constexpr uint16_t POS_START = 0x0000U;
+static constexpr uint16_t POS_STOP = 0x7f80U;
+
+// range: [-0, -inf]
+static constexpr uint16_t NEG_START = 0x8000U;
+static constexpr uint16_t NEG_STOP = 0xff80;
+
+
+using MPFRNumber = LIBC_NAMESPACE::testing::mpfr::MPFRNumber;
+
+TEST_F(LlvmLibcFBfloat16ExhaustiveTest, PositiveRange) {
+ for (uint16_t bits = POS_START; bits <= POS_STOP; bits++) {
+ bfloat16 bf16_num{bits};
+ MPFRNumber mpfr_num{bf16_num};
+ ASSERT_FP_EQ(mpfr_num.as<float>(), bf16_num.as_float());
+ }
+}
+
+TEST_F(LlvmLibcFBfloat16ExhaustiveTest, NegativeRange) {
+ for (uint16_t bits = NEG_START; bits <= NEG_STOP; bits++) {
+ bfloat16 bf16_num{bits};
+ MPFRNumber mpfr_num{bf16_num};
+ ASSERT_FP_EQ(mpfr_num.as<float>(), bf16_num.as_float());
+ }
+}
>From ea6cdae5d7955c5eaf22f6f3219f6ed61bbdf2a6 Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Fri, 13 Jun 2025 05:49:03 +0530
Subject: [PATCH 14/18] feat: implement fabsbf16 function
Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/src/math/CMakeLists.txt | 2 ++
libc/src/math/fabsbf16.h | 21 ++++++++++++++
libc/src/math/generic/CMakeLists.txt | 33 ++++++++++++++--------
libc/src/math/generic/fabsbf16.cpp | 19 +++++++++++++
libc/test/src/math/smoke/CMakeLists.txt | 12 ++++++++
libc/test/src/math/smoke/fabsbf16_test.cpp | 13 +++++++++
7 files changed, 90 insertions(+), 11 deletions(-)
create mode 100644 libc/src/math/fabsbf16.h
create mode 100644 libc/src/math/generic/fabsbf16.cpp
create mode 100644 libc/test/src/math/smoke/fabsbf16_test.cpp
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index df1922172c8ae..c4f727c985498 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -887,6 +887,7 @@ if(LIBC_TYPES_HAS_BFLOAT16)
list(APPEND TARGET_LIBM_ENTRYPOINTS
# Bfloat16 Entrypoints
libc.src.math.fbfloat16
+ libc.src.math.fabsbf16
)
endif()
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 57b72d6e712f6..9f9d50f16d841 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -558,3 +558,5 @@ add_math_entrypoint_object(ufromfpxf128)
# BFloat16 functions
add_math_entrypoint_object(fbfloat16)
+
+add_math_entrypoint_object(fabsbf16)
diff --git a/libc/src/math/fabsbf16.h b/libc/src/math/fabsbf16.h
new file mode 100644
index 0000000000000..02c6498d37a8e
--- /dev/null
+++ b/libc/src/math/fabsbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for fabsbf16 ----------------------*- 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_FABSBF16_H
+#define LLVM_LIBC_SRC_MATH_FABSBF16_H
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 fabsbf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_FABSBF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 2802c82ad9607..444058e4dc154 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5070,6 +5070,20 @@ add_entrypoint_object(
libc.src.__support.FPUtil.generic.mul
)
+add_header_library(
+ expxf16
+ HDRS
+ expxf16.h
+ DEPENDS
+ libc.src.__support.CPP.array
+ libc.src.__support.FPUtil.cast
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.multiply_add
+ libc.src.__support.FPUtil.nearest_integer
+ libc.src.__support.FPUtil.polyeval
+ libc.src.__support.macros.attributes
+)
+
# Bfloat16 Functions
add_entrypoint_object(
@@ -5080,20 +5094,17 @@ add_entrypoint_object(
../fbfloat16.h
DEPENDS
libc.src.__support.macros.config
- libc.src.__support.macros.properties.types
libc.src.__support.FPUtil.bfloat16
)
-add_header_library(
- expxf16
+add_entrypoint_object(
+ fabsbf16
+ SRCS
+ fabsbf16.cpp
HDRS
- expxf16.h
+ ../fabsbf16.h
DEPENDS
- libc.src.__support.CPP.array
- libc.src.__support.FPUtil.cast
- libc.src.__support.FPUtil.fp_bits
- libc.src.__support.FPUtil.multiply_add
- libc.src.__support.FPUtil.nearest_integer
- libc.src.__support.FPUtil.polyeval
- libc.src.__support.macros.attributes
+ libc.src.__support.macros.config
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.basic_operations
)
diff --git a/libc/src/math/generic/fabsbf16.cpp b/libc/src/math/generic/fabsbf16.cpp
new file mode 100644
index 0000000000000..976eeb25b9907
--- /dev/null
+++ b/libc/src/math/generic/fabsbf16.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of fabsbf16 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/fabsbf16.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/BasicOperations.h" // fputil::abs
+#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, fabsbf16, (bfloat16 x)) { return fputil::abs(x); }
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 4dce1cad2cf58..c9c1a5f34c324 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -5330,3 +5330,15 @@ add_fp_unittest(
DEPENDS
libc.src.math.fbfloat16
)
+
+add_fp_unittest(
+ fabsbf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ fabsbf16_test.cpp
+ HDRS
+ FAbsTest.h
+ DEPENDS
+ libc.src.math.fabsbf16
+)
diff --git a/libc/test/src/math/smoke/fabsbf16_test.cpp b/libc/test/src/math/smoke/fabsbf16_test.cpp
new file mode 100644
index 0000000000000..ee0679eb4b8bb
--- /dev/null
+++ b/libc/test/src/math/smoke/fabsbf16_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for fabsf16 ---------------------------------------------===//
+//
+// 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 "FAbsTest.h"
+
+#include "src/math/fabsbf16.h"
+
+LIST_FABS_TESTS(bfloat16, LIBC_NAMESPACE::fabsbf16)
>From e27d59d11c7f7e683fe6a3e72b04639f9edb9ded Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Sun, 13 Jul 2025 00:44:55 +0530
Subject: [PATCH 15/18] refactor: remove unused files and reorganize structure
Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
.../cmake/modules/CheckCompilerFeatures.cmake | 3 --
.../compiler_features/check_bfloat16.cpp | 1 -
libc/config/linux/x86_64/entrypoints.txt | 10 ++---
libc/src/math/CMakeLists.txt | 5 +--
libc/src/math/generic/CMakeLists.txt | 37 +++++----------
libc/src/math/generic/fbfloat16.cpp | 25 -----------
.../src/math/exhaustive/fbfloat16_test.cpp | 45 -------------------
libc/test/src/math/smoke/CMakeLists.txt | 38 ++++++----------
libc/utils/MPFRWrapper/MPCommon.h | 12 -----
9 files changed, 31 insertions(+), 145 deletions(-)
delete mode 100644 libc/cmake/modules/compiler_features/check_bfloat16.cpp
delete mode 100644 libc/src/math/generic/fbfloat16.cpp
delete mode 100644 libc/test/src/math/exhaustive/fbfloat16_test.cpp
diff --git a/libc/cmake/modules/CheckCompilerFeatures.cmake b/libc/cmake/modules/CheckCompilerFeatures.cmake
index 6db69bd5c4895..a5ea66a5935b7 100644
--- a/libc/cmake/modules/CheckCompilerFeatures.cmake
+++ b/libc/cmake/modules/CheckCompilerFeatures.cmake
@@ -15,7 +15,6 @@ set(
"fixed_point"
"cfloat16"
"cfloat128"
- "bfloat16"
)
# Making sure ALL_COMPILER_FEATURES is sorted.
@@ -117,8 +116,6 @@ foreach(feature IN LISTS ALL_COMPILER_FEATURES)
set(LIBC_TYPES_HAS_CFLOAT16 TRUE)
elseif(${feature} STREQUAL "cfloat128")
set(LIBC_TYPES_HAS_CFLOAT128 TRUE)
- elseif(${feature} STREQUAL "bfloat16")
- set(LIBC_TYPES_HAS_BFLOAT16 TRUE)
elseif(${feature} STREQUAL "builtin_ceil_floor_rint_trunc")
set(LIBC_COMPILER_HAS_BUILTIN_CEIL_FLOOR_RINT_TRUNC TRUE)
elseif(${feature} STREQUAL "builtin_fmax_fmin")
diff --git a/libc/cmake/modules/compiler_features/check_bfloat16.cpp b/libc/cmake/modules/compiler_features/check_bfloat16.cpp
deleted file mode 100644
index fac0b901ac594..0000000000000
--- a/libc/cmake/modules/compiler_features/check_bfloat16.cpp
+++ /dev/null
@@ -1 +0,0 @@
-// intentionally left empty for CheckCompilerFeatures to work.
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index c4f727c985498..439c64aa8e576 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -883,13 +883,11 @@ if(LIBC_TYPES_HAS_FLOAT128)
)
endif()
-if(LIBC_TYPES_HAS_BFLOAT16)
- list(APPEND TARGET_LIBM_ENTRYPOINTS
- # Bfloat16 Entrypoints
- libc.src.math.fbfloat16
+
+list(APPEND TARGET_LIBM_ENTRYPOINTS
+# Bfloat16 Entrypoints
libc.src.math.fabsbf16
- )
-endif()
+)
if(LIBC_COMPILER_HAS_FIXED_POINT)
list(APPEND TARGET_LIBM_ENTRYPOINTS
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 9f9d50f16d841..5a58e6a921f53 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -189,6 +189,7 @@ add_math_entrypoint_object(fabsf)
add_math_entrypoint_object(fabsl)
add_math_entrypoint_object(fabsf16)
add_math_entrypoint_object(fabsf128)
+add_math_entrypoint_object(fabsbf16)
add_math_entrypoint_object(fadd)
add_math_entrypoint_object(faddl)
@@ -556,7 +557,3 @@ add_math_entrypoint_object(ufromfpxl)
add_math_entrypoint_object(ufromfpxf16)
add_math_entrypoint_object(ufromfpxf128)
-# BFloat16 functions
-add_math_entrypoint_object(fbfloat16)
-
-add_math_entrypoint_object(fabsbf16)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 444058e4dc154..adb5d51e16c28 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -684,6 +684,18 @@ add_entrypoint_object(
libc.src.__support.FPUtil.basic_operations
)
+add_entrypoint_object(
+ fabsbf16
+ SRCS
+ fabsbf16.cpp
+ HDRS
+ ../fabsbf16.h
+ DEPENDS
+ libc.src.__support.FPUtil.basic_operations
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.macros.config
+)
+
add_entrypoint_object(
fadd
SRCS
@@ -5083,28 +5095,3 @@ add_header_library(
libc.src.__support.FPUtil.polyeval
libc.src.__support.macros.attributes
)
-
-# Bfloat16 Functions
-
-add_entrypoint_object(
- fbfloat16
- SRCS
- fbfloat16.cpp
- HDRS
- ../fbfloat16.h
- DEPENDS
- libc.src.__support.macros.config
- libc.src.__support.FPUtil.bfloat16
-)
-
-add_entrypoint_object(
- fabsbf16
- SRCS
- fabsbf16.cpp
- HDRS
- ../fabsbf16.h
- DEPENDS
- libc.src.__support.macros.config
- libc.src.__support.FPUtil.bfloat16
- libc.src.__support.FPUtil.basic_operations
-)
diff --git a/libc/src/math/generic/fbfloat16.cpp b/libc/src/math/generic/fbfloat16.cpp
deleted file mode 100644
index 1ef183668a51a..0000000000000
--- a/libc/src/math/generic/fbfloat16.cpp
+++ /dev/null
@@ -1,25 +0,0 @@
-//===-- Implementation of fbfloat16 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/fbfloat16.h"
-
-#include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
-#include "src/__support/macros/properties/types.h"
-
-namespace LIBC_NAMESPACE_DECL {
-
-LLVM_LIBC_FUNCTION(float, fbfloat16, (bfloat16 x)) {
- if (x.bits == 0)
- return 0.0f;
- else
- return 1.0f;
-}
-
-} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/math/exhaustive/fbfloat16_test.cpp b/libc/test/src/math/exhaustive/fbfloat16_test.cpp
deleted file mode 100644
index bb5e6f9854a54..0000000000000
--- a/libc/test/src/math/exhaustive/fbfloat16_test.cpp
+++ /dev/null
@@ -1,45 +0,0 @@
-//===-- Exhaustive tests for fbfloat16 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/__support/FPUtil/bfloat16.h"
-
-#include "utils/MPFRWrapper/MPCommon.h"
-
-#include "src/math/fbfloat16.h"
-#include "test/UnitTest/FPMatcher.h"
-#include "test/UnitTest/Test.h"
-
-using LlvmLibcFBfloat16ExhaustiveTest =
- LIBC_NAMESPACE::testing::FPTest<bfloat16>;
-
-// range: [0, inf]
-static constexpr uint16_t POS_START = 0x0000U;
-static constexpr uint16_t POS_STOP = 0x7f80U;
-
-// range: [-0, -inf]
-static constexpr uint16_t NEG_START = 0x8000U;
-static constexpr uint16_t NEG_STOP = 0xff80;
-
-
-using MPFRNumber = LIBC_NAMESPACE::testing::mpfr::MPFRNumber;
-
-TEST_F(LlvmLibcFBfloat16ExhaustiveTest, PositiveRange) {
- for (uint16_t bits = POS_START; bits <= POS_STOP; bits++) {
- bfloat16 bf16_num{bits};
- MPFRNumber mpfr_num{bf16_num};
- ASSERT_FP_EQ(mpfr_num.as<float>(), bf16_num.as_float());
- }
-}
-
-TEST_F(LlvmLibcFBfloat16ExhaustiveTest, NegativeRange) {
- for (uint16_t bits = NEG_START; bits <= NEG_STOP; bits++) {
- bfloat16 bf16_num{bits};
- MPFRNumber mpfr_num{bf16_num};
- ASSERT_FP_EQ(mpfr_num.as<float>(), bf16_num.as_float());
- }
-}
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index c9c1a5f34c324..2e4483c66090b 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -209,6 +209,20 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
+add_fp_unittest(
+ fabsbf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ fabsbf16_test.cpp
+ HDRS
+ FAbsTest.h
+ DEPENDS
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.math.fabsbf16
+)
+
+
add_fp_unittest(
fadd_test
SUITE
@@ -5318,27 +5332,3 @@ add_fp_unittest(
libc.src.__support.macros.properties.os
libc.src.__support.macros.properties.types
)
-
-# Bfloat16 Smoke Tests
-
-add_fp_unittest(
- fbfloat16_test
- SUITE
- libc-math-smoke-tests
- SRCS
- fbfloat16_test.cpp
- DEPENDS
- libc.src.math.fbfloat16
-)
-
-add_fp_unittest(
- fabsbf16_test
- SUITE
- libc-math-smoke-tests
- SRCS
- fabsbf16_test.cpp
- HDRS
- FAbsTest.h
- DEPENDS
- libc.src.math.fabsbf16
-)
diff --git a/libc/utils/MPFRWrapper/MPCommon.h b/libc/utils/MPFRWrapper/MPCommon.h
index 199363a5f2fba..af03ff054e1a9 100644
--- a/libc/utils/MPFRWrapper/MPCommon.h
+++ b/libc/utils/MPFRWrapper/MPCommon.h
@@ -174,18 +174,6 @@ class MPFRNumber {
mpfr_set_sj(value, x, mpfr_rounding);
}
- // BFloat16
- template <typename XType,
- cpp::enable_if_t<cpp::is_same_v<bfloat16, XType>, int> = 0>
- explicit MPFRNumber(XType x, unsigned int precision = 8,
- RoundingMode rounding = RoundingMode::Nearest)
- : mpfr_precision(precision),
- mpfr_rounding(get_mpfr_rounding_mode(rounding)) {
- mpfr_init2(value, mpfr_precision);
- // BFloat16::as_float() requires no rounding
- mpfr_set_flt(value, x.as_float(), mpfr_rounding);
- }
-
MPFRNumber(const MPFRNumber &other);
MPFRNumber(const MPFRNumber &other, unsigned int precision);
MPFRNumber(const mpfr_t x, unsigned int precision, RoundingMode rounding);
>From b44b27c6aa6571985b9fe870b81c745078bccb20 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Sun, 13 Jul 2025 01:00:07 +0530
Subject: [PATCH 16/18] refactor: use LIST_FABS_TEST api for fabsbf16
Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
libc/src/math/CMakeLists.txt | 1 -
libc/src/math/generic/fabsbf16.cpp | 4 ++--
libc/test/src/math/generic/CMakeLists.txt | 1 -
libc/test/src/math/smoke/fbfloat16_test.cpp | 26 ++++-----------------
4 files changed, 7 insertions(+), 25 deletions(-)
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 5a58e6a921f53..4db4eb909d8f6 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -556,4 +556,3 @@ add_math_entrypoint_object(ufromfpxf)
add_math_entrypoint_object(ufromfpxl)
add_math_entrypoint_object(ufromfpxf16)
add_math_entrypoint_object(ufromfpxf128)
-
diff --git a/libc/src/math/generic/fabsbf16.cpp b/libc/src/math/generic/fabsbf16.cpp
index 976eeb25b9907..ea39719e82d42 100644
--- a/libc/src/math/generic/fabsbf16.cpp
+++ b/libc/src/math/generic/fabsbf16.cpp
@@ -8,9 +8,9 @@
#include "src/math/fabsbf16.h"
+#include "src/__support/FPUtil/BasicOperations.h"
#include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/FPUtil/BasicOperations.h" // fputil::abs
-#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
+#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/test/src/math/generic/CMakeLists.txt b/libc/test/src/math/generic/CMakeLists.txt
index 1fe7801941d5a..a9d54d6424294 100644
--- a/libc/test/src/math/generic/CMakeLists.txt
+++ b/libc/test/src/math/generic/CMakeLists.txt
@@ -30,4 +30,3 @@ add_fp_unittest(
DEPENDS
libc.src.math.generic.ceill
)
-
diff --git a/libc/test/src/math/smoke/fbfloat16_test.cpp b/libc/test/src/math/smoke/fbfloat16_test.cpp
index c4f0892e4772e..c72983ec56db3 100644
--- a/libc/test/src/math/smoke/fbfloat16_test.cpp
+++ b/libc/test/src/math/smoke/fbfloat16_test.cpp
@@ -1,4 +1,4 @@
-//===-- Unittests for fbfloat16 function ----------------------------------===//
+//===-- Unittests for fabsbf16 function -----------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
@@ -6,25 +6,9 @@
//
//===----------------------------------------------------------------------===//
-#include "src/__support/FPUtil/bfloat16.h"
-
-#include "src/math/fbfloat16.h"
-#include "test/UnitTest/FPMatcher.h"
-#include "test/UnitTest/Test.h"
-
-using LlvmLibcFBfloat16SmokeTest = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
+#include "FAbsTest.h"
-TEST_F(LlvmLibcFBfloat16SmokeTest, SpecialNumbers) {
- constexpr float SPECIAL_FLOATS[] = {
- 0.0f, 1.0f, 2.0f, 4.5f, -1.0f, -0.5f, 3.140625f
- };
-
- constexpr uint16_t SPECIAL_BFLOAT16_BITS[] = {
- 0, 0x3f80U, 0x4000U, 0x4090U, 0xbf80U, 0xbf00, 0x4049U
- };
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/fabsbf16.h"
- for (int i=0; i<7; i++) {
- bfloat16 x{SPECIAL_FLOATS[i]};
- ASSERT_EQ(SPECIAL_BFLOAT16_BITS[i], x.bits);
- }
-}
+LIST_FABS_TESTS(bfloat16, LIBC_NAMESPACE::fabsbf16)
>From f3d8a3f9f2c2dfa25233b4e8a4f9ab196830ec33 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Sun, 13 Jul 2025 01:09:46 +0530
Subject: [PATCH 17/18] chore: remove redundant stuff and add indents
Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
libc/src/math/fabsbf16.h | 2 +-
libc/src/math/fbfloat16.h | 22 ---------------------
libc/src/math/generic/CMakeLists.txt | 10 +++++-----
libc/test/src/math/smoke/CMakeLists.txt | 9 ++++-----
libc/test/src/math/smoke/fabsbf16_test.cpp | 2 +-
libc/test/src/math/smoke/fbfloat16_test.cpp | 14 -------------
6 files changed, 11 insertions(+), 48 deletions(-)
delete mode 100644 libc/src/math/fbfloat16.h
delete mode 100644 libc/test/src/math/smoke/fbfloat16_test.cpp
diff --git a/libc/src/math/fabsbf16.h b/libc/src/math/fabsbf16.h
index 02c6498d37a8e..d1768b9bdc875 100644
--- a/libc/src/math/fabsbf16.h
+++ b/libc/src/math/fabsbf16.h
@@ -10,7 +10,7 @@
#define LLVM_LIBC_SRC_MATH_FABSBF16_H
#include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
+#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/math/fbfloat16.h b/libc/src/math/fbfloat16.h
deleted file mode 100644
index 5b1b04b24a563..0000000000000
--- a/libc/src/math/fbfloat16.h
+++ /dev/null
@@ -1,22 +0,0 @@
-//===-- Implementation header for fbfloat16 ---------------------*- 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_FBFLOAT16_H
-#define LLVM_LIBC_SRC_MATH_FBFLOAT16_H
-
-#include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/macros/config.h"
-#include "src/__support/macros/properties/types.h"
-
-namespace LIBC_NAMESPACE_DECL {
-
-float fbfloat16(bfloat16 x);
-
-} // namespace LIBC_NAMESPACE_DECL
-
-#endif // LLVM_LIBC_SRC_MATH_FBFLOAT16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index adb5d51e16c28..d87d01618fe47 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -687,13 +687,13 @@ add_entrypoint_object(
add_entrypoint_object(
fabsbf16
SRCS
- fabsbf16.cpp
+ fabsbf16.cpp
HDRS
- ../fabsbf16.h
+ ../fabsbf16.h
DEPENDS
- libc.src.__support.FPUtil.basic_operations
- libc.src.__support.FPUtil.bfloat16
- libc.src.__support.macros.config
+ libc.src.__support.FPUtil.basic_operations
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.macros.config
)
add_entrypoint_object(
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 2e4483c66090b..78f8d17be72dd 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -214,15 +214,14 @@ add_fp_unittest(
SUITE
libc-math-smoke-tests
SRCS
- fabsbf16_test.cpp
+ fabsbf16_test.cpp
HDRS
- FAbsTest.h
+ FAbsTest.h
DEPENDS
- libc.src.__support.FPUtil.bfloat16
- libc.src.math.fabsbf16
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.math.fabsbf16
)
-
add_fp_unittest(
fadd_test
SUITE
diff --git a/libc/test/src/math/smoke/fabsbf16_test.cpp b/libc/test/src/math/smoke/fabsbf16_test.cpp
index ee0679eb4b8bb..dc97ae3a6853e 100644
--- a/libc/test/src/math/smoke/fabsbf16_test.cpp
+++ b/libc/test/src/math/smoke/fabsbf16_test.cpp
@@ -1,4 +1,4 @@
-//===-- Unittests for fabsf16 ---------------------------------------------===//
+//===-- Unittests for fabsbf16 --------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
diff --git a/libc/test/src/math/smoke/fbfloat16_test.cpp b/libc/test/src/math/smoke/fbfloat16_test.cpp
deleted file mode 100644
index c72983ec56db3..0000000000000
--- a/libc/test/src/math/smoke/fbfloat16_test.cpp
+++ /dev/null
@@ -1,14 +0,0 @@
-//===-- Unittests for fabsbf16 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 "FAbsTest.h"
-
-#include "src/__support/FPUtil/bfloat16.h"
-#include "src/math/fabsbf16.h"
-
-LIST_FABS_TESTS(bfloat16, LIBC_NAMESPACE::fabsbf16)
>From df7471f3b9ed267c996f5ce8d88b82d61ee9aca0 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Tue, 22 Jul 2025 22:38:19 +0530
Subject: [PATCH 18/18] Update libc/config/linux/x86_64/entrypoints.txt
Co-authored-by: OverMighty <its.overmighty at gmail.com>
---
libc/config/linux/x86_64/entrypoints.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 439c64aa8e576..b4db7a3690c8f 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -885,7 +885,7 @@ endif()
list(APPEND TARGET_LIBM_ENTRYPOINTS
-# Bfloat16 Entrypoints
+ # bfloat16 entrypoints
libc.src.math.fabsbf16
)
More information about the libc-commits
mailing list