[libc][math][c++23] Add bf16div{,f,l,f128} math functions (#153191)

This PR adds the following basic math functions for BFloat16 type along
with the tests:
- bf16div
- bf16divf
- bf16divl
- bf16divf128

---------

Signed-off-by: Krishna Pandey <kpandey81930@gmail.com>
This commit is contained in:
Krishna Pandey 2025-08-12 21:46:22 +05:30 committed by GitHub
parent 38d854c6e8
commit c819c246f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
32 changed files with 499 additions and 1 deletions

View File

@ -760,6 +760,9 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
libc.src.math.bf16div
libc.src.math.bf16divf
libc.src.math.bf16divl
libc.src.math.bf16mul
libc.src.math.bf16mulf
libc.src.math.bf16mull
@ -788,6 +791,7 @@ 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.bf16divf128
libc.src.math.bf16mulf128
libc.src.math.bf16subf128
)

View File

@ -763,6 +763,9 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
libc.src.math.bf16div
libc.src.math.bf16divf
libc.src.math.bf16divl
libc.src.math.bf16mul
libc.src.math.bf16mulf
libc.src.math.bf16mull
@ -791,6 +794,7 @@ 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.bf16divf128
libc.src.math.bf16mulf128
libc.src.math.bf16subf128
)

View File

@ -763,6 +763,9 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
libc.src.math.bf16div
libc.src.math.bf16divf
libc.src.math.bf16divl
libc.src.math.bf16mul
libc.src.math.bf16mulf
libc.src.math.bf16mull
@ -791,6 +794,7 @@ 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.bf16divf128
libc.src.math.bf16mulf128
libc.src.math.bf16subf128
)

View File

@ -593,6 +593,9 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
libc.src.math.bf16div
libc.src.math.bf16divf
libc.src.math.bf16divl
libc.src.math.bf16mul
libc.src.math.bf16mulf
libc.src.math.bf16mull

View File

@ -236,6 +236,9 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
libc.src.math.bf16div
libc.src.math.bf16divf
libc.src.math.bf16divl
libc.src.math.bf16mul
libc.src.math.bf16mulf
libc.src.math.bf16mull

View File

@ -619,6 +619,9 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
libc.src.math.bf16div
libc.src.math.bf16divf
libc.src.math.bf16divl
libc.src.math.bf16mul
libc.src.math.bf16mulf
libc.src.math.bf16mull

View File

@ -620,6 +620,9 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
libc.src.math.bf16div
libc.src.math.bf16divf
libc.src.math.bf16divl
libc.src.math.bf16mul
libc.src.math.bf16mulf
libc.src.math.bf16mull

View File

@ -847,6 +847,9 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
libc.src.math.bf16div
libc.src.math.bf16divf
libc.src.math.bf16divl
libc.src.math.bf16mul
libc.src.math.bf16mulf
libc.src.math.bf16mull
@ -875,6 +878,7 @@ 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.bf16divf128
libc.src.math.bf16mulf128
libc.src.math.bf16subf128
)

View File

@ -463,6 +463,9 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
libc.src.math.bf16div
libc.src.math.bf16divf
libc.src.math.bf16divl
libc.src.math.bf16mul
libc.src.math.bf16mulf
libc.src.math.bf16mull

View File

@ -866,6 +866,9 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
libc.src.math.bf16div
libc.src.math.bf16divf
libc.src.math.bf16divl
libc.src.math.bf16mul
libc.src.math.bf16mulf
libc.src.math.bf16mull
@ -894,6 +897,7 @@ 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.bf16divf128
libc.src.math.bf16mulf128
libc.src.math.bf16subf128
)

View File

@ -898,6 +898,9 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
libc.src.math.bf16div
libc.src.math.bf16divf
libc.src.math.bf16divl
libc.src.math.bf16mul
libc.src.math.bf16mulf
libc.src.math.bf16mull
@ -926,6 +929,7 @@ 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.bf16divf128
libc.src.math.bf16mulf128
libc.src.math.bf16subf128
)

View File

@ -309,6 +309,9 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
libc.src.math.bf16div
libc.src.math.bf16divf
libc.src.math.bf16divl
libc.src.math.bf16mul
libc.src.math.bf16mulf
libc.src.math.bf16mull

View File

@ -579,6 +579,11 @@ add_math_entrypoint_object(bf16addf)
add_math_entrypoint_object(bf16addl)
add_math_entrypoint_object(bf16addf128)
add_math_entrypoint_object(bf16div)
add_math_entrypoint_object(bf16divf)
add_math_entrypoint_object(bf16divl)
add_math_entrypoint_object(bf16divf128)
add_math_entrypoint_object(bf16mul)
add_math_entrypoint_object(bf16mulf)
add_math_entrypoint_object(bf16mull)

21
libc/src/math/bf16div.h Normal file
View File

