Basic floating point operations in Java

To the programmer, the four basic mathematical operations on floating point values essentially share the same syntax as integer operations. Thus, just as we can write:

int x = 17;
x += 34;
int y = x / 3;

so we can write:

double x = 17;
x += 34;
double y = x / 3;

In current versions of Hotspot, floating point operations compile to floating point SSE instructions on Intel Pentium platforms, as listed in the table below. Approximate best-case and worst-case timings are also given in clock cycles. The exact timing of instructions depends on the surrounding instructions, on the dependency of one instruction on another, and on the specific model of processor (though in this case, the main difference is in the performance of the single-precision divide instruction).

Figure 1: Timing of compiled Java floating point instructions on Intel platforms.
OperationExample32-bit Intel machine instructionApproximate best/worst case time (clock cycles)
+double x = y + 4;
x += 7.5
ADDSD (double)
ADDSS (float)
2 / 6
-double x = y - 10;
x -= 13;
SUBSD (double)
SUBSS (float)
2 / 6
*double x = y * 15;
x *= -12.4;
MULSD (double)
MULSS (float)
2 / 7
/ double x = y / 27;
x /= 19;
DIVSD (double)
DIVSS (float)
38 / 38
23 / 32
where dividend is a power of 2double x = y / 32;
x /= 4;
MULSD (1 / x)2 / 6