From 9f347401f0ffe17a497fabacfc9efdf709b48476 Mon Sep 17 00:00:00 2001 From: OverMighty Date: Tue, 25 Jun 2024 02:03:45 +0200 Subject: [PATCH 1/8] [libc][math][c23] Add f16sqrt C23 math function --- libc/config/linux/aarch64/entrypoints.txt | 1 + libc/config/linux/x86_64/entrypoints.txt | 1 + libc/docs/math/index.rst | 2 +- libc/spec/stdc.td | 1 + .../__support/FPUtil/generic/CMakeLists.txt | 1 + libc/src/__support/FPUtil/generic/sqrt.h | 102 +++--------------- libc/src/math/CMakeLists.txt | 1 + libc/src/math/f16sqrt.h | 20 ++++ libc/src/math/generic/CMakeLists.txt | 13 +++ libc/src/math/generic/f16sqrt.cpp | 19 ++++ libc/test/src/math/CMakeLists.txt | 31 ++++-- libc/test/src/math/SqrtTest.h | 43 +++----- libc/test/src/math/f16sqrt_test.cpp | 13 +++ libc/test/src/math/smoke/CMakeLists.txt | 12 +++ libc/test/src/math/smoke/f16sqrt_test.cpp | 13 +++ libc/utils/MPFRWrapper/MPFRUtils.cpp | 5 + 16 files changed, 156 insertions(+), 122 deletions(-) create mode 100644 libc/src/math/f16sqrt.h create mode 100644 libc/src/math/generic/f16sqrt.cpp create mode 100644 libc/test/src/math/f16sqrt_test.cpp create mode 100644 libc/test/src/math/smoke/f16sqrt_test.cpp diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt index 8a26536cea9a0..ea89f8bd138d6 100644 --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -510,6 +510,7 @@ if(LIBC_TYPES_HAS_FLOAT16) libc.src.math.f16div libc.src.math.f16divf libc.src.math.f16fmaf + libc.src.math.f16sqrt libc.src.math.f16sqrtf libc.src.math.fabsf16 libc.src.math.fdimf16 diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index e1922ca94b97e..e4a2053916cbe 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -543,6 +543,7 @@ if(LIBC_TYPES_HAS_FLOAT16) libc.src.math.f16fma libc.src.math.f16fmaf libc.src.math.f16fmal + libc.src.math.f16sqrt libc.src.math.f16sqrtf libc.src.math.fabsf16 libc.src.math.fdimf16 diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst index 1bc75a9e0517f..d1a7cf56e5c65 100644 --- a/libc/docs/math/index.rst +++ b/libc/docs/math/index.rst @@ -292,7 +292,7 @@ Higher Math Functions +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | fma | |check| | |check| | | | | 7.12.13.1 | F.10.10.1 | +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ -| f16sqrt | |check| | | | N/A | | 7.12.14.6 | F.10.11 | +| f16sqrt | |check| | |check| | | N/A | | 7.12.14.6 | F.10.11 | +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | fsqrt | N/A | | | N/A | | 7.12.14.6 | F.10.11 | +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td index 481515ad439ed..a51aa088ec42f 100644 --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -731,6 +731,7 @@ def StdC : StandardSpec<"stdc"> { GuardedFunctionSpec<"f16divf128", RetValSpec, [ArgSpec, ArgSpec], "LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128">, + GuardedFunctionSpec<"f16sqrt", RetValSpec, [ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, GuardedFunctionSpec<"f16sqrtf", RetValSpec, [ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, ] >; diff --git a/libc/src/__support/FPUtil/generic/CMakeLists.txt b/libc/src/__support/FPUtil/generic/CMakeLists.txt index bd8af98473edf..fb49fd0039537 100644 --- a/libc/src/__support/FPUtil/generic/CMakeLists.txt +++ b/libc/src/__support/FPUtil/generic/CMakeLists.txt @@ -8,6 +8,7 @@ add_header_library( libc.src.__support.common libc.src.__support.CPP.bit libc.src.__support.CPP.type_traits + 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/sqrt.h b/libc/src/__support/FPUtil/generic/sqrt.h index d6e894fdfe021..e9cd3f47eef27 100644 --- a/libc/src/__support/FPUtil/generic/sqrt.h +++ b/libc/src/__support/FPUtil/generic/sqrt.h @@ -14,7 +14,7 @@ #include "src/__support/CPP/type_traits.h" #include "src/__support/FPUtil/FEnvImpl.h" #include "src/__support/FPUtil/FPBits.h" -#include "src/__support/FPUtil/rounding_mode.h" +#include "src/__support/FPUtil/dyadic_float.h" #include "src/__support/common.h" #include "src/__support/uint128.h" @@ -78,16 +78,14 @@ sqrt(InType x) { return x86::sqrt(x); } else { // IEEE floating points formats. - using OutFPBits = typename fputil::FPBits; - using OutStorageType = typename OutFPBits::StorageType; - using InFPBits = typename fputil::FPBits; + using OutFPBits = FPBits; + using InFPBits = FPBits; using InStorageType = typename InFPBits::StorageType; + using DyadicFloat = + DyadicFloat(InFPBits::STORAGE_LEN))>; + constexpr InStorageType ONE = InStorageType(1) << InFPBits::FRACTION_LEN; constexpr auto FLT_NAN = OutFPBits::quiet_nan().get_val(); - constexpr int EXTRA_FRACTION_LEN = - InFPBits::FRACTION_LEN - OutFPBits::FRACTION_LEN; - constexpr InStorageType EXTRA_FRACTION_MASK = - (InStorageType(1) << EXTRA_FRACTION_LEN) - 1; InFPBits bits(x); @@ -146,91 +144,19 @@ sqrt(InType x) { } // We compute one more iteration in order to round correctly. - bool lsb = (y & (InStorageType(1) << EXTRA_FRACTION_LEN)) != - 0; // Least significant bit - bool rb = false; // Round bit r <<= 2; - InStorageType tmp = (y << 2) + 1; + y <<= 2; + InStorageType tmp = y + 1; if (r >= tmp) { r -= tmp; - rb = true; - } - - bool sticky = false; - - if constexpr (EXTRA_FRACTION_LEN > 0) { - sticky = rb || (y & EXTRA_FRACTION_MASK) != 0; - rb = (y & (InStorageType(1) << (EXTRA_FRACTION_LEN - 1))) != 0; - } - - // Remove hidden bit and append the exponent field. - x_exp = ((x_exp >> 1) + OutFPBits::EXP_BIAS); - - OutStorageType y_out = static_cast( - ((y - ONE) >> EXTRA_FRACTION_LEN) | - (static_cast(x_exp) << OutFPBits::FRACTION_LEN)); - - if constexpr (EXTRA_FRACTION_LEN > 0) { - if (x_exp >= OutFPBits::MAX_BIASED_EXPONENT) { - switch (quick_get_round()) { - case FE_TONEAREST: - case FE_UPWARD: - return OutFPBits::inf().get_val(); - default: - return OutFPBits::max_normal().get_val(); - } - } - - if (x_exp < - -OutFPBits::EXP_BIAS - OutFPBits::SIG_LEN + EXTRA_FRACTION_LEN) { - switch (quick_get_round()) { - case FE_UPWARD: - return OutFPBits::min_subnormal().get_val(); - default: - return OutType(0.0); - } - } - - if (x_exp <= 0) { - int underflow_extra_fraction_len = EXTRA_FRACTION_LEN - x_exp + 1; - InStorageType underflow_extra_fraction_mask = - (InStorageType(1) << underflow_extra_fraction_len) - 1; - - rb = (y & (InStorageType(1) << (underflow_extra_fraction_len - 1))) != - 0; - OutStorageType subnormal_mant = - static_cast(y >> underflow_extra_fraction_len); - lsb = (subnormal_mant & 1) != 0; - sticky = sticky || (y & underflow_extra_fraction_mask) != 0; - - switch (quick_get_round()) { - case FE_TONEAREST: - if (rb && (lsb || sticky)) - ++subnormal_mant; - break; - case FE_UPWARD: - if (rb || sticky) - ++subnormal_mant; - break; - } - - return cpp::bit_cast(subnormal_mant); - } - } - - switch (quick_get_round()) { - case FE_TONEAREST: - // Round to nearest, ties to even - if (rb && (lsb || (r != 0))) - ++y_out; - break; - case FE_UPWARD: - if (rb || (r != 0) || sticky) - ++y_out; - break; + // Rounding bit. + y |= 1 << 1; } + // Sticky bit. + y |= r != 0; - return cpp::bit_cast(y_out); + DyadicFloat yd(Sign::POS, (x_exp >> 1) - 2 - InFPBits::FRACTION_LEN, y); + return yd.template as(); } } } diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt index fb0d971e88733..b92ccac94c7d9 100644 --- a/libc/src/math/CMakeLists.txt +++ b/libc/src/math/CMakeLists.txt @@ -109,6 +109,7 @@ add_math_entrypoint_object(f16fmaf) add_math_entrypoint_object(f16fmal) add_math_entrypoint_object(f16fmaf128) +add_math_entrypoint_object(f16sqrt) add_math_entrypoint_object(f16sqrtf) add_math_entrypoint_object(fabs) diff --git a/libc/src/math/f16sqrt.h b/libc/src/math/f16sqrt.h new file mode 100644 index 0000000000000..f1134ac5ee2b9 --- /dev/null +++ b/libc/src/math/f16sqrt.h @@ -0,0 +1,20 @@ +//===-- Implementation header for f16sqrt -----------------------*- 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_F16SQRT_H +#define LLVM_LIBC_SRC_MATH_F16SQRT_H + +#include "src/__support/macros/properties/types.h" + +namespace LIBC_NAMESPACE { + +float16 f16sqrt(double x); + +} // namespace LIBC_NAMESPACE + +#endif // LLVM_LIBC_SRC_MATH_F16SQRT_H diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index 0e4893cd42ee1..50f33bfc9bada 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -3880,6 +3880,19 @@ add_entrypoint_object( -O3 ) +add_entrypoint_object( + f16sqrt + SRCS + f16sqrt.cpp + HDRS + ../f16sqrt.h + DEPENDS + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.sqrt + COMPILE_OPTIONS + -O3 +) + add_entrypoint_object( f16sqrtf SRCS diff --git a/libc/src/math/generic/f16sqrt.cpp b/libc/src/math/generic/f16sqrt.cpp new file mode 100644 index 0000000000000..9d5f081f63315 --- /dev/null +++ b/libc/src/math/generic/f16sqrt.cpp @@ -0,0 +1,19 @@ +//===-- Implementation of f16sqrt 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/f16sqrt.h" +#include "src/__support/FPUtil/sqrt.h" +#include "src/__support/common.h" + +namespace LIBC_NAMESPACE { + +LLVM_LIBC_FUNCTION(float16, f16sqrt, (double x)) { + return fputil::sqrt(x); +} + +} // namespace LIBC_NAMESPACE diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt index 061feeadb40d5..365627035b708 100644 --- a/libc/test/src/math/CMakeLists.txt +++ b/libc/test/src/math/CMakeLists.txt @@ -1259,9 +1259,10 @@ add_fp_unittest( libc-math-unittests SRCS sqrtf_test.cpp + HDRS + SqrtTest.h DEPENDS libc.src.math.sqrtf - libc.src.__support.FPUtil.fp_bits ) add_fp_unittest( @@ -1271,9 +1272,10 @@ add_fp_unittest( libc-math-unittests SRCS sqrt_test.cpp + HDRS + SqrtTest.h DEPENDS libc.src.math.sqrt - libc.src.__support.FPUtil.fp_bits ) add_fp_unittest( @@ -1283,9 +1285,10 @@ add_fp_unittest( libc-math-unittests SRCS sqrtl_test.cpp + HDRS + SqrtTest.h DEPENDS libc.src.math.sqrtl - libc.src.__support.FPUtil.fp_bits ) add_fp_unittest( @@ -1295,9 +1298,10 @@ add_fp_unittest( libc-math-unittests SRCS generic_sqrtf_test.cpp + HDRS + SqrtTest.h DEPENDS libc.src.math.sqrtf - libc.src.__support.FPUtil.fp_bits libc.src.__support.FPUtil.generic.sqrt COMPILE_OPTIONS -O3 @@ -1310,9 +1314,10 @@ add_fp_unittest( libc-math-unittests SRCS generic_sqrt_test.cpp + HDRS + SqrtTest.h DEPENDS libc.src.math.sqrt - libc.src.__support.FPUtil.fp_bits libc.src.__support.FPUtil.generic.sqrt COMPILE_OPTIONS -O3 @@ -1325,9 +1330,10 @@ add_fp_unittest( libc-math-unittests SRCS generic_sqrtl_test.cpp + HDRS + SqrtTest.h DEPENDS libc.src.math.sqrtl - libc.src.__support.FPUtil.fp_bits libc.src.__support.FPUtil.generic.sqrt COMPILE_OPTIONS -O3 @@ -1986,6 +1992,19 @@ add_fp_unittest( libc.src.stdlib.srand ) +add_fp_unittest( + f16sqrt_test + NEED_MPFR + SUITE + libc-math-unittests + SRCS + f16sqrt_test.cpp + HDRS + SqrtTest.h + DEPENDS + libc.src.math.f16sqrt +) + add_subdirectory(generic) add_subdirectory(smoke) diff --git a/libc/test/src/math/SqrtTest.h b/libc/test/src/math/SqrtTest.h index 1c422e201bb23..770cc94b3b940 100644 --- a/libc/test/src/math/SqrtTest.h +++ b/libc/test/src/math/SqrtTest.h @@ -6,51 +6,36 @@ // //===----------------------------------------------------------------------===// -#include "src/__support/CPP/bit.h" #include "test/UnitTest/FEnvSafeTest.h" #include "test/UnitTest/FPMatcher.h" #include "test/UnitTest/Test.h" #include "utils/MPFRWrapper/MPFRUtils.h" -#include "hdr/math_macros.h" - namespace mpfr = LIBC_NAMESPACE::testing::mpfr; -template +template class SqrtTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { - DECLARE_SPECIAL_CONSTANTS(T) + DECLARE_SPECIAL_CONSTANTS(InType) static constexpr StorageType HIDDEN_BIT = - StorageType(1) << LIBC_NAMESPACE::fputil::FPBits::FRACTION_LEN; + StorageType(1) << LIBC_NAMESPACE::fputil::FPBits::FRACTION_LEN; public: - typedef T (*SqrtFunc)(T); - - void test_special_numbers(SqrtFunc func) { - ASSERT_FP_EQ(aNaN, func(aNaN)); - ASSERT_FP_EQ(inf, func(inf)); - ASSERT_FP_EQ(aNaN, func(neg_inf)); - ASSERT_FP_EQ(0.0, func(0.0)); - ASSERT_FP_EQ(-0.0, func(-0.0)); - ASSERT_FP_EQ(aNaN, func(T(-1.0))); - ASSERT_FP_EQ(T(1.0), func(T(1.0))); - ASSERT_FP_EQ(T(2.0), func(T(4.0))); - ASSERT_FP_EQ(T(3.0), func(T(9.0))); - } + using SqrtFunc = OutType (*)(InType); void test_denormal_values(SqrtFunc func) { for (StorageType mant = 1; mant < HIDDEN_BIT; mant <<= 1) { - FPBits denormal(T(0.0)); + FPBits denormal(zero); denormal.set_mantissa(mant); - T x = denormal.get_val(); + InType x = denormal.get_val(); EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sqrt, x, func(x), 0.5); } constexpr StorageType COUNT = 200'001; constexpr StorageType STEP = HIDDEN_BIT / COUNT; for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) { - T x = LIBC_NAMESPACE::cpp::bit_cast(v); + InType x = FPBits(i).get_val(); EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sqrt, x, func(x), 0.5); } } @@ -59,17 +44,21 @@ class SqrtTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { constexpr StorageType COUNT = 200'001; constexpr StorageType STEP = STORAGE_MAX / COUNT; for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) { - T x = LIBC_NAMESPACE::cpp::bit_cast(v); - if (isnan(x) || (x < 0)) { + FPBits x_bits(v); + InType x = x_bits.get_val(); + if (x_bits.is_nan() || (x < 0)) continue; - } EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sqrt, x, func(x), 0.5); } } }; #define LIST_SQRT_TESTS(T, func) \ - using LlvmLibcSqrtTest = SqrtTest; \ - TEST_F(LlvmLibcSqrtTest, SpecialNumbers) { test_special_numbers(&func); } \ + using LlvmLibcSqrtTest = SqrtTest; \ + TEST_F(LlvmLibcSqrtTest, DenormalValues) { test_denormal_values(&func); } \ + TEST_F(LlvmLibcSqrtTest, NormalRange) { test_normal_range(&func); } + +#define LIST_NARROWING_SQRT_TESTS(OutType, InType, func) \ + using LlvmLibcSqrtTest = SqrtTest; \ TEST_F(LlvmLibcSqrtTest, DenormalValues) { test_denormal_values(&func); } \ TEST_F(LlvmLibcSqrtTest, NormalRange) { test_normal_range(&func); } diff --git a/libc/test/src/math/f16sqrt_test.cpp b/libc/test/src/math/f16sqrt_test.cpp new file mode 100644 index 0000000000000..759b388ad3b3d --- /dev/null +++ b/libc/test/src/math/f16sqrt_test.cpp @@ -0,0 +1,13 @@ +//===-- Unittests for f16sqrt ---------------------------------------------===// +// +// 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 "SqrtTest.h" + +#include "src/math/f16sqrt.h" + +LIST_NARROWING_SQRT_TESTS(float16, double, LIBC_NAMESPACE::f16sqrt) diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt index e3d8a14191ad2..f4b72b3f64e0b 100644 --- a/libc/test/src/math/smoke/CMakeLists.txt +++ b/libc/test/src/math/smoke/CMakeLists.txt @@ -3734,6 +3734,18 @@ add_fp_unittest( libc.src.math.f16fmaf128 ) +add_fp_unittest( + f16sqrt_test + SUITE + libc-math-smoke-tests + SRCS + f16sqrt_test.cpp + HDRS + SqrtTest.h + DEPENDS + libc.src.math.f16sqrt +) + add_fp_unittest( f16sqrtf_test SUITE diff --git a/libc/test/src/math/smoke/f16sqrt_test.cpp b/libc/test/src/math/smoke/f16sqrt_test.cpp new file mode 100644 index 0000000000000..759b388ad3b3d --- /dev/null +++ b/libc/test/src/math/smoke/f16sqrt_test.cpp @@ -0,0 +1,13 @@ +//===-- Unittests for f16sqrt ---------------------------------------------===// +// +// 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 "SqrtTest.h" + +#include "src/math/f16sqrt.h" + +LIST_NARROWING_SQRT_TESTS(float16, double, LIBC_NAMESPACE::f16sqrt) diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp index 0bfe49984e7d2..1674595c2ddd0 100644 --- a/libc/utils/MPFRWrapper/MPFRUtils.cpp +++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp @@ -810,6 +810,9 @@ template void explain_unary_operation_single_output_error(Operation op, float16, template void explain_unary_operation_single_output_error(Operation op, float, float16, double, RoundingMode); +template void explain_unary_operation_single_output_error(Operation op, double, + float16, double, + RoundingMode); #endif template @@ -1015,6 +1018,8 @@ template bool compare_unary_operation_single_output(Operation, float16, float16, double, RoundingMode); template bool compare_unary_operation_single_output(Operation, float, float16, double, RoundingMode); +template bool compare_unary_operation_single_output(Operation, double, float16, + double, RoundingMode); #endif template From e182c50a3d22897cc9c3a52e1371d8dd9373106f Mon Sep 17 00:00:00 2001 From: OverMighty Date: Tue, 25 Jun 2024 16:17:52 +0200 Subject: [PATCH 2/8] [libc][math][c23] Add f16sqrtf128 C23 math function --- libc/config/linux/aarch64/entrypoints.txt | 7 +++++++ libc/config/linux/x86_64/entrypoints.txt | 3 ++- libc/docs/math/index.rst | 2 +- libc/spec/stdc.td | 1 + libc/src/math/CMakeLists.txt | 1 + libc/src/math/f16sqrtf128.h | 20 +++++++++++++++++++ libc/src/math/generic/CMakeLists.txt | 13 ++++++++++++ libc/src/math/generic/f16sqrtf128.cpp | 19 ++++++++++++++++++ libc/test/src/math/smoke/CMakeLists.txt | 12 +++++++++++ libc/test/src/math/smoke/f16sqrtf128_test.cpp | 13 ++++++++++++ 10 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 libc/src/math/f16sqrtf128.h create mode 100644 libc/src/math/generic/f16sqrtf128.cpp create mode 100644 libc/test/src/math/smoke/f16sqrtf128_test.cpp diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt index ea89f8bd138d6..09b1969697400 100644 --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -562,6 +562,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.f16sqrtf128 + ) + 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 e4a2053916cbe..3241a6654eb85 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -597,8 +597,9 @@ if(LIBC_TYPES_HAS_FLOAT16) if(LIBC_TYPES_HAS_FLOAT128) list(APPEND TARGET_LIBM_ENTRYPOINTS # math.h C23 mixed _Float16 and _Float128 entrypoints - libc.src.math.f16fmaf128 libc.src.math.f16divf128 + libc.src.math.f16fmaf128 + libc.src.math.f16sqrtf128 ) endif() endif() diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst index d1a7cf56e5c65..2f5cdae89d94e 100644 --- a/libc/docs/math/index.rst +++ b/libc/docs/math/index.rst @@ -292,7 +292,7 @@ Higher Math Functions +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | fma | |check| | |check| | | | | 7.12.13.1 | F.10.10.1 | +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ -| f16sqrt | |check| | |check| | | N/A | | 7.12.14.6 | F.10.11 | +| f16sqrt | |check| | |check| | | N/A | |check| | 7.12.14.6 | F.10.11 | +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | fsqrt | N/A | | | N/A | | 7.12.14.6 | F.10.11 | +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td index a51aa088ec42f..bee21960bd13c 100644 --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -733,6 +733,7 @@ def StdC : StandardSpec<"stdc"> { GuardedFunctionSpec<"f16sqrt", RetValSpec, [ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, GuardedFunctionSpec<"f16sqrtf", RetValSpec, [ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, + GuardedFunctionSpec<"f16sqrtf128", RetValSpec, [ArgSpec], "LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128">, ] >; diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt index b92ccac94c7d9..73a3995c83377 100644 --- a/libc/src/math/CMakeLists.txt +++ b/libc/src/math/CMakeLists.txt @@ -111,6 +111,7 @@ add_math_entrypoint_object(f16fmaf128) add_math_entrypoint_object(f16sqrt) add_math_entrypoint_object(f16sqrtf) +add_math_entrypoint_object(f16sqrtf128) add_math_entrypoint_object(fabs) add_math_entrypoint_object(fabsf) diff --git a/libc/src/math/f16sqrtf128.h b/libc/src/math/f16sqrtf128.h new file mode 100644 index 0000000000000..61a6ce9ea5a5d --- /dev/null +++ b/libc/src/math/f16sqrtf128.h @@ -0,0 +1,20 @@ +//===-- Implementation header for f16sqrtf128 -------------------*- 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_F16SQRTF128_H +#define LLVM_LIBC_SRC_MATH_F16SQRTF128_H + +#include "src/__support/macros/properties/types.h" + +namespace LIBC_NAMESPACE { + +float16 f16sqrtf128(float128 x); + +} // namespace LIBC_NAMESPACE + +#endif // LLVM_LIBC_SRC_MATH_F16SQRTF128_H diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index 50f33bfc9bada..e1093a6a04274 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -3905,3 +3905,16 @@ add_entrypoint_object( COMPILE_OPTIONS -O3 ) + +add_entrypoint_object( + f16sqrtf128 + SRCS + f16sqrtf128.cpp + HDRS + ../f16sqrtf128.h + DEPENDS + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.sqrt + COMPILE_OPTIONS + -O3 +) diff --git a/libc/src/math/generic/f16sqrtf128.cpp b/libc/src/math/generic/f16sqrtf128.cpp new file mode 100644 index 0000000000000..11a1e8252788e --- /dev/null +++ b/libc/src/math/generic/f16sqrtf128.cpp @@ -0,0 +1,19 @@ +//===-- Implementation of f16sqrtf128 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/f16sqrtf128.h" +#include "src/__support/FPUtil/sqrt.h" +#include "src/__support/common.h" + +namespace LIBC_NAMESPACE { + +LLVM_LIBC_FUNCTION(float16, f16sqrtf128, (float128 x)) { + return fputil::sqrt(x); +} + +} // namespace LIBC_NAMESPACE diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt index f4b72b3f64e0b..3267437222e70 100644 --- a/libc/test/src/math/smoke/CMakeLists.txt +++ b/libc/test/src/math/smoke/CMakeLists.txt @@ -3758,6 +3758,18 @@ add_fp_unittest( libc.src.math.f16sqrtf ) +add_fp_unittest( + f16sqrtf128_test + SUITE + libc-math-smoke-tests + SRCS + f16sqrtf128_test.cpp + HDRS + SqrtTest.h + DEPENDS + libc.src.math.f16sqrtf128 +) + add_fp_unittest( sin_test SUITE diff --git a/libc/test/src/math/smoke/f16sqrtf128_test.cpp b/libc/test/src/math/smoke/f16sqrtf128_test.cpp new file mode 100644 index 0000000000000..31c994860dcd8 --- /dev/null +++ b/libc/test/src/math/smoke/f16sqrtf128_test.cpp @@ -0,0 +1,13 @@ +//===-- Unittests for f16sqrtf128 -----------------------------------------===// +// +// 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 "SqrtTest.h" + +#include "src/math/f16sqrtf128.h" + +LIST_NARROWING_SQRT_TESTS(float16, float128, LIBC_NAMESPACE::f16sqrtf128) From 2aaea915dd3efda46f8fb925c0c7f9588dc8125e Mon Sep 17 00:00:00 2001 From: OverMighty Date: Tue, 25 Jun 2024 16:22:51 +0200 Subject: [PATCH 3/8] [libc][math][c23] Add f16sqrtl C23 math function --- libc/config/linux/aarch64/entrypoints.txt | 1 + libc/config/linux/x86_64/entrypoints.txt | 1 + libc/docs/math/index.rst | 2 +- libc/spec/stdc.td | 1 + libc/src/math/CMakeLists.txt | 1 + libc/src/math/f16sqrtl.h | 20 ++++++++++++++++++++ libc/src/math/generic/CMakeLists.txt | 13 +++++++++++++ libc/src/math/generic/f16sqrtl.cpp | 19 +++++++++++++++++++ libc/test/src/math/CMakeLists.txt | 13 +++++++++++++ libc/test/src/math/f16sqrtl_test.cpp | 13 +++++++++++++ libc/test/src/math/smoke/CMakeLists.txt | 12 ++++++++++++ libc/test/src/math/smoke/f16sqrtl_test.cpp | 13 +++++++++++++ libc/utils/MPFRWrapper/MPFRUtils.cpp | 6 ++++++ 13 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 libc/src/math/f16sqrtl.h create mode 100644 libc/src/math/generic/f16sqrtl.cpp create mode 100644 libc/test/src/math/f16sqrtl_test.cpp create mode 100644 libc/test/src/math/smoke/f16sqrtl_test.cpp diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt index 09b1969697400..46038db3ed6dc 100644 --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -512,6 +512,7 @@ if(LIBC_TYPES_HAS_FLOAT16) libc.src.math.f16fmaf libc.src.math.f16sqrt libc.src.math.f16sqrtf + libc.src.math.f16sqrtl libc.src.math.fabsf16 libc.src.math.fdimf16 libc.src.math.floorf16 diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index 3241a6654eb85..9f67dea006699 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -545,6 +545,7 @@ if(LIBC_TYPES_HAS_FLOAT16) libc.src.math.f16fmal libc.src.math.f16sqrt libc.src.math.f16sqrtf + libc.src.math.f16sqrtl libc.src.math.fabsf16 libc.src.math.fdimf16 libc.src.math.floorf16 diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst index 2f5cdae89d94e..c327a24ecb7ce 100644 --- a/libc/docs/math/index.rst +++ b/libc/docs/math/index.rst @@ -292,7 +292,7 @@ Higher Math Functions +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | fma | |check| | |check| | | | | 7.12.13.1 | F.10.10.1 | +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ -| f16sqrt | |check| | |check| | | N/A | |check| | 7.12.14.6 | F.10.11 | +| f16sqrt | |check| | |check| | |check| | N/A | |check| | 7.12.14.6 | F.10.11 | +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | fsqrt | N/A | | | N/A | | 7.12.14.6 | F.10.11 | +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td index bee21960bd13c..27541e27f57f4 100644 --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -733,6 +733,7 @@ def StdC : StandardSpec<"stdc"> { GuardedFunctionSpec<"f16sqrt", RetValSpec, [ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, GuardedFunctionSpec<"f16sqrtf", RetValSpec, [ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, + GuardedFunctionSpec<"f16sqrtl", RetValSpec, [ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, GuardedFunctionSpec<"f16sqrtf128", RetValSpec, [ArgSpec], "LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128">, ] >; diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt index 73a3995c83377..4777f669f0cb5 100644 --- a/libc/src/math/CMakeLists.txt +++ b/libc/src/math/CMakeLists.txt @@ -111,6 +111,7 @@ add_math_entrypoint_object(f16fmaf128) add_math_entrypoint_object(f16sqrt) add_math_entrypoint_object(f16sqrtf) +add_math_entrypoint_object(f16sqrtl) add_math_entrypoint_object(f16sqrtf128) add_math_entrypoint_object(fabs) diff --git a/libc/src/math/f16sqrtl.h b/libc/src/math/f16sqrtl.h new file mode 100644 index 0000000000000..fd3c55fc95f32 --- /dev/null +++ b/libc/src/math/f16sqrtl.h @@ -0,0 +1,20 @@ +//===-- Implementation header for f16sqrtl ----------------------*- 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_F16SQRTL_H +#define LLVM_LIBC_SRC_MATH_F16SQRTL_H + +#include "src/__support/macros/properties/types.h" + +namespace LIBC_NAMESPACE { + +float16 f16sqrtl(long double x); + +} // namespace LIBC_NAMESPACE + +#endif // LLVM_LIBC_SRC_MATH_F16SQRTL_H diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index e1093a6a04274..7dd6c4812fa15 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -3906,6 +3906,19 @@ add_entrypoint_object( -O3 ) +add_entrypoint_object( + f16sqrtl + SRCS + f16sqrtl.cpp + HDRS + ../f16sqrtl.h + DEPENDS + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.sqrt + COMPILE_OPTIONS + -O3 +) + add_entrypoint_object( f16sqrtf128 SRCS diff --git a/libc/src/math/generic/f16sqrtl.cpp b/libc/src/math/generic/f16sqrtl.cpp new file mode 100644 index 0000000000000..2aaac9a780f66 --- /dev/null +++ b/libc/src/math/generic/f16sqrtl.cpp @@ -0,0 +1,19 @@ +//===-- Implementation of f16sqrtl 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/f16sqrtl.h" +#include "src/__support/FPUtil/sqrt.h" +#include "src/__support/common.h" + +namespace LIBC_NAMESPACE { + +LLVM_LIBC_FUNCTION(float16, f16sqrtl, (long double x)) { + return fputil::sqrt(x); +} + +} // namespace LIBC_NAMESPACE diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt index 365627035b708..6680b53ff7f36 100644 --- a/libc/test/src/math/CMakeLists.txt +++ b/libc/test/src/math/CMakeLists.txt @@ -1962,6 +1962,19 @@ add_fp_unittest( libc.src.stdlib.srand ) +add_fp_unittest( + f16sqrtl_test + NEED_MPFR + SUITE + libc-math-unittests + SRCS + f16sqrtl_test.cpp + HDRS + SqrtTest.h + DEPENDS + libc.src.math.f16sqrtl +) + add_fp_unittest( f16fmaf_test NEED_MPFR diff --git a/libc/test/src/math/f16sqrtl_test.cpp b/libc/test/src/math/f16sqrtl_test.cpp new file mode 100644 index 0000000000000..4e069bc8dbffd --- /dev/null +++ b/libc/test/src/math/f16sqrtl_test.cpp @@ -0,0 +1,13 @@ +//===-- Unittests for f16sqrtl --------------------------------------------===// +// +// 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 "SqrtTest.h" + +#include "src/math/f16sqrtl.h" + +LIST_NARROWING_SQRT_TESTS(float16, long double, LIBC_NAMESPACE::f16sqrtl) diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt index 3267437222e70..a1b4a03eb2e85 100644 --- a/libc/test/src/math/smoke/CMakeLists.txt +++ b/libc/test/src/math/smoke/CMakeLists.txt @@ -3758,6 +3758,18 @@ add_fp_unittest( libc.src.math.f16sqrtf ) +add_fp_unittest( + f16sqrtl_test + SUITE + libc-math-smoke-tests + SRCS + f16sqrtl_test.cpp + HDRS + SqrtTest.h + DEPENDS + libc.src.math.f16sqrtl +) + add_fp_unittest( f16sqrtf128_test SUITE diff --git a/libc/test/src/math/smoke/f16sqrtl_test.cpp b/libc/test/src/math/smoke/f16sqrtl_test.cpp new file mode 100644 index 0000000000000..4e069bc8dbffd --- /dev/null +++ b/libc/test/src/math/smoke/f16sqrtl_test.cpp @@ -0,0 +1,13 @@ +//===-- Unittests for f16sqrtl --------------------------------------------===// +// +// 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 "SqrtTest.h" + +#include "src/math/f16sqrtl.h" + +LIST_NARROWING_SQRT_TESTS(float16, long double, LIBC_NAMESPACE::f16sqrtl) diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp index 1674595c2ddd0..9a0e498708c03 100644 --- a/libc/utils/MPFRWrapper/MPFRUtils.cpp +++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp @@ -813,6 +813,9 @@ template void explain_unary_operation_single_output_error(Operation op, float, template void explain_unary_operation_single_output_error(Operation op, double, float16, double, RoundingMode); +template void explain_unary_operation_single_output_error(Operation op, + long double, float16, + double, RoundingMode); #endif template @@ -1020,6 +1023,9 @@ template bool compare_unary_operation_single_output(Operation, float, float16, double, RoundingMode); template bool compare_unary_operation_single_output(Operation, double, float16, double, RoundingMode); +template bool compare_unary_operation_single_output(Operation, long double, + float16, double, + RoundingMode); #endif template From e11f560249875f8d5d97d79caece939e44310e58 Mon Sep 17 00:00:00 2001 From: OverMighty Date: Fri, 28 Jun 2024 15:47:38 +0200 Subject: [PATCH 4/8] [libc] Fix |= between potential BigInt and bool --- libc/src/__support/FPUtil/generic/sqrt.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libc/src/__support/FPUtil/generic/sqrt.h b/libc/src/__support/FPUtil/generic/sqrt.h index e9cd3f47eef27..a1f81b0d25da6 100644 --- a/libc/src/__support/FPUtil/generic/sqrt.h +++ b/libc/src/__support/FPUtil/generic/sqrt.h @@ -153,7 +153,7 @@ sqrt(InType x) { y |= 1 << 1; } // Sticky bit. - y |= r != 0; + y |= static_cast(r != 0); DyadicFloat yd(Sign::POS, (x_exp >> 1) - 2 - InFPBits::FRACTION_LEN, y); return yd.template as(); From 36c3f6b78b80ae23949ca6836f40c13dc6ff8ed6 Mon Sep 17 00:00:00 2001 From: OverMighty Date: Fri, 28 Jun 2024 16:28:36 +0200 Subject: [PATCH 5/8] fixup! [libc][math][c23] Add f16sqrtl C23 math function Sort CMake target definitions. --- libc/test/src/math/CMakeLists.txt | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt index 6680b53ff7f36..1326d0830d7d9 100644 --- a/libc/test/src/math/CMakeLists.txt +++ b/libc/test/src/math/CMakeLists.txt @@ -1962,19 +1962,6 @@ add_fp_unittest( libc.src.stdlib.srand ) -add_fp_unittest( - f16sqrtl_test - NEED_MPFR - SUITE - libc-math-unittests - SRCS - f16sqrtl_test.cpp - HDRS - SqrtTest.h - DEPENDS - libc.src.math.f16sqrtl -) - add_fp_unittest( f16fmaf_test NEED_MPFR @@ -2018,6 +2005,19 @@ add_fp_unittest( libc.src.math.f16sqrt ) +add_fp_unittest( + f16sqrtl_test + NEED_MPFR + SUITE + libc-math-unittests + SRCS + f16sqrtl_test.cpp + HDRS + SqrtTest.h + DEPENDS + libc.src.math.f16sqrtl +) + add_subdirectory(generic) add_subdirectory(smoke) From b230e24f2c144b9fe0a00c72b7d43204791aed9c Mon Sep 17 00:00:00 2001 From: OverMighty Date: Sun, 30 Jun 2024 01:08:56 +0200 Subject: [PATCH 6/8] [libc][math][c23] Move f16sqrt{,f,l} specs to llvm_libc_ext.td --- libc/docs/math/index.rst | 2 +- libc/spec/llvm_libc_ext.td | 4 ++++ libc/spec/stdc.td | 3 --- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst index c327a24ecb7ce..3ca7bf9fdb283 100644 --- a/libc/docs/math/index.rst +++ b/libc/docs/math/index.rst @@ -292,7 +292,7 @@ Higher Math Functions +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | fma | |check| | |check| | | | | 7.12.13.1 | F.10.10.1 | +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ -| f16sqrt | |check| | |check| | |check| | N/A | |check| | 7.12.14.6 | F.10.11 | +| f16sqrt | |check|\* | |check|\* | |check|\* | N/A | |check| | 7.12.14.6 | F.10.11 | +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | fsqrt | N/A | | | N/A | | 7.12.14.6 | F.10.11 | +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ diff --git a/libc/spec/llvm_libc_ext.td b/libc/spec/llvm_libc_ext.td index ed1a17ec91904..74a90a4d04132 100644 --- a/libc/spec/llvm_libc_ext.td +++ b/libc/spec/llvm_libc_ext.td @@ -60,6 +60,10 @@ def LLVMLibcExt : StandardSpec<"llvm_libc_ext"> { GuardedFunctionSpec<"f16div", RetValSpec, [ArgSpec, ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, GuardedFunctionSpec<"f16divf", RetValSpec, [ArgSpec, ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, GuardedFunctionSpec<"f16divl", RetValSpec, [ArgSpec, ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, + + GuardedFunctionSpec<"f16sqrt", RetValSpec, [ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, + GuardedFunctionSpec<"f16sqrtf", RetValSpec, [ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, + GuardedFunctionSpec<"f16sqrtl", RetValSpec, [ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, ] >; diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td index 27541e27f57f4..6e59062dde727 100644 --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -731,9 +731,6 @@ def StdC : StandardSpec<"stdc"> { GuardedFunctionSpec<"f16divf128", RetValSpec, [ArgSpec, ArgSpec], "LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128">, - GuardedFunctionSpec<"f16sqrt", RetValSpec, [ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, - GuardedFunctionSpec<"f16sqrtf", RetValSpec, [ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, - GuardedFunctionSpec<"f16sqrtl", RetValSpec, [ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, GuardedFunctionSpec<"f16sqrtf128", RetValSpec, [ArgSpec], "LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128">, ] >; From ccacf01f3d42c5698e2ca65a096eae0c31b6bd34 Mon Sep 17 00:00:00 2001 From: OverMighty Date: Sun, 30 Jun 2024 01:09:53 +0200 Subject: [PATCH 7/8] [libc][math][c23] Disable f16sqrt{l,f128} on AArch64 Linux --- libc/config/linux/aarch64/entrypoints.txt | 8 -------- 1 file changed, 8 deletions(-) diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt index 46038db3ed6dc..ea89f8bd138d6 100644 --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -512,7 +512,6 @@ if(LIBC_TYPES_HAS_FLOAT16) libc.src.math.f16fmaf libc.src.math.f16sqrt libc.src.math.f16sqrtf - libc.src.math.f16sqrtl libc.src.math.fabsf16 libc.src.math.fdimf16 libc.src.math.floorf16 @@ -563,13 +562,6 @@ 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.f16sqrtf128 - ) - endif() endif() if(LIBC_TYPES_HAS_FLOAT128) From 4d3ce8008e7457aeadb21b2fd21e6b04fb6df3a0 Mon Sep 17 00:00:00 2001 From: OverMighty Date: Mon, 1 Jul 2024 00:34:30 +0200 Subject: [PATCH 8/8] fixup! [libc][math][c23] Add f16sqrt C23 math function --- libc/src/__support/FPUtil/generic/sqrt.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libc/src/__support/FPUtil/generic/sqrt.h b/libc/src/__support/FPUtil/generic/sqrt.h index a1f81b0d25da6..1b713796f7499 100644 --- a/libc/src/__support/FPUtil/generic/sqrt.h +++ b/libc/src/__support/FPUtil/generic/sqrt.h @@ -133,6 +133,7 @@ sqrt(InType x) { InStorageType y = ONE; InStorageType r = x_mant - ONE; + // TODO: Reduce iteration count to OutFPBits::FRACTION_LEN + 2 or + 3. for (InStorageType current_bit = ONE >> 1; current_bit; current_bit >>= 1) { r <<= 1; @@ -150,7 +151,7 @@ sqrt(InType x) { if (r >= tmp) { r -= tmp; // Rounding bit. - y |= 1 << 1; + y |= 2; } // Sticky bit. y |= static_cast(r != 0);