Skip to content

Commit f96b791

Browse files
amemovakadutta
authored andcommitted
[libc][math][c23] Add rsqrtf() function (llvm#159615)
Closes llvm#159614 **Changes:** - Initial implementation of rsqrt for single precision float **Some small unrelated style changes to this PR (that I missed in my rsqrtf16 PR):** - Added extra - to the top comments to make it look nicer in libc/shared/math/rsqrtf16.h - Put rsqrtf16 inside of libc/src/__support/math/CMakeLists.txt in sorted order - Rearanged libc_math_function rsqrtf16 in Bazel to match alphabetical order
1 parent 8b5fa60 commit f96b791

File tree

25 files changed

+409
-33
lines changed

25 files changed

+409
-33
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
735735
libc.src.math.rintf16
736736
libc.src.math.roundevenf16
737737
libc.src.math.roundf16
738+
libc.src.math.rsqrtf
738739
libc.src.math.rsqrtf16
739740
libc.src.math.scalblnf16
740741
libc.src.math.scalbnf16

libc/config/linux/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
750750
libc.src.math.rintf16
751751
libc.src.math.roundevenf16
752752
libc.src.math.roundf16
753+
libc.src.math.rsqrtf
753754
libc.src.math.rsqrtf16
754755
libc.src.math.scalblnf16
755756
libc.src.math.scalbnf16

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
786786
libc.src.math.rintf16
787787
libc.src.math.roundevenf16
788788
libc.src.math.roundf16
789+
libc.src.math.rsqrtf
789790
libc.src.math.rsqrtf16
790791
libc.src.math.scalblnf16
791792
libc.src.math.scalbnf16

libc/docs/headers/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ Higher Math Functions
343343
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
344344
| rootn | | | | | | | 7.12.7.8 | F.10.4.8 |
345345
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
346-
| rsqrt | | | | |check| | | | 7.12.7.9 | F.10.4.9 |
346+
| rsqrt | |check| | | | |check| | | | 7.12.7.9 | F.10.4.9 |
347347
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
348348
| sin | |check| | |check| | | |check| | | | 7.12.4.6 | F.10.1.6 |
349349
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+

libc/include/math.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2349,6 +2349,12 @@ functions:
23492349
return_type: long double
23502350
arguments:
23512351
- type: long double
2352+
- name: rsqrtf
2353+
standards:
2354+
- stdc
2355+
return_type: float
2356+
arguments:
2357+
- type: float
23522358
- name: rsqrtf16
23532359
standards:
23542360
- stdc

libc/shared/math.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
#include "math/ldexpf.h"
5858
#include "math/ldexpf128.h"
5959
#include "math/ldexpf16.h"
60-
60+
#include "math/rsqrtf.h"
6161
#include "math/rsqrtf16.h"
6262

6363
#endif // LLVM_LIBC_SHARED_MATH_H

libc/shared/math/rsqrtf.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Shared rsqrtf function ----------------------------------*- 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_SHARED_MATH_RSQRTF_H
10+
#define LLVM_LIBC_SHARED_MATH_RSQRTF_H
11+
12+
#include "shared/libc_common.h"
13+
#include "src/__support/math/rsqrtf.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
namespace shared {
17+
18+
using math::rsqrtf;
19+
20+
} // namespace shared
21+
} // namespace LIBC_NAMESPACE_DECL
22+
23+
#endif // LLVM_LIBC_SHARED_MATH_RSQRTF_H

libc/shared/math/rsqrtf16.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===-- Shared rsqrtf16 function -------------------------------*- C++ -*-===//
1+
//===-- Shared rsqrtf16 function --------------------------------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.

libc/src/__support/math/CMakeLists.txt

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,22 +109,6 @@ add_header_library(
109109
libc.src.__support.macros.properties.types
110110
)
111111

112-
113-
add_header_library(
114-
rsqrtf16
115-
HDRS
116-
rsqrtf16.h
117-
DEPENDS
118-
libc.src.__support.FPUtil.cast
119-
libc.src.__support.FPUtil.fenv_impl
120-
libc.src.__support.FPUtil.fp_bits
121-
libc.src.__support.FPUtil.multiply_add
122-
libc.src.__support.FPUtil.polyeval
123-
libc.src.__support.FPUtil.manipulation_functions
124-
libc.src.__support.macros.optimization
125-
libc.src.__support.macros.properties.types
126-
)
127-
128112
add_header_library(
129113
asin_utils
130114
HDRS
@@ -866,6 +850,34 @@ add_header_library(
866850
libc.src.__support.common
867851
)
868852

853+
add_header_library(
854+
rsqrtf
855+
HDRS
856+
rsqrtf.h
857+
DEPENDS
858+
libc.hdr.errno_macros
859+
libc.hdr.fenv_macros
860+
libc.src.__support.FPUtil.cast
861+
libc.src.__support.FPUtil.fenv_impl
862+
libc.src.__support.FPUtil.fp_bits
863+
libc.src.__support.FPUtil.sqrt
864+
libc.src.__support.macros.optimization
865+
)
866+
867+
add_header_library(
868+
rsqrtf16
869+
HDRS
870+
rsqrtf16.h
871+
DEPENDS
872+
libc.hdr.errno_macros
873+
libc.hdr.fenv_macros
874+
libc.src.__support.FPUtil.cast
875+
libc.src.__support.FPUtil.fenv_impl
876+
libc.src.__support.FPUtil.fp_bits
877+
libc.src.__support.FPUtil.sqrt
878+
libc.src.__support.macros.optimization
879+
)
880+
869881
add_header_library(
870882
sincos_eval
871883
HDRS

libc/src/__support/math/rsqrtf.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 "hdr/errno_macros.h"
13+
#include "hdr/fenv_macros.h"
14+
#include "src/__support/FPUtil/FEnvImpl.h"
15+
#include "src/__support/FPUtil/FPBits.h"
16+
#include "src/__support/FPUtil/cast.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_BITS = FPBits::inf().uintval();
31+
32+
// x is 0, inf/nan, or negative.
33+
if (LIBC_UNLIKELY(x_u == 0 || x_u >= INF_BITS)) {
34+
// x is NaN
35+
if (x_abs > INF_BITS) {
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(xbits.sign()).get_val();
59+
}
60+
61+
// TODO: add float based approximation when
62+
// LIBC_TARGET_CPU_HAS_FPU_DOUBLE is not defined
63+
double result = 1.0 / fputil::sqrt<double>(fputil::cast<double>(x));
64+
65+
return fputil::cast<float>(result);
66+
}
67+
68+
} // namespace math
69+
} // namespace LIBC_NAMESPACE_DECL
70+
71+
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_RSQRTF_H

0 commit comments

Comments
 (0)