diff options
author | OverMighty <its.overmighty@gmail.com> | 2024-06-10 14:38:47 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-10 08:38:47 -0400 |
commit | 10cd96dd3315ca466a56a2ace2627e44bdcba333 (patch) | |
tree | cc144b8402eecfa3f8a9932a8d0ab0f38792b309 /libc | |
parent | a1c29df5725fc043291258a3af732a72a39b6902 (diff) | |
download | llvm-10cd96dd3315ca466a56a2ace2627e44bdcba333.zip llvm-10cd96dd3315ca466a56a2ace2627e44bdcba333.tar.gz llvm-10cd96dd3315ca466a56a2ace2627e44bdcba333.tar.bz2 |
[libc][math][c23] Add {frexp,ilogb,llogb,logb,modf}f16 C23 math functions (#94758)
Part of #93566.
Diffstat (limited to 'libc')
31 files changed, 480 insertions, 36 deletions
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt index ee86822..50e0a6d 100644 --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -516,12 +516,17 @@ 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.ilogbf16 + 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.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 c4b3b1a..31ad0bc 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -549,12 +549,17 @@ 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.ilogbf16 + 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.modff16 libc.src.math.nanf16 libc.src.math.nearbyintf16 libc.src.math.nextafterf16 diff --git a/libc/docs/c23.rst b/libc/docs/c23.rst index 71232cc..fec9b24 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 43f63e91..3e122fb 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 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ @@ -168,23 +168,23 @@ 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 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ -| 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 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | 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 | +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | 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 056a314..7e1283e 100644 --- a/libc/spec/spec.td +++ b/libc/spec/spec.td @@ -115,6 +115,7 @@ def IntPtr : PtrType<IntType>; def RestrictedIntPtr : RestrictedPtrType<IntType>; def FloatPtr : PtrType<FloatType>; def DoublePtr : PtrType<DoubleType>; +def Float16Ptr : PtrType<Float16Type>; def Float128Ptr : PtrType<Float128Type>; def UnsignedCharPtr : PtrType<UnsignedCharType>; diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td index ac16041..b5b6dbc 100644 --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -487,6 +487,7 @@ def StdC : StandardSpec<"stdc"> { FunctionSpec<"frexp", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntPtr>]>, FunctionSpec<"frexpf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<IntPtr>]>, FunctionSpec<"frexpl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntPtr>]>, + GuardedFunctionSpec<"frexpf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>, ArgSpec<IntPtr>], "LIBC_TYPES_HAS_FLOAT16">, GuardedFunctionSpec<"frexpf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<IntPtr>], "LIBC_TYPES_HAS_FLOAT128">, FunctionSpec<"fromfp", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>, @@ -519,11 +520,13 @@ def StdC : StandardSpec<"stdc"> { FunctionSpec<"ilogb", RetValSpec<IntType>, [ArgSpec<DoubleType>]>, FunctionSpec<"ilogbf", RetValSpec<IntType>, [ArgSpec<FloatType>]>, FunctionSpec<"ilogbl", RetValSpec<IntType>, [ArgSpec<LongDoubleType>]>, + GuardedFunctionSpec<"ilogbf16", RetValSpec<IntType>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">, GuardedFunctionSpec<"ilogbf128", RetValSpec<IntType>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">, FunctionSpec<"llogb", RetValSpec<LongType>, [ArgSpec<DoubleType>]>, FunctionSpec<"llogbf", RetValSpec<LongType>, [ArgSpec<FloatType>]>, FunctionSpec<"llogbl", RetValSpec<LongType>, [ArgSpec<LongDoubleType>]>, + GuardedFunctionSpec<"llogbf16", RetValSpec<LongType>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">, GuardedFunctionSpec<"llogbf128", RetValSpec<LongType>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">, FunctionSpec<"ldexp", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntType>]>, @@ -546,11 +549,13 @@ def StdC : StandardSpec<"stdc"> { FunctionSpec<"logb", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>, FunctionSpec<"logbf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>, FunctionSpec<"logbl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>, + GuardedFunctionSpec<"logbf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">, GuardedFunctionSpec<"logbf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">, FunctionSpec<"modf", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoublePtr>]>, FunctionSpec<"modff", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatPtr>]>, FunctionSpec<"modfl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoublePtr>]>, + GuardedFunctionSpec<"modff16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>, ArgSpec<Float16Ptr>], "LIBC_TYPES_HAS_FLOAT16">, GuardedFunctionSpec<"modff128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Ptr>], "LIBC_TYPES_HAS_FLOAT128">, FunctionSpec<"cos", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>, diff --git a/libc/src/__support/FPUtil/CMakeLists.txt b/libc/src/__support/FPUtil/CMakeLists.txt index 01ca425..1744f8c 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 a289c2e..f695b83 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>::T_MAX; } - DyadicFloat<FPBits<U>::STORAGE_LEN> normal(bits.get_val()); + DyadicFloat<cpp::max(FPBits<U>::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 @@ -138,7 +139,7 @@ LIBC_INLINE constexpr T logb(T x) { return FPBits<T>::inf().get_val(); } - DyadicFloat<FPBits<T>::STORAGE_LEN> normal(bits.get_val()); + DyadicFloat<cpp::max(FPBits<T>::STORAGE_LEN, 32)> normal(bits.get_val()); return static_cast<T>(normal.get_unbiased_exponent()); } diff --git a/libc/src/__support/FPUtil/NormalFloat.h b/libc/src/__support/FPUtil/NormalFloat.h index 8bc1fec..33529d5 100644 --- a/libc/src/__support/FPUtil/NormalFloat.h +++ b/libc/src/__support/FPUtil/NormalFloat.h @@ -110,9 +110,11 @@ template <typename T> struct NormalFloat { if (shift <= FPBits<T>::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>(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>(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 <typename T> struct NormalFloat { } } - result.set_biased_exponent(exponent + FPBits<T>::EXP_BIAS); + result.set_biased_exponent( + static_cast<StorageType>(exponent + FPBits<T>::EXP_BIAS)); result.set_mantissa(mantissa); return result.get_val(); } @@ -155,7 +158,7 @@ private: // Normalize subnormal numbers. if (bits.is_subnormal()) { unsigned shift = evaluate_normalization_shift(bits.get_mantissa()); - mantissa = StorageType(bits.get_mantissa()) << shift; + mantissa = static_cast<StorageType>(bits.get_mantissa() << shift); exponent = 1 - FPBits<T>::EXP_BIAS - shift; } else { exponent = bits.get_biased_exponent() - FPBits<T>::EXP_BIAS; diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt index 68b8924..f8582d8 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) @@ -211,11 +212,13 @@ 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) 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) @@ -238,6 +241,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) @@ -267,6 +271,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/frexpf16.h b/libc/src/math/frexpf16.h new file mode 100644 index 0000000..dc1898c --- /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 4f94468..caaa0ac 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -1302,6 +1302,19 @@ add_entrypoint_object( ) 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 frexpf128.cpp @@ -1351,6 +1364,19 @@ add_entrypoint_object( ) 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 ilogbf128.cpp @@ -1400,6 +1426,19 @@ add_entrypoint_object( ) 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 llogbf128.cpp @@ -1673,6 +1712,19 @@ add_entrypoint_object( ) 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 logbf128.cpp @@ -1681,6 +1733,7 @@ add_entrypoint_object( COMPILE_OPTIONS -O3 DEPENDS + libc.src.__support.macros.properties.types libc.src.__support.FPUtil.manipulation_functions ) @@ -1721,6 +1774,19 @@ add_entrypoint_object( ) 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 modff128.cpp diff --git a/libc/src/math/generic/frexpf16.cpp b/libc/src/math/generic/frexpf16.cpp new file mode 100644 index 0000000..2d29c07 --- /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/src/math/generic/ilogbf16.cpp b/libc/src/math/generic/ilogbf16.cpp new file mode 100644 index 0000000..87e43f8 --- /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<int>(x); +} + +} // namespace LIBC_NAMESPACE diff --git a/libc/src/math/generic/llogbf16.cpp b/libc/src/math/generic/llogbf16.cpp new file mode 100644 index 0000000..b7a21b9 --- /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<long>(x); +} + +} // namespace LIBC_NAMESPACE diff --git a/libc/src/math/generic/logbf16.cpp b/libc/src/math/generic/logbf16.cpp new file mode 100644 index 0000000..52eb9ac --- /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/generic/modff16.cpp b/libc/src/math/generic/modff16.cpp new file mode 100644 index 0000000..50cc5b5 --- /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/ilogbf16.h b/libc/src/math/ilogbf16.h new file mode 100644 index 0000000..4884a14 --- /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/src/math/llogbf16.h b/libc/src/math/llogbf16.h new file mode 100644 index 0000000..267ae41 --- /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/src/math/logbf16.h b/libc/src/math/logbf16.h new file mode 100644 index 0000000..8082e06 --- /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/src/math/modff16.h b/libc/src/math/modff16.h new file mode 100644 index 0000000..a3017c5 --- /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 cc0e00a..84aa76c 100644 --- a/libc/test/src/math/smoke/CMakeLists.txt +++ b/libc/test/src/math/smoke/CMakeLists.txt @@ -1092,6 +1092,18 @@ add_fp_unittest( ) 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 libc-math-smoke-tests @@ -1353,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 ) @@ -1368,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 ) @@ -1383,7 +1395,22 @@ 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 +) + +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.algorithm libc.src.__support.FPUtil.fp_bits libc.src.__support.FPUtil.manipulation_functions ) @@ -1398,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 ) @@ -1413,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 ) @@ -1428,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 ) @@ -1443,7 +1470,22 @@ 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 +) + +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.algorithm libc.src.__support.FPUtil.fp_bits libc.src.__support.FPUtil.manipulation_functions ) @@ -1458,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 ) @@ -1529,8 +1571,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 ) @@ -1540,8 +1585,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 ) @@ -1555,6 +1603,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 ) @@ -1564,8 +1627,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 ) @@ -1579,6 +1645,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 ) @@ -1593,6 +1660,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 ) @@ -1607,6 +1675,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 ) @@ -1621,6 +1705,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/FrexpTest.h b/libc/test/src/math/smoke/FrexpTest.h index e9e4964..fc2313a 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/ILogbTest.h b/libc/test/src/math/smoke/ILogbTest.h index 05f906b..3315ac2 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" @@ -76,10 +76,12 @@ public: 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<StorageType>((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; @@ -94,10 +96,12 @@ public: 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<StorageType>((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 4938fcf..0bb6e12 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,10 +70,12 @@ public: 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) { - FPBits x_bits = FPBits(v); + constexpr int COUNT = 100'000; + constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max( + static_cast<StorageType>(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/ModfTest.h b/libc/test/src/math/smoke/ModfTest.h index 85db2d6..6226e5d 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 @@ public: } 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<StorageType>(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/frexpf16_test.cpp b/libc/test/src/math/smoke/frexpf16_test.cpp new file mode 100644 index 0000000..4d5492c --- /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); 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 0000000..e046709 --- /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); 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 0000000..8907681 --- /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); 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 0000000..cfc1a05 --- /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) 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 0000000..7093377 --- /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) |