On gcc 4.7.3
, my fegetround()
function returns FE_TONEAREST
. According to the c++ reference, this means rounding away from zero. Essentially, it means saving the last bit that was shifted out when adjusting the precision of the mantissa after multiplication (since it will be twice as long as it should be). Afterwards, the saved bit is added to the final mantissa result.
For example, floating point multiplication gives the following results:
0x38b7aad5 * 0x38b7aad5 = 0x3203c5af
The mantissa after multiplication is
1011 0111 1010 1010 1101 0101
x 1011 0111 1010 1010 1101 0101
-------------------------------
1[000 0011 1100 0101 1010 1110] [1]000 0101 1001 0101 0011 1001
The [23'b]
set holds the significant digits, whereas the [1'b]
set holds the last bit shifted out. Note that the mantissa for the result is
[000 0011 1100 0101 1010 1111]
The last bit switched to 1
because the [1'b1]
set was added to the spliced mantissa (the [23'b]
set) due to the rounding mode.
Here is an example that is stumping me, because it looks to me like the hardware isn't rounding correctly.
0x20922800 * 0x20922800 = 0x1a6e34c (check this on your machine)
1010 0110 1110 0011 0100 1101
x 1010 0110 1110 0011 0100 1101
-------------------------------
01[01 0011 0111 0001 1010 0110 0][1]00 0000 0000 0000 0000 0000
Final Mantissas:
Their Result: 01 0011 0111 0001 1010 0110 0
Correct Result(?): 01 0011 0111 0001 1010 0110 1
I've been crunching binary all day, so it's possible I'm missing something simple here. Which answer is correct with the given rounding mode?