Skip to content

[libc][math][c++23] Add {ceil,floor,round,roundeven,trunc}bf16 math functions #152352

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 19 commits into from
Aug 8, 2025
Merged
Show file tree
Hide file tree
Changes from 17 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
5 changes: 5 additions & 0 deletions libc/config/baremetal/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,12 @@ endif()

list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
libc.src.math.ceilbf16
libc.src.math.fabsbf16
libc.src.math.floorbf16
libc.src.math.roundbf16
libc.src.math.roundevenbf16
libc.src.math.truncbf16
)

if(LIBC_COMPILER_HAS_FIXED_POINT)
Expand Down
5 changes: 5 additions & 0 deletions libc/config/baremetal/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,12 @@ endif()

list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
libc.src.math.ceilbf16
libc.src.math.fabsbf16
libc.src.math.floorbf16
libc.src.math.roundbf16
libc.src.math.roundevenbf16
libc.src.math.truncbf16
)

if(LIBC_COMPILER_HAS_FIXED_POINT)
Expand Down
5 changes: 5 additions & 0 deletions libc/config/baremetal/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,12 @@ endif()

list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
libc.src.math.ceilbf16
libc.src.math.fabsbf16
libc.src.math.floorbf16
libc.src.math.roundbf16
libc.src.math.roundevenbf16
libc.src.math.truncbf16
)

if(LIBC_COMPILER_HAS_FIXED_POINT)
Expand Down
5 changes: 5 additions & 0 deletions libc/config/darwin/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,12 @@ endif()

list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
libc.src.math.ceilbf16
libc.src.math.fabsbf16
libc.src.math.floorbf16
libc.src.math.roundbf16
libc.src.math.roundevenbf16
libc.src.math.truncbf16
)

if(LIBC_COMPILER_HAS_FIXED_POINT)
Expand Down
5 changes: 5 additions & 0 deletions libc/config/darwin/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,12 @@ set(TARGET_LIBM_ENTRYPOINTS

list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
libc.src.math.ceilbf16
libc.src.math.fabsbf16
libc.src.math.floorbf16
libc.src.math.roundbf16
libc.src.math.roundevenbf16
libc.src.math.truncbf16
)

set(TARGET_LLVMLIBC_ENTRYPOINTS
Expand Down
5 changes: 5 additions & 0 deletions libc/config/gpu/amdgpu/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,12 @@ endif()

list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
libc.src.math.ceilbf16
libc.src.math.fabsbf16
libc.src.math.floorbf16
libc.src.math.roundbf16
libc.src.math.roundevenbf16
libc.src.math.truncbf16
)

set(TARGET_LLVMLIBC_ENTRYPOINTS
Expand Down
5 changes: 5 additions & 0 deletions libc/config/gpu/nvptx/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,12 @@ endif()

list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
libc.src.math.ceilbf16
libc.src.math.fabsbf16
libc.src.math.floorbf16
libc.src.math.roundbf16
libc.src.math.roundevenbf16
libc.src.math.truncbf16
)

set(TARGET_LLVMLIBC_ENTRYPOINTS
Expand Down
5 changes: 5 additions & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,12 @@ endif()

list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
libc.src.math.ceilbf16
libc.src.math.fabsbf16
libc.src.math.floorbf16
libc.src.math.roundbf16
libc.src.math.roundevenbf16
libc.src.math.truncbf16
)

if(LLVM_LIBC_FULL_BUILD)
Expand Down
5 changes: 5 additions & 0 deletions libc/config/linux/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,12 @@ set(TARGET_LIBM_ENTRYPOINTS

list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
libc.src.math.ceilbf16
libc.src.math.fabsbf16
libc.src.math.floorbf16
libc.src.math.roundbf16
libc.src.math.roundevenbf16
libc.src.math.truncbf16
)

set(TARGET_LLVMLIBC_ENTRYPOINTS
Expand Down
5 changes: 5 additions & 0 deletions libc/config/linux/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,12 @@ endif()

list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
libc.src.math.ceilbf16
libc.src.math.fabsbf16
libc.src.math.floorbf16
libc.src.math.roundbf16
libc.src.math.roundevenbf16
libc.src.math.truncbf16
)

if(LIBC_COMPILER_HAS_FIXED_POINT)
Expand Down
6 changes: 5 additions & 1 deletion libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -893,10 +893,14 @@ if(LIBC_TYPES_HAS_FLOAT128)
)
endif()


list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
libc.src.math.ceilbf16
libc.src.math.fabsbf16
libc.src.math.floorbf16
libc.src.math.roundbf16
libc.src.math.roundevenbf16
libc.src.math.truncbf16
)

if(LIBC_COMPILER_HAS_FIXED_POINT)
Expand Down
5 changes: 5 additions & 0 deletions libc/config/windows/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,12 @@ set(TARGET_LIBM_ENTRYPOINTS

list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
libc.src.math.ceilbf16
libc.src.math.fabsbf16
libc.src.math.floorbf16
libc.src.math.roundbf16
libc.src.math.roundevenbf16
libc.src.math.truncbf16
)

