Skip to content

Commit aa6e7a6

Browse files
authored
[libc][NFC] Remove integer_utils.h (#84466)
Its sole user is `BigInt` so moving `full_mul` inside UInt.h.
1 parent 9baa414 commit aa6e7a6

File tree

4 files changed

+54
-100
lines changed

4 files changed

+54
-100
lines changed

libc/src/__support/CMakeLists.txt

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,24 +194,11 @@ add_header_library(
194194
libc.src.__support.CPP.type_traits
195195
)
196196

197-
add_header_library(
198-
integer_utils
199-
HDRS
200-
integer_utils.h
201-
DEPENDS
202-
.math_extras
203-
.number_pair
204-
libc.src.__support.common
205-
libc.src.__support.CPP.bit
206-
libc.src.__support.CPP.type_traits
207-
)
208-
209197
add_header_library(
210198
uint
211199
HDRS
212200
UInt.h
213201
DEPENDS
214-
.integer_utils
215202
.math_extras
216203
.number_pair
217204
libc.src.__support.CPP.array

libc/src/__support/UInt.h

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "src/__support/CPP/limits.h"
1515
#include "src/__support/CPP/optional.h"
1616
#include "src/__support/CPP/type_traits.h"
17-
#include "src/__support/integer_utils.h"
1817
#include "src/__support/macros/attributes.h" // LIBC_INLINE
1918
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
2019
#include "src/__support/math_extras.h" // SumCarry, DiffBorrow
@@ -36,6 +35,52 @@ template <> struct half_width<__uint128_t> : cpp::type_identity<uint64_t> {};
3635
#endif // __SIZEOF_INT128__
3736

3837
template <typename T> using half_width_t = typename half_width<T>::type;
38+
39+
template <typename T> constexpr NumberPair<T> full_mul(T a, T b) {
40+
NumberPair<T> pa = split(a);
41+
NumberPair<T> pb = split(b);
42+
NumberPair<T> prod;
43+
44+
prod.lo = pa.lo * pb.lo; // exact
45+
prod.hi = pa.hi * pb.hi; // exact
46+
NumberPair<T> lo_hi = split(pa.lo * pb.hi); // exact
47+
NumberPair<T> hi_lo = split(pa.hi * pb.lo); // exact
48+
49+
constexpr size_t HALF_BIT_WIDTH = sizeof(T) * CHAR_BIT / 2;
50+
51+
auto r1 = add_with_carry(prod.lo, lo_hi.lo << HALF_BIT_WIDTH, T(0));
52+
prod.lo = r1.sum;
53+
prod.hi = add_with_carry(prod.hi, lo_hi.hi, r1.carry).sum;
54+
55+
auto r2 = add_with_carry(prod.lo, hi_lo.lo << HALF_BIT_WIDTH, T(0));
56+
prod.lo = r2.sum;
57+
prod.hi = add_with_carry(prod.hi, hi_lo.hi, r2.carry).sum;
58+
59+
return prod;
60+
}
61+
62+
template <>
63+
LIBC_INLINE constexpr NumberPair<uint32_t> full_mul<uint32_t>(uint32_t a,
64+
uint32_t b) {
65+
uint64_t prod = uint64_t(a) * uint64_t(b);
66+
NumberPair<uint32_t> result;
67+
result.lo = uint32_t(prod);
68+
result.hi = uint32_t(prod >> 32);
69+
return result;
70+
}
71+
72+
#ifdef __SIZEOF_INT128__
73+
template <>
74+
LIBC_INLINE constexpr NumberPair<uint64_t> full_mul<uint64_t>(uint64_t a,
75+
uint64_t b) {
76+
__uint128_t prod = __uint128_t(a) * __uint128_t(b);
77+
NumberPair<uint64_t> result;
78+
result.lo = uint64_t(prod);
79+
result.hi = uint64_t(prod >> 64);
80+
return result;
81+
}
82+
#endif // __SIZEOF_INT128__
83+
3984
} // namespace internal
4085

4186
template <size_t Bits, bool Signed, typename WordType = uint64_t>
@@ -263,7 +308,7 @@ struct BigInt {
263308
LIBC_INLINE constexpr WordType mul(WordType x) {
264309
BigInt<2 * WORD_SIZE, Signed, WordType> partial_sum(0);
265310
for (size_t i = 0; i < WORD_COUNT; ++i) {
266-
NumberPair<WordType> prod = full_mul(val[i], x);
311+
NumberPair<WordType> prod = internal::full_mul(val[i], x);
267312
BigInt<2 * WORD_SIZE, Signed, WordType> tmp({prod.lo, prod.hi});
268313
const WordType carry = partial_sum.add(tmp);
269314
val[i] = partial_sum.val[0];
@@ -296,7 +341,8 @@ struct BigInt {
296341
WordType carry = 0;
297342
for (size_t i = 0; i < WORD_COUNT; ++i) {
298343
for (size_t j = 0; j <= i; j++) {
299-
NumberPair<WordType> prod = full_mul(val[j], other.val[i - j]);
344+
NumberPair<WordType> prod =
345+
internal::full_mul(val[j], other.val[i - j]);
300346
BigInt<2 * WORD_SIZE, Signed, WordType> tmp({prod.lo, prod.hi});
301347
carry += partial_sum.add(tmp);
302348
}
@@ -324,7 +370,8 @@ struct BigInt {
324370
i < OTHER_WORDCOUNT ? 0 : i - OTHER_WORDCOUNT + 1;
325371
const size_t upper_idx = i < WORD_COUNT ? i : WORD_COUNT - 1;
326372
for (size_t j = lower_idx; j <= upper_idx; ++j) {
327-
NumberPair<WordType> prod = full_mul(val[j], other.val[i - j]);
373+
NumberPair<WordType> prod =
374+
internal::full_mul(val[j], other.val[i - j]);
328375
BigInt<2 * WORD_SIZE, Signed, WordType> tmp({prod.lo, prod.hi});
329376
carry += partial_sum.add(tmp);
330377
}
@@ -366,7 +413,7 @@ struct BigInt {
366413
// product.
367414
for (size_t i = 0; i < WORD_COUNT; ++i) {
368415
NumberPair<WordType> prod =
369-
full_mul(val[i], other.val[WORD_COUNT - 1 - i]);
416+
internal::full_mul(val[i], other.val[WORD_COUNT - 1 - i]);
370417
BigInt<2 * WORD_SIZE, Signed, WordType> tmp({prod.lo, prod.hi});
371418
carry += partial_sum.add(tmp);
372419
}
@@ -375,7 +422,8 @@ struct BigInt {
375422
partial_sum.val[1] = carry;
376423
carry = 0;
377424
for (size_t j = i - WORD_COUNT + 1; j < WORD_COUNT; ++j) {
378-
NumberPair<WordType> prod = full_mul(val[j], other.val[i - j]);
425+
NumberPair<WordType> prod =
426+
internal::full_mul(val[j], other.val[i - j]);
379427
BigInt<2 * WORD_SIZE, Signed, WordType> tmp({prod.lo, prod.hi});
380428
carry += partial_sum.add(tmp);
381429
}

libc/src/__support/integer_utils.h

Lines changed: 0 additions & 69 deletions
This file was deleted.

utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -438,17 +438,6 @@ libc_support_library(
438438
],
439439
)
440440

441-
libc_support_library(
442-
name = "__support_integer_utils",
443-
hdrs = ["src/__support/integer_utils.h"],
444-
deps = [
445-
":__support_common",
446-
":__support_cpp_type_traits",
447-
":__support_math_extras",
448-
":__support_number_pair",
449-
],
450-
)
451-
452441
libc_support_library(
453442
name = "__support_uint",
454443
hdrs = ["src/__support/UInt.h"],
@@ -458,7 +447,6 @@ libc_support_library(
458447
":__support_cpp_limits",
459448
":__support_cpp_optional",
460449
":__support_cpp_type_traits",
461-
":__support_integer_utils",
462450
":__support_macros_attributes",
463451
":__support_macros_optimization",
464452
":__support_math_extras",

0 commit comments

Comments
 (0)