diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt index a875a17f06b3e3..086bd4e26cbf8b 100644 --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -506,7 +506,9 @@ if(LIBC_TYPES_HAS_FLOAT16) libc.src.math.ceilf16 libc.src.math.copysignf16 libc.src.math.f16divf + libc.src.math.f16fma libc.src.math.f16fmaf + libc.src.math.f16fmal libc.src.math.f16sqrtf libc.src.math.fabsf16 libc.src.math.fdimf16 @@ -558,6 +560,13 @@ if(LIBC_TYPES_HAS_FLOAT16) libc.src.math.ufromfpf16 libc.src.math.ufromfpxf16 ) + + if(LIBC_TYPES_HAS_FLOAT128) + list(APPEND TARGET_LIBM_ENTRYPOINTS + # math.h C23 mixed _Float16 and _Float128 entrypoints + libc.src.math.f16fmaf128 + ) + endif() endif() if(LIBC_TYPES_HAS_FLOAT128) diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index 34748ff5950ad0..8125dcb7a5633f 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -537,7 +537,9 @@ if(LIBC_TYPES_HAS_FLOAT16) libc.src.math.ceilf16 libc.src.math.copysignf16 libc.src.math.f16divf + libc.src.math.f16fma libc.src.math.f16fmaf + libc.src.math.f16fmal libc.src.math.f16sqrtf libc.src.math.fabsf16 libc.src.math.fdimf16 @@ -587,6 +589,13 @@ if(LIBC_TYPES_HAS_FLOAT16) libc.src.math.ufromfpf16 libc.src.math.ufromfpxf16 ) + + if(LIBC_TYPES_HAS_FLOAT128) + list(APPEND TARGET_LIBM_ENTRYPOINTS + # math.h C23 mixed _Float16 and _Float128 entrypoints + libc.src.math.f16fmaf128 + ) + endif() endif() if(LIBC_TYPES_HAS_FLOAT128) diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst index 95f450ab759605..b19ec3b8d9085c 100644 --- a/libc/docs/math/index.rst +++ b/libc/docs/math/index.rst @@ -126,7 +126,7 @@ Basic Operations +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | f16div | |check| | | | N/A | | 7.12.14.4 | F.10.11 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ -| f16fma | |check| | | | N/A | | 7.12.14.5 | F.10.11 | +| f16fma | |check| | |check| | |check| | N/A | |check| | 7.12.14.5 | F.10.11 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | fabs | |check| | |check| | |check| | |check| | |check| | 7.12.7.3 | F.10.4.3 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ diff --git a/libc/include/llvm-libc-macros/float16-macros.h b/libc/include/llvm-libc-macros/float16-macros.h index e7d8d93bca1b60..9a11ecc49307e2 100644 --- a/libc/include/llvm-libc-macros/float16-macros.h +++ b/libc/include/llvm-libc-macros/float16-macros.h @@ -9,10 +9,18 @@ #ifndef LLVM_LIBC_MACROS_FLOAT16_MACROS_H #define LLVM_LIBC_MACROS_FLOAT16_MACROS_H +#include "../llvm-libc-types/float128.h" + #if defined(__FLT16_MANT_DIG__) && \ (!defined(__GNUC__) || __GNUC__ >= 13 || defined(__clang__)) && \ !defined(__arm__) && !defined(_M_ARM) && !defined(__riscv) #define LIBC_TYPES_HAS_FLOAT16 + +// TODO: This would no longer be required if HdrGen let us guard function +// declarations with multiple macros. +#ifdef LIBC_TYPES_HAS_FLOAT128 +#define LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128 +#endif // LIBC_TYPES_HAS_FLOAT128 #endif #endif // LLVM_LIBC_MACROS_FLOAT16_MACROS_H diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td index 651f49deef4c11..adac7d59324286 100644 --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -477,7 +477,10 @@ def StdC : StandardSpec<"stdc"> { FunctionSpec<"fma", RetValSpec, [ArgSpec, ArgSpec, ArgSpec]>, FunctionSpec<"fmaf", RetValSpec, [ArgSpec, ArgSpec, ArgSpec]>, + GuardedFunctionSpec<"f16fma", RetValSpec, [ArgSpec, ArgSpec, ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, GuardedFunctionSpec<"f16fmaf", RetValSpec, [ArgSpec, ArgSpec, ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, + GuardedFunctionSpec<"f16fmal", RetValSpec, [ArgSpec, ArgSpec, ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, + GuardedFunctionSpec<"f16fmaf128", RetValSpec, [ArgSpec, ArgSpec, ArgSpec], "LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128">, FunctionSpec<"fmod", RetValSpec, [ArgSpec, ArgSpec]>, FunctionSpec<"fmodf", RetValSpec, [ArgSpec, ArgSpec]>, diff --git a/libc/src/__support/FPUtil/CMakeLists.txt b/libc/src/__support/FPUtil/CMakeLists.txt index 900a7022c38683..84c5f802710c4a 100644 --- a/libc/src/__support/FPUtil/CMakeLists.txt +++ b/libc/src/__support/FPUtil/CMakeLists.txt @@ -154,7 +154,6 @@ add_header_library( HDRS multiply_add.h DEPENDS - .fma libc.src.__support.common ) diff --git a/libc/src/__support/FPUtil/dyadic_float.h b/libc/src/__support/FPUtil/dyadic_float.h index 1e1bec676d444a..fb1b22467f9406 100644 --- a/libc/src/__support/FPUtil/dyadic_float.h +++ b/libc/src/__support/FPUtil/dyadic_float.h @@ -156,13 +156,13 @@ template struct DyadicFloat { // d_lo is denormal, but the output is normal. int scale_up_exponent = 1 - exp_lo; T scale_up_factor = - FPBits::create_value(sign, + FPBits::create_value(Sign::POS, static_cast( FPBits::EXP_BIAS + scale_up_exponent), IMPLICIT_MASK) .get_val(); T scale_down_factor = - FPBits::create_value(sign, + FPBits::create_value(Sign::POS, static_cast( FPBits::EXP_BIAS - scale_up_exponent), IMPLICIT_MASK) diff --git a/libc/src/__support/FPUtil/generic/CMakeLists.txt b/libc/src/__support/FPUtil/generic/CMakeLists.txt index 33b2564bfa087b..bd8af98473edf5 100644 --- a/libc/src/__support/FPUtil/generic/CMakeLists.txt +++ b/libc/src/__support/FPUtil/generic/CMakeLists.txt @@ -24,6 +24,8 @@ add_header_library( libc.src.__support.CPP.bit libc.src.__support.CPP.limits libc.src.__support.CPP.type_traits + libc.src.__support.FPUtil.basic_operations + libc.src.__support.FPUtil.dyadic_float libc.src.__support.FPUtil.fenv_impl libc.src.__support.FPUtil.fp_bits libc.src.__support.FPUtil.rounding_mode diff --git a/libc/src/__support/FPUtil/generic/FMA.h b/libc/src/__support/FPUtil/generic/FMA.h index 71b150758d4191..d0a01c3092c42c 100644 --- a/libc/src/__support/FPUtil/generic/FMA.h +++ b/libc/src/__support/FPUtil/generic/FMA.h @@ -12,7 +12,9 @@ #include "src/__support/CPP/bit.h" #include "src/__support/CPP/limits.h" #include "src/__support/CPP/type_traits.h" +#include "src/__support/FPUtil/BasicOperations.h" #include "src/__support/FPUtil/FPBits.h" +#include "src/__support/FPUtil/dyadic_float.h" #include "src/__support/FPUtil/rounding_mode.h" #include "src/__support/big_int.h" #include "src/__support/macros/attributes.h" // LIBC_INLINE @@ -106,20 +108,52 @@ LIBC_INLINE cpp::enable_if_t && sizeof(OutType) <= sizeof(InType), OutType> fma(InType x, InType y, InType z) { - using OutFPBits = fputil::FPBits; + using OutFPBits = FPBits; using OutStorageType = typename OutFPBits::StorageType; - using InFPBits = fputil::FPBits; + using InFPBits = FPBits; using InStorageType = typename InFPBits::StorageType; constexpr int IN_EXPLICIT_MANT_LEN = InFPBits::FRACTION_LEN + 1; constexpr size_t PROD_LEN = 2 * IN_EXPLICIT_MANT_LEN; constexpr size_t TMP_RESULT_LEN = cpp::bit_ceil(PROD_LEN + 1); using TmpResultType = UInt; + using DyadicFloat = DyadicFloat; - constexpr size_t EXTRA_FRACTION_LEN = - TMP_RESULT_LEN - 1 - OutFPBits::FRACTION_LEN; - constexpr TmpResultType EXTRA_FRACTION_STICKY_MASK = - (TmpResultType(1) << (EXTRA_FRACTION_LEN - 1)) - 1; + InFPBits x_bits(x), y_bits(y), z_bits(z); + + if (LIBC_UNLIKELY(x_bits.is_nan() || y_bits.is_nan() || z_bits.is_nan())) { + if (x_bits.is_nan() || y_bits.is_nan()) { + if (x_bits.is_signaling_nan() || y_bits.is_signaling_nan() || + z_bits.is_signaling_nan()) + raise_except_if_required(FE_INVALID); + + if (x_bits.is_quiet_nan()) { + InStorageType x_payload = static_cast(getpayload(x)); + if ((x_payload & ~(OutFPBits::FRACTION_MASK >> 1)) == 0) + return OutFPBits::quiet_nan(x_bits.sign(), + static_cast(x_payload)) + .get_val(); + } + + if (y_bits.is_quiet_nan()) { + InStorageType y_payload = static_cast(getpayload(y)); + if ((y_payload & ~(OutFPBits::FRACTION_MASK >> 1)) == 0) + return OutFPBits::quiet_nan(y_bits.sign(), + static_cast(y_payload)) + .get_val(); + } + + if (z_bits.is_quiet_nan()) { + InStorageType z_payload = static_cast(getpayload(z)); + if ((z_payload & ~(OutFPBits::FRACTION_MASK >> 1)) == 0) + return OutFPBits::quiet_nan(z_bits.sign(), + static_cast(z_payload)) + .get_val(); + } + + return OutFPBits::quiet_nan().get_val(); + } + } if (LIBC_UNLIKELY(x == 0 || y == 0 || z == 0)) return static_cast(x * y + z); @@ -142,7 +176,9 @@ fma(InType x, InType y, InType z) { z *= InType(InStorageType(1) << InFPBits::FRACTION_LEN); } - InFPBits x_bits(x), y_bits(y), z_bits(z); + x_bits = InFPBits(x); + y_bits = InFPBits(y); + z_bits = InFPBits(z); const Sign z_sign = z_bits.sign(); Sign prod_sign = (x_bits.sign() == y_bits.sign()) ? Sign::POS : Sign::NEG; x_exp += x_bits.get_biased_exponent(); @@ -182,7 +218,6 @@ fma(InType x, InType y, InType z) { constexpr int RESULT_MIN_LEN = PROD_LEN - InFPBits::FRACTION_LEN; z_mant <<= RESULT_MIN_LEN; int z_lsb_exp = z_exp - (InFPBits::FRACTION_LEN + RESULT_MIN_LEN); - bool round_bit = false; bool sticky_bits = false; bool z_shifted = false; @@ -221,85 +256,18 @@ fma(InType x, InType y, InType z) { } } - OutStorageType result = 0; - int r_exp = 0; // Unbiased exponent of the result - - int round_mode = fputil::quick_get_round(); - - // Normalize the result. - if (prod_mant != 0) { - int lead_zeros = cpp::countl_zero(prod_mant); - // Move the leading 1 to the most significant bit. - prod_mant <<= lead_zeros; - prod_lsb_exp -= lead_zeros; - r_exp = prod_lsb_exp + (cpp::numeric_limits::digits - 1) - - InFPBits::EXP_BIAS + OutFPBits::EXP_BIAS; - - if (r_exp > 0) { - // The result is normal. We will shift the mantissa to the right by the - // amount of extra bits compared to the length of the explicit mantissa in - // the output type. The rounding bit then becomes the highest bit that is - // shifted out, and the following lower bits are merged into sticky bits. - round_bit = - (prod_mant & (TmpResultType(1) << (EXTRA_FRACTION_LEN - 1))) != 0; - sticky_bits |= (prod_mant & EXTRA_FRACTION_STICKY_MASK) != 0; - result = static_cast(prod_mant >> EXTRA_FRACTION_LEN); - } else { - if (r_exp < -OutFPBits::FRACTION_LEN) { - // The result is smaller than 1/2 of the smallest denormal number. - sticky_bits = true; // since the result is non-zero. - result = 0; - } else { - // The result is denormal. - TmpResultType mask = TmpResultType(1) << (EXTRA_FRACTION_LEN - r_exp); - round_bit = (prod_mant & mask) != 0; - sticky_bits |= (prod_mant & (mask - 1)) != 0; - if (r_exp > -OutFPBits::FRACTION_LEN) - result = static_cast( - prod_mant >> (EXTRA_FRACTION_LEN + 1 - r_exp)); - else - result = 0; - } - - r_exp = 0; - } - } else { + if (prod_mant == 0) { // When there is exact cancellation, i.e., x*y == -z exactly, return -0.0 if // rounding downward and +0.0 for other rounding modes. - if (round_mode == FE_DOWNWARD) + if (quick_get_round() == FE_DOWNWARD) prod_sign = Sign::NEG; else prod_sign = Sign::POS; } - // Finalize the result. - if (LIBC_UNLIKELY(r_exp >= OutFPBits::MAX_BIASED_EXPONENT)) { - if ((round_mode == FE_TOWARDZERO) || - (round_mode == FE_UPWARD && prod_sign.is_neg()) || - (round_mode == FE_DOWNWARD && prod_sign.is_pos())) { - return OutFPBits::max_normal(prod_sign).get_val(); - } - return OutFPBits::inf(prod_sign).get_val(); - } - - // Remove hidden bit and append the exponent field and sign bit. - result = static_cast( - (result & OutFPBits::FRACTION_MASK) | - (static_cast(r_exp) << OutFPBits::FRACTION_LEN)); - if (prod_sign.is_neg()) - result |= OutFPBits::SIGN_MASK; - - // Rounding. - if (round_mode == FE_TONEAREST) { - if (round_bit && (sticky_bits || ((result & 1) != 0))) - ++result; - } else if ((round_mode == FE_UPWARD && prod_sign.is_pos()) || - (round_mode == FE_DOWNWARD && prod_sign.is_neg())) { - if (round_bit || sticky_bits) - ++result; - } - - return cpp::bit_cast(result); + DyadicFloat result(prod_sign, prod_lsb_exp - InFPBits::EXP_BIAS, prod_mant); + result.mantissa |= sticky_bits; + return result.template as(); } } // namespace generic diff --git a/libc/src/__support/FPUtil/multiply_add.h b/libc/src/__support/FPUtil/multiply_add.h index 622914e4265c91..1b9eee4ace62d0 100644 --- a/libc/src/__support/FPUtil/multiply_add.h +++ b/libc/src/__support/FPUtil/multiply_add.h @@ -39,17 +39,18 @@ multiply_add(T x, T y, T z) { #if defined(LIBC_TARGET_CPU_HAS_FMA) // FMA instructions are available. -#include "FMA.h" +// We use builtins directly instead of including FMA.h to avoid a circular +// dependency: multiply_add.h -> FMA.h -> generic/FMA.h -> dyadic_float.h. namespace LIBC_NAMESPACE { namespace fputil { LIBC_INLINE float multiply_add(float x, float y, float z) { - return fma(x, y, z); + return __builtin_fmaf(x, y, z); } LIBC_INLINE double multiply_add(double x, double y, double z) { - return fma(x, y, z); + return __builtin_fma(x, y, z); } } // namespace fputil diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt index 711cbf8bbfdca6..3dfc4ac94827df 100644 --- a/libc/src/math/CMakeLists.txt +++ b/libc/src/math/CMakeLists.txt @@ -101,7 +101,10 @@ add_math_entrypoint_object(expm1f) add_math_entrypoint_object(f16divf) +add_math_entrypoint_object(f16fma) add_math_entrypoint_object(f16fmaf) +add_math_entrypoint_object(f16fmal) +add_math_entrypoint_object(f16fmaf128) add_math_entrypoint_object(f16sqrtf) diff --git a/libc/src/math/f16fma.h b/libc/src/math/f16fma.h new file mode 100644 index 00000000000000..d9505f88f37af2 --- /dev/null +++ b/libc/src/math/f16fma.h @@ -0,0 +1,20 @@ +//===-- Implementation header for f16fma ------------------------*- 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_F16FMA_H +#define LLVM_LIBC_SRC_MATH_F16FMA_H + +#include "src/__support/macros/properties/types.h" + +namespace LIBC_NAMESPACE { + +float16 f16fma(double x, double y, double z); + +} // namespace LIBC_NAMESPACE + +#endif // LLVM_LIBC_SRC_MATH_F16FMA_H diff --git a/libc/src/math/f16fmaf128.h b/libc/src/math/f16fmaf128.h new file mode 100644 index 00000000000000..9203b4d30d2124 --- /dev/null +++ b/libc/src/math/f16fmaf128.h @@ -0,0 +1,20 @@ +//===-- Implementation header for f16fmaf128 --------------------*- 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_F16FMAF128_H +#define LLVM_LIBC_SRC_MATH_F16FMAF128_H + +#include "src/__support/macros/properties/types.h" + +namespace LIBC_NAMESPACE { + +float16 f16fmaf128(float128 x, float128 y, float128 z); + +} // namespace LIBC_NAMESPACE + +#endif // LLVM_LIBC_SRC_MATH_F16FMAF128_H diff --git a/libc/src/math/f16fmal.h b/libc/src/math/f16fmal.h new file mode 100644 index 00000000000000..6f5dd33aa18f63 --- /dev/null +++ b/libc/src/math/f16fmal.h @@ -0,0 +1,20 @@ +//===-- Implementation header for f16fmal -----------------------*- 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_F16FMAL_H +#define LLVM_LIBC_SRC_MATH_F16FMAL_H + +#include "src/__support/macros/properties/types.h" + +namespace LIBC_NAMESPACE { + +float16 f16fmal(long double x, long double y, long double z); + +} // namespace LIBC_NAMESPACE + +#endif // LLVM_LIBC_SRC_MATH_F16FMAL_H diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index fc2024c89b5df7..d7c9817fc457b6 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -3744,6 +3744,19 @@ add_entrypoint_object( -O3 ) +add_entrypoint_object( + f16fma + SRCS + f16fma.cpp + HDRS + ../f16fma.h + DEPENDS + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.fma + COMPILE_OPTIONS + -O0 -ggdb3 +) + add_entrypoint_object( f16fmaf SRCS @@ -3757,6 +3770,32 @@ add_entrypoint_object( -O3 ) +add_entrypoint_object( + f16fmal + SRCS + f16fmal.cpp + HDRS + ../f16fmal.h + DEPENDS + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.fma + COMPILE_OPTIONS + -O3 +) + +add_entrypoint_object( + f16fmaf128 + SRCS + f16fmaf128.cpp + HDRS + ../f16fmaf128.h + DEPENDS + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.fma + COMPILE_OPTIONS + -O3 +) + add_entrypoint_object( f16sqrtf SRCS diff --git a/libc/src/math/generic/f16fma.cpp b/libc/src/math/generic/f16fma.cpp new file mode 100644 index 00000000000000..10ee028c069301 --- /dev/null +++ b/libc/src/math/generic/f16fma.cpp @@ -0,0 +1,19 @@ +//===-- Implementation of f16fma 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/f16fma.h" +#include "src/__support/FPUtil/FMA.h" +#include "src/__support/common.h" + +namespace LIBC_NAMESPACE { + +LLVM_LIBC_FUNCTION(float16, f16fma, (double x, double y, double z)) { + return fputil::fma(x, y, z); +} + +} // namespace LIBC_NAMESPACE diff --git a/libc/src/math/generic/f16fmaf128.cpp b/libc/src/math/generic/f16fmaf128.cpp new file mode 100644 index 00000000000000..5b2f801cf21fa5 --- /dev/null +++ b/libc/src/math/generic/f16fmaf128.cpp @@ -0,0 +1,19 @@ +//===-- Implementation of f16fmaf128 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/f16fmaf128.h" +#include "src/__support/FPUtil/FMA.h" +#include "src/__support/common.h" + +namespace LIBC_NAMESPACE { + +LLVM_LIBC_FUNCTION(float16, f16fmaf128, (float128 x, float128 y, float128 z)) { + return fputil::fma(x, y, z); +} + +} // namespace LIBC_NAMESPACE diff --git a/libc/src/math/generic/f16fmal.cpp b/libc/src/math/generic/f16fmal.cpp new file mode 100644 index 00000000000000..067483629a3362 --- /dev/null +++ b/libc/src/math/generic/f16fmal.cpp @@ -0,0 +1,20 @@ +//===-- Implementation of f16fmal 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/f16fmal.h" +#include "src/__support/FPUtil/FMA.h" +#include "src/__support/common.h" + +namespace LIBC_NAMESPACE { + +LLVM_LIBC_FUNCTION(float16, f16fmal, + (long double x, long double y, long double z)) { + return fputil::fma(x, y, z); +} + +} // namespace LIBC_NAMESPACE diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt index ba588662f469e3..62ffadd12f03b1 100644 --- a/libc/test/src/math/CMakeLists.txt +++ b/libc/test/src/math/CMakeLists.txt @@ -1903,6 +1903,21 @@ add_fp_unittest( libc.src.math.f16divf ) +add_fp_unittest( + f16fma_test + NEED_MPFR + SUITE + libc-math-unittests + SRCS + f16fma_test.cpp + HDRS + FmaTest.h + DEPENDS + libc.src.math.f16fma + libc.src.stdlib.rand + libc.src.stdlib.srand +) + add_fp_unittest( f16fmaf_test NEED_MPFR @@ -1918,6 +1933,21 @@ add_fp_unittest( libc.src.stdlib.srand ) +add_fp_unittest( + f16fmal_test + NEED_MPFR + SUITE + libc-math-unittests + SRCS + f16fmal_test.cpp + HDRS + FmaTest.h + DEPENDS + libc.src.math.f16fmal + libc.src.stdlib.rand + libc.src.stdlib.srand +) + add_subdirectory(generic) add_subdirectory(smoke) diff --git a/libc/test/src/math/FmaTest.h b/libc/test/src/math/FmaTest.h index 53895e7d633c29..01143331d4ab57 100644 --- a/libc/test/src/math/FmaTest.h +++ b/libc/test/src/math/FmaTest.h @@ -45,9 +45,6 @@ class FmaTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest { static constexpr InStorageType IN_MIN_SUBNORMAL_U = InFPBits::min_subnormal().uintval(); - OutConstants out; - InConstants in; - InStorageType get_random_bit_pattern() { InStorageType bits{0}; for (InStorageType i = 0; i < sizeof(InStorageType) / 2; ++i) { @@ -92,4 +89,14 @@ class FmaTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest { } }; +#define LIST_FMA_TESTS(T, func) \ + using LlvmLibcFmaTest = FmaTestTemplate; \ + TEST_F(LlvmLibcFmaTest, SubnormalRange) { test_subnormal_range(&func); } \ + TEST_F(LlvmLibcFmaTest, NormalRange) { test_normal_range(&func); } + +#define LIST_NARROWING_FMA_TESTS(OutType, InType, func) \ + using LlvmLibcFmaTest = FmaTestTemplate; \ + TEST_F(LlvmLibcFmaTest, SubnormalRange) { test_subnormal_range(&func); } \ + TEST_F(LlvmLibcFmaTest, NormalRange) { test_normal_range(&func); } + #endif // LLVM_LIBC_TEST_SRC_MATH_FMATEST_H diff --git a/libc/test/src/math/f16fma_test.cpp b/libc/test/src/math/f16fma_test.cpp new file mode 100644 index 00000000000000..2e46b5bdd46829 --- /dev/null +++ b/libc/test/src/math/f16fma_test.cpp @@ -0,0 +1,13 @@ +//===-- Unittests for f16fma ----------------------------------------------===// +// +// 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 "FmaTest.h" + +#include "src/math/f16fma.h" + +LIST_NARROWING_FMA_TESTS(float16, double, LIBC_NAMESPACE::f16fma) diff --git a/libc/test/src/math/f16fmaf_test.cpp b/libc/test/src/math/f16fmaf_test.cpp index e4ca88b8810e16..5e3aec768c1910 100644 --- a/libc/test/src/math/f16fmaf_test.cpp +++ b/libc/test/src/math/f16fmaf_test.cpp @@ -10,12 +10,4 @@ #include "src/math/f16fmaf.h" -using LlvmLibcF16fmafTest = FmaTestTemplate; - -TEST_F(LlvmLibcF16fmafTest, SubnormalRange) { - test_subnormal_range(&LIBC_NAMESPACE::f16fmaf); -} - -TEST_F(LlvmLibcF16fmafTest, NormalRange) { - test_normal_range(&LIBC_NAMESPACE::f16fmaf); -} +LIST_NARROWING_FMA_TESTS(float16, float, LIBC_NAMESPACE::f16fmaf) diff --git a/libc/test/src/math/f16fmal_test.cpp b/libc/test/src/math/f16fmal_test.cpp new file mode 100644 index 00000000000000..5394268a9cd8f3 --- /dev/null +++ b/libc/test/src/math/f16fmal_test.cpp @@ -0,0 +1,13 @@ +//===-- Unittests for f16fmal ---------------------------------------------===// +// +// 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 "FmaTest.h" + +#include "src/math/f16fmal.h" + +LIST_NARROWING_FMA_TESTS(float16, long double, LIBC_NAMESPACE::f16fmal) diff --git a/libc/test/src/math/fmaf_test.cpp b/libc/test/src/math/fmaf_test.cpp index 0e498d46ecfb0d..09e9c504b942a0 100644 --- a/libc/test/src/math/fmaf_test.cpp +++ b/libc/test/src/math/fmaf_test.cpp @@ -10,12 +10,4 @@ #include "src/math/fmaf.h" -using LlvmLibcFmafTest = FmaTestTemplate; - -TEST_F(LlvmLibcFmafTest, SubnormalRange) { - test_subnormal_range(&LIBC_NAMESPACE::fmaf); -} - -TEST_F(LlvmLibcFmafTest, NormalRange) { - test_normal_range(&LIBC_NAMESPACE::fmaf); -} +LIST_FMA_TESTS(float, LIBC_NAMESPACE::fmaf) diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt index ee99fb96a52cee..2362ee12bcf417 100644 --- a/libc/test/src/math/smoke/CMakeLists.txt +++ b/libc/test/src/math/smoke/CMakeLists.txt @@ -3644,6 +3644,18 @@ add_fp_unittest( libc.src.math.f16divf ) +add_fp_unittest( + f16fma_test + SUITE + libc-math-smoke-tests + SRCS + f16fma_test.cpp + HDRS + FmaTest.h + DEPENDS + libc.src.math.f16fma +) + add_fp_unittest( f16fmaf_test SUITE @@ -3656,6 +3668,30 @@ add_fp_unittest( libc.src.math.f16fmaf ) +add_fp_unittest( + f16fmal_test + SUITE + libc-math-smoke-tests + SRCS + f16fmal_test.cpp + HDRS + FmaTest.h + DEPENDS + libc.src.math.f16fmal +) + +add_fp_unittest( + f16fmaf128_test + SUITE + libc-math-smoke-tests + SRCS + f16fmaf128_test.cpp + HDRS + FmaTest.h + DEPENDS + libc.src.math.f16fmaf128 +) + add_fp_unittest( f16sqrtf_test SUITE diff --git a/libc/test/src/math/smoke/f16fma_test.cpp b/libc/test/src/math/smoke/f16fma_test.cpp new file mode 100644 index 00000000000000..2e46b5bdd46829 --- /dev/null +++ b/libc/test/src/math/smoke/f16fma_test.cpp @@ -0,0 +1,13 @@ +//===-- Unittests for f16fma ----------------------------------------------===// +// +// 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 "FmaTest.h" + +#include "src/math/f16fma.h" + +LIST_NARROWING_FMA_TESTS(float16, double, LIBC_NAMESPACE::f16fma) diff --git a/libc/test/src/math/smoke/f16fmaf128_test.cpp b/libc/test/src/math/smoke/f16fmaf128_test.cpp new file mode 100644 index 00000000000000..ea4003bc5f77ee --- /dev/null +++ b/libc/test/src/math/smoke/f16fmaf128_test.cpp @@ -0,0 +1,13 @@ +//===-- Unittests for f16fmaf128 ------------------------------------------===// +// +// 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 "FmaTest.h" + +#include "src/math/f16fmaf128.h" + +LIST_NARROWING_FMA_TESTS(float16, float128, LIBC_NAMESPACE::f16fmaf128) diff --git a/libc/test/src/math/smoke/f16fmal_test.cpp b/libc/test/src/math/smoke/f16fmal_test.cpp new file mode 100644 index 00000000000000..5394268a9cd8f3 --- /dev/null +++ b/libc/test/src/math/smoke/f16fmal_test.cpp @@ -0,0 +1,13 @@ +//===-- Unittests for f16fmal ---------------------------------------------===// +// +// 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 "FmaTest.h" + +#include "src/math/f16fmal.h" + +LIST_NARROWING_FMA_TESTS(float16, long double, LIBC_NAMESPACE::f16fmal) diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp index 521c2658b327a5..97ce0f63b57fb9 100644 --- a/libc/utils/MPFRWrapper/MPFRUtils.cpp +++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp @@ -977,6 +977,12 @@ explain_ternary_operation_one_output_error(Operation, #ifdef LIBC_TYPES_HAS_FLOAT16 template void explain_ternary_operation_one_output_error( Operation, const TernaryInput &, float16, double, RoundingMode); +template void explain_ternary_operation_one_output_error( + Operation, const TernaryInput &, float16, double, RoundingMode); +template void +explain_ternary_operation_one_output_error(Operation, + const TernaryInput &, + float16, double, RoundingMode); #endif template @@ -1124,6 +1130,14 @@ template bool compare_ternary_operation_one_output(Operation, const TernaryInput &, float16, double, RoundingMode); +template bool compare_ternary_operation_one_output(Operation, + const TernaryInput &, + float16, double, + RoundingMode); +template bool +compare_ternary_operation_one_output(Operation, + const TernaryInput &, float16, + double, RoundingMode); #endif } // namespace internal