Skip to content

Commit 38a132a

Browse files
committed
[libc][math][c23] Add sqrtf16 C23 math function
Part of #95250.
1 parent b1c21c8 commit 38a132a

File tree

14 files changed

+125
-2
lines changed

14 files changed

+125
-2
lines changed

libc/config/gpu/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
590590
libc.src.math.setpayloadf16
591591
libc.src.math.setpayloadsigf16
592592
libc.src.math.sinhf16
593+
libc.src.math.sqrtf16
593594
libc.src.math.tanhf16
594595
libc.src.math.totalorderf16
595596
libc.src.math.totalordermagf16

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
680680
libc.src.math.setpayloadf16
681681
libc.src.math.setpayloadsigf16
682682
libc.src.math.sinpif16
683+
libc.src.math.sqrtf16
683684
libc.src.math.totalorderf16
684685
libc.src.math.totalordermagf16
685686
libc.src.math.truncf16

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
684684
libc.src.math.setpayloadsigf16
685685
libc.src.math.sinhf16
686686
libc.src.math.sinpif16
687+
libc.src.math.sqrtf16
687688
libc.src.math.tanhf16
688689
libc.src.math.totalorderf16
689690
libc.src.math.totalordermagf16

libc/docs/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ Higher Math Functions
344344
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
345345
| sinpi | |check| | | | |check| | | 7.12.4.13 | F.10.1.13 |
346346
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
347-
| sqrt | |check| | |check| | |check| | | |check| | 7.12.7.10 | F.10.4.10 |
347+
| sqrt | |check| | |check| | |check| | |check| | |check| | 7.12.7.10 | F.10.4.10 |
348348
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
349349
| tan | |check| | |check| | | | | 7.12.4.7 | F.10.1.7 |
350350
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/spec/stdc.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,7 @@ def StdC : StandardSpec<"stdc"> {
754754
FunctionSpec<"sqrt", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
755755
FunctionSpec<"sqrtf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
756756
FunctionSpec<"sqrtl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
757+
GuardedFunctionSpec<"sqrtf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
757758
GuardedFunctionSpec<"sqrtf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
758759

759760
FunctionSpec<"trunc", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,

libc/src/__support/FPUtil/generic/sqrt.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ sqrt(InType x) {
139139
for (InStorageType current_bit = ONE >> 1; current_bit;
140140
current_bit >>= 1) {
141141
r <<= 1;
142-
InStorageType tmp = (y << 1) + current_bit; // 2*y(n - 1) + 2^(-n-1)
142+
// 2*y(n - 1) + 2^(-n-1)
143+
InStorageType tmp = static_cast<InStorageType>((y << 1) + current_bit);
143144
if (r >= tmp) {
144145
r -= tmp;
145146
y += current_bit;

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ add_math_entrypoint_object(sinhf16)
492492
add_math_entrypoint_object(sqrt)
493493
add_math_entrypoint_object(sqrtf)
494494
add_math_entrypoint_object(sqrtl)
495+
add_math_entrypoint_object(sqrtf16)
495496
add_math_entrypoint_object(sqrtf128)
496497

497498
add_math_entrypoint_object(tan)

libc/src/math/generic/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3249,6 +3249,18 @@ add_entrypoint_object(
32493249
-O3
32503250
)
32513251

3252+
add_entrypoint_object(
3253+
sqrtf16
3254+
SRCS
3255+
sqrtf16.cpp
3256+
HDRS
3257+
../sqrtf16.h
3258+
DEPENDS
3259+
libc.src.__support.FPUtil.sqrt
3260+
COMPILE_OPTIONS
3261+
-O3
3262+
)
3263+
32523264
add_entrypoint_object(
32533265
sqrtf128
32543266
SRCS

libc/src/math/generic/sqrtf16.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of sqrtf16 function --------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/math/sqrtf16.h"
10+
#include "src/__support/FPUtil/sqrt.h"
11+
#include "src/__support/common.h"
12+
#include "src/__support/macros/config.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
LLVM_LIBC_FUNCTION(float16, sqrtf16, (float16 x)) {
17+
return fputil::sqrt<float16>(x);
18+
}
19+
20+
} // namespace LIBC_NAMESPACE_DECL

libc/src/math/sqrtf16.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for sqrtf16 -----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_MATH_SQRTF16_H
10+
#define LLVM_LIBC_SRC_MATH_SQRTF16_H
11+
12+
#include "src/__support/macros/config.h"
13+
#include "src/__support/macros/properties/types.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
float16 sqrtf16(float16 x);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_MATH_SQRTF16_H

libc/test/src/math/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,6 +1490,17 @@ add_fp_unittest(
14901490
libc.src.math.sqrtl
14911491
)
14921492

1493+
add_fp_unittest(
1494+
sqrtf16_test
1495+
NEED_MPFR
1496+
SUITE
1497+
libc-math-unittests
1498+
SRCS
1499+
sqrtf16_test.cpp
1500+
DEPENDS
1501+
libc.src.math.sqrtf16
1502+
)
1503+
14931504
add_fp_unittest(
14941505
generic_sqrtf_test
14951506
NEED_MPFR

libc/test/src/math/smoke/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2889,6 +2889,18 @@ add_fp_unittest(
28892889
libc.src.math.sqrtl
28902890
)
28912891

2892+
add_fp_unittest(
2893+
sqrtf16_test
2894+
SUITE
2895+
libc-math-smoke-tests
2896+
SRCS
2897+
sqrtf16_test.cpp
2898+
HDRS
2899+
SqrtTest.h
2900+
DEPENDS
2901+
libc.src.math.sqrtf16
2902+
)
2903+
28922904
add_fp_unittest(
28932905
sqrtf128_test
28942906
SUITE
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for sqrtf16 ---------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "SqrtTest.h"
10+
11+
#include "src/math/sqrtf16.h"
12+
13+
LIST_SQRT_TESTS(float16, LIBC_NAMESPACE::sqrtf16)

libc/test/src/math/sqrtf16_test.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===-- Exhaustive test for sqrtf16 ---------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/math/sqrtf16.h"
10+
#include "test/UnitTest/FPMatcher.h"
11+
#include "test/UnitTest/Test.h"
12+
#include "utils/MPFRWrapper/MPFRUtils.h"
13+
14+
using LlvmLibcSqrtf16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
15+
16+
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
17+
18+
// Range: [0, Inf];
19+
static constexpr uint16_t POS_START = 0x0000U;
20+
static constexpr uint16_t POS_STOP = 0x7c00U;
21+
22+
TEST_F(LlvmLibcSqrtf16Test, PositiveRange) {
23+
for (uint16_t v = POS_START; v <= POS_STOP; ++v) {
24+
float16 x = FPBits(v).get_val();
25+
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sqrt, x,
26+
LIBC_NAMESPACE::sqrtf16(x), 0.5);
27+
}
28+
}

0 commit comments

Comments
 (0)