diff options
Diffstat (limited to 'libc/src/math/generic')
22 files changed, 865 insertions, 8 deletions
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index 1a61ccb107bc..263c5dfd0832 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -53,6 +53,20 @@ add_entrypoint_object( ) add_entrypoint_object( + canonicalizebf16 + SRCS + canonicalizebf16.cpp + HDRS + ../canonicalizebf16.h + DEPENDS + libc.src.__support.common + libc.src.__support.macros.config + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.basic_operations + libc.src.__support.FPUtil.bfloat16 +) + +add_entrypoint_object( iscanonical SRCS iscanonical.cpp @@ -97,6 +111,20 @@ add_entrypoint_object( ) add_entrypoint_object( + iscanonicalbf16 + SRCS + iscanonicalbf16.cpp + HDRS + ../iscanonicalbf16.h + DEPENDS + libc.src.__support.common + libc.src.__support.macros.config + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.basic_operations + libc.src.__support.FPUtil.bfloat16 +) + +add_entrypoint_object( ceil SRCS ceil.cpp @@ -1749,6 +1777,22 @@ add_entrypoint_object( ) add_entrypoint_object( + copysignbf16 + SRCS + copysignbf16.cpp + HDRS + ../copysignbf16.h + DEPENDS + libc.src.__support.common + libc.src.__support.macros.config + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.bfloat16 + libc.src.__support.FPUtil.manipulation_functions + FLAGS + MISC_MATH_BASIC_OPS_OPT +) + +add_entrypoint_object( frexp SRCS frexp.cpp @@ -1799,6 +1843,20 @@ add_entrypoint_object( ) add_entrypoint_object( + frexpbf16 + SRCS + frexpbf16.cpp + HDRS + ../frexpbf16.h + DEPENDS + libc.src.__support.common + libc.src.__support.FPUtil.bfloat16 + libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.macros.config + libc.src.__support.macros.properties.types +) + +add_entrypoint_object( ilogb SRCS ilogb.cpp @@ -1851,6 +1909,20 @@ add_entrypoint_object( ) add_entrypoint_object( + ilogbbf16 + SRCS + ilogbbf16.cpp + HDRS + ../ilogbbf16.h + DEPENDS + libc.src.__support.common + libc.src.__support.FPUtil.bfloat16 + libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.macros.config + libc.src.__support.macros.properties.types +) + +add_entrypoint_object( llogb SRCS llogb.cpp @@ -1903,6 +1975,20 @@ add_entrypoint_object( ) add_entrypoint_object( + llogbbf16 + SRCS + llogbbf16.cpp + HDRS + ../llogbbf16.h + DEPENDS + libc.src.__support.common + libc.src.__support.FPUtil.bfloat16 + libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.macros.config + libc.src.__support.macros.properties.types +) + +add_entrypoint_object( ldexp SRCS ldexp.cpp @@ -1952,6 +2038,20 @@ add_entrypoint_object( libc.src.__support.math.ldexpf128 ) +add_entrypoint_object( + ldexpbf16 + SRCS + ldexpbf16.cpp + HDRS + ../ldexpbf16.h + DEPENDS + libc.src.__support.common + libc.src.__support.FPUtil.bfloat16 + libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.macros.config + libc.src.__support.macros.properties.types +) + add_object_library( common_constants HDRS @@ -2225,6 +2325,20 @@ add_entrypoint_object( ) add_entrypoint_object( + logbbf16 + SRCS + logbbf16.cpp + HDRS + ../logbbf16.h + DEPENDS + libc.src.__support.common + libc.src.__support.FPUtil.bfloat16 + libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.macros.config + libc.src.__support.macros.properties.types +) + +add_entrypoint_object( modf SRCS modf.cpp @@ -2277,6 +2391,20 @@ add_entrypoint_object( ) add_entrypoint_object( + modfbf16 + SRCS + modfbf16.cpp + HDRS + ../modfbf16.h + DEPENDS + libc.src.__support.common + libc.src.__support.FPUtil.bfloat16 + libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.macros.config + libc.src.__support.macros.properties.types +) + +add_entrypoint_object( fmin SRCS fmin.cpp @@ -3127,7 +3255,21 @@ add_entrypoint_object( libc.src.__support.macros.optimization libc.src.__support.macros.properties.types libc.src.__support.FPUtil.sqrt - ) +) + +add_entrypoint_object( + sqrtbf16 + SRCS + sqrtbf16.cpp + HDRS + ../sqrtbf16.h + DEPENDS + libc.src.__support.common + libc.src.__support.FPUtil.bfloat16 + libc.src.__support.FPUtil.sqrt + libc.src.__support.macros.config + libc.src.__support.macros.properties.types +) add_entrypoint_object( remquof @@ -3181,6 +3323,20 @@ add_entrypoint_object( ) add_entrypoint_object( + remquobf16 + SRCS + remquobf16.cpp + HDRS + ../remquobf16.h + DEPENDS + libc.src.__support.common + libc.src.__support.FPUtil.bfloat16 + libc.src.__support.FPUtil.division_and_remainder_operations + libc.src.__support.macros.properties.types + libc.src.__support.macros.config +) + +add_entrypoint_object( remainderf SRCS remainderf.cpp @@ -3233,6 +3389,20 @@ add_entrypoint_object( ) add_entrypoint_object( + remainderbf16 + SRCS + remainderbf16.cpp + HDRS + ../remainderbf16.h + DEPENDS + libc.src.__support.common + libc.src.__support.FPUtil.bfloat16 + libc.src.__support.FPUtil.division_and_remainder_operations + libc.src.__support.macros.properties.types + libc.src.__support.macros.config +) + +add_entrypoint_object( hypotf SRCS hypotf.cpp @@ -3316,6 +3486,20 @@ add_entrypoint_object( ) add_entrypoint_object( + fdimbf16 + SRCS + fdimbf16.cpp + HDRS + ../fdimbf16.h + DEPENDS + libc.src.__support.common + libc.src.__support.macros.config + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.basic_operations + libc.src.__support.FPUtil.bfloat16 +) + +add_entrypoint_object( fdiv SRCS fdiv.cpp @@ -3432,6 +3616,20 @@ add_entrypoint_object( ) add_entrypoint_object( + issignalingbf16 + SRCS + issignalingbf16.cpp + HDRS + ../issignalingbf16.h + DEPENDS + libc.src.__support.common + libc.src.__support.macros.config + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.basic_operations + libc.src.__support.FPUtil.bfloat16 +) + +add_entrypoint_object( isnan SRCS isnan.cpp @@ -3832,6 +4030,20 @@ add_entrypoint_object( ) add_entrypoint_object( + fmodbf16 + SRCS + fmodbf16.cpp + HDRS + ../fmodbf16.h + DEPENDS + libc.src.__support.common + libc.src.__support.macros.config + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.bfloat16 + libc.src.__support.FPUtil.generic.fmod +) + +add_entrypoint_object( fromfp SRCS fromfp.cpp @@ -4262,6 +4474,25 @@ add_entrypoint_object( ) add_entrypoint_object( + atanpif16 + SRCS + atanpif16.cpp + HDRS + ../atanpif16.h + DEPENDS + libc.hdr.errno_macros + libc.hdr.fenv_macros + libc.src.__support.FPUtil.cast + libc.src.__support.FPUtil.fenv_impl + libc.src.__support.FPUtil.fp_bits + libc.src.__support.FPUtil.multiply_add + libc.src.__support.FPUtil.polyeval + libc.src.__support.FPUtil.sqrt + libc.src.__support.macros.optimization + libc.src.__support.macros.properties.types +) + +add_entrypoint_object( asinf SRCS asinf.cpp @@ -4463,6 +4694,21 @@ add_entrypoint_object( ) add_entrypoint_object( + scalblnbf16 + SRCS + scalblnbf16.cpp + HDRS + ../scalblnbf16.h + DEPENDS + libc.hdr.float_macros + libc.src.__support.common + libc.src.__support.macros.config + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.bfloat16 + libc.src.__support.FPUtil.manipulation_functions +) + +add_entrypoint_object( scalbn SRCS scalbn.cpp @@ -4520,6 +4766,21 @@ add_entrypoint_object( ) add_entrypoint_object( + scalbnbf16 + SRCS + scalbnbf16.cpp + HDRS + ../scalblnbf16.h + DEPENDS + libc.hdr.float_macros + libc.src.__support.common + libc.src.__support.macros.config + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.bfloat16 + libc.src.__support.FPUtil.manipulation_functions +) + +add_entrypoint_object( fmaf SRCS fmaf.cpp @@ -4600,6 +4861,21 @@ add_entrypoint_object( libc.src.__support.FPUtil.basic_operations libc.src.__support.macros.properties.types ) + +add_entrypoint_object( + totalorderbf16 + SRCS + totalorderbf16.cpp + HDRS + ../totalorderbf16.h + DEPENDS + libc.src.__support.common + libc.src.__support.macros.config + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.basic_operations + libc.src.__support.FPUtil.bfloat16 +) + add_entrypoint_object( totalordermag SRCS @@ -4652,6 +4928,20 @@ add_entrypoint_object( ) add_entrypoint_object( + totalordermagbf16 + SRCS + totalordermagbf16.cpp + HDRS + ../totalordermagbf16.h + DEPENDS + libc.src.__support.common + libc.src.__support.macros.config + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.basic_operations + libc.src.__support.FPUtil.bfloat16 +) + +add_entrypoint_object( getpayload SRCS getpayload.cpp diff --git a/libc/src/math/generic/atanpif16.cpp b/libc/src/math/generic/atanpif16.cpp new file mode 100644 index 000000000000..c54087c7165f --- /dev/null +++ b/libc/src/math/generic/atanpif16.cpp @@ -0,0 +1,157 @@ +//===-- Half-precision atanpi 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/atanpif16.h" +#include "hdr/errno_macros.h" +#include "hdr/fenv_macros.h" +#include "src/__support/FPUtil/FEnvImpl.h" +#include "src/__support/FPUtil/FPBits.h" +#include "src/__support/FPUtil/PolyEval.h" +#include "src/__support/FPUtil/cast.h" +#include "src/__support/FPUtil/multiply_add.h" +#include "src/__support/FPUtil/sqrt.h" +#include "src/__support/macros/optimization.h" + +namespace LIBC_NAMESPACE_DECL { + +// Using Python's SymPy library, we can obtain the polynomial approximation of +// arctan(x)/pi. The steps are as follows: +// >>> from sympy import * +// >>> import math +// >>> x = symbols('x') +// >>> print(series(atan(x)/math.pi, x, 0, 17)) +// +// Output: +// 0.318309886183791*x - 0.106103295394597*x**3 + 0.0636619772367581*x**5 - +// 0.0454728408833987*x**7 + 0.0353677651315323*x**9 - 0.0289372623803446*x**11 +// + 0.0244853758602916*x**13 - 0.0212206590789194*x**15 + O(x**17) +// +// We will assign this degree-15 Taylor polynomial as g(x). This polynomial +// approximation is accurate for arctan(x)/pi when |x| is in the range [0, 0.5]. +// +// +// To compute arctan(x) for all real x, we divide the domain into the following +// cases: +// +// * Case 1: |x| <= 0.5 +// In this range, the direct polynomial approximation is used: +// arctan(x)/pi = sign(x) * g(|x|) +// or equivalently, arctan(x) = sign(x) * pi * g(|x|). +// +// * Case 2: 0.5 < |x| <= 1 +// We use the double-angle identity for the tangent function, specifically: +// arctan(x) = 2 * arctan(x / (1 + sqrt(1 + x^2))). +// Applying this, we have: +// arctan(x)/pi = sign(x) * 2 * arctan(x')/pi, +// where x' = |x| / (1 + sqrt(1 + x^2)). +// Thus, arctan(x)/pi = sign(x) * 2 * g(x') +// +// When |x| is in (0.5, 1], the value of x' will always fall within the +// interval [0.207, 0.414], which is within the accurate range of g(x). +// +// * Case 3: |x| > 1 +// For values of |x| greater than 1, we use the reciprocal transformation +// identity: +// arctan(x) = pi/2 - arctan(1/x) for x > 0. +// For any x (real number), this generalizes to: +// arctan(x)/pi = sign(x) * (1/2 - arctan(1/|x|)/pi). +// Then, using g(x) for arctan(1/|x|)/pi: +// arctan(x)/pi = sign(x) * (1/2 - g(1/|x|)). +// +// Note that if 1/|x| still falls outside the +// g(x)'s primary range of accuracy (i.e., if 0.5 < 1/|x| <= 1), the rule +// from Case 2 must be applied recursively to 1/|x|. + +LLVM_LIBC_FUNCTION(float16, atanpif16, (float16 x)) { + using FPBits = fputil::FPBits<float16>; + + FPBits xbits(x); + bool is_neg = xbits.is_neg(); + + auto signed_result = [is_neg](double r) -> float16 { + return fputil::cast<float16>(is_neg ? -r : r); + }; + + if (LIBC_UNLIKELY(xbits.is_inf_or_nan())) { + if (xbits.is_nan()) { + if (xbits.is_signaling_nan()) { + fputil::raise_except_if_required(FE_INVALID); + return FPBits::quiet_nan().get_val(); + } + return x; + } + // atanpi(±∞) = ±0.5 + return signed_result(0.5); + } + + if (LIBC_UNLIKELY(xbits.is_zero())) + return x; + + double x_abs = fputil::cast<double>(xbits.abs().get_val()); + + if (LIBC_UNLIKELY(x_abs == 1.0)) + return signed_result(0.25); + + // evaluate atan(x)/pi using polynomial approximation, valid for |x| <= 0.5 + constexpr auto atanpi_eval = [](double x) -> double { + // polynomial coefficients for atan(x)/pi taylor series + // generated using sympy: series(atan(x)/pi, x, 0, 17) + constexpr static double POLY_COEFFS[] = { + 0x1.45f306dc9c889p-2, // x^1: 1/pi + -0x1.b2995e7b7b60bp-4, // x^3: -1/(3*pi) + 0x1.04c26be3b06ccp-4, // x^5: 1/(5*pi) + -0x1.7483758e69c08p-5, // x^7: -1/(7*pi) + 0x1.21bb945252403p-5, // x^9: 1/(9*pi) + -0x1.da1bace3cc68ep-6, // x^11: -1/(11*pi) + 0x1.912b1c2336cf2p-6, // x^13: 1/(13*pi) + -0x1.5bade52f95e7p-6, // x^15: -1/(15*pi) + }; + double x_sq = x * x; + return x * fputil::polyeval(x_sq, POLY_COEFFS[0], POLY_COEFFS[1], + POLY_COEFFS[2], POLY_COEFFS[3], POLY_COEFFS[4], + POLY_COEFFS[5], POLY_COEFFS[6], POLY_COEFFS[7]); + }; + + // Case 1: |x| <= 0.5 - Direct polynomial evaluation + if (LIBC_LIKELY(x_abs <= 0.5)) { + double result = atanpi_eval(x_abs); + return signed_result(result); + } + + // case 2: 0.5 < |x| <= 1 - use double-angle reduction + // atan(x) = 2 * atan(x / (1 + sqrt(1 + x^2))) + // so atanpi(x) = 2 * atanpi(x') where x' = x / (1 + sqrt(1 + x^2)) + if (x_abs <= 1.0) { + double x_abs_sq = x_abs * x_abs; + double sqrt_term = fputil::sqrt<double>(1.0 + x_abs_sq); + double x_prime = x_abs / (1.0 + sqrt_term); + double result = 2.0 * atanpi_eval(x_prime); + return signed_result(result); + } + + // case 3: |x| > 1 - use reciprocal transformation + // atan(x) = pi/2 - atan(1/x) for x > 0 + // so atanpi(x) = 1/2 - atanpi(1/x) + double x_recip = 1.0 / x_abs; + double result; + + // if 1/|x| > 0.5, we need to apply Case 2 transformation to 1/|x| + if (x_recip > 0.5) { + double x_recip_sq = x_recip * x_recip; + double sqrt_term = fputil::sqrt<double>(1.0 + x_recip_sq); + double x_prime = x_recip / (1.0 + sqrt_term); + result = fputil::multiply_add(-2.0, atanpi_eval(x_prime), 0.5); + } else { + // direct evaluation since 1/|x| <= 0.5 + result = 0.5 - atanpi_eval(x_recip); + } + + return signed_result(result); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/canonicalizebf16.cpp b/libc/src/math/generic/canonicalizebf16.cpp new file mode 100644 index 000000000000..9cc379060c3d --- /dev/null +++ b/libc/src/math/generic/canonicalizebf16.cpp @@ -0,0 +1,21 @@ +//===-- Implementation of canonicalizebf16 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/canonicalizebf16.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(int, canonicalizebf16, (bfloat16 * cx, const bfloat16 *x)) { + return fputil::canonicalize(*cx, *x); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/copysignbf16.cpp b/libc/src/math/generic/copysignbf16.cpp new file mode 100644 index 000000000000..48ade2b26981 --- /dev/null +++ b/libc/src/math/generic/copysignbf16.cpp @@ -0,0 +1,21 @@ +//===-- Implementation of copysignbf16 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/copysignbf16.h" +#include "src/__support/FPUtil/ManipulationFunctions.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, copysignbf16, (bfloat16 x, bfloat16 y)) { + return fputil::copysign(x, y); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/fdimbf16.cpp b/libc/src/math/generic/fdimbf16.cpp new file mode 100644 index 000000000000..0f54055b763e --- /dev/null +++ b/libc/src/math/generic/fdimbf16.cpp @@ -0,0 +1,21 @@ +//===-- Implementation of fdimbf16 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/fdimbf16.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, fdimbf16, (bfloat16 x, bfloat16 y)) { + return fputil::fdim(x, y); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/fmodbf16.cpp b/libc/src/math/generic/fmodbf16.cpp new file mode 100644 index 000000000000..902a680fe860 --- /dev/null +++ b/libc/src/math/generic/fmodbf16.cpp @@ -0,0 +1,21 @@ +//===-- Implementation of fmodbf16 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/fmodbf16.h" +#include "src/__support/FPUtil/bfloat16.h" +#include "src/__support/FPUtil/generic/FMod.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(bfloat16, fmodbf16, (bfloat16 x, bfloat16 y)) { + return fputil::generic::FMod<bfloat16>::eval(x, y); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/frexpbf16.cpp b/libc/src/math/generic/frexpbf16.cpp new file mode 100644 index 000000000000..004f64f282f7 --- /dev/null +++ b/libc/src/math/generic/frexpbf16.cpp @@ -0,0 +1,21 @@ +//===-- Implementation of frexpbf16 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/frexpbf16.h" +#include "src/__support/FPUtil/ManipulationFunctions.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, frexpbf16, (bfloat16 x, int *exp)) { + return fputil::frexp(x, *exp); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/hypotf16.cpp b/libc/src/math/generic/hypotf16.cpp index d782c2687cdb..fa90069f9ff0 100644 --- a/libc/src/math/generic/hypotf16.cpp +++ b/libc/src/math/generic/hypotf16.cpp @@ -48,16 +48,15 @@ LLVM_LIBC_FUNCTION(float16, hypotf16, (float16 x, float16 y)) { return a_bits.get_val(); } - // TODO: Investigate why replacing the return line below with: - // return x_bits.get_val() + y_bits.get_val(); - // fails the hypotf16 smoke tests. + float af = fputil::cast<float>(a_bits.get_val()); + float bf = fputil::cast<float>(b_bits.get_val()); + + // Compiler runtime basic operations for float16 might not be correctly + // rounded for all rounding modes. if (LIBC_UNLIKELY(a_u - b_u >= static_cast<uint16_t>((FPBits::FRACTION_LEN + 2) << FPBits::FRACTION_LEN))) - return a_bits.get_val() + b_bits.get_val(); - - float af = fputil::cast<float>(a_bits.get_val()); - float bf = fputil::cast<float>(b_bits.get_val()); + return fputil::cast<float16>(af + bf); // These squares are exact. float a_sq = af * af; diff --git a/libc/src/math/generic/ilogbbf16.cpp b/libc/src/math/generic/ilogbbf16.cpp new file mode 100644 index 000000000000..6811139c6dcf --- /dev/null +++ b/libc/src/math/generic/ilogbbf16.cpp @@ -0,0 +1,21 @@ +//===-- Implementation of ilogbbf16 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/ilogbbf16.h" +#include "src/__support/FPUtil/ManipulationFunctions.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(int, ilogbbf16, (bfloat16 x)) { + return fputil::intlogb<int>(x); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/iscanonicalbf16.cpp b/libc/src/math/generic/iscanonicalbf16.cpp new file mode 100644 index 000000000000..34c11bfcb83d --- /dev/null +++ b/libc/src/math/generic/iscanonicalbf16.cpp @@ -0,0 +1,22 @@ +//===-- Implementation of iscanonicalbf16 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/iscanonicalbf16.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(int, iscanonicalbf16, (bfloat16 x)) { + bfloat16 tmp; + return fputil::canonicalize(tmp, x) == 0; +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/issignalingbf16.cpp b/libc/src/math/generic/issignalingbf16.cpp new file mode 100644 index 000000000000..3bb17efaa0ff --- /dev/null +++ b/libc/src/math/generic/issignalingbf16.cpp @@ -0,0 +1,21 @@ +//===-- Implementation of issignalingbf16 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/issignalingbf16.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(int, issignalingbf16, (bfloat16 x)) { + return fputil::issignaling_impl(x); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/ldexpbf16.cpp b/libc/src/math/generic/ldexpbf16.cpp new file mode 100644 index 000000000000..42a5039e81bc --- /dev/null +++ b/libc/src/math/generic/ldexpbf16.cpp @@ -0,0 +1,21 @@ +//===-- Implementation of ldexpbf16 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/ldexpbf16.h" +#include "src/__support/FPUtil/ManipulationFunctions.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, ldexpbf16, (bfloat16 x, int exp)) { + return fputil::ldexp(x, exp); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/llogbbf16.cpp b/libc/src/math/generic/llogbbf16.cpp new file mode 100644 index 000000000000..74c27620053d --- /dev/null +++ b/libc/src/math/generic/llogbbf16.cpp @@ -0,0 +1,21 @@ +//===-- Implementation of llogbbf16 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/llogbbf16.h" +#include "src/__support/FPUtil/ManipulationFunctions.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(long, llogbbf16, (bfloat16 x)) { + return fputil::intlogb<long>(x); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/logbbf16.cpp b/libc/src/math/generic/logbbf16.cpp new file mode 100644 index 000000000000..5a43ddfafde7 --- /dev/null +++ b/libc/src/math/generic/logbbf16.cpp @@ -0,0 +1,19 @@ +//===-- Implementation of logbbf16 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/logbbf16.h" +#include "src/__support/FPUtil/ManipulationFunctions.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, logbbf16, (bfloat16 x)) { return fputil::logb(x); } + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/modfbf16.cpp b/libc/src/math/generic/modfbf16.cpp new file mode 100644 index 000000000000..09458f6a2db4 --- /dev/null +++ b/libc/src/math/generic/modfbf16.cpp @@ -0,0 +1,21 @@ +//===-- Implementation of modfbf16 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/modfbf16.h" +#include "src/__support/FPUtil/ManipulationFunctions.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, modfbf16, (bfloat16 x, bfloat16 *iptr)) { + return fputil::modf(x, *iptr); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/remainderbf16.cpp b/libc/src/math/generic/remainderbf16.cpp new file mode 100644 index 000000000000..e70726a6cbc5 --- /dev/null +++ b/libc/src/math/generic/remainderbf16.cpp @@ -0,0 +1,22 @@ +//===-- Implementation of remainderbf16 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/remainderbf16.h" +#include "src/__support/FPUtil/DivisionAndRemainderOperations.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, remainderbf16, (bfloat16 x, bfloat16 y)) { + int quotient; + return fputil::remquo(x, y, quotient); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/remquobf16.cpp b/libc/src/math/generic/remquobf16.cpp new file mode 100644 index 000000000000..e1b13f8deb19 --- /dev/null +++ b/libc/src/math/generic/remquobf16.cpp @@ -0,0 +1,21 @@ +//===-- Implementation of remquobf16 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/remquobf16.h" +#include "src/__support/FPUtil/DivisionAndRemainderOperations.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, remquobf16, (bfloat16 x, bfloat16 y, int *exp)) { + return fputil::remquo(x, y, *exp); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/scalblnbf16.cpp b/libc/src/math/generic/scalblnbf16.cpp new file mode 100644 index 000000000000..f85f702660ed --- /dev/null +++ b/libc/src/math/generic/scalblnbf16.cpp @@ -0,0 +1,26 @@ +//===-- Implementation of scalblnbf16 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/scalblnbf16.h" +#include "hdr/float_macros.h" +#include "src/__support/FPUtil/ManipulationFunctions.h" +#include "src/__support/FPUtil/bfloat16.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +#if FLT_RADIX != 2 +#error "FLT_RADIX != 2 is not supported." +#endif + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(bfloat16, scalblnbf16, (bfloat16 x, long n)) { + return fputil::ldexp(x, n); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/scalbnbf16.cpp b/libc/src/math/generic/scalbnbf16.cpp new file mode 100644 index 000000000000..108f9e7d4d05 --- /dev/null +++ b/libc/src/math/generic/scalbnbf16.cpp @@ -0,0 +1,26 @@ +//===-- Implementation of scalbnbf16 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/scalbnbf16.h" +#include "hdr/float_macros.h" +#include "src/__support/FPUtil/ManipulationFunctions.h" +#include "src/__support/FPUtil/bfloat16.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +#if FLT_RADIX != 2 +#error "FLT_RADIX != 2 is not supported." +#endif + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(bfloat16, scalbnbf16, (bfloat16 x, int n)) { + return fputil::ldexp(x, n); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/sqrtbf16.cpp b/libc/src/math/generic/sqrtbf16.cpp new file mode 100644 index 000000000000..061e5522a4d8 --- /dev/null +++ b/libc/src/math/generic/sqrtbf16.cpp @@ -0,0 +1,21 @@ +//===-- Implementation of sqrtbf16 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/sqrtbf16.h" +#include "src/__support/FPUtil/bfloat16.h" +#include "src/__support/FPUtil/sqrt.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(bfloat16, sqrtbf16, (bfloat16 x)) { + return fputil::sqrt<bfloat16>(x); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/totalorderbf16.cpp b/libc/src/math/generic/totalorderbf16.cpp new file mode 100644 index 000000000000..bb9c86e281f6 --- /dev/null +++ b/libc/src/math/generic/totalorderbf16.cpp @@ -0,0 +1,22 @@ +//===-- Implementation of totalorderbf16 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/totalorderbf16.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(int, totalorderbf16, + (const bfloat16 *x, const bfloat16 *y)) { + return static_cast<int>(fputil::totalorder(*x, *y)); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/totalordermagbf16.cpp b/libc/src/math/generic/totalordermagbf16.cpp new file mode 100644 index 000000000000..3fc61d9d8bcb --- /dev/null +++ b/libc/src/math/generic/totalordermagbf16.cpp @@ -0,0 +1,22 @@ +//===-- Implementation of totalordermagbf16 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/totalordermagbf16.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(int, totalordermagbf16, + (const bfloat16 *x, const bfloat16 *y)) { + return static_cast<int>(fputil::totalordermag(*x, *y)); +} + +} // namespace LIBC_NAMESPACE_DECL |
