-
Notifications
You must be signed in to change notification settings - Fork 11k
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
[libc][NFC] Annotate LIBC_INLINE and constexpr to BigInt and DyadicFloat methods. #81912
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-libc Author: None (lntue) ChangesFull diff: https://github.com/llvm/llvm-project/pull/81912.diff 4 Files Affected:
diff --git a/libc/src/__support/FPUtil/dyadic_float.h b/libc/src/__support/FPUtil/dyadic_float.h
index 382904cf13bddb..7797c57b96fd85 100644
--- a/libc/src/__support/FPUtil/dyadic_float.h
+++ b/libc/src/__support/FPUtil/dyadic_float.h
@@ -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();
@@ -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;
@@ -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;
@@ -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();
@@ -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;
@@ -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()))
@@ -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);
@@ -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;
@@ -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;
diff --git a/libc/src/__support/UInt.h b/libc/src/__support/UInt.h
index 84e6ad3bcb800d..b90275035a23ea 100644
--- a/libc/src/__support/UInt.h
+++ b/libc/src/__support/UInt.h
@@ -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) {
@@ -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;
@@ -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;
@@ -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;
@@ -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;
diff --git a/libc/src/__support/integer_utils.h b/libc/src/__support/integer_utils.h
index dd407f9b2ef9a6..15e04bda808231 100644
--- a/libc/src/__support/integer_utils.h
+++ b/libc/src/__support/integer_utils.h
@@ -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;
@@ -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);
@@ -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);
diff --git a/libc/src/__support/number_pair.h b/libc/src/__support/number_pair.h
index 12e730836af2c6..934f41806b5f30 100644
--- a/libc/src/__support/number_pair.h
+++ b/libc/src/__support/number_pair.h
@@ -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>
|
michaelrj-google
approved these changes
Feb 15, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.