Skip to content

Commit 1ffb995

Browse files
authored
[libc][math][c++23] Add bf16{add,sub}{,f,l,f128} math functions (#152774)
This PR adds implements following basic math functions for BFloat16 type along with the tests: - bf16add - bf16addf - bf16addl - bf16addf128 - bf16sub - bf16subf - bf16subl - bf16subf128 --------- Signed-off-by: Krishna Pandey <[email protected]>
1 parent 08873be commit 1ffb995

38 files changed

+972
-0
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,13 +896,27 @@ endif()
896896
list(APPEND TARGET_LIBM_ENTRYPOINTS
897897
# bfloat16 entrypoints
898898
libc.src.math.ceilbf16
899+
libc.src.math.bf16add
900+
libc.src.math.bf16addf
901+
libc.src.math.bf16addl
902+
libc.src.math.bf16sub
903+
libc.src.math.bf16subf
904+
libc.src.math.bf16subl
899905
libc.src.math.fabsbf16
900906
libc.src.math.floorbf16
901907
libc.src.math.roundbf16
902908
libc.src.math.roundevenbf16
903909
libc.src.math.truncbf16
904910
)
905911

912+
if(LIBC_TYPES_HAS_FLOAT128)
913+
list(APPEND TARGET_LIBM_ENTRYPOINTS
914+
# math.h C++23 mixed bfloat16 and _Float128 entrypoints
915+
libc.src.math.bf16addf128
916+
libc.src.math.bf16subf128
917+
)
918+
endif()
919+
906920
if(LIBC_COMPILER_HAS_FIXED_POINT)
907921
list(APPEND TARGET_LIBM_ENTRYPOINTS
908922
# stdfix.h _Fract and _Accum entrypoints

libc/src/math/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,3 +563,13 @@ add_math_entrypoint_object(ufromfpxf)
563563
add_math_entrypoint_object(ufromfpxl)
564564
add_math_entrypoint_object(ufromfpxf16)
565565
add_math_entrypoint_object(ufromfpxf128)
566+
567+
add_math_entrypoint_object(bf16add)
568+
add_math_entrypoint_object(bf16addf)
569+
add_math_entrypoint_object(bf16addl)
570+
add_math_entrypoint_object(bf16addf128)
571+
572+
add_math_entrypoint_object(bf16sub)
573+
add_math_entrypoint_object(bf16subf)
574+
add_math_entrypoint_object(bf16subl)
575+
add_math_entrypoint_object(bf16subf128)

libc/src/math/bf16add.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for bf16add -----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_MATH_BF16ADD_H
10+
#define LLVM_LIBC_SRC_MATH_BF16ADD_H
11+
12+
#include "src/__support/macros/config.h"
13+
#include "src/__support/macros/properties/types.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
bfloat16 bf16add(double x, double y);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_MATH_BF16ADD_H

libc/src/math/bf16addf.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for bf16addf ----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_MATH_BF16ADDF_H
10+
#define LLVM_LIBC_SRC_MATH_BF16ADDF_H
11+
12+
#include "src/__support/macros/config.h"
13+
#include "src/__support/macros/properties/types.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
bfloat16 bf16addf(float x, float y);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_MATH_BF16ADDF_H

libc/src/math/bf16addf128.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for bf16addf128 -------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_MATH_BF16ADDF128_H
10+
#define LLVM_LIBC_SRC_MATH_BF16ADDF128_H
11+
12+
#include "src/__support/macros/config.h"
13+
#include "src/__support/macros/properties/types.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
bfloat16 bf16addf128(float128 x, float128 y);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_MATH_BF16ADDF128_H

libc/src/math/bf16addl.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for bf16addl ----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_MATH_BF16ADDL_H
10+
#define LLVM_LIBC_SRC_MATH_BF16ADDL_H
11+
12+
#include "src/__support/macros/config.h"
13+
#include "src/__support/macros/properties/types.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
bfloat16 bf16addl(long double x, long double y);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_MATH_BF16ADDL_H

libc/src/math/bf16sub.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for bf16sub -----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_MATH_BF16SUB_H
10+
#define LLVM_LIBC_SRC_MATH_BF16SUB_H
11+
12+
#include "src/__support/macros/config.h"
13+
#include "src/__support/macros/properties/types.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
bfloat16 bf16sub(double x, double y);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_MATH_BF16SUB_H

libc/src/math/bf16subf.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for bf16subf ----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_MATH_BF16SUBF_H
10+
#define LLVM_LIBC_SRC_MATH_BF16SUBF_H
11+
12+
#include "src/__support/macros/config.h"
13+
#include "src/__support/macros/properties/types.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
bfloat16 bf16subf(float x, float y);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_MATH_BF16SUBF_H

libc/src/math/bf16subf128.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for bf16subf128 -------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_MATH_BF16SUBF128_H
10+
#define LLVM_LIBC_SRC_MATH_BF16SUBF128_H
11+
12+
#include "src/__support/macros/config.h"
13+
#include "src/__support/macros/properties/types.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
bfloat16 bf16subf128(float128 x, float128 y);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_MATH_BF16SUBF128_H

libc/src/math/bf16subl.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for bf16subl ----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_MATH_BF16SUBL_H
10+
#define LLVM_LIBC_SRC_MATH_BF16SUBL_H
11+
12+
#include "src/__support/macros/config.h"
13+
#include "src/__support/macros/properties/types.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
bfloat16 bf16subl(long double x, long double y);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_MATH_BF16SUBL_H

0 commit comments

Comments
 (0)