Skip to content

Commit d97f6d1

Browse files
authored
[libc][math][c23] Add sqrtf16 C23 math function (#112406)
Part of #95250.
1 parent 69d3a44 commit d97f6d1

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

0 commit comments

Comments
 (0)