-
Notifications
You must be signed in to change notification settings - Fork 12.3k
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
[libc][math][c23] Add {nearbyint,rint,lrint,llrint,lround,llround}f16 C23 math functions #94218
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-libc Author: OverMighty (overmighty) Changescc @lntue Patch is 39.10 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/94218.diff 27 Files Affected:
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index ca0418c3618ae..0541c1c7d335e 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -500,6 +500,12 @@ if(LIBC_TYPES_HAS_FLOAT16)
list(APPEND TARGET_LIBM_ENTRYPOINTS
# math.h C23 _Float16 entrypoints
libc.src.math.fabsf16
+ libc.src.math.llrintf16
+ libc.src.math.llroundf16
+ libc.src.math.lrintf16
+ libc.src.math.lroundf16
+ libc.src.math.nearbyintf16
+ libc.src.math.rintf16
)
endif()
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 367db7d384d23..c57847c9f2539 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -533,6 +533,12 @@ if(LIBC_TYPES_HAS_FLOAT16)
list(APPEND TARGET_LIBM_ENTRYPOINTS
# math.h C23 _Float16 entrypoints
libc.src.math.fabsf16
+ libc.src.math.llrintf16
+ libc.src.math.llroundf16
+ libc.src.math.lrintf16
+ libc.src.math.lroundf16
+ libc.src.math.nearbyintf16
+ libc.src.math.rintf16
)
endif()
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index cd90b6ae85769..54c3e83a58c96 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -174,21 +174,21 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| llogb | |check| | |check| | |check| | | |check| | 7.12.6.10 | F.10.3.10 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| llrint | |check| | |check| | |check| | | |check| | 7.12.9.5 | F.10.6.5 |
+| llrint | |check| | |check| | |check| | |check| | |check| | 7.12.9.5 | F.10.6.5 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| llround | |check| | |check| | |check| | | |check| | 7.12.9.7 | F.10.6.7 |
+| 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 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| lrint | |check| | |check| | |check| | | |check| | 7.12.9.5 | F.10.6.5 |
+| lrint | |check| | |check| | |check| | |check| | |check| | 7.12.9.5 | F.10.6.5 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| lround | |check| | |check| | |check| | | |check| | 7.12.9.7 | F.10.6.7 |
+| 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 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| nan | |check| | |check| | |check| | | |check| | 7.12.11.2 | F.10.8.2 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| nearbyint | |check| | |check| | |check| | | |check| | 7.12.9.3 | F.10.6.3 |
+| nearbyint | |check| | |check| | |check| | |check| | |check| | 7.12.9.3 | F.10.6.3 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| nextafter | |check| | |check| | |check| | | |check| | 7.12.11.3 | F.10.8.3 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
@@ -202,7 +202,7 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| remquo | |check| | |check| | |check| | | | 7.12.10.3 | F.10.7.3 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| rint | |check| | |check| | |check| | | |check| | 7.12.9.4 | F.10.6.4 |
+| rint | |check| | |check| | |check| | |check| | |check| | 7.12.9.4 | F.10.6.4 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| round | |check| | |check| | |check| | | |check| | 7.12.9.6 | F.10.6.6 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 109721b8b12a0..9ac1d4eb15383 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -570,26 +570,31 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"lround", RetValSpec<LongType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"lroundf", RetValSpec<LongType>, [ArgSpec<FloatType>]>,
FunctionSpec<"lroundl", RetValSpec<LongType>, [ArgSpec<LongDoubleType>]>,
+ GuardedFunctionSpec<"lroundf16", RetValSpec<LongType>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"lroundf128", RetValSpec<LongType>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
FunctionSpec<"llround", RetValSpec<LongLongType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"llroundf", RetValSpec<LongLongType>, [ArgSpec<FloatType>]>,
FunctionSpec<"llroundl", RetValSpec<LongLongType>, [ArgSpec<LongDoubleType>]>,
+ GuardedFunctionSpec<"llroundf16", RetValSpec<LongLongType>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"llroundf128", RetValSpec<LongLongType>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
FunctionSpec<"rint", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"rintf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
FunctionSpec<"rintl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
+ GuardedFunctionSpec<"rintf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"rintf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
FunctionSpec<"lrint", RetValSpec<LongType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"lrintf", RetValSpec<LongType>, [ArgSpec<FloatType>]>,
FunctionSpec<"lrintl", RetValSpec<LongType>, [ArgSpec<LongDoubleType>]>,
+ GuardedFunctionSpec<"lrintf16", RetValSpec<LongType>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"lrintf128", RetValSpec<LongType>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
FunctionSpec<"llrint", RetValSpec<LongLongType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"llrintf", RetValSpec<LongLongType>, [ArgSpec<FloatType>]>,
FunctionSpec<"llrintl", RetValSpec<LongLongType>, [ArgSpec<LongDoubleType>]>,
+ GuardedFunctionSpec<"llrintf16", RetValSpec<LongLongType>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"llrintf128", RetValSpec<LongLongType>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
FunctionSpec<"sqrt", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
@@ -605,6 +610,7 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"nearbyint", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"nearbyintf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
FunctionSpec<"nearbyintl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
+ GuardedFunctionSpec<"nearbyintf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"nearbyintf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
FunctionSpec<"nextafterf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
diff --git a/libc/src/__support/FPUtil/NearestIntegerOperations.h b/libc/src/__support/FPUtil/NearestIntegerOperations.h
index 4645ab0b5350b..5ec3c3bf325de 100644
--- a/libc/src/__support/FPUtil/NearestIntegerOperations.h
+++ b/libc/src/__support/FPUtil/NearestIntegerOperations.h
@@ -181,7 +181,9 @@ round_using_specific_rounding_mode(T x, int rnd) {
uint32_t trim_size = FPBits<T>::FRACTION_LEN - exponent;
FPBits<T> new_bits = bits;
- new_bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
+ StorageType trunc_mantissa =
+ static_cast<StorageType>((bits.get_mantissa() >> trim_size) << trim_size);
+ new_bits.set_mantissa(trunc_mantissa);
T trunc_value = new_bits.get_val();
// If x is already an integer, return it.
@@ -190,7 +192,8 @@ round_using_specific_rounding_mode(T x, int rnd) {
StorageType trim_value =
bits.get_mantissa() & ((StorageType(1) << trim_size) - 1);
- StorageType half_value = (StorageType(1) << (trim_size - 1));
+ StorageType half_value =
+ static_cast<StorageType>((StorageType(1) << (trim_size - 1)));
// If exponent is 0, trimSize will be equal to the mantissa width, and
// truncIsOdd` will not be correct. So, we handle it as a special case
// below.
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 31df5d0ab8809..50bea4e58d710 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -223,21 +223,25 @@ add_math_entrypoint_object(logbf128)
add_math_entrypoint_object(llrint)
add_math_entrypoint_object(llrintf)
add_math_entrypoint_object(llrintl)
+add_math_entrypoint_object(llrintf16)
add_math_entrypoint_object(llrintf128)
add_math_entrypoint_object(llround)
add_math_entrypoint_object(llroundf)
add_math_entrypoint_object(llroundl)
+add_math_entrypoint_object(llroundf16)
add_math_entrypoint_object(llroundf128)
add_math_entrypoint_object(lrint)
add_math_entrypoint_object(lrintf)
add_math_entrypoint_object(lrintl)
+add_math_entrypoint_object(lrintf16)
add_math_entrypoint_object(lrintf128)
add_math_entrypoint_object(lround)
add_math_entrypoint_object(lroundf)
add_math_entrypoint_object(lroundl)
+add_math_entrypoint_object(lroundf16)
add_math_entrypoint_object(lroundf128)
add_math_entrypoint_object(modf)
@@ -253,6 +257,7 @@ add_math_entrypoint_object(nanf128)
add_math_entrypoint_object(nearbyint)
add_math_entrypoint_object(nearbyintf)
add_math_entrypoint_object(nearbyintl)
+add_math_entrypoint_object(nearbyintf16)
add_math_entrypoint_object(nearbyintf128)
add_math_entrypoint_object(nextafter)
@@ -288,6 +293,7 @@ add_math_entrypoint_object(remquol)
add_math_entrypoint_object(rint)
add_math_entrypoint_object(rintf)
add_math_entrypoint_object(rintl)
+add_math_entrypoint_object(rintf16)
add_math_entrypoint_object(rintf128)
add_math_entrypoint_object(round)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 04656e3186181..ab13c2554db0f 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -499,6 +499,19 @@ add_entrypoint_object(
libc.src.__support.FPUtil.nearest_integer_operations
)
+add_entrypoint_object(
+ lroundf16
+ SRCS
+ lroundf16.cpp
+ HDRS
+ ../lroundf16.h
+ COMPILE_OPTIONS
+ -O3
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.nearest_integer_operations
+)
+
add_entrypoint_object(
lroundf128
SRCS
@@ -548,6 +561,19 @@ add_entrypoint_object(
libc.src.__support.FPUtil.nearest_integer_operations
)
+add_entrypoint_object(
+ llroundf16
+ SRCS
+ llroundf16.cpp
+ HDRS
+ ../llroundf16.h
+ COMPILE_OPTIONS
+ -O3
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.nearest_integer_operations
+)
+
add_entrypoint_object(
llroundf128
SRCS
@@ -597,6 +623,19 @@ add_entrypoint_object(
libc.src.__support.FPUtil.nearest_integer_operations
)
+add_entrypoint_object(
+ rintf16
+ SRCS
+ rintf16.cpp
+ HDRS
+ ../rintf16.h
+ COMPILE_OPTIONS
+ -O3
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.nearest_integer_operations
+)
+
add_entrypoint_object(
rintf128
SRCS
@@ -646,6 +685,19 @@ add_entrypoint_object(
libc.src.__support.FPUtil.nearest_integer_operations
)
+add_entrypoint_object(
+ lrintf16
+ SRCS
+ lrintf16.cpp
+ HDRS
+ ../lrintf16.h
+ COMPILE_OPTIONS
+ -O3
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.nearest_integer_operations
+)
+
add_entrypoint_object(
lrintf128
SRCS
@@ -695,6 +747,19 @@ add_entrypoint_object(
libc.src.__support.FPUtil.nearest_integer_operations
)
+add_entrypoint_object(
+ llrintf16
+ SRCS
+ llrintf16.cpp
+ HDRS
+ ../llrintf16.h
+ COMPILE_OPTIONS
+ -O3
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.nearest_integer_operations
+)
+
add_entrypoint_object(
llrintf128
SRCS
@@ -744,6 +809,19 @@ add_entrypoint_object(
-O3
)
+add_entrypoint_object(
+ nearbyintf16
+ SRCS
+ nearbyintf16.cpp
+ HDRS
+ ../nearbyintf16.h
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.nearest_integer_operations
+ COMPILE_OPTIONS
+ -O3
+)
+
add_entrypoint_object(
nearbyintf128
SRCS
diff --git a/libc/src/math/generic/llrintf16.cpp b/libc/src/math/generic/llrintf16.cpp
new file mode 100644
index 0000000000000..ccde2db1cbf8f
--- /dev/null
+++ b/libc/src/math/generic/llrintf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of llrintf16 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/llrintf16.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(long long, llrintf16, (float16 x)) {
+ return fputil::round_to_signed_integer_using_current_rounding_mode<float128,
+ long long>(
+ x);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/generic/llroundf16.cpp b/libc/src/math/generic/llroundf16.cpp
new file mode 100644
index 0000000000000..e12421aa1f6fa
--- /dev/null
+++ b/libc/src/math/generic/llroundf16.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of llroundf16 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/llroundf16.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(long long, llroundf16, (float16 x)) {
+ return fputil::round_to_signed_integer<float128, long long>(x);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/generic/lrintf16.cpp b/libc/src/math/generic/lrintf16.cpp
new file mode 100644
index 0000000000000..d49be281a686b
--- /dev/null
+++ b/libc/src/math/generic/lrintf16.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of lrintf16 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/lrintf16.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(long, lrintf16, (float16 x)) {
+ return fputil::round_to_signed_integer_using_current_rounding_mode<float16,
+ long>(x);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/generic/lroundf16.cpp b/libc/src/math/generic/lroundf16.cpp
new file mode 100644
index 0000000000000..c854168d80786
--- /dev/null
+++ b/libc/src/math/generic/lroundf16.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of lroundf16 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/lroundf16.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(long, lroundf16, (float16 x)) {
+ return fputil::round_to_signed_integer<float128, long>(x);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/generic/nearbyintf16.cpp b/libc/src/math/generic/nearbyintf16....
[truncated]
|
70 tasks
overmighty
force-pushed
the
libc-math-nearbyintf16
branch
from
June 3, 2024 21:57
fdd290c
to
c9c1068
Compare
Rebased and resolved the merge conflicts. |
lntue
reviewed
Jun 4, 2024
lntue
reviewed
Jun 4, 2024
lntue
reviewed
Jun 4, 2024
lntue
approved these changes
Jun 4, 2024
keith
added a commit
that referenced
this pull request
Jun 4, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
cc @lntue