I wanted to try making my own absolute value function. I figured that the fastest way to calculate absolute value would be to simply mask out the sign bit (the last bit in IEEE 754). I wanted to compare it's speed to the standard abs
function. Here is my implementation:
// Union used for type punning
union float_uint_u
{
float f_val;
unsigned int ui_val;
};
// 'MASK' has all bits == 1 except the last one
constexpr unsigned int MASK = ~(1 << (sizeof(int) * 8 - 1));
float abs_bitwise(float value)
{
float_uint_u ret;
ret.f_val = value;
ret.ui_val &= MASK;
return ret.f_val;
}
For the record, I know that this sort of type punning is not standard C++. However, this is just for educational purposes, and according to the docs, this is supported in GCC.
I figured this should be the fastest way to calculate absolute value, so it should at the very least be as fast as the standard implementation. However, timing 100000000 iterations of random values, I got the following results:
Bitwise time: 5.47385 | STL time: 5.15662
Ratio: 1.06152
My abs
function is about 6% slower.
Assembly output
I compiled with -O2
optimization and the -S
option (assembly output) to help determine what was going on. I have extracted the relevant portions:
; 16(%rsp) is a value obtained from standard input
movss 16(%rsp), %xmm0
andps .LC5(%rip), %xmm0 ; .LC5 == 2147483647
movq %rbp, %rdi
cvtss2sd %xmm0, %xmm0
movl 16(%rsp), %eax
movq %rbp, %rdi
andl $2147483647, %eax
movd %eax, %xmm0
cvtss2sd %xmm0, %xmm0
Observations
I'm not great at assembly, but the main thing I noticed is that the standard function operates directly on the xmm0
register. But with mine, it first moves the value to eax
(for some reason), performs the and
, and then moves it into xmm0
. I'm assuming the extra mov
is where the slow down happens. I also noticed that, for the standard, it stores the bit mask elsewhere in the program vs an immediate. I'm guessing that's not significant, however. The two versions also use different instructions (e.g. movl
vs movss
).
System info
This was compiled with g++ on Debian Linux (unstable branch). g++ --version
output:
g++ (Debian 10.2.1-6) 10.2.1 20210110
If these two versions of the code both calculate absolute value the same way (via an and
), why doesn't the optimizer generate the same code? Specifically, why does it feel the need to include an extra mov
when it optimizes my implementation?