-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[libc][math][c23] Add {frexp,ilogb,llogb,logb,modf}f16 C23 math functions #94758
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
cc @lntue |
@llvm/pr-subscribers-libc Author: OverMighty (overmighty) ChangesPatch is 49.19 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/94758.diff 36 Files Affected:
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 33ecff813a1fb..9e17ee65616dd 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -515,12 +515,18 @@ 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.ldexpf16
+ 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.nearbyintf16
libc.src.math.nextafterf16
libc.src.math.nextdownf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index e3ca544ae0185..befbdf7ff4e3b 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -547,12 +547,18 @@ 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.ldexpf16
+ 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.nearbyintf16
libc.src.math.nextafterf16
libc.src.math.nextdownf16
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 b9507f0887cd7..c26a9699f2da5 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -160,7 +160,7 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| fmul | N/A | | | 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 |
+| 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 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| 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| | 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<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 9a436c8ae38d2..cbbcfb902e494 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -483,6 +483,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>]>,
@@ -515,16 +516,19 @@ 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>]>,
FunctionSpec<"ldexpf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<IntType>]>,
FunctionSpec<"ldexpl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntType>]>,
+ GuardedFunctionSpec<"ldexpf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>, ArgSpec<IntType>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"ldexpf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<IntType>], "LIBC_TYPES_HAS_FLOAT128">,
FunctionSpec<"log10", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
@@ -542,11 +546,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 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..09cfff31d64bb 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());
}
@@ -185,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<FPBits<T>::STORAGE_LEN> normal(bits.get_val());
+ DyadicFloat<cpp::max(FPBits<T>::STORAGE_LEN, 32)> normal(bits.get_val());
normal.exponent += exp;
return static_cast<T>(normal);
}
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 <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 @@ template <typename T> 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<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/__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 <stddef.h>
@@ -183,6 +184,10 @@ template <size_t Bits> struct DyadicFloat {
return r;
}
+ LIBC_INLINE explicit constexpr operator float16() const {
+ return static_cast<float16>(static_cast<float>(*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 7a349ddc53724..b6bbe815b0c90 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -188,6 +188,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)
@@ -208,16 +209,19 @@ 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)
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)
@@ -235,6 +239,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)
@@ -264,6 +269,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 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...
[truncated]
|
@@ -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()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DyadicFloat<Bits>
uses UInt<Bits>
which requires Bits
to be either 32 or a multiple of the size of WordType
which is never uint16_t
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe update DyadicFloat
to accept Bits
to also accept 16?
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was unused. I did similar cleanups in test templates that I edited in previous PRs, but I don't think I ever left a comment explaining it.
constexpr int COUNT = 10'001; | ||
constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max( | ||
static_cast<StorageType>((MAX_NORMAL - MIN_NORMAL) / COUNT), | ||
StorageType(1)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, COUNT
was small enough and MAX_NORMAL - MIN_NORMAL
large enough for STEP
to be > 0 for float16
/uint16_t
, but I thought it wouldn't hurt to also do the max(..., 1)
change here anyway.
This reverts commit 7bee516.
c757364
to
e7adac7
Compare
Rebased and resolved merge conflitcts. |
Wait, I haven't pushed a change to address this yet:
|
A separate PR is better. |
Part of #93566.