set(TARGET_LLVMLIBC_ENTRYPOINTS
Expand Down
5 changes: 5 additions & 0 deletions libc/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ add_math_entrypoint_object(ceilf)
add_math_entrypoint_object(ceill)
add_math_entrypoint_object(ceilf16)
add_math_entrypoint_object(ceilf128)
add_math_entrypoint_object(ceilbf16)

add_math_entrypoint_object(copysign)
add_math_entrypoint_object(copysignf)
Expand Down Expand Up @@ -214,6 +215,7 @@ add_math_entrypoint_object(floorf)
add_math_entrypoint_object(floorl)
add_math_entrypoint_object(floorf16)
add_math_entrypoint_object(floorf128)
add_math_entrypoint_object(floorbf16)

add_math_entrypoint_object(fma)
add_math_entrypoint_object(fmaf)
Expand Down Expand Up @@ -463,12 +465,14 @@ add_math_entrypoint_object(roundf)
add_math_entrypoint_object(roundl)
add_math_entrypoint_object(roundf16)
add_math_entrypoint_object(roundf128)
add_math_entrypoint_object(roundbf16)

add_math_entrypoint_object(roundeven)
add_math_entrypoint_object(roundevenf)
add_math_entrypoint_object(roundevenl)
add_math_entrypoint_object(roundevenf16)
add_math_entrypoint_object(roundevenf128)
add_math_entrypoint_object(roundevenbf16)

add_math_entrypoint_object(scalbln)
add_math_entrypoint_object(scalblnf)
Expand Down Expand Up @@ -546,6 +550,7 @@ add_math_entrypoint_object(truncf)
add_math_entrypoint_object(truncl)
add_math_entrypoint_object(truncf16)
add_math_entrypoint_object(truncf128)
add_math_entrypoint_object(truncbf16)

add_math_entrypoint_object(ufromfp)
add_math_entrypoint_object(ufromfpf)
Expand Down
21 changes: 21 additions & 0 deletions libc/src/math/ceilbf16.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation header for ceilbf16 ----------------------*- 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_CEILBF16_H
#define LLVM_LIBC_SRC_MATH_CEILBF16_H

#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/types.h"

namespace LIBC_NAMESPACE_DECL {

bfloat16 ceilbf16(bfloat16 x);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_MATH_CEILBF16_H
21 changes: 21 additions & 0 deletions libc/src/math/floorbf16.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation header for floorbf16 ---------------------*- 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_FLOORBF16_H
#define LLVM_LIBC_SRC_MATH_FLOORBF16_H

#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/types.h"

namespace LIBC_NAMESPACE_DECL {

bfloat16 floorbf16(bfloat16 x);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_MATH_FLOORBF16_H
75 changes: 75 additions & 0 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,21 @@ add_entrypoint_object(
libc.src.__support.FPUtil.nearest_integer_operations
)

add_entrypoint_object(
ceilbf16
SRCS
ceilbf16.cpp
HDRS
../ceilbf16.h
DEPENDS
libc.src.__support.common
libc.src.__support.FPUtil.bfloat16
libc.src.__support.FPUtil.nearest_integer_operations
libc.src.__support.macros.config
FLAGS
ROUND_OPT
)

add_entrypoint_object(
daddl
SRCS
Expand Down Expand Up @@ -801,6 +816,21 @@ add_entrypoint_object(
libc.src.__support.FPUtil.nearest_integer_operations
)

add_entrypoint_object(
truncbf16
SRCS
truncbf16.cpp
HDRS
../truncbf16.h
DEPENDS
libc.src.__support.common
libc.src.__support.FPUtil.bfloat16
libc.src.__support.FPUtil.nearest_integer_operations
libc.src.__support.macros.config
FLAGS
ROUND_OPT
)

add_entrypoint_object(
floor
SRCS
Expand Down Expand Up @@ -861,6 +891,21 @@ add_entrypoint_object(
libc.src.__support.FPUtil.nearest_integer_operations
)

add_entrypoint_object(
floorbf16
SRCS
floorbf16.cpp
HDRS
../floorbf16.h
DEPENDS
libc.src.__support.common
libc.src.__support.FPUtil.bfloat16
libc.src.__support.FPUtil.nearest_integer_operations
libc.src.__support.macros.config
FLAGS
ROUND_OPT
)

add_entrypoint_object(
round
SRCS
Expand Down Expand Up @@ -921,6 +966,21 @@ add_entrypoint_object(
libc.src.__support.FPUtil.nearest_integer_operations
)

add_entrypoint_object(
roundbf16
SRCS
roundbf16.cpp
HDRS
../roundbf16.h
DEPENDS
libc.src.__support.common
libc.src.__support.FPUtil.bfloat16
libc.src.__support.FPUtil.nearest_integer_operations
libc.src.__support.macros.config
FLAGS
ROUND_OPT
)

add_entrypoint_object(
roundeven
SRCS
Expand Down Expand Up @@ -981,6 +1041,21 @@ add_entrypoint_object(
libc.src.__support.FPUtil.nearest_integer_operations
)

add_entrypoint_object(
roundevenbf16
SRCS
roundevenbf16.cpp
HDRS
../roundevenbf16.h
DEPENDS
libc.src.__support.common
libc.src.__support.FPUtil.bfloat16
libc.src.__support.FPUtil.nearest_integer_operations
libc.src.__support.macros.config
FLAGS
ROUND_OPT
)

add_entrypoint_object(
lround
SRCS
Expand Down
19 changes: 19 additions & 0 deletions libc/src/math/generic/ceilbf16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===-- Implementation of ceilbf16 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/ceilbf16.h"
#include "src/__support/FPUtil/NearestIntegerOperations.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, ceilbf16, (bfloat16 x)) { return fputil::ceil(x); }

} // namespace LIBC_NAMESPACE_DECL
21 changes: 21 additions & 0 deletions libc/src/math/generic/floorbf16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation of floorbf16 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/floorbf16.h"
#include "src/__support/FPUtil/NearestIntegerOperations.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, floorbf16, (bfloat16 x)) {
return fputil::floor(x);
}

} // namespace LIBC_NAMESPACE_DECL
Loading
Loading