Skip to content

Commit bed1c8a

Browse files
committed
Stop padding mantissa, and replace shifts with ranges
1 parent 612a6ea commit bed1c8a

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

device/ArithmeticOperations.cpp

+6-15
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,17 @@ ap_uint<bits> DynamicLeftShift(ap_uint<bits> num, ap_uint<hlslib::ConstLog2(bits
3636

3737
PackedFloat Multiply(PackedFloat const &a, PackedFloat const &b) {
3838
// Pad mantissas to avoid passing awkward sizes to Karatsuba
39-
const ap_uint<kBits> a_mantissa_padded(a.GetMantissa());
40-
const ap_uint<kBits> b_mantissa_padded(b.GetMantissa());
41-
#ifdef APFP_GMP_SEMANTICS // Use GMP semantics
42-
constexpr auto kLimbBits = 8 * sizeof(mp_limb_t);
43-
// Meat of the computation. Only keep the top bits of the computation and throw away the rest
44-
const ap_uint<(2 * kMantissaBits)> _m_mantissa = Karatsuba(a_mantissa_padded, b_mantissa_padded);
45-
const bool limb_zero = _m_mantissa.range(kMantissaBits + kLimbBits - 1, kMantissaBits) == 0;
46-
ap_uint<kMantissaBits + kLimbBits> m_mantissa = _m_mantissa; // Truncate
47-
const Exponent m_exponent = a.GetExponent() + b.GetExponent() - limb_zero;
48-
#else // Otherwise use MPFR semantics
39+
const ap_uint<kMantissaBits> a_mantissa = a.GetMantissa();
40+
const ap_uint<kMantissaBits> b_mantissa = b.GetMantissa();
4941
const ap_uint<kMantissaBits + 1> _m_mantissa =
50-
Karatsuba(a_mantissa_padded, b_mantissa_padded) >> (kMantissaBits - 1);
42+
Karatsuba(a_mantissa, b_mantissa).range(2 * kMantissaBits - 1, kMantissaBits - 1);
5143
// We need to shift the mantissa forward if the most significant bit is not set
5244
const bool should_be_shifted = !IsMostSignificantBitSet(_m_mantissa);
53-
ap_uint<kMantissaBits + 1> m_mantissa = should_be_shifted ? _m_mantissa : (_m_mantissa >> 1);
45+
const ap_uint<kMantissaBits> m_mantissa =
46+
should_be_shifted ? _m_mantissa.range(kMantissaBits - 1, 0) : _m_mantissa.range(kMantissaBits, 1);
5447
// Add up exponents. If the most significant bit was 1, we're done. Otherwise subtract 1 due to
5548
// the shift.
56-
const Exponent m_exponent = a.GetExponent() + b.GetExponent() - (should_be_shifted ? 1 : 0);
57-
#endif
49+
const Exponent m_exponent = a.GetExponent() + b.GetExponent() - should_be_shifted;
5850
// The sign is just the XOR of the existing signs
5951
PackedFloat result;
6052
result.SetMantissa(m_mantissa);
@@ -66,7 +58,6 @@ PackedFloat Multiply(PackedFloat const &a, PackedFloat const &b) {
6658
// Does this correctly output the result if a and b are different signs?
6759
// The mantissa of the result should depend on the sign bits of a and b
6860
PackedFloat Add(PackedFloat const &a_in, PackedFloat const &b_in) {
69-
7061
// Retrieve once and for all to make sure there's no overhead from unpacking them
7162
const bool _a_sign = a_in.GetSign();
7263
const bool _b_sign = b_in.GetSign();

0 commit comments

Comments
 (0)