>From www.godbolt.org we have with -O3:
#define eqabs(a, b) a*a == b*b
int t(int x) {
return eqabs(x, 9) or eqabs(x, 81);
}
t(int):
imul edi, edi
cmp edi, 81
sete al
cmp edi, 25
sete dl
or eax, edx
movzx eax, al
ret
That seems to be fastest possible code with only one mul op. (I am still not
sure if that is really faster than abs().)
For Nim, I think that inline procs do not help, so I would need a template. But
how can I ensure that the integer literal parameter is evaluated at compile
time and that "common subexpression elimination" works to get also best
possible code? Would untyped template parameters suffice?
Have not tried to look at Nim's assembly code yet, as finding the template
instructions in assembly listing is some work.