[libc-commits] [libc] [libc] Add bigint casting between word types (PR #111914)
via libc-commits
libc-commits at lists.llvm.org
Thu Oct 10 16:19:25 PDT 2024
================
@@ -361,17 +361,90 @@ struct BigInt {
LIBC_INLINE constexpr BigInt(const BigInt &other) = default;
- template <size_t OtherBits, bool OtherSigned>
+ template <size_t OtherBits, bool OtherSigned, typename OtherWordType>
LIBC_INLINE constexpr BigInt(
- const BigInt<OtherBits, OtherSigned, WordType> &other) {
- if (OtherBits >= Bits) { // truncate
- for (size_t i = 0; i < WORD_COUNT; ++i)
- val[i] = other[i];
- } else { // zero or sign extend
- size_t i = 0;
- for (; i < OtherBits / WORD_SIZE; ++i)
- val[i] = other[i];
- extend(i, Signed && other.is_neg());
+ const BigInt<OtherBits, OtherSigned, OtherWordType> &other) {
+ using BigIntOther = BigInt<OtherBits, OtherSigned, OtherWordType>;
+ const bool should_sign_extend = Signed && other.is_neg();
+
+ if constexpr (BigIntOther::WORD_SIZE < WORD_SIZE) {
+ // OtherWordType is smaller
+ constexpr size_t WORD_SIZE_RATIO = WORD_SIZE / BigIntOther::WORD_SIZE;
+ static_assert(
+ (WORD_SIZE % BigIntOther::WORD_SIZE) == 0 &&
+ "Word types must be multiples of each other for correct conversion.");
+ if (OtherBits >= Bits) { // truncate
----------------
lntue wrote:
I guess this can be `if constexpr` also?
https://github.com/llvm/llvm-project/pull/111914
More information about the libc-commits
mailing list