-
Notifications
You must be signed in to change notification settings - Fork 11.8k
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 sqrtf16 C23 math function #112406
Merged
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
overmighty
changed the base branch from
main
to
users/overmighty/libc-math-log10f16
October 15, 2024 17:11
overmighty
changed the title
users/overmighty/libc math sqrtf16
[libc][math][c23] Add sqrtf16 C23 math function
Oct 15, 2024
Re-opening this as #106102 was merged when it shouldn't have been (it was merged into the previous stacked PR's branch). |
54 tasks
lntue
approved these changes
Oct 15, 2024
overmighty
force-pushed
the
users/overmighty/libc-math-log10f16
branch
from
October 17, 2024 18:53
4fffbcc
to
67699bf
Compare
overmighty
force-pushed
the
users/overmighty/libc-math-sqrtf16
branch
from
October 17, 2024 18:56
2a9fe1a
to
52c1c1b
Compare
@llvm/pr-subscribers-libc Author: OverMighty (overmighty) ChangesPart of #95250. Full diff: https://github.com/llvm/llvm-project/pull/112406.diff 14 Files Affected:
diff --git a/libc/config/gpu/entrypoints.txt b/libc/config/gpu/entrypoints.txt
index 13bb88894297ca..38e9f2e685caed 100644
--- a/libc/config/gpu/entrypoints.txt
+++ b/libc/config/gpu/entrypoints.txt
@@ -590,6 +590,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.setpayloadf16
libc.src.math.setpayloadsigf16
libc.src.math.sinhf16
+ libc.src.math.sqrtf16
libc.src.math.tanhf16
libc.src.math.totalorderf16
libc.src.math.totalordermagf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 885827d304efe3..71c6e874429fed 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -680,6 +680,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.setpayloadf16
libc.src.math.setpayloadsigf16
libc.src.math.sinpif16
+ libc.src.math.sqrtf16
libc.src.math.totalorderf16
libc.src.math.totalordermagf16
libc.src.math.truncf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 6ed6ad8c2400f3..9bc63edf06f28c 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -684,6 +684,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.setpayloadsigf16
libc.src.math.sinhf16
libc.src.math.sinpif16
+ libc.src.math.sqrtf16
libc.src.math.tanhf16
libc.src.math.totalorderf16
libc.src.math.totalordermagf16
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index 88751e2453f2c8..ce4df92393ce7f 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -344,7 +344,7 @@ Higher Math Functions
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| sinpi | |check| | | | |check| | | 7.12.4.13 | F.10.1.13 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| sqrt | |check| | |check| | |check| | | |check| | 7.12.7.10 | F.10.4.10 |
+| sqrt | |check| | |check| | |check| | |check| | |check| | 7.12.7.10 | F.10.4.10 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| tan | |check| | |check| | | | | 7.12.4.7 | F.10.1.7 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 33bec2f71627e5..d1ebc6ffb5821e 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -754,6 +754,7 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"sqrt", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"sqrtf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
FunctionSpec<"sqrtl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
+ GuardedFunctionSpec<"sqrtf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"sqrtf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
FunctionSpec<"trunc", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
diff --git a/libc/src/__support/FPUtil/generic/sqrt.h b/libc/src/__support/FPUtil/generic/sqrt.h
index 01af4bb7c90092..497ebd145c6b42 100644
--- a/libc/src/__support/FPUtil/generic/sqrt.h
+++ b/libc/src/__support/FPUtil/generic/sqrt.h
@@ -139,7 +139,8 @@ sqrt(InType x) {
for (InStorageType current_bit = ONE >> 1; current_bit;
current_bit >>= 1) {
r <<= 1;
- InStorageType tmp = (y << 1) + current_bit; // 2*y(n - 1) + 2^(-n-1)
+ // 2*y(n - 1) + 2^(-n-1)
+ InStorageType tmp = static_cast<InStorageType>((y << 1) + current_bit);
if (r >= tmp) {
r -= tmp;
y += current_bit;
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 9239f029abf87a..cb4817348cbba5 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -492,6 +492,7 @@ add_math_entrypoint_object(sinhf16)
add_math_entrypoint_object(sqrt)
add_math_entrypoint_object(sqrtf)
add_math_entrypoint_object(sqrtl)
+add_math_entrypoint_object(sqrtf16)
add_math_entrypoint_object(sqrtf128)
add_math_entrypoint_object(tan)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 00cad6f85b4b98..35e7347b91362e 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3249,6 +3249,18 @@ add_entrypoint_object(
-O3
)
+add_entrypoint_object(
+ sqrtf16
+ SRCS
+ sqrtf16.cpp
+ HDRS
+ ../sqrtf16.h
+ DEPENDS
+ libc.src.__support.FPUtil.sqrt
+ COMPILE_OPTIONS
+ -O3
+)
+
add_entrypoint_object(
sqrtf128
SRCS
diff --git a/libc/src/math/generic/sqrtf16.cpp b/libc/src/math/generic/sqrtf16.cpp
new file mode 100644
index 00000000000000..0aa4a201b3e68c
--- /dev/null
+++ b/libc/src/math/generic/sqrtf16.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of sqrtf16 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/sqrtf16.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float16, sqrtf16, (float16 x)) {
+ return fputil::sqrt<float16>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/sqrtf16.h b/libc/src/math/sqrtf16.h
new file mode 100644
index 00000000000000..bb09c4fdaf8d00
--- /dev/null
+++ b/libc/src/math/sqrtf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for sqrtf16 -----------------------*- 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_SQRTF16_H
+#define LLVM_LIBC_SRC_MATH_SQRTF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float16 sqrtf16(float16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_SQRTF16_H
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index befa8cff0720cf..262c717dd27d55 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -1490,6 +1490,17 @@ add_fp_unittest(
libc.src.math.sqrtl
)
+add_fp_unittest(
+ sqrtf16_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ sqrtf16_test.cpp
+ DEPENDS
+ libc.src.math.sqrtf16
+)
+
add_fp_unittest(
generic_sqrtf_test
NEED_MPFR
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index d41041c9bb0a4a..b2d1871541efc9 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -2889,6 +2889,18 @@ add_fp_unittest(
libc.src.math.sqrtl
)
+add_fp_unittest(
+ sqrtf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ sqrtf16_test.cpp
+ HDRS
+ SqrtTest.h
+ DEPENDS
+ libc.src.math.sqrtf16
+)
+
add_fp_unittest(
sqrtf128_test
SUITE
diff --git a/libc/test/src/math/smoke/sqrtf16_test.cpp b/libc/test/src/math/smoke/sqrtf16_test.cpp
new file mode 100644
index 00000000000000..d62049661eecbf
--- /dev/null
+++ b/libc/test/src/math/smoke/sqrtf16_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for sqrtf16 ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "SqrtTest.h"
+
+#include "src/math/sqrtf16.h"
+
+LIST_SQRT_TESTS(float16, LIBC_NAMESPACE::sqrtf16)
diff --git a/libc/test/src/math/sqrtf16_test.cpp b/libc/test/src/math/sqrtf16_test.cpp
new file mode 100644
index 00000000000000..f6e8996761245d
--- /dev/null
+++ b/libc/test/src/math/sqrtf16_test.cpp
@@ -0,0 +1,28 @@
+//===-- Exhaustive test for sqrtf16 ---------------------------------------===//
+//
+// 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/sqrtf16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcSqrtf16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+// Range: [0, Inf];
+static constexpr uint16_t POS_START = 0x0000U;
+static constexpr uint16_t POS_STOP = 0x7c00U;
+
+TEST_F(LlvmLibcSqrtf16Test, PositiveRange) {
+ for (uint16_t v = POS_START; v <= POS_STOP; ++v) {
+ float16 x = FPBits(v).get_val();
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sqrt, x,
+ LIBC_NAMESPACE::sqrtf16(x), 0.5);
+ }
+}
|
lntue
approved these changes
Oct 17, 2024
lntue
approved these changes
Oct 18, 2024
overmighty
force-pushed
the
users/overmighty/libc-math-log10f16
branch
from
October 18, 2024 20:47
67699bf
to
b1c21c8
Compare
overmighty
force-pushed
the
users/overmighty/libc-math-sqrtf16
branch
from
October 18, 2024 20:49
52c1c1b
to
38a132a
Compare
overmighty
force-pushed
the
users/overmighty/libc-math-log10f16
branch
from
October 18, 2024 23:35
b1c21c8
to
bbc612b
Compare
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.
Part of #95250.