Skip to content

[libc][NFC] Annotate LIBC_INLINE and constexpr to BigInt and DyadicFloat methods. #81912

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
merged 1 commit into from
Feb 15, 2024
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
36 changes: 19 additions & 17 deletions libc/src/__support/FPUtil/dyadic_float.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ template <size_t Bits> struct DyadicFloat {
int exponent = 0;
MantissaType mantissa = MantissaType(0);

constexpr DyadicFloat() = default;
LIBC_INLINE constexpr DyadicFloat() = default;

template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
DyadicFloat(T x) {
LIBC_INLINE constexpr DyadicFloat(T x) {
static_assert(FPBits<T>::FRACTION_LEN < Bits);
FPBits<T> x_bits(x);
sign = x_bits.sign();
Expand All @@ -49,14 +49,14 @@ template <size_t Bits> struct DyadicFloat {
normalize();
}

constexpr DyadicFloat(Sign s, int e, MantissaType m)
LIBC_INLINE constexpr DyadicFloat(Sign s, int e, MantissaType m)
: sign(s), exponent(e), mantissa(m) {
normalize();
}

// Normalizing the mantissa, bringing the leading 1 bit to the most
// significant bit.
constexpr DyadicFloat &normalize() {
LIBC_INLINE constexpr DyadicFloat &normalize() {
if (!mantissa.is_zero()) {
int shift_length = static_cast<int>(mantissa.clz());
exponent -= shift_length;
Expand All @@ -66,14 +66,14 @@ template <size_t Bits> struct DyadicFloat {
}

// Used for aligning exponents. Output might not be normalized.
DyadicFloat &shift_left(int shift_length) {
LIBC_INLINE constexpr DyadicFloat &shift_left(int shift_length) {
exponent -= shift_length;
mantissa <<= static_cast<size_t>(shift_length);
return *this;
}

// Used for aligning exponents. Output might not be normalized.
DyadicFloat &shift_right(int shift_length) {
LIBC_INLINE constexpr DyadicFloat &shift_right(int shift_length) {
exponent += shift_length;
mantissa >>= static_cast<size_t>(shift_length);
return *this;
Expand All @@ -85,7 +85,7 @@ template <size_t Bits> struct DyadicFloat {
typename = cpp::enable_if_t<cpp::is_floating_point_v<T> &&
(FPBits<T>::FRACTION_LEN < Bits),
void>>
explicit operator T() const {
LIBC_INLINE explicit constexpr operator T() const {
if (LIBC_UNLIKELY(mantissa.is_zero()))
return FPBits<T>::zero(sign).get_val();

Expand Down Expand Up @@ -176,7 +176,7 @@ template <size_t Bits> struct DyadicFloat {
return r;
}

explicit operator MantissaType() const {
LIBC_INLINE explicit constexpr operator MantissaType() const {
if (mantissa.is_zero())
return 0;

Expand Down Expand Up @@ -208,8 +208,8 @@ template <size_t Bits> struct DyadicFloat {
// don't need to normalize the inputs again in this function. If the inputs are
// not normalized, the results might lose precision significantly.
template <size_t Bits>
constexpr DyadicFloat<Bits> quick_add(DyadicFloat<Bits> a,
DyadicFloat<Bits> b) {
LIBC_INLINE constexpr DyadicFloat<Bits> quick_add(DyadicFloat<Bits> a,
DyadicFloat<Bits> b) {
if (LIBC_UNLIKELY(a.mantissa.is_zero()))
return b;
if (LIBC_UNLIKELY(b.mantissa.is_zero()))
Expand Down Expand Up @@ -263,8 +263,8 @@ constexpr DyadicFloat<Bits> quick_add(DyadicFloat<Bits> a,
// don't need to normalize the inputs again in this function. If the inputs are
// not normalized, the results might lose precision significantly.
template <size_t Bits>
constexpr DyadicFloat<Bits> quick_mul(DyadicFloat<Bits> a,
DyadicFloat<Bits> b) {
LIBC_INLINE constexpr DyadicFloat<Bits> quick_mul(DyadicFloat<Bits> a,
DyadicFloat<Bits> b) {
DyadicFloat<Bits> result;
result.sign = (a.sign != b.sign) ? Sign::NEG : Sign::POS;
result.exponent = a.exponent + b.exponent + int(Bits);
Expand All @@ -285,16 +285,17 @@ constexpr DyadicFloat<Bits> quick_mul(DyadicFloat<Bits> a,

// Simple polynomial approximation.
template <size_t Bits>
constexpr DyadicFloat<Bits> multiply_add(const DyadicFloat<Bits> &a,
const DyadicFloat<Bits> &b,
const DyadicFloat<Bits> &c) {
LIBC_INLINE constexpr DyadicFloat<Bits>
multiply_add(const DyadicFloat<Bits> &a, const DyadicFloat<Bits> &b,
const DyadicFloat<Bits> &c) {
return quick_add(c, quick_mul(a, b));
}

// Simple exponentiation implementation for printf. Only handles positive
// exponents, since division isn't implemented.
template <size_t Bits>
constexpr DyadicFloat<Bits> pow_n(DyadicFloat<Bits> a, uint32_t power) {
LIBC_INLINE constexpr DyadicFloat<Bits> pow_n(DyadicFloat<Bits> a,
uint32_t power) {
DyadicFloat<Bits> result = 1.0;
DyadicFloat<Bits> cur_power = a;

Expand All @@ -309,7 +310,8 @@ constexpr DyadicFloat<Bits> pow_n(DyadicFloat<Bits> a, uint32_t power) {
}

template <size_t Bits>
constexpr DyadicFloat<Bits> mul_pow_2(DyadicFloat<Bits> a, int32_t pow_2) {
LIBC_INLINE constexpr DyadicFloat<Bits> mul_pow_2(DyadicFloat<Bits> a,
int32_t pow_2) {
DyadicFloat<Bits> result = a;
result.exponent += pow_2;
return result;
Expand Down
10 changes: 5 additions & 5 deletions libc/src/__support/UInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ struct BigInt {

LIBC_INLINE constexpr explicit operator bool() const { return !is_zero(); }

LIBC_INLINE BigInt &operator=(const BigInt &other) = default;
LIBC_INLINE constexpr BigInt &operator=(const BigInt &other) = default;

LIBC_INLINE constexpr bool is_zero() const {
for (size_t i = 0; i < WORD_COUNT; ++i) {
Expand All @@ -172,7 +172,7 @@ struct BigInt {
LIBC_INLINE constexpr WordType add(const BigInt &x) {
SumCarry<WordType> s{0, 0};
for (size_t i = 0; i < WORD_COUNT; ++i) {
s = add_with_carry_const(val[i], x.val[i], s.carry);
s = add_with_carry(val[i], x.val[i], s.carry);
val[i] = s.sum;
}
return s.carry;
Expand All @@ -194,7 +194,7 @@ struct BigInt {
BigInt result;
SumCarry<WordType> s{0, 0};
for (size_t i = 0; i < WORD_COUNT; ++i) {
s = add_with_carry_const(val[i], other.val[i], s.carry);
s = add_with_carry(val[i], other.val[i], s.carry);
result.val[i] = s.sum;
}
return result;
Expand All @@ -210,7 +210,7 @@ struct BigInt {
LIBC_INLINE constexpr WordType sub(const BigInt &x) {
DiffBorrow<WordType> d{0, 0};
for (size_t i = 0; i < WORD_COUNT; ++i) {
d = sub_with_borrow_const(val[i], x.val[i], d.borrow);
d = sub_with_borrow(val[i], x.val[i], d.borrow);
val[i] = d.diff;
}
return d.borrow;
Expand All @@ -230,7 +230,7 @@ struct BigInt {
BigInt result;
DiffBorrow<WordType> d{0, 0};
for (size_t i = 0; i < WORD_COUNT; ++i) {
d = sub_with_borrow_const(val[i], other.val[i], d.borrow);
d = sub_with_borrow(val[i], other.val[i], d.borrow);
result.val[i] = d.diff;
}
return result;
Expand Down
8 changes: 5 additions & 3 deletions libc/src/__support/integer_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

namespace LIBC_NAMESPACE {

template <typename T> NumberPair<T> full_mul(T a, T b) {
template <typename T> constexpr NumberPair<T> full_mul(T a, T b) {
NumberPair<T> pa = split(a);
NumberPair<T> pb = split(b);
NumberPair<T> prod;
Expand All @@ -43,7 +43,8 @@ template <typename T> NumberPair<T> full_mul(T a, T b) {
}

template <>
LIBC_INLINE NumberPair<uint32_t> full_mul<uint32_t>(uint32_t a, uint32_t b) {
LIBC_INLINE constexpr NumberPair<uint32_t> full_mul<uint32_t>(uint32_t a,
uint32_t b) {
uint64_t prod = uint64_t(a) * uint64_t(b);
NumberPair<uint32_t> result;
result.lo = uint32_t(prod);
Expand All @@ -53,7 +54,8 @@ LIBC_INLINE NumberPair<uint32_t> full_mul<uint32_t>(uint32_t a, uint32_t b) {

#ifdef __SIZEOF_INT128__
template <>
LIBC_INLINE NumberPair<uint64_t> full_mul<uint64_t>(uint64_t a, uint64_t b) {
LIBC_INLINE constexpr NumberPair<uint64_t> full_mul<uint64_t>(uint64_t a,
uint64_t b) {
__uint128_t prod = __uint128_t(a) * __uint128_t(b);
NumberPair<uint64_t> result;
result.lo = uint64_t(prod);
Expand Down
4 changes: 2 additions & 2 deletions libc/src/__support/number_pair.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
namespace LIBC_NAMESPACE {

template <typename T> struct NumberPair {
T lo;
T hi;
T lo = T(0);
T hi = T(0);
};

template <typename T>
Expand Down