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

0 commit comments

Comments
 (0)