Skip to content

[libc][math][c++23] Add f{max,min}bf16 math functions #152782

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

Merged
merged 4 commits into from
Aug 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libc/config/baremetal/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions libc/config/baremetal/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions libc/config/baremetal/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions libc/config/darwin/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions libc/config/darwin/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions libc/config/gpu/amdgpu/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions libc/config/gpu/nvptx/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions libc/config/linux/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions libc/config/linux/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions libc/config/windows/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions libc/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions libc/src/math/fmaxbf16.h
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions libc/src/math/fminbf16.h
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions libc/src/math/generic/fmaxbf16.cpp
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions libc/src/math/generic/fminbf16.cpp
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions libc/test/src/math/smoke/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions libc/test/src/math/smoke/fmaxbf16_test.cpp
Original file line number Diff line number Diff line change
@@ -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)
14 changes: 14 additions & 0 deletions libc/test/src/math/smoke/fminbf16_test.cpp
Original file line number Diff line number Diff line change
@@ -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)
Loading