Skip to content

[libc][math][c23] Add sqrtf16 C23 math function #106102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/config/gpu/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.setpayloadf16
libc.src.math.setpayloadsigf16
libc.src.math.sinhf16
libc.src.math.sqrtf16
libc.src.math.tanhf16
libc.src.math.totalorderf16
libc.src.math.totalordermagf16
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.scalbnf16
libc.src.math.setpayloadf16
libc.src.math.setpayloadsigf16
libc.src.math.sqrtf16
libc.src.math.totalorderf16
libc.src.math.totalordermagf16
libc.src.math.truncf16
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.setpayloadf16
libc.src.math.setpayloadsigf16
libc.src.math.sinhf16
libc.src.math.sqrtf16
libc.src.math.tanhf16
libc.src.math.totalorderf16
libc.src.math.totalordermagf16
Expand Down
2 changes: 1 addition & 1 deletion libc/docs/math/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ Higher Math Functions
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| sinpi | |check| | | | | | 7.12.4.13 | F.10.1.13 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| sqrt | |check| | |check| | |check| | | |check| | 7.12.7.10 | F.10.4.10 |
| sqrt | |check| | |check| | |check| | |check| | |check| | 7.12.7.10 | F.10.4.10 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| tan | |check| | |check| | | | | 7.12.4.7 | F.10.1.7 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
Expand Down
1 change: 1 addition & 0 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"sqrt", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"sqrtf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
FunctionSpec<"sqrtl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
GuardedFunctionSpec<"sqrtf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"sqrtf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,

FunctionSpec<"trunc", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
Expand Down
3 changes: 2 additions & 1 deletion libc/src/__support/FPUtil/generic/sqrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ sqrt(InType x) {
for (InStorageType current_bit = ONE >> 1; current_bit;
current_bit >>= 1) {
r <<= 1;
InStorageType tmp = (y << 1) + current_bit; // 2*y(n - 1) + 2^(-n-1)
// 2*y(n - 1) + 2^(-n-1)
InStorageType tmp = static_cast<InStorageType>((y << 1) + current_bit);
if (r >= tmp) {
r -= tmp;
y += current_bit;
Expand Down
1 change: 1 addition & 0 deletions libc/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ add_math_entrypoint_object(sinhf16)
add_math_entrypoint_object(sqrt)
add_math_entrypoint_object(sqrtf)
add_math_entrypoint_object(sqrtl)
add_math_entrypoint_object(sqrtf16)
add_math_entrypoint_object(sqrtf128)

add_math_entrypoint_object(tan)
Expand Down
12 changes: 12 additions & 0 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3166,6 +3166,18 @@ add_entrypoint_object(
-O3
)

add_entrypoint_object(
sqrtf16
SRCS
sqrtf16.cpp
HDRS
../sqrtf16.h
DEPENDS
libc.src.__support.FPUtil.sqrt
COMPILE_OPTIONS
-O3
)

add_entrypoint_object(
sqrtf128
SRCS
Expand Down
20 changes: 20 additions & 0 deletions libc/src/math/generic/sqrtf16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation of sqrtf16 function --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/math/sqrtf16.h"
#include "src/__support/FPUtil/sqrt.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(float16, sqrtf16, (float16 x)) {
return fputil::sqrt<float16>(x);
}

} // namespace LIBC_NAMESPACE_DECL
21 changes: 21 additions & 0 deletions libc/src/math/sqrtf16.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation header for sqrtf16 -----------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_MATH_SQRTF16_H
#define LLVM_LIBC_SRC_MATH_SQRTF16_H

#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/types.h"

namespace LIBC_NAMESPACE_DECL {

float16 sqrtf16(float16 x);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_MATH_SQRTF16_H
11 changes: 11 additions & 0 deletions libc/test/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,17 @@ add_fp_unittest(
libc.src.math.sqrtl
)

add_fp_unittest(
sqrtf16_test
NEED_MPFR
SUITE
libc-math-unittests
SRCS
sqrtf16_test.cpp
DEPENDS
libc.src.math.sqrtf16
)

add_fp_unittest(
generic_sqrtf_test
NEED_MPFR
Expand Down
12 changes: 12 additions & 0 deletions libc/test/src/math/smoke/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2746,6 +2746,18 @@ add_fp_unittest(
libc.src.math.sqrtl
)

add_fp_unittest(
sqrtf16_test
SUITE
libc-math-smoke-tests
SRCS
sqrtf16_test.cpp
HDRS
SqrtTest.h
DEPENDS
libc.src.math.sqrtf16
)

add_fp_unittest(
sqrtf128_test
SUITE
Expand Down
13 changes: 13 additions & 0 deletions libc/test/src/math/smoke/sqrtf16_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for sqrtf16 ---------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "SqrtTest.h"

#include "src/math/sqrtf16.h"

LIST_SQRT_TESTS(float16, LIBC_NAMESPACE::sqrtf16)
28 changes: 28 additions & 0 deletions libc/test/src/math/sqrtf16_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===-- Exhaustive test for sqrtf16 ---------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/math/sqrtf16.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
#include "utils/MPFRWrapper/MPFRUtils.h"

using LlvmLibcSqrtf16Test = LIBC_NAMESPACE::testing::FPTest<float16>;

namespace mpfr = LIBC_NAMESPACE::testing::mpfr;

// Range: [0, Inf];
static constexpr uint16_t POS_START = 0x0000U;
static constexpr uint16_t POS_STOP = 0x7c00U;

TEST_F(LlvmLibcSqrtf16Test, PositiveRange) {
for (uint16_t v = POS_START; v <= POS_STOP; ++v) {
float16 x = FPBits(v).get_val();
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sqrt, x,
LIBC_NAMESPACE::sqrtf16(x), 0.5);
}
}
Loading