@ -0,0 +1,21 @@
//===-- Implementation header for bf16div -----------------------*- 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_BF16DIV_H
#define LLVM_LIBC_SRC_MATH_BF16DIV_H
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/types.h"
namespace LIBC_NAMESPACE_DECL {
bfloat16 bf16div(double x, double y);
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SRC_MATH_BF16DIV_H

21
libc/src/math/bf16divf.h Normal file
View File

@ -0,0 +1,21 @@
//===-- Implementation header for bf16divf ----------------------*- 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_BF16DIVF_H
#define LLVM_LIBC_SRC_MATH_BF16DIVF_H
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/types.h"
namespace LIBC_NAMESPACE_DECL {
bfloat16 bf16divf(float x, float y);
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SRC_MATH_BF16DIVF_H

View File

@ -0,0 +1,21 @@
//===-- Implementation header for bf16divf128 -------------------*- 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_BF16DIVF128_H
#define LLVM_LIBC_SRC_MATH_BF16DIVF128_H
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/types.h"
namespace LIBC_NAMESPACE_DECL {
bfloat16 bf16divf128(float128 x, float128 y);
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SRC_MATH_BF16DIVF128_H

21
libc/src/math/bf16divl.h Normal file
View File

@ -0,0 +1,21 @@
//===-- Implementation header for bf16divl ----------------------*- 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_BF16DIVL_H
#define LLVM_LIBC_SRC_MATH_BF16DIVL_H
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/types.h"
namespace LIBC_NAMESPACE_DECL {
bfloat16 bf16divl(long double x, long double y);
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SRC_MATH_BF16DIVL_H

View File

@ -5049,6 +5049,61 @@ add_entrypoint_object(
libc.src.__support.macros.properties.types
)
add_entrypoint_object(
bf16div
SRCS
bf16div.cpp
HDRS
../bf16div.h
DEPENDS
libc.src.__support.common
libc.src.__support.FPUtil.bfloat16
libc.src.__support.FPUtil.generic.div
libc.src.__support.macros.config
libc.src.__support.macros.properties.types
)
add_entrypoint_object(
bf16divf
SRCS
bf16divf.cpp
HDRS
../bf16divf.h
DEPENDS
libc.src.__support.common
libc.src.__support.FPUtil.bfloat16
libc.src.__support.FPUtil.generic.div
libc.src.__support.macros.config
libc.src.__support.macros.properties.types
)
add_entrypoint_object(
bf16divl
SRCS
bf16divl.cpp
HDRS
../bf16divl.h
DEPENDS
libc.src.__support.common
libc.src.__support.FPUtil.bfloat16
libc.src.__support.FPUtil.generic.div
libc.src.__support.macros.config
libc.src.__support.macros.properties.types
)
add_entrypoint_object(
bf16divf128
SRCS
bf16divf128.cpp
HDRS
../bf16divf128.h
DEPENDS
libc.src.__support.common
libc.src.__support.FPUtil.bfloat16
libc.src.__support.FPUtil.generic.div
libc.src.__support.macros.config
libc.src.__support.macros.properties.types
)
add_entrypoint_object(
bf16sub

View File

@ -0,0 +1,21 @@
//===-- Implementation of bf16div 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/bf16div.h"
#include "src/__support/FPUtil/bfloat16.h"
#include "src/__support/FPUtil/generic/div.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(bfloat16, bf16div, (double x, double y)) {
return fputil::generic::div<bfloat16>(x, y);
}
} // namespace LIBC_NAMESPACE_DECL

View File

@ -0,0 +1,21 @@
//===-- Implementation of bf16divf 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/bf16divf.h"
#include "src/__support/FPUtil/bfloat16.h"
#include "src/__support/FPUtil/generic/div.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(bfloat16, bf16divf, (float x, float y)) {
return fputil::generic::div<bfloat16>(x, y);
}
} // namespace LIBC_NAMESPACE_DECL

View File

@ -0,0 +1,21 @@
//===-- Implementation of bf16divf128 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/bf16divf128.h"
#include "src/__support/FPUtil/bfloat16.h"
#include "src/__support/FPUtil/generic/div.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(bfloat16, bf16divf128, (float128 x, float128 y)) {
return fputil::generic::div<bfloat16>(x, y);
}
} // namespace LIBC_NAMESPACE_DECL

View File

@ -0,0 +1,21 @@
//===-- Implementation of bf16divl 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/bf16divl.h"
#include "src/__support/FPUtil/bfloat16.h"
#include "src/__support/FPUtil/generic/div.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(bfloat16, bf16divl, (long double x, long double y)) {
return fputil::generic::div<bfloat16>(x, y);
}
} // namespace LIBC_NAMESPACE_DECL

View File

