[libc-commits] [libc] [libc][math][c++23] Add f{max, min}bf16 math functions (PR #152782)
Krishna Pandey via libc-commits
libc-commits at lists.llvm.org
Fri Aug 8 12:38:26 PDT 2025
https://github.com/krishna2803 created https://github.com/llvm/llvm-project/pull/152782
None
>From d8fb97c57474917d9d5d398a12ebe3cdd1ebbb42 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Sat, 9 Aug 2025 01:04:03 +0530
Subject: [PATCH 1/4] feat: implement fmaxbf16 function
Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
libc/src/math/fmaxbf16.h | 21 +++++++++++++++++++++
libc/src/math/generic/fmaxbf16.cpp | 21 +++++++++++++++++++++
libc/test/src/math/smoke/fmaxbf16_test.cpp | 14 ++++++++++++++
3 files changed, 56 insertions(+)
create mode 100644 libc/src/math/fmaxbf16.h
create mode 100644 libc/src/math/generic/fmaxbf16.cpp
create mode 100644 libc/test/src/math/smoke/fmaxbf16_test.cpp
diff --git a/libc/src/math/fmaxbf16.h b/libc/src/math/fmaxbf16.h
new file mode 100644
index 0000000000000..bdbd14cd578d5
--- /dev/null
+++ b/libc/src/math/fmaxbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for fmaxbf16 ----------------------*- 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_FMAXBF16_H
+#define LLVM_LIBC_SRC_MATH_FMAXBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 fmaxbf16(bfloat16 x, bfloat16 y);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_FMAXBF16_H
diff --git a/libc/src/math/generic/fmaxbf16.cpp b/libc/src/math/generic/fmaxbf16.cpp
new file mode 100644
index 0000000000000..01d395bc04fa6
--- /dev/null
+++ b/libc/src/math/generic/fmaxbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of fmaxbf16 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/fmaxbf16.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, fmaxbf16, (bfloat16 x, bfloat16 y)) {
+ return fputil::fmax(x, y);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/math/smoke/fmaxbf16_test.cpp b/libc/test/src/math/smoke/fmaxbf16_test.cpp
new file mode 100644
index 0000000000000..8ff9313a1994b
--- /dev/null
+++ b/libc/test/src/math/smoke/fmaxbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for fmaxbf16 --------------------------------------------===//
+//
+// 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 "FMaxTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/fmaxbf16.h"
+
+LIST_FMAX_TESTS(bfloat16, LIBC_NAMESPACE::fmaxbf16)
>From 0918c75c73e6182cad3dd3f1db62f4262b290016 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Sat, 9 Aug 2025 01:04:20 +0530
Subject: [PATCH 2/4] feat: implement fminbf16 function
Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
libc/src/math/fminbf16.h | 21 +++++++++++++++++++++
libc/src/math/generic/fminbf16.cpp | 21 +++++++++++++++++++++
libc/test/src/math/smoke/fminbf16_test.cpp | 14 ++++++++++++++
3 files changed, 56 insertions(+)
create mode 100644 libc/src/math/fminbf16.h
create mode 100644 libc/src/math/generic/fminbf16.cpp
create mode 100644 libc/test/src/math/smoke/fminbf16_test.cpp
diff --git a/libc/src/math/fminbf16.h b/libc/src/math/fminbf16.h
new file mode 100644
index 0000000000000..4c1ada95f33dc
--- /dev/null
+++ b/libc/src/math/fminbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for fminbf16 ----------------------*- 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_FMINBF16_H
+#define LLVM_LIBC_SRC_MATH_FMINBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 fminbf16(bfloat16 x, bfloat16 y);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_FMINBF16_H
diff --git a/libc/src/math/generic/fminbf16.cpp b/libc/src/math/generic/fminbf16.cpp
new file mode 100644
index 0000000000000..c3e29ee10c8b6
--- /dev/null
+++ b/libc/src/math/generic/fminbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of fminbf16 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/fminbf16.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, fminbf16, (bfloat16 x, bfloat16 y)) {
+ return fputil::fmin(x, y);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/math/smoke/fminbf16_test.cpp b/libc/test/src/math/smoke/fminbf16_test.cpp
new file mode 100644
index 0000000000000..195a0ac07c8ff
--- /dev/null
+++ b/libc/test/src/math/smoke/fminbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for fminbf16 --------------------------------------------===//
+//
+// 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 "FMinTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/fminbf16.h"
+
+LIST_FMIN_TESTS(bfloat16, LIBC_NAMESPACE::fminbf16)
>From 02557ad2c6f4fcef6bd4e41d125b72d7e699a251 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Sat, 9 Aug 2025 01:04:36 +0530
Subject: [PATCH 3/4] chore: update CMakeLists
Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
libc/src/math/CMakeLists.txt | 2 ++
libc/src/math/generic/CMakeLists.txt | 31 +++++++++++++++++++++++++
libc/test/src/math/smoke/CMakeLists.txt | 30 ++++++++++++++++++++++++
3 files changed, 63 insertions(+)
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index c3840d3c4aa89..ebf2a0e728d00 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -226,12 +226,14 @@ add_math_entrypoint_object(fmaxf)
add_math_entrypoint_object(fmaxl)
add_math_entrypoint_object(fmaxf128)
add_math_entrypoint_object(fmaxf16)
+add_math_entrypoint_object(fmaxbf16)
add_math_entrypoint_object(fmin)
add_math_entrypoint_object(fminf)
add_math_entrypoint_object(fminl)
add_math_entrypoint_object(fminf128)
add_math_entrypoint_object(fminf16)
+add_math_entrypoint_object(fminbf16)
add_math_entrypoint_object(fmaximum)
add_math_entrypoint_object(fmaximumf)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 893606609dfc3..544339de7694e 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2361,6 +2361,21 @@ add_entrypoint_object(
MISC_MATH_BASIC_OPS_OPT
)
+add_entrypoint_object(
+ fminbf16
+ SRCS
+ fminbf16.cpp
+ HDRS
+ ../fminbf16.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
+ FLAGS
+ MISC_MATH_BASIC_OPS_OPT
+)
add_entrypoint_object(
fmax
@@ -2420,6 +2435,22 @@ add_entrypoint_object(
MISC_MATH_BASIC_OPS_OPT
)
+add_entrypoint_object(
+ fmaxbf16
+ SRCS
+ fmaxbf16.cpp
+ HDRS
+ ../fmaxbf16.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
+ FLAGS
+ MISC_MATH_BASIC_OPS_OPT
+)
+
add_entrypoint_object(
fmaximum
SRCS
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 5f497c63759f4..786c33c964289 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -2346,6 +2346,21 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
+add_fp_unittest(
+ fminbf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ fminbf16_test.cpp
+ HDRS
+ FMinTest.h
+ DEPENDS
+ libc.src.math.fminbf16
+ libc.src.__support.CPP.algorithm
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.fp_bits
+)
+
add_fp_unittest(
fmaxf_test
SUITE
@@ -2416,6 +2431,21 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
+add_fp_unittest(
+ fmaxbf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ fmaxbf16_test.cpp
+ HDRS
+ FMaxTest.h
+ DEPENDS
+ libc.src.math.fmaxbf16
+ libc.src.__support.CPP.algorithm
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.fp_bits
+)
+
add_fp_unittest(
fmaximuml_test
SUITE
>From e7ee2359577d237c082856b4b2e7d147b7d93a14 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Sat, 9 Aug 2025 01:04:45 +0530
Subject: [PATCH 4/4] chore: update entrypoints
Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
libc/config/baremetal/aarch64/entrypoints.txt | 2 ++
libc/config/baremetal/arm/entrypoints.txt | 2 ++
libc/config/baremetal/riscv/entrypoints.txt | 2 ++
libc/config/darwin/aarch64/entrypoints.txt | 2 ++
libc/config/darwin/x86_64/entrypoints.txt | 2 ++
libc/config/gpu/amdgpu/entrypoints.txt | 2 ++
libc/config/gpu/nvptx/entrypoints.txt | 2 ++
libc/config/linux/aarch64/entrypoints.txt | 2 ++
libc/config/linux/arm/entrypoints.txt | 2 ++
libc/config/linux/riscv/entrypoints.txt | 2 ++
libc/config/linux/x86_64/entrypoints.txt | 2 ++
libc/config/windows/entrypoints.txt | 2 ++
12 files changed, 24 insertions(+)
diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index 683c7464abde9..6b11c4ee62a61 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -760,6 +760,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.ceilbf16
libc.src.math.fabsbf16
libc.src.math.floorbf16
+ libc.src.math.fmaxbf16
+ libc.src.math.fminbf16
libc.src.math.roundbf16
libc.src.math.roundevenbf16
libc.src.math.truncbf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index f8ecc2ea8a3a4..7e146f8d58303 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -763,6 +763,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.ceilbf16
libc.src.math.fabsbf16
libc.src.math.floorbf16
+ libc.src.math.fmaxbf16
+ libc.src.math.fminbf16
libc.src.math.roundbf16
libc.src.math.roundevenbf16
libc.src.math.truncbf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 679bfb3803686..ccf9001d7cae2 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -763,6 +763,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.ceilbf16
libc.src.math.fabsbf16
libc.src.math.floorbf16
+ libc.src.math.fmaxbf16
+ libc.src.math.fminbf16
libc.src.math.roundbf16
libc.src.math.roundevenbf16
libc.src.math.truncbf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 72b0265552ab8..54845dfc28145 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -593,6 +593,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.ceilbf16
libc.src.math.fabsbf16
libc.src.math.floorbf16
+ libc.src.math.fmaxbf16
+ libc.src.math.fminbf16
libc.src.math.roundbf16
libc.src.math.roundevenbf16
libc.src.math.truncbf16
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index b5ab1ee26a867..092f40132b6f7 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -236,6 +236,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.ceilbf16
libc.src.math.fabsbf16
libc.src.math.floorbf16
+ libc.src.math.fmaxbf16
+ libc.src.math.fminbf16
libc.src.math.roundbf16
libc.src.math.roundevenbf16
libc.src.math.truncbf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index 77a13a66da545..5ceb98211fa32 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -619,6 +619,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.ceilbf16
libc.src.math.fabsbf16
libc.src.math.floorbf16
+ libc.src.math.fmaxbf16
+ libc.src.math.fminbf16
libc.src.math.roundbf16
libc.src.math.roundevenbf16
libc.src.math.truncbf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index 61c9c717d37ef..0c1d45235778b 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -620,6 +620,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.ceilbf16
libc.src.math.fabsbf16
libc.src.math.floorbf16
+ libc.src.math.fmaxbf16
+ libc.src.math.fminbf16
libc.src.math.roundbf16
libc.src.math.roundevenbf16
libc.src.math.truncbf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index fbdf8fb621b0f..8b498427d77c2 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -847,6 +847,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.ceilbf16
libc.src.math.fabsbf16
libc.src.math.floorbf16
+ libc.src.math.fmaxbf16
+ libc.src.math.fminbf16
libc.src.math.roundbf16
libc.src.math.roundevenbf16
libc.src.math.truncbf16
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index e3f5feecc896a..b8c22841efec5 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -463,6 +463,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.ceilbf16
libc.src.math.fabsbf16
libc.src.math.floorbf16
+ libc.src.math.fmaxbf16
+ libc.src.math.fminbf16
libc.src.math.roundbf16
libc.src.math.roundevenbf16
libc.src.math.truncbf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index ba67dddbe8b98..5b21feb9dad39 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -866,6 +866,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.ceilbf16
libc.src.math.fabsbf16
libc.src.math.floorbf16
+ libc.src.math.fmaxbf16
+ libc.src.math.fminbf16
libc.src.math.roundbf16
libc.src.math.roundevenbf16
libc.src.math.truncbf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 066dc21497691..4350589934ca1 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -898,6 +898,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.ceilbf16
libc.src.math.fabsbf16
libc.src.math.floorbf16
+ libc.src.math.fmaxbf16
+ libc.src.math.fminbf16
libc.src.math.roundbf16
libc.src.math.roundevenbf16
libc.src.math.truncbf16
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 994078c0ed274..9b46d64cb3cfd 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -309,6 +309,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.ceilbf16
libc.src.math.fabsbf16
libc.src.math.floorbf16
+ libc.src.math.fmaxbf16
+ libc.src.math.fminbf16
libc.src.math.roundbf16
libc.src.math.roundevenbf16
libc.src.math.truncbf16
More information about the libc-commits
mailing list