I recently tried to read assemblies of the binary of my code and found that a lot of floating-point operations are done using XMM registers and SSE instructions. For example, the following code:
float square(float a) {
float b = a + (a * a);
return b;
}
will be compiled into
push rbp
mov rbp, rsp
movss DWORD PTR [rbp-20], xmm0
movss xmm0, DWORD PTR [rbp-20]
mulss xmm0, xmm0
movss xmm1, DWORD PTR [rbp-20]
addss xmm0, xmm1
movss DWORD PTR [rbp-4], xmm0
movss xmm0, DWORD PTR [rbp-4]
pop rbp
ret
and the result is similar for other compilers.
https://godbolt.org/z/G988PGo6j
And with -O3
flag
movaps xmm1, xmm0
mulss xmm0, xmm0
addss xmm0, xmm1
ret
Does this mean operations using SIMD registers and instructions are usually faster than using normal registers and the FPU?
Also, I’m curious about specific cases where the compiler’s decision to use SSE might fail.
2
SSE was developed as a replacement for the x87 FPU as the x87 FPU’s design is a bit idiosyncratic and hard to generate code for. The main issues are:
- code generation for stack-based processors like the x87 FPU is not as well understood as for register-based processors, making the code generated by many compilers full of inefficient extra
fxch
,fld
, andfst(p)
instructions. This is much easier to get right with a register-based architecture like SSE. - SSE supports SIMD operation, greatly speeding up floating point operations on arrays if used. X87 supports no such thing
- on the x87 FPU, the data type used is Intel’s 80 bit floating point format. The precision of floating-point operations is configured using a control register and is expensive to change. Therefore, compilers generating code for the x87 unit would run all computations with full precision, even when only single precision is called for by the programmer. This both changes the result slightly and reduces performance as higher precision operations may take more time to complete. Additionally, each load and store from/to the x87 unit involves an implicit data type conversion. The SSE unit on the other hand encodes precision in the instruction used, allowing the compiler to use exactly the precision the programmer called for.
- recently, CPU manufacturers have reduced investments into improving the x87 FPU (or even took back existing improvements, like
fxch
being a rename), leading to a widening gap in performance between x87 and SSE.
I recommend to only use the x87 FPU if code size is an issue or if you require the 80 bits floating point format. Otherwise stick with SSE or (on recent processors) AVX.
7