Skip to content

Commit cbb0576

Browse files
authored
[libc][math][c23] Add sqrtf16 C23 math function (#106102)
Part of #95250.
1 parent 5797271 commit cbb0576

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
@@ -554,6 +554,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
554554
libc.src.math.setpayloadf16
555555
libc.src.math.setpayloadsigf16
556556
libc.src.math.sinhf16
557+
libc.src.math.sqrtf16
557558
libc.src.math.tanhf16
558559
libc.src.math.totalorderf16
559560
libc.src.math.totalordermagf16

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
661661
libc.src.math.scalbnf16
662662
libc.src.math.setpayloadf16
663663
libc.src.math.setpayloadsigf16
664+
libc.src.math.sqrtf16
664665
libc.src.math.totalorderf16
665666
libc.src.math.totalordermagf16
666667
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
@@ -665,6 +665,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
665665
libc.src.math.setpayloadf16
666666
libc.src.math.setpayloadsigf16
667667
libc.src.math.sinhf16
668+
libc.src.math.sqrtf16
668669
libc.src.math.tanhf16
669670
libc.src.math.totalorderf16
670671
libc.src.math.totalordermagf16

libc/docs/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ Higher Math Functions
340340
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
341341
| sinpi | |check| | | | | | 7.12.4.13 | F.10.1.13 |
342342
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
343-
| sqrt | |check| | |check| | |check| | | |check| | 7.12.7.10 | F.10.4.10 |
343+
| sqrt | |check| | |check| | |check| | |check| | |check| | 7.12.7.10 | F.10.4.10 |
344344
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
345345
| tan | |check| | |check| | | | | 7.12.4.7 | F.10.1.7 |
346346
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/spec/stdc.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ def StdC : StandardSpec<"stdc"> {
668668
FunctionSpec<"sqrt", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
669669
FunctionSpec<"sqrtf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
670670
FunctionSpec<"sqrtl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
671+
GuardedFunctionSpec<"sqrtf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
671672
GuardedFunctionSpec<"sqrtf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
672673

673674
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
@@ -138,7 +138,8 @@ sqrt(InType x) {
138138
for (InStorageType current_bit = ONE >> 1; current_bit;
139139
current_bit >>= 1) {
140140
r <<= 1;
141-
InStorageType tmp = (y << 1) + current_bit; // 2*y(n - 1) + 2^(-n-1)
141+
// 2*y(n - 1) + 2^(-n-1)
142+
InStorageType tmp = static_cast<InStorageType>((y << 1) + current_bit);
142143
if (r >= tmp) {
143144
r -= tmp;
144145
y += current_bit;

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ add_math_entrypoint_object(sinhf16)
479479
add_math_entrypoint_object(sqrt)
480480
add_math_entrypoint_object(sqrtf)
481481
add_math_entrypoint_object(sqrtl)
482+
add_math_entrypoint_object(sqrtf16)
482483
add_math_entrypoint_object(sqrtf128)
483484

484485
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
@@ -3166,6 +3166,18 @@ add_entrypoint_object(
31663166
-O3
31673167
)
31683168

3169+
add_entrypoint_object(
3170+
sqrtf16
3171+
SRCS
3172+
sqrtf16.cpp
3173+
HDRS
3174+
../sqrtf16.h
3175+
DEPENDS
3176+
libc.src.__support.FPUtil.sqrt
3177+
COMPILE_OPTIONS
3178+
-O3
3179+
)
3180+
31693181
add_entrypoint_object(
31703182
sqrtf128
31713183
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
@@ -2746,6 +2746,18 @@ add_fp_unittest(
27462746
libc.src.math.sqrtl
27472747
)
27482748

2749+
add_fp_unittest(
2750+
sqrtf16_test
2751+
SUITE
2752+
libc-math-smoke-tests
2753+
SRCS
2754+
sqrtf16_test.cpp
2755+
HDRS
2756+
SqrtTest.h
2757+
DEPENDS
2758+
libc.src.math.sqrtf16
2759+
)
2760+
27492761
add_fp_unittest(
27502762
sqrtf128_test
27512763
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)