[libc-commits] [libc] 4592744 - [libc][Obvious] Fix variable naming in the generic sqrt implementations.
Siva Chandra Reddy via libc-commits
libc-commits at lists.llvm.org
Wed Dec 8 12:02:23 PST 2021
Author: Siva Chandra Reddy
Date: 2021-12-08T19:57:46Z
New Revision: 45927444e7baaab457b8299f9def5f8944017fad
URL: https://github.com/llvm/llvm-project/commit/45927444e7baaab457b8299f9def5f8944017fad
DIFF: https://github.com/llvm/llvm-project/commit/45927444e7baaab457b8299f9def5f8944017fad.diff
LOG: [libc][Obvious] Fix variable naming in the generic sqrt implementations.
Added:
Modified:
libc/src/__support/FPUtil/Sqrt.h
libc/src/__support/FPUtil/x86_64/SqrtLongDouble.h
Removed:
################################################################################
diff --git a/libc/src/__support/FPUtil/Sqrt.h b/libc/src/__support/FPUtil/Sqrt.h
index 246535aab8146..652883ffc96b9 100644
--- a/libc/src/__support/FPUtil/Sqrt.h
+++ b/libc/src/__support/FPUtil/Sqrt.h
@@ -32,15 +32,15 @@ template <> inline void normalize<float>(int &exponent, uint32_t &mantissa) {
// Step 3: 000X XXXX XXXX XXXX XXXX XXXX
// Step 4: 00XX XXXX XXXX XXXX XXXX XXXX
// Step 5: 0XXX XXXX XXXX XXXX XXXX XXXX
- constexpr int nsteps = 5; // = ceil(log2(MantissaWidth))
- constexpr uint32_t bounds[nsteps] = {1 << 12, 1 << 18, 1 << 21, 1 << 22,
+ constexpr int NSTEPS = 5; // = ceil(log2(MantissaWidth))
+ constexpr uint32_t BOUNDS[NSTEPS] = {1 << 12, 1 << 18, 1 << 21, 1 << 22,
1 << 23};
- constexpr int shifts[nsteps] = {12, 6, 3, 2, 1};
+ constexpr int SHIFTS[NSTEPS] = {12, 6, 3, 2, 1};
- for (int i = 0; i < nsteps; ++i) {
- if (mantissa < bounds[i]) {
- exponent -= shifts[i];
- mantissa <<= shifts[i];
+ for (int i = 0; i < NSTEPS; ++i) {
+ if (mantissa < BOUNDS[i]) {
+ exponent -= SHIFTS[i];
+ mantissa <<= SHIFTS[i];
}
}
}
@@ -49,15 +49,15 @@ template <> inline void normalize<double>(int &exponent, uint64_t &mantissa) {
// Use binary search to shift the leading 1 bit similar to float.
// With MantissaWidth<double> = 52, it will take
// ceil(log2(52)) = 6 steps checking the mantissa bits.
- constexpr int nsteps = 6; // = ceil(log2(MantissaWidth))
- constexpr uint64_t bounds[nsteps] = {1ULL << 26, 1ULL << 39, 1ULL << 46,
+ constexpr int NSTEPS = 6; // = ceil(log2(MantissaWidth))
+ constexpr uint64_t BOUNDS[NSTEPS] = {1ULL << 26, 1ULL << 39, 1ULL << 46,
1ULL << 49, 1ULL << 51, 1ULL << 52};
- constexpr int shifts[nsteps] = {27, 14, 7, 4, 2, 1};
+ constexpr int SHIFTS[NSTEPS] = {27, 14, 7, 4, 2, 1};
- for (int i = 0; i < nsteps; ++i) {
- if (mantissa < bounds[i]) {
- exponent -= shifts[i];
- mantissa <<= shifts[i];
+ for (int i = 0; i < NSTEPS; ++i) {
+ if (mantissa < BOUNDS[i]) {
+ exponent -= SHIFTS[i];
+ mantissa <<= SHIFTS[i];
}
}
}
@@ -73,17 +73,17 @@ inline void normalize<long double>(int &exponent, __uint128_t &mantissa) {
// Use binary search to shift the leading 1 bit similar to float.
// With MantissaWidth<long double> = 112, it will take
// ceil(log2(112)) = 7 steps checking the mantissa bits.
- constexpr int nsteps = 7; // = ceil(log2(MantissaWidth))
- constexpr __uint128_t bounds[nsteps] = {
+ constexpr int NSTEPS = 7; // = ceil(log2(MantissaWidth))
+ constexpr __uint128_t BOUNDS[NSTEPS] = {
__uint128_t(1) << 56, __uint128_t(1) << 84, __uint128_t(1) << 98,
__uint128_t(1) << 105, __uint128_t(1) << 109, __uint128_t(1) << 111,
__uint128_t(1) << 112};
- constexpr int shifts[nsteps] = {57, 29, 15, 8, 4, 2, 1};
+ constexpr int SHIFTS[NSTEPS] = {57, 29, 15, 8, 4, 2, 1};
- for (int i = 0; i < nsteps; ++i) {
- if (mantissa < bounds[i]) {
- exponent -= shifts[i];
- mantissa <<= shifts[i];
+ for (int i = 0; i < NSTEPS; ++i) {
+ if (mantissa < BOUNDS[i]) {
+ exponent -= SHIFTS[i];
+ mantissa <<= SHIFTS[i];
}
}
}
@@ -97,14 +97,14 @@ template <typename T,
cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
static inline T sqrt(T x) {
using UIntType = typename FPBits<T>::UIntType;
- constexpr UIntType One = UIntType(1) << MantissaWidth<T>::VALUE;
+ constexpr UIntType ONE = UIntType(1) << MantissaWidth<T>::VALUE;
FPBits<T> bits(x);
if (bits.is_inf_or_nan()) {
if (bits.get_sign() && (bits.get_mantissa() == 0)) {
// sqrt(-Inf) = NaN
- return FPBits<T>::build_nan(One >> 1);
+ return FPBits<T>::build_nan(ONE >> 1);
} else {
// sqrt(NaN) = NaN
// sqrt(+Inf) = +Inf
@@ -116,41 +116,41 @@ static inline T sqrt(T x) {
return x;
} else if (bits.get_sign()) {
// sqrt( negative numbers ) = NaN
- return FPBits<T>::build_nan(One >> 1);
+ return FPBits<T>::build_nan(ONE >> 1);
} else {
- int xExp = bits.get_exponent();
- UIntType xMant = bits.get_mantissa();
+ int x_exp = bits.get_exponent();
+ UIntType x_mant = bits.get_mantissa();
// Step 1a: Normalize denormal input and append hidden bit to the mantissa
if (bits.get_unbiased_exponent() == 0) {
- ++xExp; // let xExp be the correct exponent of One bit.
- internal::normalize<T>(xExp, xMant);
+ ++x_exp; // let x_exp be the correct exponent of ONE bit.
+ internal::normalize<T>(x_exp, x_mant);
} else {
- xMant |= One;
+ x_mant |= ONE;
}
// Step 1b: Make sure the exponent is even.
- if (xExp & 1) {
- --xExp;
- xMant <<= 1;
+ if (x_exp & 1) {
+ --x_exp;
+ x_mant <<= 1;
}
- // After step 1b, x = 2^(xExp) * xMant, where xExp is even, and
- // 1 <= xMant < 4. So sqrt(x) = 2^(xExp / 2) * y, with 1 <= y < 2.
+ // After step 1b, x = 2^(x_exp) * x_mant, where x_exp is even, and
+ // 1 <= x_mant < 4. So sqrt(x) = 2^(x_exp / 2) * y, with 1 <= y < 2.
// Notice that the output of sqrt is always in the normal range.
// To perform shift-and-add algorithm to find y, let denote:
// y(n) = 1.y_1 y_2 ... y_n, we can define the nth residue to be:
- // r(n) = 2^n ( xMant - y(n)^2 ).
+ // r(n) = 2^n ( x_mant - y(n)^2 ).
// That leads to the following recurrence formula:
// r(n) = 2*r(n-1) - y_n*[ 2*y(n-1) + 2^(-n-1) ]
// with the initial conditions: y(0) = 1, and r(0) = x - 1.
// So the nth digit y_n of the mantissa of sqrt(x) can be found by:
// y_n = 1 if 2*r(n-1) >= 2*y(n - 1) + 2^(-n-1)
// 0 otherwise.
- UIntType y = One;
- UIntType r = xMant - One;
+ UIntType y = ONE;
+ UIntType r = x_mant - ONE;
- for (UIntType current_bit = One >> 1; current_bit; current_bit >>= 1) {
+ for (UIntType current_bit = ONE >> 1; current_bit; current_bit >>= 1) {
r <<= 1;
UIntType tmp = (y << 1) + current_bit; // 2*y(n - 1) + 2^(-n-1)
if (r >= tmp) {
@@ -170,9 +170,9 @@ static inline T sqrt(T x) {
}
// Remove hidden bit and append the exponent field.
- xExp = ((xExp >> 1) + FPBits<T>::EXPONENT_BIAS);
+ x_exp = ((x_exp >> 1) + FPBits<T>::EXPONENT_BIAS);
- y = (y - One) | (static_cast<UIntType>(xExp) << MantissaWidth<T>::VALUE);
+ y = (y - ONE) | (static_cast<UIntType>(x_exp) << MantissaWidth<T>::VALUE);
// Round to nearest, ties to even
if (rb && (lsb || (r != 0))) {
++y;
diff --git a/libc/src/__support/FPUtil/x86_64/SqrtLongDouble.h b/libc/src/__support/FPUtil/x86_64/SqrtLongDouble.h
index 584015665c283..22d2ba2592c8f 100644
--- a/libc/src/__support/FPUtil/x86_64/SqrtLongDouble.h
+++ b/libc/src/__support/FPUtil/x86_64/SqrtLongDouble.h
@@ -29,16 +29,16 @@ inline void normalize<long double>(int &exponent, __uint128_t &mantissa) {
// Use binary search to shift the leading 1 bit similar to float.
// With MantissaWidth<long double> = 63, it will take
// ceil(log2(63)) = 6 steps checking the mantissa bits.
- constexpr int nsteps = 6; // = ceil(log2(MantissaWidth))
- constexpr __uint128_t bounds[nsteps] = {
+ constexpr int NSTEPS = 6; // = ceil(log2(MantissaWidth))
+ constexpr __uint128_t BOUNDS[NSTEPS] = {
__uint128_t(1) << 32, __uint128_t(1) << 48, __uint128_t(1) << 56,
__uint128_t(1) << 60, __uint128_t(1) << 62, __uint128_t(1) << 63};
- constexpr int shifts[nsteps] = {32, 16, 8, 4, 2, 1};
+ constexpr int SHIFTS[NSTEPS] = {32, 16, 8, 4, 2, 1};
- for (int i = 0; i < nsteps; ++i) {
- if (mantissa < bounds[i]) {
- exponent -= shifts[i];
- mantissa <<= shifts[i];
+ for (int i = 0; i < NSTEPS; ++i) {
+ if (mantissa < BOUNDS[i]) {
+ exponent -= SHIFTS[i];
+ mantissa <<= SHIFTS[i];
}
}
}
@@ -49,7 +49,7 @@ inline void normalize<long double>(int &exponent, __uint128_t &mantissa) {
// Shift-and-add algorithm.
template <> inline long double sqrt<long double, 0>(long double x) {
using UIntType = typename FPBits<long double>::UIntType;
- constexpr UIntType One = UIntType(1)
+ constexpr UIntType ONE = UIntType(1)
<< int(MantissaWidth<long double>::VALUE);
FPBits<long double> bits(x);
@@ -57,7 +57,7 @@ template <> inline long double sqrt<long double, 0>(long double x) {
if (bits.is_inf_or_nan()) {
if (bits.get_sign() && (bits.get_mantissa() == 0)) {
// sqrt(-Inf) = NaN
- return FPBits<long double>::build_nan(One >> 1);
+ return FPBits<long double>::build_nan(ONE >> 1);
} else {
// sqrt(NaN) = NaN
// sqrt(+Inf) = +Inf
@@ -69,40 +69,40 @@ template <> inline long double sqrt<long double, 0>(long double x) {
return x;
} else if (bits.get_sign()) {
// sqrt( negative numbers ) = NaN
- return FPBits<long double>::build_nan(One >> 1);
+ return FPBits<long double>::build_nan(ONE >> 1);
} else {
- int xExp = bits.get_exponent();
- UIntType xMant = bits.get_mantissa();
+ int x_exp = bits.get_exponent();
+ UIntType x_mant = bits.get_mantissa();
// Step 1a: Normalize denormal input
if (bits.get_implicit_bit()) {
- xMant |= One;
+ x_mant |= ONE;
} else if (bits.get_unbiased_exponent() == 0) {
- internal::normalize<long double>(xExp, xMant);
+ internal::normalize<long double>(x_exp, x_mant);
}
// Step 1b: Make sure the exponent is even.
- if (xExp & 1) {
- --xExp;
- xMant <<= 1;
+ if (x_exp & 1) {
+ --x_exp;
+ x_mant <<= 1;
}
- // After step 1b, x = 2^(xExp) * xMant, where xExp is even, and
- // 1 <= xMant < 4. So sqrt(x) = 2^(xExp / 2) * y, with 1 <= y < 2.
+ // After step 1b, x = 2^(x_exp) * x_mant, where x_exp is even, and
+ // 1 <= x_mant < 4. So sqrt(x) = 2^(x_exp / 2) * y, with 1 <= y < 2.
// Notice that the output of sqrt is always in the normal range.
// To perform shift-and-add algorithm to find y, let denote:
// y(n) = 1.y_1 y_2 ... y_n, we can define the nth residue to be:
- // r(n) = 2^n ( xMant - y(n)^2 ).
+ // r(n) = 2^n ( x_mant - y(n)^2 ).
// That leads to the following recurrence formula:
// r(n) = 2*r(n-1) - y_n*[ 2*y(n-1) + 2^(-n-1) ]
// with the initial conditions: y(0) = 1, and r(0) = x - 1.
// So the nth digit y_n of the mantissa of sqrt(x) can be found by:
// y_n = 1 if 2*r(n-1) >= 2*y(n - 1) + 2^(-n-1)
// 0 otherwise.
- UIntType y = One;
- UIntType r = xMant - One;
+ UIntType y = ONE;
+ UIntType r = x_mant - ONE;
- for (UIntType current_bit = One >> 1; current_bit; current_bit >>= 1) {
+ for (UIntType current_bit = ONE >> 1; current_bit; current_bit >>= 1) {
r <<= 1;
UIntType tmp = (y << 1) + current_bit; // 2*y(n - 1) + 2^(-n-1)
if (r >= tmp) {
@@ -122,8 +122,8 @@ template <> inline long double sqrt<long double, 0>(long double x) {
}
// Append the exponent field.
- xExp = ((xExp >> 1) + FPBits<long double>::EXPONENT_BIAS);
- y |= (static_cast<UIntType>(xExp)
+ x_exp = ((x_exp >> 1) + FPBits<long double>::EXPONENT_BIAS);
+ y |= (static_cast<UIntType>(x_exp)
<< (MantissaWidth<long double>::VALUE + 1));
// Round to nearest, ties to even
@@ -133,9 +133,9 @@ template <> inline long double sqrt<long double, 0>(long double x) {
// Extract output
FPBits<long double> out(0.0L);
- out.set_unbiased_exponent(xExp);
+ out.set_unbiased_exponent(x_exp);
out.set_implicit_bit(1);
- out.set_mantissa((y & (One - 1)));
+ out.set_mantissa((y & (ONE - 1)));
return out;
}
More information about the libc-commits
mailing list