Skip to content

Commit 5b8b21a

Browse files
committed
- clang-formatted the files
- added missing header
1 parent e84a1e6 commit 5b8b21a

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

libc/src/__support/math/rsqrtf.h

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//===-- Implementation header for rsqrtf ----------------------*- 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___SUPPORT_MATH_RSQRTF_H
10+
#define LLVM_LIBC_SRC___SUPPORT_MATH_RSQRTF_H
11+
12+
#include "src/__support/FPUtil/FEnvImpl.h"
13+
#include "src/__support/FPUtil/FPBits.h"
14+
#include "src/__support/FPUtil/ManipulationFunctions.h"
15+
#include "src/__support/FPUtil/cast.h"
16+
#include "src/__support/FPUtil/multiply_add.h"
17+
#include "src/__support/FPUtil/sqrt.h"
18+
#include "src/__support/macros/optimization.h"
19+
20+
namespace LIBC_NAMESPACE_DECL {
21+
namespace math {
22+
23+
LIBC_INLINE static constexpr float rsqrtf(float x) {
24+
using FPBits = fputil::FPBits<float>;
25+
FPBits xbits(x);
26+
27+
uint32_t x_u = xbits.uintval();
28+
uint32_t x_abs = x_u & 0x7fff'ffffU;
29+
30+
constexpr uint32_t INF_BIT = FPBits::inf().uintval();
31+
32+
// x is 0, inf/nan, or negative.
33+
if (LIBC_UNLIKELY(x_u == 0 || x_u >= INF_BIT)) {
34+
// x is NaN
35+
if (x_abs > INF_BIT) {
36+
if (xbits.is_signaling_nan()) {
37+
fputil::raise_except_if_required(FE_INVALID);
38+
return FPBits::quiet_nan().get_val();
39+
}
40+
return x;
41+
}
42+
43+
// |x| = 0
44+
if (x_abs == 0) {
45+
fputil::raise_except_if_required(FE_DIVBYZERO);
46+
fputil::set_errno_if_required(ERANGE);
47+
return FPBits::inf(xbits.sign()).get_val();
48+
}
49+
50+
// -inf <= x < 0
51+
if (x_u > 0x7fff'ffffU) {
52+
fputil::raise_except_if_required(FE_INVALID);
53+
fputil::set_errno_if_required(EDOM);
54+
return FPBits::quiet_nan().get_val();
55+
}
56+
57+
// x = +inf => rsqrt(x) = 0
58+
return FPBits::zero().get_val();
59+
}
60+
61+
// TODO: add integer based implementation when LIBC_TARGET_CPU_HAS_FPU_FLOAT
62+
// is not defined
63+
double result = 1.0f / fputil::sqrt<double>(fputil::cast<double>(x));
64+
65+
// Targeted post-corrections to ensure correct rounding in half for specific
66+
// mantissa patterns
67+
/*
68+
const uint32_t half_mantissa = x_abs & 0x3ff;
69+
if (LIBC_UNLIKELY(half_mantissa == 0x011F)) {
70+
result = fputil::multiply_add(result, 0x1.0p-21f, result);
71+
} else if (LIBC_UNLIKELY(half_mantissa == 0x0313)) {
72+
result = fputil::multiply_add(result, -0x1.0p-21f, result);
73+
}*/
74+
75+
return fputil::cast<float>(result);
76+
}
77+
78+
} // namespace math
79+
} // namespace LIBC_NAMESPACE_DECL
80+
81+
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_RSQRTF_H

libc/test/src/math/rsqrtf_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ using LlvmLibcRsqrtfTest = LIBC_NAMESPACE::testing::FPTest<float>;
1616
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
1717

1818
// Range: [0, Inf]
19-
static constexpr uint32_t POS_START = 0x00000000u;
19+
static constexpr uint32_t POS_START = 0x00000000u;
2020
static constexpr uint32_t POS_STOP = 0x7F800000u;
2121

2222
// Range: [-Inf, 0)

libc/test/src/math/smoke/rsqrtf_test.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ TEST_F(LlvmLibcRsqrtfTest, SpecialNumbers) {
2626
EXPECT_FP_EQ(neg_inf, LIBC_NAMESPACE::rsqrtf(neg_zero));
2727
EXPECT_MATH_ERRNO(ERANGE);
2828

29-
EXPECT_FP_EQ(
30-
1.0f,
31-
LIBC_NAMESPACE::rsqrtf(1.0f));
29+
EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::rsqrtf(1.0f));
3230
EXPECT_MATH_ERRNO(0);
3331

3432
EXPECT_FP_EQ(zero, LIBC_NAMESPACE::rsqrtf(inf));

0 commit comments

Comments
 (0)