From ec4a01ac646ecd286e34a0cd18ec655d502efd21 Mon Sep 17 00:00:00 2001 From: OverMighty Date: Thu, 6 Jun 2024 21:14:25 +0200 Subject: [PATCH 1/9] [libc][math][c23] Add frexpf16 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/__support/FPUtil/NormalFloat.h | 11 +++++++---- libc/src/math/CMakeLists.txt | 1 + libc/src/math/frexpf16.h | 20 ++++++++++++++++++++ libc/src/math/generic/CMakeLists.txt | 13 +++++++++++++ libc/src/math/generic/frexpf16.cpp | 19 +++++++++++++++++++ libc/test/src/math/smoke/CMakeLists.txt | 12 ++++++++++++ libc/test/src/math/smoke/FrexpTest.h | 1 - libc/test/src/math/smoke/frexpf16_test.cpp | 13 +++++++++++++ 12 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 libc/src/math/frexpf16.h create mode 100644 libc/src/math/generic/frexpf16.cpp create mode 100644 libc/test/src/math/smoke/frexpf16_test.cpp diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt index ee86822d96d91..6a03b54ed8b36 100644 --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -516,6 +516,7 @@ if(LIBC_TYPES_HAS_FLOAT16) libc.src.math.fminimum_magf16 libc.src.math.fminimum_mag_numf16 libc.src.math.fminimum_numf16 + libc.src.math.frexpf16 libc.src.math.fromfpf16 libc.src.math.fromfpxf16 libc.src.math.llrintf16 diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index c4b3b1aece639..139d66afb84ae 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -549,6 +549,7 @@ if(LIBC_TYPES_HAS_FLOAT16) libc.src.math.fminimum_mag_numf16 libc.src.math.fminimum_numf16 libc.src.math.fmodf16 + libc.src.math.frexpf16 libc.src.math.fromfpf16 libc.src.math.fromfpxf16 libc.src.math.llrintf16 diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst index 43f63e910750f..b13c26e595f8b 100644 --- a/libc/docs/math/index.rst +++ b/libc/docs/math/index.rst @@ -160,7 +160,7 @@ Basic Operations +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | fmul | N/A | |check| | | N/A | | 7.12.14.3 | F.10.11 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ -| frexp | |check| | |check| | |check| | | |check| | 7.12.6.7 | F.10.3.7 | +| frexp | |check| | |check| | |check| | |check| | |check| | 7.12.6.7 | F.10.3.7 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | fromfp | |check| | |check| | |check| | |check| | |check| | 7.12.9.10 | F.10.6.10 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td index ac16041e44c99..35f699f835771 100644 --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -487,6 +487,7 @@ def StdC : StandardSpec<"stdc"> { FunctionSpec<"frexp", RetValSpec, [ArgSpec, ArgSpec]>, FunctionSpec<"frexpf", RetValSpec, [ArgSpec, ArgSpec]>, FunctionSpec<"frexpl", RetValSpec, [ArgSpec, ArgSpec]>, + GuardedFunctionSpec<"frexpf16", RetValSpec, [ArgSpec, ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, GuardedFunctionSpec<"frexpf128", RetValSpec, [ArgSpec, ArgSpec], "LIBC_TYPES_HAS_FLOAT128">, FunctionSpec<"fromfp", RetValSpec, [ArgSpec, ArgSpec, ArgSpec]>, diff --git a/libc/src/__support/FPUtil/NormalFloat.h b/libc/src/__support/FPUtil/NormalFloat.h index 8bc1fecd653bf..33529d5e9b80a 100644 --- a/libc/src/__support/FPUtil/NormalFloat.h +++ b/libc/src/__support/FPUtil/NormalFloat.h @@ -110,9 +110,11 @@ template struct NormalFloat { if (shift <= FPBits::FRACTION_LEN + 1) { // Generate a subnormal number. Might lead to loss of precision. // We round to nearest and round halfway cases to even. - const StorageType shift_out_mask = (StorageType(1) << shift) - 1; + const StorageType shift_out_mask = + static_cast(StorageType(1) << shift) - 1; const StorageType shift_out_value = mantissa & shift_out_mask; - const StorageType halfway_value = StorageType(1) << (shift - 1); + const StorageType halfway_value = + static_cast(StorageType(1) << (shift - 1)); result.set_biased_exponent(0); result.set_mantissa(mantissa >> shift); StorageType new_mantissa = result.get_mantissa(); @@ -135,7 +137,8 @@ template struct NormalFloat { } } - result.set_biased_exponent(exponent + FPBits::EXP_BIAS); + result.set_biased_exponent( + static_cast(exponent + FPBits::EXP_BIAS)); result.set_mantissa(mantissa); return result.get_val(); } @@ -155,7 +158,7 @@ template struct NormalFloat { // Normalize subnormal numbers. if (bits.is_subnormal()) { unsigned shift = evaluate_normalization_shift(bits.get_mantissa()); - mantissa = StorageType(bits.get_mantissa()) << shift; + mantissa = static_cast(bits.get_mantissa() << shift); exponent = 1 - FPBits::EXP_BIAS - shift; } else { exponent = bits.get_biased_exponent() - FPBits::EXP_BIAS; diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt index 68b8924460bf4..4e99e31d024c0 100644 --- a/libc/src/math/CMakeLists.txt +++ b/libc/src/math/CMakeLists.txt @@ -191,6 +191,7 @@ add_math_entrypoint_object(fmodf128) add_math_entrypoint_object(frexp) add_math_entrypoint_object(frexpf) add_math_entrypoint_object(frexpl) +add_math_entrypoint_object(frexpf16) add_math_entrypoint_object(frexpf128) add_math_entrypoint_object(fromfp) diff --git a/libc/src/math/frexpf16.h b/libc/src/math/frexpf16.h new file mode 100644 index 0000000000000..dc1898cc5f702 --- /dev/null +++ b/libc/src/math/frexpf16.h @@ -0,0 +1,20 @@ +//===-- Implementation header for frexpf16 ----------------------*- 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_FREXPF16_H +#define LLVM_LIBC_SRC_MATH_FREXPF16_H + +#include "src/__support/macros/properties/types.h" + +namespace LIBC_NAMESPACE { + +float16 frexpf16(float16 x, int *exp); + +} // namespace LIBC_NAMESPACE + +#endif // LLVM_LIBC_SRC_MATH_FREXPF16_H diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index 4f94468939ed8..496b12648b2d2 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -1301,6 +1301,19 @@ add_entrypoint_object( libc.src.__support.FPUtil.manipulation_functions ) +add_entrypoint_object( + frexpf16 + SRCS + frexpf16.cpp + HDRS + ../frexpf16.h + COMPILE_OPTIONS + -O3 + DEPENDS + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.manipulation_functions +) + add_entrypoint_object( frexpf128 SRCS diff --git a/libc/src/math/generic/frexpf16.cpp b/libc/src/math/generic/frexpf16.cpp new file mode 100644 index 0000000000000..2d29c07c7217d --- /dev/null +++ b/libc/src/math/generic/frexpf16.cpp @@ -0,0 +1,19 @@ +//===-- Implementation of frexpf16 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/frexpf16.h" +#include "src/__support/FPUtil/ManipulationFunctions.h" +#include "src/__support/common.h" + +namespace LIBC_NAMESPACE { + +LLVM_LIBC_FUNCTION(float16, frexpf16, (float16 x, int *exp)) { + return fputil::frexp(x, *exp); +} + +} // namespace LIBC_NAMESPACE diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt index cc0e00aeb1dd2..0eb7b6d80bd68 100644 --- a/libc/test/src/math/smoke/CMakeLists.txt +++ b/libc/test/src/math/smoke/CMakeLists.txt @@ -1091,6 +1091,18 @@ add_fp_unittest( libc.src.math.frexpl ) +add_fp_unittest( + frexpf16_test + SUITE + libc-math-smoke-tests + SRCS + frexpf16_test.cpp + HDRS + FrexpTest.h + DEPENDS + libc.src.math.frexpf16 +) + add_fp_unittest( frexpf128_test SUITE diff --git a/libc/test/src/math/smoke/FrexpTest.h b/libc/test/src/math/smoke/FrexpTest.h index e9e496422f732..fc2313a94ef09 100644 --- a/libc/test/src/math/smoke/FrexpTest.h +++ b/libc/test/src/math/smoke/FrexpTest.h @@ -6,7 +6,6 @@ // //===----------------------------------------------------------------------===// -#include "src/__support/FPUtil/BasicOperations.h" #include "test/UnitTest/FEnvSafeTest.h" #include "test/UnitTest/FPMatcher.h" #include "test/UnitTest/Test.h" diff --git a/libc/test/src/math/smoke/frexpf16_test.cpp b/libc/test/src/math/smoke/frexpf16_test.cpp new file mode 100644 index 0000000000000..4d5492c8cf0c0 --- /dev/null +++ b/libc/test/src/math/smoke/frexpf16_test.cpp @@ -0,0 +1,13 @@ +//===-- Unittests for frexpf16 --------------------------------------------===// +// +// 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 "FrexpTest.h" + +#include "src/math/frexpf16.h" + +LIST_FREXP_TESTS(float16, LIBC_NAMESPACE::frexpf16); From 72eb0a4302138f5cefe8563b7602e4b59157de4f Mon Sep 17 00:00:00 2001 From: OverMighty Date: Thu, 6 Jun 2024 23:03:58 +0200 Subject: [PATCH 2/9] [libc][math][c23] Add ilogbf16 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/__support/FPUtil/CMakeLists.txt | 1 + .../__support/FPUtil/ManipulationFunctions.h | 3 ++- libc/src/math/CMakeLists.txt | 1 + libc/src/math/generic/CMakeLists.txt | 13 ++++++++++++ libc/src/math/generic/ilogbf16.cpp | 19 ++++++++++++++++++ libc/src/math/ilogbf16.h | 20 +++++++++++++++++++ libc/test/src/math/smoke/CMakeLists.txt | 15 ++++++++++++++ libc/test/src/math/smoke/ILogbTest.h | 12 +++++++---- libc/test/src/math/smoke/ilogbf16_test.cpp | 13 ++++++++++++ 13 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 libc/src/math/generic/ilogbf16.cpp create mode 100644 libc/src/math/ilogbf16.h create mode 100644 libc/test/src/math/smoke/ilogbf16_test.cpp diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt index 6a03b54ed8b36..00c420cc155b2 100644 --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -519,6 +519,7 @@ if(LIBC_TYPES_HAS_FLOAT16) libc.src.math.frexpf16 libc.src.math.fromfpf16 libc.src.math.fromfpxf16 + libc.src.math.ilogbf16 libc.src.math.llrintf16 libc.src.math.llroundf16 libc.src.math.lrintf16 diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index 139d66afb84ae..44c1149234998 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -552,6 +552,7 @@ if(LIBC_TYPES_HAS_FLOAT16) libc.src.math.frexpf16 libc.src.math.fromfpf16 libc.src.math.fromfpxf16 + libc.src.math.ilogbf16 libc.src.math.llrintf16 libc.src.math.llroundf16 libc.src.math.lrintf16 diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst index b13c26e595f8b..9a151d85afa1b 100644 --- a/libc/docs/math/index.rst +++ b/libc/docs/math/index.rst @@ -168,7 +168,7 @@ Basic Operations +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | fsub | N/A | | | N/A | | 7.12.14.2 | F.10.11 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ -| ilogb | |check| | |check| | |check| | | |check| | 7.12.6.8 | F.10.3.8 | +| ilogb | |check| | |check| | |check| | |check| | |check| | 7.12.6.8 | F.10.3.8 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | ldexp | |check| | |check| | |check| | | |check| | 7.12.6.9 | F.10.3.9 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td index 35f699f835771..871fe638c05de 100644 --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -520,6 +520,7 @@ def StdC : StandardSpec<"stdc"> { FunctionSpec<"ilogb", RetValSpec, [ArgSpec]>, FunctionSpec<"ilogbf", RetValSpec, [ArgSpec]>, FunctionSpec<"ilogbl", RetValSpec, [ArgSpec]>, + GuardedFunctionSpec<"ilogbf16", RetValSpec, [ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, GuardedFunctionSpec<"ilogbf128", RetValSpec, [ArgSpec], "LIBC_TYPES_HAS_FLOAT128">, FunctionSpec<"llogb", RetValSpec, [ArgSpec]>, diff --git a/libc/src/__support/FPUtil/CMakeLists.txt b/libc/src/__support/FPUtil/CMakeLists.txt index 01ca4254c7996..1744f8cf626f3 100644 --- a/libc/src/__support/FPUtil/CMakeLists.txt +++ b/libc/src/__support/FPUtil/CMakeLists.txt @@ -217,6 +217,7 @@ add_header_library( .nearest_integer_operations .normal_float libc.hdr.math_macros + libc.src.__support.CPP.algorithm libc.src.__support.CPP.bit libc.src.__support.CPP.limits libc.src.__support.CPP.type_traits diff --git a/libc/src/__support/FPUtil/ManipulationFunctions.h b/libc/src/__support/FPUtil/ManipulationFunctions.h index a289c2ef70467..3296ffe7e25b5 100644 --- a/libc/src/__support/FPUtil/ManipulationFunctions.h +++ b/libc/src/__support/FPUtil/ManipulationFunctions.h @@ -16,6 +16,7 @@ #include "rounding_mode.h" #include "hdr/math_macros.h" +#include "src/__support/CPP/algorithm.h" #include "src/__support/CPP/bit.h" #include "src/__support/CPP/limits.h" // INT_MAX, INT_MIN #include "src/__support/CPP/type_traits.h" @@ -102,7 +103,7 @@ intlogb(U x) { return IntLogbConstants::T_MAX; } - DyadicFloat::STORAGE_LEN> normal(bits.get_val()); + DyadicFloat::STORAGE_LEN, 32)> normal(bits.get_val()); int exponent = normal.get_unbiased_exponent(); // The C standard does not specify the return value when an exponent is // out of int range. However, XSI conformance required that INT_MAX or diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt index 4e99e31d024c0..526a73c7fe895 100644 --- a/libc/src/math/CMakeLists.txt +++ b/libc/src/math/CMakeLists.txt @@ -212,6 +212,7 @@ add_math_entrypoint_object(hypotf) add_math_entrypoint_object(ilogb) add_math_entrypoint_object(ilogbf) add_math_entrypoint_object(ilogbl) +add_math_entrypoint_object(ilogbf16) add_math_entrypoint_object(ilogbf128) add_math_entrypoint_object(llogb) diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index 496b12648b2d2..376d188e9658d 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -1363,6 +1363,19 @@ add_entrypoint_object( libc.src.__support.FPUtil.manipulation_functions ) +add_entrypoint_object( + ilogbf16 + SRCS + ilogbf16.cpp + HDRS + ../ilogbf16.h + COMPILE_OPTIONS + -O3 + DEPENDS + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.manipulation_functions +) + add_entrypoint_object( ilogbf128 SRCS diff --git a/libc/src/math/generic/ilogbf16.cpp b/libc/src/math/generic/ilogbf16.cpp new file mode 100644 index 0000000000000..87e43f8a5ada3 --- /dev/null +++ b/libc/src/math/generic/ilogbf16.cpp @@ -0,0 +1,19 @@ +//===-- Implementation of ilogbf16 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/ilogbf16.h" +#include "src/__support/FPUtil/ManipulationFunctions.h" +#include "src/__support/common.h" + +namespace LIBC_NAMESPACE { + +LLVM_LIBC_FUNCTION(int, ilogbf16, (float16 x)) { + return fputil::intlogb(x); +} + +} // namespace LIBC_NAMESPACE diff --git a/libc/src/math/ilogbf16.h b/libc/src/math/ilogbf16.h new file mode 100644 index 0000000000000..4884a140cb4f6 --- /dev/null +++ b/libc/src/math/ilogbf16.h @@ -0,0 +1,20 @@ +//===-- Implementation header for ilogbf16 ----------------------*- 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_ILOGBF16_H +#define LLVM_LIBC_SRC_MATH_ILOGBF16_H + +#include "src/__support/macros/properties/types.h" + +namespace LIBC_NAMESPACE { + +int ilogbf16(float16 x); + +} // namespace LIBC_NAMESPACE + +#endif // LLVM_LIBC_SRC_MATH_ILOGBF16_H diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt index 0eb7b6d80bd68..7ebdf74bae825 100644 --- a/libc/test/src/math/smoke/CMakeLists.txt +++ b/libc/test/src/math/smoke/CMakeLists.txt @@ -1400,6 +1400,21 @@ add_fp_unittest( libc.src.__support.FPUtil.manipulation_functions ) +add_fp_unittest( + ilogbf16_test + SUITE + libc-math-smoke-tests + SRCS + ilogbf16_test.cpp + HDRS + ILogbTest.h + DEPENDS + libc.src.math.ilogbf16 + libc.src.__support.CPP.limits + libc.src.__support.FPUtil.fp_bits + libc.src.__support.FPUtil.manipulation_functions +) + add_fp_unittest( ilogbf128_test SUITE diff --git a/libc/test/src/math/smoke/ILogbTest.h b/libc/test/src/math/smoke/ILogbTest.h index 05f906b69947b..65ccd1ec6e755 100644 --- a/libc/test/src/math/smoke/ILogbTest.h +++ b/libc/test/src/math/smoke/ILogbTest.h @@ -76,8 +76,10 @@ class LlvmLibcILogbTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { void test_subnormal_range(Func func) { constexpr StorageType MIN_SUBNORMAL = FPBits::min_subnormal().uintval(); constexpr StorageType MAX_SUBNORMAL = FPBits::max_subnormal().uintval(); - constexpr StorageType COUNT = 10'001; - constexpr StorageType STEP = (MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT; + constexpr int COUNT = 10'001; + constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max( + static_cast((MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT), + StorageType(1)); for (StorageType v = MIN_SUBNORMAL; v <= MAX_SUBNORMAL; v += STEP) { FPBits x_bits = FPBits(v); if (x_bits.is_zero() || x_bits.is_inf_or_nan()) @@ -94,8 +96,10 @@ class LlvmLibcILogbTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { void test_normal_range(Func func) { constexpr StorageType MIN_NORMAL = FPBits::min_normal().uintval(); constexpr StorageType MAX_NORMAL = FPBits::max_normal().uintval(); - constexpr StorageType COUNT = 10'001; - constexpr StorageType STEP = (MAX_NORMAL - MIN_NORMAL) / COUNT; + constexpr int COUNT = 10'001; + constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max( + static_cast((MAX_NORMAL - MIN_NORMAL) / COUNT), + StorageType(1)); for (StorageType v = MIN_NORMAL; v <= MAX_NORMAL; v += STEP) { FPBits x_bits = FPBits(v); if (x_bits.is_zero() || x_bits.is_inf_or_nan()) diff --git a/libc/test/src/math/smoke/ilogbf16_test.cpp b/libc/test/src/math/smoke/ilogbf16_test.cpp new file mode 100644 index 0000000000000..e046709523202 --- /dev/null +++ b/libc/test/src/math/smoke/ilogbf16_test.cpp @@ -0,0 +1,13 @@ +//===-- Unittests for ilogbf16 --------------------------------------------===// +// +// 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 "ILogbTest.h" + +#include "src/math/ilogbf16.h" + +LIST_INTLOGB_TESTS(int, float16, LIBC_NAMESPACE::ilogbf16); From dcc2be43d2ba103ca47c3601f243bb4fe7dcbffc Mon Sep 17 00:00:00 2001 From: OverMighty Date: Fri, 7 Jun 2024 15:27:58 +0200 Subject: [PATCH 3/9] [libc][math][c23] Add ldexpf16 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/ManipulationFunctions.h | 2 +- libc/src/__support/FPUtil/dyadic_float.h | 5 +++++ libc/src/math/CMakeLists.txt | 1 + libc/src/math/generic/CMakeLists.txt | 13 ++++++++++++ libc/src/math/generic/ldexpf16.cpp | 19 ++++++++++++++++++ libc/src/math/ldexpf16.h | 20 +++++++++++++++++++ libc/test/src/math/smoke/CMakeLists.txt | 15 ++++++++++++++ libc/test/src/math/smoke/LdExpTest.h | 16 +++++++-------- libc/test/src/math/smoke/ldexpf16_test.cpp | 13 ++++++++++++ 13 files changed, 99 insertions(+), 10 deletions(-) create mode 100644 libc/src/math/generic/ldexpf16.cpp create mode 100644 libc/src/math/ldexpf16.h create mode 100644 libc/test/src/math/smoke/ldexpf16_test.cpp diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt index 00c420cc155b2..9963a58943599 100644 --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -520,6 +520,7 @@ if(LIBC_TYPES_HAS_FLOAT16) libc.src.math.fromfpf16 libc.src.math.fromfpxf16 libc.src.math.ilogbf16 + libc.src.math.ldexpf16 libc.src.math.llrintf16 libc.src.math.llroundf16 libc.src.math.lrintf16 diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index 44c1149234998..48b9f7741b87e 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -553,6 +553,7 @@ if(LIBC_TYPES_HAS_FLOAT16) libc.src.math.fromfpf16 libc.src.math.fromfpxf16 libc.src.math.ilogbf16 + libc.src.math.ldexpf16 libc.src.math.llrintf16 libc.src.math.llroundf16 libc.src.math.lrintf16 diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst index 9a151d85afa1b..8b4738d600f2f 100644 --- a/libc/docs/math/index.rst +++ b/libc/docs/math/index.rst @@ -170,7 +170,7 @@ Basic Operations +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | ilogb | |check| | |check| | |check| | |check| | |check| | 7.12.6.8 | F.10.3.8 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ -| ldexp | |check| | |check| | |check| | | |check| | 7.12.6.9 | F.10.3.9 | +| ldexp | |check| | |check| | |check| | |check| | |check| | 7.12.6.9 | F.10.3.9 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | llogb | |check| | |check| | |check| | | |check| | 7.12.6.10 | F.10.3.10 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td index 871fe638c05de..daace4583397d 100644 --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -531,6 +531,7 @@ def StdC : StandardSpec<"stdc"> { FunctionSpec<"ldexp", RetValSpec, [ArgSpec, ArgSpec]>, FunctionSpec<"ldexpf", RetValSpec, [ArgSpec, ArgSpec]>, FunctionSpec<"ldexpl", RetValSpec, [ArgSpec, ArgSpec]>, + GuardedFunctionSpec<"ldexpf16", RetValSpec, [ArgSpec, ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, GuardedFunctionSpec<"ldexpf128", RetValSpec, [ArgSpec, ArgSpec], "LIBC_TYPES_HAS_FLOAT128">, FunctionSpec<"log10", RetValSpec, [ArgSpec]>, diff --git a/libc/src/__support/FPUtil/ManipulationFunctions.h b/libc/src/__support/FPUtil/ManipulationFunctions.h index 3296ffe7e25b5..b3005f67b29cd 100644 --- a/libc/src/__support/FPUtil/ManipulationFunctions.h +++ b/libc/src/__support/FPUtil/ManipulationFunctions.h @@ -186,7 +186,7 @@ LIBC_INLINE constexpr T ldexp(T x, int exp) { } // For all other values, NormalFloat to T conversion handles it the right way. - DyadicFloat::STORAGE_LEN> normal(bits.get_val()); + DyadicFloat::STORAGE_LEN, 32)> normal(bits.get_val()); normal.exponent += exp; return static_cast(normal); } diff --git a/libc/src/__support/FPUtil/dyadic_float.h b/libc/src/__support/FPUtil/dyadic_float.h index 12a69228d36c7..f1791f9da56ad 100644 --- a/libc/src/__support/FPUtil/dyadic_float.h +++ b/libc/src/__support/FPUtil/dyadic_float.h @@ -14,6 +14,7 @@ #include "src/__support/CPP/type_traits.h" #include "src/__support/big_int.h" #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY +#include "src/__support/macros/properties/types.h" // float16 #include @@ -183,6 +184,10 @@ template struct DyadicFloat { return r; } + LIBC_INLINE explicit constexpr operator float16() const { + return static_cast(static_cast(*this)); + } + LIBC_INLINE explicit constexpr operator MantissaType() const { if (mantissa.is_zero()) return 0; diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt index 526a73c7fe895..3d6f9556333ee 100644 --- a/libc/src/math/CMakeLists.txt +++ b/libc/src/math/CMakeLists.txt @@ -223,6 +223,7 @@ add_math_entrypoint_object(llogbf128) add_math_entrypoint_object(ldexp) add_math_entrypoint_object(ldexpf) add_math_entrypoint_object(ldexpl) +add_math_entrypoint_object(ldexpf16) add_math_entrypoint_object(ldexpf128) add_math_entrypoint_object(log10) diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index 376d188e9658d..386138fab3e39 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -1474,6 +1474,19 @@ add_entrypoint_object( libc.src.__support.FPUtil.manipulation_functions ) +add_entrypoint_object( + ldexpf16 + SRCS + ldexpf16.cpp + HDRS + ../ldexpf16.h + COMPILE_OPTIONS + -O0 -ggdb3 + DEPENDS + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.manipulation_functions +) + add_entrypoint_object( ldexpf128 SRCS diff --git a/libc/src/math/generic/ldexpf16.cpp b/libc/src/math/generic/ldexpf16.cpp new file mode 100644 index 0000000000000..ed15c4572edf5 --- /dev/null +++ b/libc/src/math/generic/ldexpf16.cpp @@ -0,0 +1,19 @@ +//===-- Implementation of ldexpf16 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/ldexpf16.h" +#include "src/__support/FPUtil/ManipulationFunctions.h" +#include "src/__support/common.h" + +namespace LIBC_NAMESPACE { + +LLVM_LIBC_FUNCTION(float16, ldexpf16, (float16 x, int exp)) { + return fputil::ldexp(x, exp); +} + +} // namespace LIBC_NAMESPACE diff --git a/libc/src/math/ldexpf16.h b/libc/src/math/ldexpf16.h new file mode 100644 index 0000000000000..7303610b18dff --- /dev/null +++ b/libc/src/math/ldexpf16.h @@ -0,0 +1,20 @@ +//===-- Implementation header for ldexpf16 ----------------------*- 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_LDEXPF16_H +#define LLVM_LIBC_SRC_MATH_LDEXPF16_H + +#include "src/__support/macros/properties/types.h" + +namespace LIBC_NAMESPACE { + +float16 ldexpf16(float16 x, int exp); + +} // namespace LIBC_NAMESPACE + +#endif // LLVM_LIBC_SRC_MATH_LDEXPF16_H diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt index 7ebdf74bae825..3e92ce1ffd7e3 100644 --- a/libc/test/src/math/smoke/CMakeLists.txt +++ b/libc/test/src/math/smoke/CMakeLists.txt @@ -1535,6 +1535,21 @@ add_fp_unittest( libc.src.__support.FPUtil.normal_float ) +add_fp_unittest( + ldexpf16_test + SUITE + libc-math-smoke-tests + SRCS + ldexpf16_test.cpp + HDRS + LdExpTest.h + DEPENDS + libc.src.math.ldexpf16 + libc.src.__support.CPP.limits + libc.src.__support.FPUtil.fp_bits + libc.src.__support.FPUtil.normal_float +) + add_fp_unittest( ldexpf128_test SUITE diff --git a/libc/test/src/math/smoke/LdExpTest.h b/libc/test/src/math/smoke/LdExpTest.h index 713d305c47494..19c02d2dc7d0f 100644 --- a/libc/test/src/math/smoke/LdExpTest.h +++ b/libc/test/src/math/smoke/LdExpTest.h @@ -31,7 +31,7 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest { const T nan = FPBits::quiet_nan().get_val(); // A normalized mantissa to be used with tests. - static constexpr StorageType MANTISSA = NormalFloat::ONE + 0x1234; + static constexpr StorageType MANTISSA = NormalFloat::ONE + 0x123; public: typedef T (*LdExpFunc)(T, int); @@ -60,7 +60,7 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest { void testOverflow(LdExpFunc func) { NormalFloat x(Sign::POS, FPBits::MAX_BIASED_EXPONENT - 10, - NormalFloat::ONE + 0xF00BA); + NormalFloat::ONE + 0xF00); for (int32_t exp = 10; exp < 100; ++exp) { ASSERT_FP_EQ(inf, func(T(x), exp)); ASSERT_FP_EQ(neg_inf, func(-T(x), exp)); @@ -95,10 +95,10 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest { void testNormalOperation(LdExpFunc func) { T val_array[] = {// Normal numbers - NormalFloat(Sign::POS, 100, MANTISSA), - NormalFloat(Sign::POS, -100, MANTISSA), - NormalFloat(Sign::NEG, 100, MANTISSA), - NormalFloat(Sign::NEG, -100, MANTISSA), + NormalFloat(Sign::POS, 10, MANTISSA), + NormalFloat(Sign::POS, -10, MANTISSA), + NormalFloat(Sign::NEG, 10, MANTISSA), + NormalFloat(Sign::NEG, -10, MANTISSA), // Subnormal numbers NormalFloat(Sign::POS, -FPBits::EXP_BIAS, MANTISSA), NormalFloat(Sign::NEG, -FPBits::EXP_BIAS, MANTISSA)}; @@ -114,8 +114,8 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest { NormalFloat two_to_exp = NormalFloat(static_cast(1.L)); two_to_exp = two_to_exp.mul2(exp); - ASSERT_FP_EQ(func(x, exp), x * two_to_exp); - ASSERT_FP_EQ(func(x, -exp), x / two_to_exp); + ASSERT_FP_EQ(func(x, exp), x * static_cast(two_to_exp)); + ASSERT_FP_EQ(func(x, -exp), x / static_cast(two_to_exp)); } } diff --git a/libc/test/src/math/smoke/ldexpf16_test.cpp b/libc/test/src/math/smoke/ldexpf16_test.cpp new file mode 100644 index 0000000000000..ecf8f76a3aac8 --- /dev/null +++ b/libc/test/src/math/smoke/ldexpf16_test.cpp @@ -0,0 +1,13 @@ +//===-- Unittests for ldexpf16 --------------------------------------------===// +// +// 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 "LdExpTest.h" + +#include "src/math/ldexpf16.h" + +LIST_LDEXP_TESTS(float16, LIBC_NAMESPACE::ldexpf16); From 6e0e85175b3653b193ea860521f9aa26b4a10b29 Mon Sep 17 00:00:00 2001 From: OverMighty Date: Fri, 7 Jun 2024 15:36:24 +0200 Subject: [PATCH 4/9] [libc][math][c23] Add llogbf16 C23 math function --- libc/config/linux/aarch64/entrypoints.txt | 1 + libc/config/linux/x86_64/entrypoints.txt | 1 + libc/docs/c23.rst | 2 +- libc/docs/math/index.rst | 2 +- libc/spec/stdc.td | 1 + libc/src/math/CMakeLists.txt | 1 + libc/src/math/generic/CMakeLists.txt | 13 +++++++++++++ libc/src/math/generic/llogbf16.cpp | 19 +++++++++++++++++++ libc/src/math/llogbf16.h | 20 ++++++++++++++++++++ libc/test/src/math/smoke/CMakeLists.txt | 15 +++++++++++++++ libc/test/src/math/smoke/llogbf16_test.cpp | 13 +++++++++++++ 11 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 libc/src/math/generic/llogbf16.cpp create mode 100644 libc/src/math/llogbf16.h create mode 100644 libc/test/src/math/smoke/llogbf16_test.cpp diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt index 9963a58943599..479819734edc8 100644 --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -521,6 +521,7 @@ if(LIBC_TYPES_HAS_FLOAT16) libc.src.math.fromfpxf16 libc.src.math.ilogbf16 libc.src.math.ldexpf16 + libc.src.math.llogbf16 libc.src.math.llrintf16 libc.src.math.llroundf16 libc.src.math.lrintf16 diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index 48b9f7741b87e..7dd216ce34aa2 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -554,6 +554,7 @@ if(LIBC_TYPES_HAS_FLOAT16) libc.src.math.fromfpxf16 libc.src.math.ilogbf16 libc.src.math.ldexpf16 + libc.src.math.llogbf16 libc.src.math.llrintf16 libc.src.math.llroundf16 libc.src.math.lrintf16 diff --git a/libc/docs/c23.rst b/libc/docs/c23.rst index 71232cc004c70..fec9b24bbd581 100644 --- a/libc/docs/c23.rst +++ b/libc/docs/c23.rst @@ -50,7 +50,7 @@ Additions: * issignaling * issubnormal * iszero - * llogb* + * llogb* |check| * pown* * powr* * rootn* diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst index 8b4738d600f2f..9d5c5011cfed0 100644 --- a/libc/docs/math/index.rst +++ b/libc/docs/math/index.rst @@ -172,7 +172,7 @@ Basic Operations +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | ldexp | |check| | |check| | |check| | |check| | |check| | 7.12.6.9 | F.10.3.9 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ -| llogb | |check| | |check| | |check| | | |check| | 7.12.6.10 | F.10.3.10 | +| llogb | |check| | |check| | |check| | |check| | |check| | 7.12.6.10 | F.10.3.10 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | llrint | |check| | |check| | |check| | |check| | |check| | 7.12.9.5 | F.10.6.5 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td index daace4583397d..f6dec16e62fc7 100644 --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -526,6 +526,7 @@ def StdC : StandardSpec<"stdc"> { FunctionSpec<"llogb", RetValSpec, [ArgSpec]>, FunctionSpec<"llogbf", RetValSpec, [ArgSpec]>, FunctionSpec<"llogbl", RetValSpec, [ArgSpec]>, + GuardedFunctionSpec<"llogbf16", RetValSpec, [ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, GuardedFunctionSpec<"llogbf128", RetValSpec, [ArgSpec], "LIBC_TYPES_HAS_FLOAT128">, FunctionSpec<"ldexp", RetValSpec, [ArgSpec, ArgSpec]>, diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt index 3d6f9556333ee..531658b655609 100644 --- a/libc/src/math/CMakeLists.txt +++ b/libc/src/math/CMakeLists.txt @@ -218,6 +218,7 @@ add_math_entrypoint_object(ilogbf128) add_math_entrypoint_object(llogb) add_math_entrypoint_object(llogbf) add_math_entrypoint_object(llogbl) +add_math_entrypoint_object(llogbf16) add_math_entrypoint_object(llogbf128) add_math_entrypoint_object(ldexp) diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index 386138fab3e39..dfcc350ab6fad 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -1425,6 +1425,19 @@ add_entrypoint_object( libc.src.__support.FPUtil.manipulation_functions ) +add_entrypoint_object( + llogbf16 + SRCS + llogbf16.cpp + HDRS + ../llogbf16.h + COMPILE_OPTIONS + -O3 + DEPENDS + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.manipulation_functions +) + add_entrypoint_object( llogbf128 SRCS diff --git a/libc/src/math/generic/llogbf16.cpp b/libc/src/math/generic/llogbf16.cpp new file mode 100644 index 0000000000000..b7a21b95604d7 --- /dev/null +++ b/libc/src/math/generic/llogbf16.cpp @@ -0,0 +1,19 @@ +//===-- Implementation of llogbf16 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/llogbf16.h" +#include "src/__support/FPUtil/ManipulationFunctions.h" +#include "src/__support/common.h" + +namespace LIBC_NAMESPACE { + +LLVM_LIBC_FUNCTION(long, llogbf16, (float16 x)) { + return fputil::intlogb(x); +} + +} // namespace LIBC_NAMESPACE diff --git a/libc/src/math/llogbf16.h b/libc/src/math/llogbf16.h new file mode 100644 index 0000000000000..267ae410a31d8 --- /dev/null +++ b/libc/src/math/llogbf16.h @@ -0,0 +1,20 @@ +//===-- Implementation header for llogbf16 ----------------------*- 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_LLOGBF16_H +#define LLVM_LIBC_SRC_MATH_LLOGBF16_H + +#include "src/__support/macros/properties/types.h" + +namespace LIBC_NAMESPACE { + +long llogbf16(float16 x); + +} // namespace LIBC_NAMESPACE + +#endif // LLVM_LIBC_SRC_MATH_LLOGBF16_H diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt index 3e92ce1ffd7e3..b945e8491d233 100644 --- a/libc/test/src/math/smoke/CMakeLists.txt +++ b/libc/test/src/math/smoke/CMakeLists.txt @@ -1475,6 +1475,21 @@ add_fp_unittest( libc.src.__support.FPUtil.manipulation_functions ) +add_fp_unittest( + llogbf16_test + SUITE + libc-math-smoke-tests + SRCS + llogbf16_test.cpp + HDRS + ILogbTest.h + DEPENDS + libc.src.math.llogbf16 + libc.src.__support.CPP.limits + libc.src.__support.FPUtil.fp_bits + libc.src.__support.FPUtil.manipulation_functions +) + add_fp_unittest( llogbf128_test SUITE diff --git a/libc/test/src/math/smoke/llogbf16_test.cpp b/libc/test/src/math/smoke/llogbf16_test.cpp new file mode 100644 index 0000000000000..8907681024b1b --- /dev/null +++ b/libc/test/src/math/smoke/llogbf16_test.cpp @@ -0,0 +1,13 @@ +//===-- Unittests for llogbf16 --------------------------------------------===// +// +// 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 "ILogbTest.h" + +#include "src/math/llogbf16.h" + +LIST_INTLOGB_TESTS(long, float16, LIBC_NAMESPACE::llogbf16); From 1ecf74785f521d530735988a61f2589bca6f92c8 Mon Sep 17 00:00:00 2001 From: OverMighty Date: Fri, 7 Jun 2024 15:39:17 +0200 Subject: [PATCH 5/9] [libc][math] Fix smoke/ILogbTest.h dependencies --- libc/test/src/math/smoke/CMakeLists.txt | 20 ++++++++++---------- libc/test/src/math/smoke/ILogbTest.h | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt index b945e8491d233..ce6b262244513 100644 --- a/libc/test/src/math/smoke/CMakeLists.txt +++ b/libc/test/src/math/smoke/CMakeLists.txt @@ -1365,7 +1365,7 @@ add_fp_unittest( ILogbTest.h DEPENDS libc.src.math.ilogb - libc.src.__support.CPP.limits + libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.fp_bits libc.src.__support.FPUtil.manipulation_functions ) @@ -1380,7 +1380,7 @@ add_fp_unittest( ILogbTest.h DEPENDS libc.src.math.ilogbf - libc.src.__support.CPP.limits + libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.fp_bits libc.src.__support.FPUtil.manipulation_functions ) @@ -1395,7 +1395,7 @@ add_fp_unittest( ILogbTest.h DEPENDS libc.src.math.ilogbl - libc.src.__support.CPP.limits + libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.fp_bits libc.src.__support.FPUtil.manipulation_functions ) @@ -1410,7 +1410,7 @@ add_fp_unittest( ILogbTest.h DEPENDS libc.src.math.ilogbf16 - libc.src.__support.CPP.limits + libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.fp_bits libc.src.__support.FPUtil.manipulation_functions ) @@ -1425,7 +1425,7 @@ add_fp_unittest( ILogbTest.h DEPENDS libc.src.math.ilogbf128 - libc.src.__support.CPP.limits + libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.fp_bits libc.src.__support.FPUtil.manipulation_functions ) @@ -1440,7 +1440,7 @@ add_fp_unittest( ILogbTest.h DEPENDS libc.src.math.llogb - libc.src.__support.CPP.limits + libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.fp_bits libc.src.__support.FPUtil.manipulation_functions ) @@ -1455,7 +1455,7 @@ add_fp_unittest( ILogbTest.h DEPENDS libc.src.math.llogbf - libc.src.__support.CPP.limits + libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.fp_bits libc.src.__support.FPUtil.manipulation_functions ) @@ -1470,7 +1470,7 @@ add_fp_unittest( ILogbTest.h DEPENDS libc.src.math.llogbl - libc.src.__support.CPP.limits + libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.fp_bits libc.src.__support.FPUtil.manipulation_functions ) @@ -1485,7 +1485,7 @@ add_fp_unittest( ILogbTest.h DEPENDS libc.src.math.llogbf16 - libc.src.__support.CPP.limits + libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.fp_bits libc.src.__support.FPUtil.manipulation_functions ) @@ -1500,7 +1500,7 @@ add_fp_unittest( ILogbTest.h DEPENDS libc.src.math.llogbf128 - libc.src.__support.CPP.limits + libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.fp_bits libc.src.__support.FPUtil.manipulation_functions ) diff --git a/libc/test/src/math/smoke/ILogbTest.h b/libc/test/src/math/smoke/ILogbTest.h index 65ccd1ec6e755..f817b490b9861 100644 --- a/libc/test/src/math/smoke/ILogbTest.h +++ b/libc/test/src/math/smoke/ILogbTest.h @@ -9,7 +9,7 @@ #ifndef LLVM_LIBC_TEST_SRC_MATH_ILOGBTEST_H #define LLVM_LIBC_TEST_SRC_MATH_ILOGBTEST_H -#include "src/__support/CPP/limits.h" // INT_MAX +#include "src/__support/CPP/algorithm.h" #include "src/__support/FPUtil/FPBits.h" #include "src/__support/FPUtil/ManipulationFunctions.h" #include "test/UnitTest/FEnvSafeTest.h" From ec1c915a49d037f836d3ffb38807e7e07b00a69b Mon Sep 17 00:00:00 2001 From: OverMighty Date: Fri, 7 Jun 2024 15:50:54 +0200 Subject: [PATCH 6/9] [libc][math][c23] Add logbf16 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/ManipulationFunctions.h | 2 +- libc/src/math/CMakeLists.txt | 1 + libc/src/math/generic/CMakeLists.txt | 14 +++++++++++ libc/src/math/generic/logbf16.cpp | 17 +++++++++++++ libc/src/math/logbf16.h | 20 ++++++++++++++++ libc/test/src/math/smoke/CMakeLists.txt | 24 +++++++++++++++++++ libc/test/src/math/smoke/LogbTest.h | 9 ++++--- libc/test/src/math/smoke/logbf16_test.cpp | 13 ++++++++++ 12 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 libc/src/math/generic/logbf16.cpp create mode 100644 libc/src/math/logbf16.h create mode 100644 libc/test/src/math/smoke/logbf16_test.cpp diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt index 479819734edc8..c2bc178803928 100644 --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -524,6 +524,7 @@ if(LIBC_TYPES_HAS_FLOAT16) libc.src.math.llogbf16 libc.src.math.llrintf16 libc.src.math.llroundf16 + libc.src.math.logbf16 libc.src.math.lrintf16 libc.src.math.lroundf16 libc.src.math.nanf16 diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index 7dd216ce34aa2..98dad7d9f1ca1 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -557,6 +557,7 @@ if(LIBC_TYPES_HAS_FLOAT16) libc.src.math.llogbf16 libc.src.math.llrintf16 libc.src.math.llroundf16 + libc.src.math.logbf16 libc.src.math.lrintf16 libc.src.math.lroundf16 libc.src.math.nanf16 diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst index 9d5c5011cfed0..3fc19c598a073 100644 --- a/libc/docs/math/index.rst +++ b/libc/docs/math/index.rst @@ -178,7 +178,7 @@ Basic Operations +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | llround | |check| | |check| | |check| | |check| | |check| | 7.12.9.7 | F.10.6.7 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ -| logb | |check| | |check| | |check| | | |check| | 7.12.6.17 | F.10.3.17 | +| logb | |check| | |check| | |check| | |check| | |check| | 7.12.6.17 | F.10.3.17 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | lrint | |check| | |check| | |check| | |check| | |check| | 7.12.9.5 | F.10.6.5 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td index f6dec16e62fc7..4f49526b1180e 100644 --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -550,6 +550,7 @@ def StdC : StandardSpec<"stdc"> { FunctionSpec<"logb", RetValSpec, [ArgSpec]>, FunctionSpec<"logbf", RetValSpec, [ArgSpec]>, FunctionSpec<"logbl", RetValSpec, [ArgSpec]>, + GuardedFunctionSpec<"logbf16", RetValSpec, [ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, GuardedFunctionSpec<"logbf128", RetValSpec, [ArgSpec], "LIBC_TYPES_HAS_FLOAT128">, FunctionSpec<"modf", RetValSpec, [ArgSpec, ArgSpec]>, diff --git a/libc/src/__support/FPUtil/ManipulationFunctions.h b/libc/src/__support/FPUtil/ManipulationFunctions.h index b3005f67b29cd..09cfff31d64bb 100644 --- a/libc/src/__support/FPUtil/ManipulationFunctions.h +++ b/libc/src/__support/FPUtil/ManipulationFunctions.h @@ -139,7 +139,7 @@ LIBC_INLINE constexpr T logb(T x) { return FPBits::inf().get_val(); } - DyadicFloat::STORAGE_LEN> normal(bits.get_val()); + DyadicFloat::STORAGE_LEN, 32)> normal(bits.get_val()); return static_cast(normal.get_unbiased_exponent()); } diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt index 531658b655609..27e3c9d14f9fe 100644 --- a/libc/src/math/CMakeLists.txt +++ b/libc/src/math/CMakeLists.txt @@ -242,6 +242,7 @@ add_math_entrypoint_object(logf) add_math_entrypoint_object(logb) add_math_entrypoint_object(logbf) add_math_entrypoint_object(logbl) +add_math_entrypoint_object(logbf16) add_math_entrypoint_object(logbf128) add_math_entrypoint_object(llrint) diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index dfcc350ab6fad..7fdf2f6117d0e 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -1724,6 +1724,19 @@ add_entrypoint_object( libc.src.__support.FPUtil.manipulation_functions ) +add_entrypoint_object( + logbf16 + SRCS + logbf16.cpp + HDRS + ../logbf16.h + COMPILE_OPTIONS + -O3 + DEPENDS + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.manipulation_functions +) + add_entrypoint_object( logbf128 SRCS @@ -1733,6 +1746,7 @@ add_entrypoint_object( COMPILE_OPTIONS -O3 DEPENDS + libc.src.__support.macros.properties.types libc.src.__support.FPUtil.manipulation_functions ) diff --git a/libc/src/math/generic/logbf16.cpp b/libc/src/math/generic/logbf16.cpp new file mode 100644 index 0000000000000..52eb9ac6a342d --- /dev/null +++ b/libc/src/math/generic/logbf16.cpp @@ -0,0 +1,17 @@ +//===-- Implementation of logbf16 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/logbf16.h" +#include "src/__support/FPUtil/ManipulationFunctions.h" +#include "src/__support/common.h" + +namespace LIBC_NAMESPACE { + +LLVM_LIBC_FUNCTION(float16, logbf16, (float16 x)) { return fputil::logb(x); } + +} // namespace LIBC_NAMESPACE diff --git a/libc/src/math/logbf16.h b/libc/src/math/logbf16.h new file mode 100644 index 0000000000000..8082e06c33d52 --- /dev/null +++ b/libc/src/math/logbf16.h @@ -0,0 +1,20 @@ +//===-- Implementation header for logbf16 -----------------------*- 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_LOGBF16_H +#define LLVM_LIBC_SRC_MATH_LOGBF16_H + +#include "src/__support/macros/properties/types.h" + +namespace LIBC_NAMESPACE { + +float16 logbf16(float16 x); + +} // namespace LIBC_NAMESPACE + +#endif // LLVM_LIBC_SRC_MATH_LOGBF16_H diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt index ce6b262244513..9474baf9aa40a 100644 --- a/libc/test/src/math/smoke/CMakeLists.txt +++ b/libc/test/src/math/smoke/CMakeLists.txt @@ -1586,8 +1586,11 @@ add_fp_unittest( libc-math-smoke-tests SRCS logb_test.cpp + HDRS + LogbTest.h DEPENDS libc.src.math.logb + libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.manipulation_functions ) @@ -1597,8 +1600,11 @@ add_fp_unittest( libc-math-smoke-tests SRCS logbf_test.cpp + HDRS + LogbTest.h DEPENDS libc.src.math.logbf + libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.manipulation_functions ) @@ -1612,6 +1618,21 @@ add_fp_unittest( LogbTest.h DEPENDS libc.src.math.logbl + libc.src.__support.CPP.algorithm + libc.src.__support.FPUtil.manipulation_functions +) + +add_fp_unittest( + logbf16_test + SUITE + libc-math-smoke-tests + SRCS + logbf16_test.cpp + HDRS + LogbTest.h + DEPENDS + libc.src.math.logbf16 + libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.manipulation_functions ) @@ -1621,8 +1642,11 @@ add_fp_unittest( libc-math-smoke-tests SRCS logbf128_test.cpp + HDRS + LogbTest.h DEPENDS libc.src.math.logbf128 + libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.manipulation_functions ) diff --git a/libc/test/src/math/smoke/LogbTest.h b/libc/test/src/math/smoke/LogbTest.h index 4938fcf8f6f16..07865e3d6cc23 100644 --- a/libc/test/src/math/smoke/LogbTest.h +++ b/libc/test/src/math/smoke/LogbTest.h @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// +#include "src/__support/CPP/algorithm.h" #include "src/__support/FPUtil/ManipulationFunctions.h" #include "test/UnitTest/FEnvSafeTest.h" #include "test/UnitTest/FPMatcher.h" @@ -69,9 +70,11 @@ class LogbTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { void testRange(LogbFunc func) { using StorageType = typename FPBits::StorageType; - constexpr StorageType COUNT = 100'000; - constexpr StorageType STEP = STORAGE_MAX / COUNT; - for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) { + constexpr int COUNT = 100'000; + constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max( + static_cast(STORAGE_MAX / COUNT), StorageType(1)); + StorageType v = 0; + for (int i = 0; i <= COUNT; ++i, v += STEP) { FPBits x_bits = FPBits(v); if (x_bits.is_zero() || x_bits.is_inf_or_nan()) continue; diff --git a/libc/test/src/math/smoke/logbf16_test.cpp b/libc/test/src/math/smoke/logbf16_test.cpp new file mode 100644 index 0000000000000..cfc1a0521b339 --- /dev/null +++ b/libc/test/src/math/smoke/logbf16_test.cpp @@ -0,0 +1,13 @@ +//===-- Unittests for logbf16 ---------------------------------------------===// +// +// 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 "LogbTest.h" + +#include "src/math/logbf16.h" + +LIST_LOGB_TESTS(float16, LIBC_NAMESPACE::logbf16) From a66079a361b82171317ee5457855c8291810a180 Mon Sep 17 00:00:00 2001 From: OverMighty Date: Fri, 7 Jun 2024 15:59:39 +0200 Subject: [PATCH 7/9] [libc][math][c23] Add modff16 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/spec.td | 1 + libc/spec/stdc.td | 1 + libc/src/math/CMakeLists.txt | 1 + libc/src/math/generic/CMakeLists.txt | 13 +++++++++++++ libc/src/math/generic/modff16.cpp | 19 +++++++++++++++++++ libc/src/math/modff16.h | 20 ++++++++++++++++++++ libc/test/src/math/smoke/CMakeLists.txt | 19 +++++++++++++++++++ libc/test/src/math/smoke/ModfTest.h | 11 +++++++---- libc/test/src/math/smoke/modff16_test.cpp | 13 +++++++++++++ 12 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 libc/src/math/generic/modff16.cpp create mode 100644 libc/src/math/modff16.h create mode 100644 libc/test/src/math/smoke/modff16_test.cpp diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt index c2bc178803928..93ba46b4d7ec5 100644 --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -527,6 +527,7 @@ if(LIBC_TYPES_HAS_FLOAT16) libc.src.math.logbf16 libc.src.math.lrintf16 libc.src.math.lroundf16 + libc.src.math.modff16 libc.src.math.nanf16 libc.src.math.nearbyintf16 libc.src.math.nextafterf16 diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index 98dad7d9f1ca1..7137bf82ef63c 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -560,6 +560,7 @@ if(LIBC_TYPES_HAS_FLOAT16) libc.src.math.logbf16 libc.src.math.lrintf16 libc.src.math.lroundf16 + libc.src.math.modff16 libc.src.math.nanf16 libc.src.math.nearbyintf16 libc.src.math.nextafterf16 diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst index 3fc19c598a073..8e74969ce4045 100644 --- a/libc/docs/math/index.rst +++ b/libc/docs/math/index.rst @@ -184,7 +184,7 @@ Basic Operations +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | lround | |check| | |check| | |check| | |check| | |check| | 7.12.9.7 | F.10.6.7 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ -| modf | |check| | |check| | |check| | | |check| | 7.12.6.18 | F.10.3.18 | +| modf | |check| | |check| | |check| | |check| | |check| | 7.12.6.18 | F.10.3.18 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | nan | |check| | |check| | |check| | |check| | |check| | 7.12.11.2 | F.10.8.2 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ diff --git a/libc/spec/spec.td b/libc/spec/spec.td index 056a3143c5a71..7e1283e67fab2 100644 --- a/libc/spec/spec.td +++ b/libc/spec/spec.td @@ -115,6 +115,7 @@ def IntPtr : PtrType; def RestrictedIntPtr : RestrictedPtrType; def FloatPtr : PtrType; def DoublePtr : PtrType; +def Float16Ptr : PtrType; def Float128Ptr : PtrType; def UnsignedCharPtr : PtrType; diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td index 4f49526b1180e..8051e05e99168 100644 --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -556,6 +556,7 @@ def StdC : StandardSpec<"stdc"> { FunctionSpec<"modf", RetValSpec, [ArgSpec, ArgSpec]>, FunctionSpec<"modff", RetValSpec, [ArgSpec, ArgSpec]>, FunctionSpec<"modfl", RetValSpec, [ArgSpec, ArgSpec]>, + GuardedFunctionSpec<"modff16", RetValSpec, [ArgSpec, ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, GuardedFunctionSpec<"modff128", RetValSpec, [ArgSpec, ArgSpec], "LIBC_TYPES_HAS_FLOAT128">, FunctionSpec<"cos", RetValSpec, [ArgSpec]>, diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt index 27e3c9d14f9fe..c186650a8b3fb 100644 --- a/libc/src/math/CMakeLists.txt +++ b/libc/src/math/CMakeLists.txt @@ -272,6 +272,7 @@ add_math_entrypoint_object(lroundf128) add_math_entrypoint_object(modf) add_math_entrypoint_object(modff) add_math_entrypoint_object(modfl) +add_math_entrypoint_object(modff16) add_math_entrypoint_object(modff128) add_math_entrypoint_object(nan) diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index 7fdf2f6117d0e..908ca66ac7a02 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -1786,6 +1786,19 @@ add_entrypoint_object( -O3 ) +add_entrypoint_object( + modff16 + SRCS + modff16.cpp + HDRS + ../modff16.h + DEPENDS + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.manipulation_functions + COMPILE_OPTIONS + -O3 +) + add_entrypoint_object( modff128 SRCS diff --git a/libc/src/math/generic/modff16.cpp b/libc/src/math/generic/modff16.cpp new file mode 100644 index 0000000000000..50cc5b5783ade --- /dev/null +++ b/libc/src/math/generic/modff16.cpp @@ -0,0 +1,19 @@ +//===-- Implementation of modff16 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/modff16.h" +#include "src/__support/FPUtil/ManipulationFunctions.h" +#include "src/__support/common.h" + +namespace LIBC_NAMESPACE { + +LLVM_LIBC_FUNCTION(float16, modff16, (float16 x, float16 *iptr)) { + return fputil::modf(x, *iptr); +} + +} // namespace LIBC_NAMESPACE diff --git a/libc/src/math/modff16.h b/libc/src/math/modff16.h new file mode 100644 index 0000000000000..a3017c5a92a64 --- /dev/null +++ b/libc/src/math/modff16.h @@ -0,0 +1,20 @@ +//===-- Implementation header for modff16 -----------------------*- 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_MODFF16_H +#define LLVM_LIBC_SRC_MATH_MODFF16_H + +#include "src/__support/macros/properties/types.h" + +namespace LIBC_NAMESPACE { + +float16 modff16(float16 x, float16 *iptr); + +} // namespace LIBC_NAMESPACE + +#endif // LLVM_LIBC_SRC_MATH_MODFF16_H diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt index 9474baf9aa40a..23a4420ab4d32 100644 --- a/libc/test/src/math/smoke/CMakeLists.txt +++ b/libc/test/src/math/smoke/CMakeLists.txt @@ -1660,6 +1660,7 @@ add_fp_unittest( ModfTest.h DEPENDS libc.src.math.modf + libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.basic_operations libc.src.__support.FPUtil.nearest_integer_operations ) @@ -1674,6 +1675,7 @@ add_fp_unittest( ModfTest.h DEPENDS libc.src.math.modff + libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.basic_operations libc.src.__support.FPUtil.nearest_integer_operations ) @@ -1688,6 +1690,22 @@ add_fp_unittest( ModfTest.h DEPENDS libc.src.math.modfl + libc.src.__support.CPP.algorithm + libc.src.__support.FPUtil.basic_operations + libc.src.__support.FPUtil.nearest_integer_operations +) + +add_fp_unittest( + modff16_test + SUITE + libc-math-smoke-tests + SRCS + modff16_test.cpp + HDRS + ModfTest.h + DEPENDS + libc.src.math.modff16 + libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.basic_operations libc.src.__support.FPUtil.nearest_integer_operations ) @@ -1702,6 +1720,7 @@ add_fp_unittest( ModfTest.h DEPENDS libc.src.math.modff128 + libc.src.__support.CPP.algorithm libc.src.__support.FPUtil.basic_operations libc.src.__support.FPUtil.nearest_integer_operations ) diff --git a/libc/test/src/math/smoke/ModfTest.h b/libc/test/src/math/smoke/ModfTest.h index 85db2d6d967b2..6226e5d55f40c 100644 --- a/libc/test/src/math/smoke/ModfTest.h +++ b/libc/test/src/math/smoke/ModfTest.h @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// +#include "src/__support/CPP/algorithm.h" #include "src/__support/FPUtil/BasicOperations.h" #include "src/__support/FPUtil/NearestIntegerOperations.h" #include "test/UnitTest/FEnvSafeTest.h" @@ -83,10 +84,12 @@ class ModfTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { } void testRange(ModfFunc func) { - constexpr StorageType COUNT = 100'000; - constexpr StorageType STEP = STORAGE_MAX / COUNT; - for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) { - FPBits x_bits = FPBits(v); + constexpr int COUNT = 100'000; + constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max( + static_cast(STORAGE_MAX / COUNT), StorageType(1)); + StorageType v = 0; + for (int i = 0; i <= COUNT; ++i, v += STEP) { + FPBits x_bits(v); if (x_bits.is_zero() || x_bits.is_inf_or_nan()) continue; diff --git a/libc/test/src/math/smoke/modff16_test.cpp b/libc/test/src/math/smoke/modff16_test.cpp new file mode 100644 index 0000000000000..7093377182306 --- /dev/null +++ b/libc/test/src/math/smoke/modff16_test.cpp @@ -0,0 +1,13 @@ +//===-- Unittests for modff16 ---------------------------------------------===// +// +// 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 "ModfTest.h" + +#include "src/math/modff16.h" + +LIST_MODF_TESTS(float16, LIBC_NAMESPACE::modff16) From 141f1550165ecc2aa1ebfbb56e39371910b5e414 Mon Sep 17 00:00:00 2001 From: OverMighty Date: Fri, 7 Jun 2024 16:01:40 +0200 Subject: [PATCH 8/9] [libc][math] Refactor initialization of FPBits-typed variables --- libc/test/src/math/smoke/ILogbTest.h | 4 ++-- libc/test/src/math/smoke/LogbTest.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libc/test/src/math/smoke/ILogbTest.h b/libc/test/src/math/smoke/ILogbTest.h index f817b490b9861..3315ac2cbbc64 100644 --- a/libc/test/src/math/smoke/ILogbTest.h +++ b/libc/test/src/math/smoke/ILogbTest.h @@ -81,7 +81,7 @@ class LlvmLibcILogbTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { static_cast((MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT), StorageType(1)); for (StorageType v = MIN_SUBNORMAL; v <= MAX_SUBNORMAL; v += STEP) { - FPBits x_bits = FPBits(v); + FPBits x_bits(v); if (x_bits.is_zero() || x_bits.is_inf_or_nan()) continue; @@ -101,7 +101,7 @@ class LlvmLibcILogbTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { static_cast((MAX_NORMAL - MIN_NORMAL) / COUNT), StorageType(1)); for (StorageType v = MIN_NORMAL; v <= MAX_NORMAL; v += STEP) { - FPBits x_bits = FPBits(v); + FPBits x_bits(v); if (x_bits.is_zero() || x_bits.is_inf_or_nan()) continue; diff --git a/libc/test/src/math/smoke/LogbTest.h b/libc/test/src/math/smoke/LogbTest.h index 07865e3d6cc23..0bb6e12665b93 100644 --- a/libc/test/src/math/smoke/LogbTest.h +++ b/libc/test/src/math/smoke/LogbTest.h @@ -75,7 +75,7 @@ class LogbTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { static_cast(STORAGE_MAX / COUNT), StorageType(1)); StorageType v = 0; for (int i = 0; i <= COUNT; ++i, v += STEP) { - FPBits x_bits = FPBits(v); + FPBits x_bits(v); if (x_bits.is_zero() || x_bits.is_inf_or_nan()) continue; From e7adac7ad4a90d9c2dc04c19c9dfb0f7c4632641 Mon Sep 17 00:00:00 2001 From: OverMighty Date: Fri, 7 Jun 2024 21:54:14 +0200 Subject: [PATCH 9/9] Revert "[libc][math][c23] Add ldexpf16 C23 math function" This reverts commit 7bee51692f0a834f4831e296b1de82d48c0715fb. --- 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/ManipulationFunctions.h | 2 +- libc/src/__support/FPUtil/dyadic_float.h | 5 ----- libc/src/math/CMakeLists.txt | 1 - libc/src/math/generic/CMakeLists.txt | 13 ------------ libc/src/math/generic/ldexpf16.cpp | 19 ------------------ libc/src/math/ldexpf16.h | 20 ------------------- libc/test/src/math/smoke/CMakeLists.txt | 15 -------------- libc/test/src/math/smoke/LdExpTest.h | 16 +++++++-------- libc/test/src/math/smoke/ldexpf16_test.cpp | 13 ------------ 13 files changed, 10 insertions(+), 99 deletions(-) delete mode 100644 libc/src/math/generic/ldexpf16.cpp delete mode 100644 libc/src/math/ldexpf16.h delete mode 100644 libc/test/src/math/smoke/ldexpf16_test.cpp diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt index 93ba46b4d7ec5..50e0a6d1017e3 100644 --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -520,7 +520,6 @@ if(LIBC_TYPES_HAS_FLOAT16) libc.src.math.fromfpf16 libc.src.math.fromfpxf16 libc.src.math.ilogbf16 - libc.src.math.ldexpf16 libc.src.math.llogbf16 libc.src.math.llrintf16 libc.src.math.llroundf16 diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index 7137bf82ef63c..31ad0bc412836 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -553,7 +553,6 @@ if(LIBC_TYPES_HAS_FLOAT16) libc.src.math.fromfpf16 libc.src.math.fromfpxf16 libc.src.math.ilogbf16 - libc.src.math.ldexpf16 libc.src.math.llogbf16 libc.src.math.llrintf16 libc.src.math.llroundf16 diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst index 8e74969ce4045..3e122fb8bc26e 100644 --- a/libc/docs/math/index.rst +++ b/libc/docs/math/index.rst @@ -170,7 +170,7 @@ Basic Operations +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | ilogb | |check| | |check| | |check| | |check| | |check| | 7.12.6.8 | F.10.3.8 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ -| ldexp | |check| | |check| | |check| | |check| | |check| | 7.12.6.9 | F.10.3.9 | +| ldexp | |check| | |check| | |check| | | |check| | 7.12.6.9 | F.10.3.9 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | llogb | |check| | |check| | |check| | |check| | |check| | 7.12.6.10 | F.10.3.10 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td index 8051e05e99168..b5b6dbc481bd7 100644 --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -532,7 +532,6 @@ def StdC : StandardSpec<"stdc"> { FunctionSpec<"ldexp", RetValSpec, [ArgSpec, ArgSpec]>, FunctionSpec<"ldexpf", RetValSpec, [ArgSpec, ArgSpec]>, FunctionSpec<"ldexpl", RetValSpec, [ArgSpec, ArgSpec]>, - GuardedFunctionSpec<"ldexpf16", RetValSpec, [ArgSpec, ArgSpec], "LIBC_TYPES_HAS_FLOAT16">, GuardedFunctionSpec<"ldexpf128", RetValSpec, [ArgSpec, ArgSpec], "LIBC_TYPES_HAS_FLOAT128">, FunctionSpec<"log10", RetValSpec, [ArgSpec]>, diff --git a/libc/src/__support/FPUtil/ManipulationFunctions.h b/libc/src/__support/FPUtil/ManipulationFunctions.h index 09cfff31d64bb..f695b83fb0b0b 100644 --- a/libc/src/__support/FPUtil/ManipulationFunctions.h +++ b/libc/src/__support/FPUtil/ManipulationFunctions.h @@ -186,7 +186,7 @@ LIBC_INLINE constexpr T ldexp(T x, int exp) { } // For all other values, NormalFloat to T conversion handles it the right way. - DyadicFloat::STORAGE_LEN, 32)> normal(bits.get_val()); + DyadicFloat::STORAGE_LEN> normal(bits.get_val()); normal.exponent += exp; return static_cast(normal); } diff --git a/libc/src/__support/FPUtil/dyadic_float.h b/libc/src/__support/FPUtil/dyadic_float.h index f1791f9da56ad..12a69228d36c7 100644 --- a/libc/src/__support/FPUtil/dyadic_float.h +++ b/libc/src/__support/FPUtil/dyadic_float.h @@ -14,7 +14,6 @@ #include "src/__support/CPP/type_traits.h" #include "src/__support/big_int.h" #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY -#include "src/__support/macros/properties/types.h" // float16 #include @@ -184,10 +183,6 @@ template struct DyadicFloat { return r; } - LIBC_INLINE explicit constexpr operator float16() const { - return static_cast(static_cast(*this)); - } - LIBC_INLINE explicit constexpr operator MantissaType() const { if (mantissa.is_zero()) return 0; diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt index c186650a8b3fb..f8582d8d42683 100644 --- a/libc/src/math/CMakeLists.txt +++ b/libc/src/math/CMakeLists.txt @@ -224,7 +224,6 @@ add_math_entrypoint_object(llogbf128) add_math_entrypoint_object(ldexp) add_math_entrypoint_object(ldexpf) add_math_entrypoint_object(ldexpl) -add_math_entrypoint_object(ldexpf16) add_math_entrypoint_object(ldexpf128) add_math_entrypoint_object(log10) diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index 908ca66ac7a02..caaa0ac23dc7a 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -1487,19 +1487,6 @@ add_entrypoint_object( libc.src.__support.FPUtil.manipulation_functions ) -add_entrypoint_object( - ldexpf16 - SRCS - ldexpf16.cpp - HDRS - ../ldexpf16.h - COMPILE_OPTIONS - -O0 -ggdb3 - DEPENDS - libc.src.__support.macros.properties.types - libc.src.__support.FPUtil.manipulation_functions -) - add_entrypoint_object( ldexpf128 SRCS diff --git a/libc/src/math/generic/ldexpf16.cpp b/libc/src/math/generic/ldexpf16.cpp deleted file mode 100644 index ed15c4572edf5..0000000000000 --- a/libc/src/math/generic/ldexpf16.cpp +++ /dev/null @@ -1,19 +0,0 @@ -//===-- Implementation of ldexpf16 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/ldexpf16.h" -#include "src/__support/FPUtil/ManipulationFunctions.h" -#include "src/__support/common.h" - -namespace LIBC_NAMESPACE { - -LLVM_LIBC_FUNCTION(float16, ldexpf16, (float16 x, int exp)) { - return fputil::ldexp(x, exp); -} - -} // namespace LIBC_NAMESPACE diff --git a/libc/src/math/ldexpf16.h b/libc/src/math/ldexpf16.h deleted file mode 100644 index 7303610b18dff..0000000000000 --- a/libc/src/math/ldexpf16.h +++ /dev/null @@ -1,20 +0,0 @@ -//===-- Implementation header for ldexpf16 ----------------------*- 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_LDEXPF16_H -#define LLVM_LIBC_SRC_MATH_LDEXPF16_H - -#include "src/__support/macros/properties/types.h" - -namespace LIBC_NAMESPACE { - -float16 ldexpf16(float16 x, int exp); - -} // namespace LIBC_NAMESPACE - -#endif // LLVM_LIBC_SRC_MATH_LDEXPF16_H diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt index 23a4420ab4d32..84aa76c0a0881 100644 --- a/libc/test/src/math/smoke/CMakeLists.txt +++ b/libc/test/src/math/smoke/CMakeLists.txt @@ -1550,21 +1550,6 @@ add_fp_unittest( libc.src.__support.FPUtil.normal_float ) -add_fp_unittest( - ldexpf16_test - SUITE - libc-math-smoke-tests - SRCS - ldexpf16_test.cpp - HDRS - LdExpTest.h - DEPENDS - libc.src.math.ldexpf16 - libc.src.__support.CPP.limits - libc.src.__support.FPUtil.fp_bits - libc.src.__support.FPUtil.normal_float -) - add_fp_unittest( ldexpf128_test SUITE diff --git a/libc/test/src/math/smoke/LdExpTest.h b/libc/test/src/math/smoke/LdExpTest.h index 19c02d2dc7d0f..713d305c47494 100644 --- a/libc/test/src/math/smoke/LdExpTest.h +++ b/libc/test/src/math/smoke/LdExpTest.h @@ -31,7 +31,7 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest { const T nan = FPBits::quiet_nan().get_val(); // A normalized mantissa to be used with tests. - static constexpr StorageType MANTISSA = NormalFloat::ONE + 0x123; + static constexpr StorageType MANTISSA = NormalFloat::ONE + 0x1234; public: typedef T (*LdExpFunc)(T, int); @@ -60,7 +60,7 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest { void testOverflow(LdExpFunc func) { NormalFloat x(Sign::POS, FPBits::MAX_BIASED_EXPONENT - 10, - NormalFloat::ONE + 0xF00); + NormalFloat::ONE + 0xF00BA); for (int32_t exp = 10; exp < 100; ++exp) { ASSERT_FP_EQ(inf, func(T(x), exp)); ASSERT_FP_EQ(neg_inf, func(-T(x), exp)); @@ -95,10 +95,10 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest { void testNormalOperation(LdExpFunc func) { T val_array[] = {// Normal numbers - NormalFloat(Sign::POS, 10, MANTISSA), - NormalFloat(Sign::POS, -10, MANTISSA), - NormalFloat(Sign::NEG, 10, MANTISSA), - NormalFloat(Sign::NEG, -10, MANTISSA), + NormalFloat(Sign::POS, 100, MANTISSA), + NormalFloat(Sign::POS, -100, MANTISSA), + NormalFloat(Sign::NEG, 100, MANTISSA), + NormalFloat(Sign::NEG, -100, MANTISSA), // Subnormal numbers NormalFloat(Sign::POS, -FPBits::EXP_BIAS, MANTISSA), NormalFloat(Sign::NEG, -FPBits::EXP_BIAS, MANTISSA)}; @@ -114,8 +114,8 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest { NormalFloat two_to_exp = NormalFloat(static_cast(1.L)); two_to_exp = two_to_exp.mul2(exp); - ASSERT_FP_EQ(func(x, exp), x * static_cast(two_to_exp)); - ASSERT_FP_EQ(func(x, -exp), x / static_cast(two_to_exp)); + ASSERT_FP_EQ(func(x, exp), x * two_to_exp); + ASSERT_FP_EQ(func(x, -exp), x / two_to_exp); } } diff --git a/libc/test/src/math/smoke/ldexpf16_test.cpp b/libc/test/src/math/smoke/ldexpf16_test.cpp deleted file mode 100644 index ecf8f76a3aac8..0000000000000 --- a/libc/test/src/math/smoke/ldexpf16_test.cpp +++ /dev/null @@ -1,13 +0,0 @@ -//===-- Unittests for ldexpf16 --------------------------------------------===// -// -// 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 "LdExpTest.h" - -#include "src/math/ldexpf16.h" - -LIST_LDEXP_TESTS(float16, LIBC_NAMESPACE::ldexpf16);