Skip to content

[libc][math][c++23] Add bf16{add,sub}{,f,l,f128} math functions #152774

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 12 commits into from
Aug 8, 2025
Merged
14 changes: 14 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -896,13 +896,27 @@ endif()
list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
libc.src.math.ceilbf16
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
libc.src.math.bf16sub
libc.src.math.bf16subf
libc.src.math.bf16subl
libc.src.math.fabsbf16
libc.src.math.floorbf16
libc.src.math.roundbf16
libc.src.math.roundevenbf16
libc.src.math.truncbf16
)

if(LIBC_TYPES_HAS_FLOAT128)
list(APPEND TARGET_LIBM_ENTRYPOINTS
# math.h C++23 mixed bfloat16 and _Float128 entrypoints
libc.src.math.bf16addf128
libc.src.math.bf16subf128
)
endif()

if(LIBC_COMPILER_HAS_FIXED_POINT)
list(APPEND TARGET_LIBM_ENTRYPOINTS
# stdfix.h _Fract and _Accum entrypoints
Expand Down
10 changes: 10 additions & 0 deletions libc/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -563,3 +563,13 @@ add_math_entrypoint_object(ufromfpxf)
add_math_entrypoint_object(ufromfpxl)
add_math_entrypoint_object(ufromfpxf16)
add_math_entrypoint_object(ufromfpxf128)

add_math_entrypoint_object(bf16add)
add_math_entrypoint_object(bf16addf)
add_math_entrypoint_object(bf16addl)
add_math_entrypoint_object(bf16addf128)

add_math_entrypoint_object(bf16sub)
add_math_entrypoint_object(bf16subf)
add_math_entrypoint_object(bf16subl)
add_math_entrypoint_object(bf16subf128)
21 changes: 21 additions & 0 deletions libc/src/math/bf16add.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation header for bf16add -----------------------*- 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_BF16ADD_H
#define LLVM_LIBC_SRC_MATH_BF16ADD_H

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

namespace LIBC_NAMESPACE_DECL {

bfloat16 bf16add(double x, double y);

} // namespace LIBC_NAMESPACE_DECL

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

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

namespace LIBC_NAMESPACE_DECL {

bfloat16 bf16addf(float x, float y);

} // namespace LIBC_NAMESPACE_DECL

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

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

namespace LIBC_NAMESPACE_DECL {

bfloat16 bf16addf128(float128 x, float128 y);

} // namespace LIBC_NAMESPACE_DECL

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

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

namespace LIBC_NAMESPACE_DECL {

bfloat16 bf16addl(long double x, long double y);

} // namespace LIBC_NAMESPACE_DECL

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

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

namespace LIBC_NAMESPACE_DECL {

bfloat16 bf16sub(double x, double y);

} // namespace LIBC_NAMESPACE_DECL

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

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

namespace LIBC_NAMESPACE_DECL {

bfloat16 bf16subf(float x, float y);

} // namespace LIBC_NAMESPACE_DECL

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

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

namespace LIBC_NAMESPACE_DECL {

bfloat16 bf16subf128(float128 x, float128 y);

} // namespace LIBC_NAMESPACE_DECL

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

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

namespace LIBC_NAMESPACE_DECL {

bfloat16 bf16subl(long double x, long double y);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_MATH_BF16SUBL_H
113 changes: 113 additions & 0 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4911,3 +4911,116 @@ add_header_library(
libc.src.__support.math.expf16_utils
libc.src.__support.math.exp10_float16_constants
)

add_entrypoint_object(
bf16add
SRCS
bf16add.cpp
HDRS
../bf16add.h
DEPENDS
libc.src.__support.common
libc.src.__support.FPUtil.bfloat16
libc.src.__support.FPUtil.generic.add_sub
libc.src.__support.macros.config
libc.src.__support.macros.properties.types
)

add_entrypoint_object(
bf16addf
SRCS
bf16addf.cpp
HDRS
../bf16addf.h
DEPENDS
libc.src.__support.common
libc.src.__support.FPUtil.bfloat16
libc.src.__support.FPUtil.generic.add_sub
libc.src.__support.macros.config
libc.src.__support.macros.properties.types
)

add_entrypoint_object(
bf16addl
SRCS
bf16addl.cpp
HDRS
../bf16addl.h
DEPENDS
libc.src.__support.common
libc.src.__support.FPUtil.bfloat16
libc.src.__support.FPUtil.generic.add_sub
libc.src.__support.macros.config
libc.src.__support.macros.properties.types
)

add_entrypoint_object(
bf16addf128
SRCS
bf16addf128.cpp
HDRS
../bf16addf128.h
DEPENDS
libc.src.__support.common
libc.src.__support.FPUtil.bfloat16
libc.src.__support.FPUtil.generic.add_sub
libc.src.__support.macros.config
libc.src.__support.macros.properties.types
)


add_entrypoint_object(
bf16sub
SRCS
bf16sub.cpp
HDRS
../bf16sub.h
DEPENDS
libc.src.__support.common
libc.src.__support.FPUtil.bfloat16
libc.src.__support.FPUtil.generic.add_sub
libc.src.__support.macros.config
libc.src.__support.macros.properties.types
)

add_entrypoint_object(
bf16subf
SRCS
bf16subf.cpp
HDRS
../bf16subf.h
DEPENDS
libc.src.__support.common
libc.src.__support.FPUtil.bfloat16
libc.src.__support.FPUtil.generic.add_sub
libc.src.__support.macros.config
libc.src.__support.macros.properties.types
)

add_entrypoint_object(
bf16subl
SRCS
bf16subl.cpp
HDRS
../bf16subl.h
DEPENDS
libc.src.__support.common
libc.src.__support.FPUtil.bfloat16
libc.src.__support.FPUtil.generic.add_sub
libc.src.__support.macros.config
libc.src.__support.macros.properties.types
)

add_entrypoint_object(
bf16subf128
SRCS
bf16subf128.cpp
HDRS
../bf16subf128.h
DEPENDS
libc.src.__support.common
libc.src.__support.FPUtil.bfloat16
libc.src.__support.FPUtil.generic.add_sub
libc.src.__support.macros.config
libc.src.__support.macros.properties.types
)
21 changes: 21 additions & 0 deletions libc/src/math/generic/bf16add.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation of bf16add 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/bf16add.h"
#include "src/__support/FPUtil/bfloat16.h"
#include "src/__support/FPUtil/generic/add_sub.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(bfloat16, bf16add, (double x, double y)) {
return fputil::generic::add<bfloat16>(x, y);
}

} // namespace LIBC_NAMESPACE_DECL
21 changes: 21 additions & 0 deletions libc/src/math/generic/bf16addf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation of bf16addf 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/bf16addf.h"
#include "src/__support/FPUtil/bfloat16.h"
#include "src/__support/FPUtil/generic/add_sub.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(bfloat16, bf16addf, (float x, float y)) {
return fputil::generic::add<bfloat16>(x, y);
}

} // namespace LIBC_NAMESPACE_DECL
Loading
Loading