@@ -36,25 +36,17 @@ ap_uint<bits> DynamicLeftShift(ap_uint<bits> num, ap_uint<hlslib::ConstLog2(bits
36
36
37
37
PackedFloat Multiply (PackedFloat const &a, PackedFloat const &b) {
38
38
// 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 ();
49
41
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 );
51
43
// We need to shift the mantissa forward if the most significant bit is not set
52
44
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 );
54
47
// Add up exponents. If the most significant bit was 1, we're done. Otherwise subtract 1 due to
55
48
// 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;
58
50
// The sign is just the XOR of the existing signs
59
51
PackedFloat result;
60
52
result.SetMantissa (m_mantissa);
@@ -66,7 +58,6 @@ PackedFloat Multiply(PackedFloat const &a, PackedFloat const &b) {
66
58
// Does this correctly output the result if a and b are different signs?
67
59
// The mantissa of the result should depend on the sign bits of a and b
68
60
PackedFloat Add (PackedFloat const &a_in, PackedFloat const &b_in) {
69
-
70
61
// Retrieve once and for all to make sure there's no overhead from unpacking them
71
62
const bool _a_sign = a_in.GetSign ();
72
63
const bool _b_sign = b_in.GetSign ();
0 commit comments