@ -3028,6 +3028,62 @@ add_fp_unittest(
libc.src.__support.FPUtil.bfloat16
)
add_fp_unittest(
bf16div_test
NEED_MPFR
SUITE
libc-math-unittests
SRCS
bf16div_test.cpp
HDRS
DivTest.h
DEPENDS
libc.src.math.bf16div
libc.src.__support.FPUtil.bfloat16
)
add_fp_unittest(
bf16divf_test
NEED_MPFR
SUITE
libc-math-unittests
SRCS
bf16divf_test.cpp
HDRS
DivTest.h
DEPENDS
libc.src.math.bf16divf
libc.src.__support.FPUtil.bfloat16
)
add_fp_unittest(
bf16divl_test
NEED_MPFR
SUITE
libc-math-unittests
SRCS
bf16divl_test.cpp
HDRS
DivTest.h
DEPENDS
libc.src.math.bf16divl
libc.src.__support.FPUtil.bfloat16
)
add_fp_unittest(
bf16divf128_test
NEED_MPFR
SUITE
libc-math-unittests
SRCS
bf16divf128_test.cpp
HDRS
DivTest.h
DEPENDS
libc.src.math.bf16divf128
libc.src.__support.FPUtil.bfloat16
)
add_fp_unittest(
bf16mul_test
NEED_MPFR

View File

@ -0,0 +1,14 @@
//===-- Unittests for bf16div ---------------------------------------------===//
//
// 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 "DivTest.h"
#include "src/__support/FPUtil/bfloat16.h"
#include "src/math/bf16div.h"
LIST_DIV_TESTS(bfloat16, double, LIBC_NAMESPACE::bf16div)

View File

@ -0,0 +1,14 @@
//===-- Unittests for bf16divf128 -----------------------------------------===//
//
// 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 "DivTest.h"
#include "src/__support/FPUtil/bfloat16.h"
#include "src/math/bf16divf128.h"
LIST_DIV_TESTS(bfloat16, float128, LIBC_NAMESPACE::bf16divf128)

View File

@ -0,0 +1,14 @@
//===-- Unittests for bf16divf --------------------------------------------===//
//
// 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 "DivTest.h"
#include "src/__support/FPUtil/bfloat16.h"
#include "src/math/bf16divf.h"
LIST_DIV_TESTS(bfloat16, float, LIBC_NAMESPACE::bf16divf)

View File

@ -0,0 +1,14 @@
//===-- Unittests for bf16divl --------------------------------------------===//
//
// 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 "DivTest.h"
#include "src/__support/FPUtil/bfloat16.h"
#include "src/math/bf16divl.h"
LIST_DIV_TESTS(bfloat16, long double, LIBC_NAMESPACE::bf16divl)

View File

@ -5680,6 +5680,66 @@ add_fp_unittest(
libc.src.__support.macros.properties.os
)
add_fp_unittest(
bf16div_test
SUITE
libc-math-smoke-tests
SRCS
bf16div_test.cpp
HDRS
DivTest.h
DEPENDS
libc.hdr.errno_macros
libc.hdr.fenv_macros
libc.src.math.bf16div
libc.src.__support.FPUtil.bfloat16
)
add_fp_unittest(
bf16divf_test
SUITE
libc-math-smoke-tests
SRCS
bf16divf_test.cpp
HDRS
DivTest.h
DEPENDS
libc.hdr.errno_macros
libc.hdr.fenv_macros
libc.src.math.bf16divf
libc.src.__support.FPUtil.bfloat16
)
add_fp_unittest(
bf16divl_test
SUITE
libc-math-smoke-tests
SRCS
bf16divl_test.cpp
HDRS
DivTest.h
DEPENDS
libc.hdr.errno_macros
libc.hdr.fenv_macros
libc.src.math.bf16divl
libc.src.__support.FPUtil.bfloat16
)
add_fp_unittest(
bf16divf128_test
SUITE
libc-math-smoke-tests
SRCS
bf16divf128_test.cpp
HDRS
DivTest.h
DEPENDS
libc.hdr.errno_macros
libc.hdr.fenv_macros
libc.src.math.bf16divf128
libc.src.__support.FPUtil.bfloat16
)
add_fp_unittest(
bf16mul_test
SUITE
@ -5687,7 +5747,7 @@ add_fp_unittest(
SRCS
bf16mul_test.cpp
HDRS
AddTest.h
MulTest.h
DEPENDS
libc.hdr.errno_macros
libc.hdr.fenv_macros

View File

@ -0,0 +1,14 @@
//===-- Unittests for bf16div ---------------------------------------------===//
//
// 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 "DivTest.h"
#include "src/__support/FPUtil/bfloat16.h"
#include "src/math/bf16div.h"
LIST_DIV_TESTS(bfloat16, double, LIBC_NAMESPACE::bf16div)

View File

@ -0,0 +1,14 @@
//===-- Unittests for bf16divf128 -----------------------------------------===//
//
// 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 "DivTest.h"
#include "src/__support/FPUtil/bfloat16.h"
#include "src/math/bf16divf128.h"
LIST_DIV_TESTS(bfloat16, float128, LIBC_NAMESPACE::bf16divf128)

View File

@ -0,0 +1,14 @@
//===-- Unittests for bf16divf --------------------------------------------===//
//
// 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 "DivTest.h"
#include "src/__support/FPUtil/bfloat16.h"
#include "src/math/bf16divf.h"
LIST_DIV_TESTS(bfloat16, float, LIBC_NAMESPACE::bf16divf)

View File

@ -0,0 +1,14 @@
//===-- Unittests for bf16divl --------------------------------------------===//
//
// 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 "DivTest.h"
#include "src/__support/FPUtil/bfloat16.h"
#include "src/math/bf16divl.h"
LIST_DIV_TESTS(bfloat16, long double, LIBC_NAMESPACE::bf16divl)