Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
[libc][NFC] Clean up clang-tidy warnings for src/__support and `src…
Browse files Browse the repository at this point in the history
…/math`.

Clean up some warnings from running libc-lint for these folders.

Reviewed By: michaelrj, sivachandra

Differential Revision: https://reviews.llvm.org/D146048
  • Loading branch information
lntue authored and agozillon committed Mar 17, 2023
1 parent 2318c67 commit a69578d
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 104 deletions.
10 changes: 7 additions & 3 deletions libc/.clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ CheckOptions:
- key: readability-identifier-naming.MemberCase
value: lower_case
- key: readability-identifier-naming.MemberIgnoredRegexp
value: "^_[A-Za-z0-9_]+$"
value: "_?(_[A-Za-z0-9]+)*"
- key: readability-identifier-naming.VariableCase
value: lower_case
- key: readability-identifier-naming.VariableIgnoredRegexp
value: "^_[A-Za-z0-9_]+$"
value: "_?(_[A-Za-z0-9]+)*"
- key: readability-identifier-naming.FunctionCase
value: lower_case
- key: readability-identifier-naming.FunctionIgnoredRegexp
value: "^_[A-Za-z0-9_]+$"
value: "_?(_[A-Za-z0-9]+)*"
- key: readability-identifier-naming.GlobalConstantCase
value: UPPER_CASE
- key: readability-identifier-naming.LocalConstantIgnoredRegexp
value: "[A-Z]+[A-Z0-9]*(_[A-Z0-9]+)*"
- key: readability-identifier-naming.ClassConstantCase
value: UPPER_CASE
- key: readability-identifier-naming.ConstexprVariableCase
value: UPPER_CASE
- key: readability-identifier-naming.GetConfigPerFile
Expand Down
36 changes: 18 additions & 18 deletions libc/src/__support/FPUtil/XFloat.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace fputil {
template <size_t Precision> class XFloat {
static constexpr uint64_t OneMask = (uint64_t(1) << 63);
UInt<Precision> man;
static constexpr uint64_t WordCount = Precision / 64;
static constexpr uint64_t WORDCOUNT = Precision / 64;
int exp;

size_t bit_width(uint64_t x) {
Expand All @@ -35,25 +35,25 @@ template <size_t Precision> class XFloat {

public:
XFloat() : exp(0) {
for (int i = 0; i < WordCount; ++i)
for (int i = 0; i < WORDCOUNT; ++i)
man[i] = 0;
}

XFloat(const XFloat &other) : exp(other.exp) {
for (int i = 0; i < WordCount; ++i)
for (int i = 0; i < WORDCOUNT; ++i)
man[i] = other.man[i];
}

explicit XFloat(double x) {
auto xn = NormalFloat<double>(x);
exp = xn.exponent;
man[WordCount - 1] = xn.mantissa << 11;
for (int i = 0; i < WordCount - 1; ++i)
man[WORDCOUNT - 1] = xn.mantissa << 11;
for (int i = 0; i < WORDCOUNT - 1; ++i)
man[i] = 0;
}

XFloat(int e, const UInt<Precision> &bits) : exp(e) {
for (size_t i = 0; i < WordCount; ++i)
for (size_t i = 0; i < WORDCOUNT; ++i)
man[i] = bits[i];
}

Expand All @@ -65,7 +65,7 @@ template <size_t Precision> class XFloat {
size_t carry_width = bit_width(carry);
carry_width = (carry_width == 64 ? 64 : 63);
man.shift_right(carry_width);
man[WordCount - 1] = man[WordCount - 1] + (carry << (64 - carry_width));
man[WORDCOUNT - 1] = man[WORDCOUNT - 1] + (carry << (64 - carry_width));
exp += carry_width == 64 ? 1 : 0;
normalize();
}
Expand All @@ -74,7 +74,7 @@ template <size_t Precision> class XFloat {
if (exp < 0)
return;
if (exp > int(Precision - 1)) {
for (size_t i = 0; i < WordCount; ++i)
for (size_t i = 0; i < WORDCOUNT; ++i)
man[i] = 0;
return;
}
Expand All @@ -86,14 +86,14 @@ template <size_t Precision> class XFloat {
}

double mul(const XFloat<Precision> &other) {
constexpr size_t row_words = 2 * WordCount + 1;
constexpr size_t row_words = 2 * WORDCOUNT + 1;
constexpr size_t row_precision = row_words * 64;
constexpr size_t result_bits = 2 * Precision;
UInt<row_precision> rows[WordCount];
UInt<row_precision> rows[WORDCOUNT];

for (size_t r = 0; r < WordCount; ++r) {
for (size_t r = 0; r < WORDCOUNT; ++r) {
for (size_t i = 0; i < row_words; ++i) {
if (i < WordCount)
if (i < WORDCOUNT)
rows[r][i] = man[i];
else
rows[r][i] = 0;
Expand All @@ -102,7 +102,7 @@ template <size_t Precision> class XFloat {
rows[r].shift_left(r * 64);
}

for (size_t r = 1; r < WordCount; ++r) {
for (size_t r = 1; r < WORDCOUNT; ++r) {
rows[0].add(rows[r]);
}
int result_exp = exp + other.exp;
Expand Down Expand Up @@ -134,14 +134,14 @@ template <size_t Precision> class XFloat {

constexpr uint64_t one = uint64_t(1) << 10;
constexpr uint64_t excess_mask = (one << 1) - 1;
uint64_t excess = man[WordCount - 1] & excess_mask;
uint64_t new_man = man[WordCount - 1] >> 11;
uint64_t excess = man[WORDCOUNT - 1] & excess_mask;
uint64_t new_man = man[WORDCOUNT - 1] >> 11;
if (excess > one) {
// We have to round up.
++new_man;
} else if (excess == one) {
bool greater_than_one = false;
for (size_t i = 0; i < WordCount - 1; ++i) {
for (size_t i = 0; i < WORDCOUNT - 1; ++i) {
greater_than_one = (man[i] != 0);
if (greater_than_one)
break;
Expand All @@ -163,13 +163,13 @@ template <size_t Precision> class XFloat {
// Normalizes this number.
void normalize() {
uint64_t man_bits = 0;
for (size_t i = 0; i < WordCount; ++i)
for (size_t i = 0; i < WORDCOUNT; ++i)
man_bits |= man[i];

if (man_bits == 0)
return;

while ((man[WordCount - 1] & OneMask) == 0) {
while ((man[WORDCOUNT - 1] & OneMask) == 0) {
man.shift_left(1);
--exp;
}
Expand Down
14 changes: 7 additions & 7 deletions libc/src/__support/FPUtil/dyadic_float.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ template <size_t Bits> struct DyadicFloat {
sign, exponent + (Bits - 1) + FloatProperties<T>::EXPONENT_BIAS,
output_bits_t(m_hi) & FloatProperties<T>::MANTISSA_MASK);

const MantissaType ROUND_MASK = MantissaType(1) << (Bits - PRECISION - 1);
const MantissaType STICKY_MASK = ROUND_MASK - MantissaType(1);
const MantissaType round_mask = MantissaType(1) << (Bits - PRECISION - 1);
const MantissaType sticky_mask = round_mask - MantissaType(1);

bool round_bit = !(mantissa & ROUND_MASK).is_zero();
bool sticky_bit = !(mantissa & STICKY_MASK).is_zero();
bool round_bit = !(mantissa & round_mask).is_zero();
bool sticky_bit = !(mantissa & sticky_mask).is_zero();
int round_and_sticky = int(round_bit) * 2 + int(sticky_bit);
auto d_lo = FPBits<T>::create_value(sign,
exponent + (Bits - PRECISION - 2) +
Expand Down Expand Up @@ -156,7 +156,7 @@ constexpr DyadicFloat<Bits> quick_add(DyadicFloat<Bits> a,
if (result.mantissa.add(b.mantissa)) {
// Mantissa addition overflow.
result.shift_right(1);
result.mantissa.val[DyadicFloat<Bits>::MantissaType::WordCount - 1] |=
result.mantissa.val[DyadicFloat<Bits>::MantissaType::WORDCOUNT - 1] |=
(uint64_t(1) << 63);
}
// Result is already normalized.
Expand All @@ -183,7 +183,7 @@ constexpr DyadicFloat<Bits> quick_add(DyadicFloat<Bits> a,
// result.mantissa = quick_mul_hi(a.mantissa + b.mantissa)
// ~ (full product a.mantissa * b.mantissa) >> Bits.
// The errors compared to the mathematical product is bounded by:
// 2 * errors of quick_mul_hi = 2 * (UInt<Bits>::WordCount - 1) in ULPs.
// 2 * errors of quick_mul_hi = 2 * (UInt<Bits>::WORDCOUNT - 1) in ULPs.
// Assume inputs are normalized (by constructors or other functions) so that we
// don't need to normalize the inputs again in this function. If the inputs are
// not normalized, the results might lose precision significantly.
Expand All @@ -198,7 +198,7 @@ constexpr DyadicFloat<Bits> quick_mul(DyadicFloat<Bits> a,
result.mantissa = a.mantissa.quick_mul_hi(b.mantissa);
// Check the leading bit directly, should be faster than using clz in
// normalize().
if (result.mantissa.val[DyadicFloat<Bits>::MantissaType::WordCount - 1] >>
if (result.mantissa.val[DyadicFloat<Bits>::MantissaType::WORDCOUNT - 1] >>
63 ==
0)
result.shift_left(1);
Expand Down
17 changes: 9 additions & 8 deletions libc/src/__support/FPUtil/generic/FMod.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ template <typename T> struct FModExceptionalInputHandler {
static_assert(cpp::is_floating_point_v<T>,
"FModCStandardWrapper instantiated with invalid type.");

LIBC_INLINE static bool PreCheck(T x, T y, T &out) {
LIBC_INLINE static bool pre_check(T x, T y, T &out) {
using FPB = fputil::FPBits<T>;
const T quiet_NaN = FPB::build_quiet_nan(0);
const T quiet_nan = FPB::build_quiet_nan(0);
FPB sx(x), sy(y);
if (LIBC_LIKELY(!sy.is_zero() && !sy.is_inf_or_nan() &&
!sx.is_inf_or_nan())) {
Expand All @@ -134,14 +134,15 @@ template <typename T> struct FModExceptionalInputHandler {
if (sx.is_nan() || sy.is_nan()) {
if ((sx.is_nan() && !sx.is_quiet_nan()) ||
(sy.is_nan() && !sy.is_quiet_nan()))
fputil::set_except(FE_INVALID);
out = quiet_NaN;
fputil::raise_except_if_required(FE_INVALID);
out = quiet_nan;
return true;
}

if (sx.is_inf() || sy.is_zero()) {
fputil::set_except(FE_INVALID);
out = with_errno(quiet_NaN, EDOM);
fputil::raise_except_if_required(FE_INVALID);
fputil::set_errno_if_required(EDOM);
out = quiet_nan;
return true;
}

Expand All @@ -161,7 +162,7 @@ template <typename T> struct FModFastMathWrapper {
static_assert(cpp::is_floating_point_v<T>,
"FModFastMathWrapper instantiated with invalid type.");

static bool PreCheck(T, T, T &) { return false; }
static bool pre_check(T, T, T &) { return false; }
};

template <typename T> class FModDivisionSimpleHelper {
Expand Down Expand Up @@ -302,7 +303,7 @@ class FMod {

public:
LIBC_INLINE static T eval(T x, T y) {
if (T out; Wrapper::PreCheck(x, y, out))
if (T out; Wrapper::pre_check(x, y, out))
return out;
FPB sx(x), sy(y);
bool sign = sx.get_sign();
Expand Down
Loading

0 comments on commit a69578d

Please sign in to comment.