Mike Trader wrote:
Am I understanding your point correctly, in that it doesn't matter if the intermediate result is beyond the scope of the DataType as long as the final result written to memory IS?
It does matter. For the final result of an integer calculation to be mathematically correct each result must fit into whatever data type it is stored in. Depending on the calculation and how it is performed, the processor may automatically store the intermediate results in a larger data type than the operands or the final result. For the calculation:
x = a * b / b
Where x, a, and b are 32-bit integers, a=3,000,000,000 and b=3, even though the final result will fit in a 32-bit integer, the intermediate result from the multiply operation will not. Doing this on the CPU, one obvious coding (using MASM code) would be:
Code: Select all
mov eax, a
mov ecx, b
mul ecx
div ecx
mov x, eax
Where the MUL instruction stores the 64-bit result in the EDX:EAX register pair, and the DIV instruction uses the EDX:EAX register pair as the dividend and returns the quotient in EAX.
Doing this on the FPU, one obvious coding would be:
The “i” in the instruction mnemonics indicates that the memory operand is an integer instead of a real. Values loaded into the FPU, whether integer, floating-point or BCD, are converted to the 80-bit real number format that the FPU uses internally, and the intermediate results are stored internally in this same format. This format includes a 64-bit significand, 15 exponent bits, and a sign bit. The 64-bit significand allows the FPU to handle the full range of values for 64-bit integers. The last instruction converts the final result from the internal format to a 32-bit integer and stores it in x.
why not use the FPU for signed integer calcs and everything else too?
One reason to avoid using the FPU for everything is execution speed. Even if the system has an FPU, for integer operations the CPU is generally faster, and in some cases much faster.