Skip to content

Commit 2a9fe1a

Browse files
committed
[libc][math][c23] Add sqrtf16 C23 math function
Part of #95250.
1 parent 4fffbcc commit 2a9fe1a

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
@@ -679,6 +679,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
679679
libc.src.math.scalbnf16
680680
libc.src.math.setpayloadf16
681681
libc.src.math.setpayloadsigf16
682+
libc.src.math.sqrtf16
682683
libc.src.math.totalorderf16
683684
libc.src.math.totalordermagf16
684685
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
@@ -683,6 +683,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
683683
libc.src.math.setpayloadf16
684684
libc.src.math.setpayloadsigf16
685685
libc.src.math.sinhf16
686+
libc.src.math.sqrtf16
686687
libc.src.math.tanhf16
687688
libc.src.math.totalorderf16
688689
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| | | | | | 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
@@ -491,6 +491,7 @@ add_math_entrypoint_object(sinhf16)
491491
add_math_entrypoint_object(sqrt)
492492
add_math_entrypoint_object(sqrtf)
493493
add_math_entrypoint_object(sqrtl)
494+
add_math_entrypoint_object(sqrtf16)
494495
add_math_entrypoint_object(sqrtf128)
495496

496497
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
@@ -3230,6 +3230,18 @@ add_entrypoint_object(
32303230
-O3
32313231
)
32323232

3233+
add_entrypoint_object(
3234+
sqrtf16
3235+
SRCS
3236+
sqrtf16.cpp
3237+
HDRS
3238+
../sqrtf16.h
3239+
DEPENDS
3240+
libc.src.__support.FPUtil.sqrt
3241+
COMPILE_OPTIONS
3242+
-O3
3243+
)
3244+
32333245
add_entrypoint_object(
32343246
sqrtf128
32353247
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
@@ -1479,6 +1479,17 @@ add_fp_unittest(
14791479
libc.src.math.sqrtl
14801480
)
14811481

1482+
add_fp_unittest(
1483+
sqrtf16_test
1484+
NEED_MPFR
1485+
SUITE
1486+
libc-math-unittests
1487+
SRCS
1488+
sqrtf16_test.cpp
1489+
DEPENDS
1490+
libc.src.math.sqrtf16
1491+
)
1492+
14821493
add_fp_unittest(
14831494
generic_sqrtf_test
14841495
NEED_MPFR

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2878,6 +2878,18 @@ add_fp_unittest(
28782878
libc.src.math.sqrtl
28792879
)
28802880

2881+
add_fp_unittest(
2882+
sqrtf16_test
2883+
SUITE
2884+
libc-math-smoke-tests
2885+
SRCS
2886+
sqrtf16_test.cpp
2887+
HDRS
2888+
SqrtTest.h
2889+
DEPENDS
2890+
libc.src.math.sqrtf16
2891+
)
2892+
28812893
add_fp_unittest(
28822894
sqrtf128_test
28832895
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)