AK+LibCrypto: Delete 64x64 wide multiplication workarounds

Now UFixedBigInt exposes API to do wide multiplications of this kind
efficiently.
This commit is contained in:
Dan Klishch 2023-02-04 19:03:52 +03:00 committed by Andrew Kaster
parent 67ec347bfa
commit 8f8e31e780
Notes: sideshowbarker 2024-07-17 05:02:42 +09:00
3 changed files with 4 additions and 26 deletions

View file

@ -1227,14 +1227,7 @@ static i32 decimal_exponent_to_binary_exponent(i32 exponent)
static u128 multiply(u64 a, u64 b) static u128 multiply(u64 a, u64 b)
{ {
#ifdef __SIZEOF_INT128__ return UFixedBigInt<64>(a).wide_multiply(b);
unsigned __int128 result = (unsigned __int128)a * b;
u64 low = result;
u64 high = result >> 64;
return u128 { low, high };
#else
return u128 { a }.wide_multiply(u128 { b }).low;
#endif
} }
template<unsigned Precision> template<unsigned Precision>
@ -1249,10 +1242,7 @@ u128 multiplication_approximation(u64 value, i32 exponent)
if ((lower_result.high() & mask) == mask) { if ((lower_result.high() & mask) == mask) {
auto upper_result = multiply(z.low(), value); auto upper_result = multiply(z.low(), value);
lower_result.low() += upper_result.high(); lower_result += upper_result.high();
if (upper_result.high() > lower_result.low()) {
++lower_result.high();
}
} }
return lower_result; return lower_result;

View file

@ -171,25 +171,14 @@ FloatingPointExponentialForm inner_convert_floating_point_to_decimal_exponential
static u128 multiply(u64 a, u64 b) static u128 multiply(u64 a, u64 b)
{ {
#ifdef __SIZEOF_INT128__ return UFixedBigInt<64>(a).wide_multiply(b);
unsigned __int128 result = (unsigned __int128)a * b;
u64 low = result;
u64 high = result >> 64;
return u128 { low, high };
#else
return u128 { a }.wide_multiply(u128 { b }).low;
#endif
} }
template<> template<>
FloatingPointExponentialForm convert_floating_point_to_decimal_exponential_form<float>(float value) FloatingPointExponentialForm convert_floating_point_to_decimal_exponential_form<float>(float value)
{ {
auto multiply_and_shift = [](u64 operand, u64 multiplier, i32 shift) { auto multiply_and_shift = [](u64 operand, u64 multiplier, i32 shift) {
#ifdef __SIZEOF_INT128__
auto result = (unsigned __int128)operand * multiplier;
#else
auto result = multiply(operand, multiplier); auto result = multiply(operand, multiplier);
#endif
if (shift < 0) if (shift < 0)
return static_cast<u64>(result << static_cast<u32>(-shift)); return static_cast<u64>(result << static_cast<u32>(-shift));
else else

View file

@ -57,8 +57,7 @@ static u256 select(u256 const& left, u256 const& right, bool condition)
static u512 multiply(u256 const& left, u256 const& right) static u512 multiply(u256 const& left, u256 const& right)
{ {
auto result = left.wide_multiply(right); return left.wide_multiply(right);
return { result.low, result.high };
} }
static u256 modular_reduce(u256 const& value) static u256 modular_reduce(u256 const& value)