Gabriel Ravier <gabrav...@gmail.com> wrote: Independent from the defunct flow analysis in the presence of NaNs, my example demonstrates another minor deficiency: know thy instruction set! See the comments in the assembly below.
> On 8/13/21 8:58 PM, Stefan Kanthak wrote: >> Hi, >> >> compile the following naive implementation of nextafter() for AMD64: >> >> JFTR: ignore the aliasing casts, they don't matter here! >> >> $ cat repro.c >> double nextafter(double from, double to) >> { >> if (to != to) >> return to; // to is NAN >> >> if (from != from) >> return from; // from is NAN >> >> if (from == to) // neither from nor to can be NAN here! >> return to; >> >> if (from == 0.0) // dito! >> return to < 0.0 ? -0x1.0p-1074 : 0x1.0p-1074; >> >> unsigned long long ull = *(unsigned long long *) &from; >> >> if ((from < to) == (from < 0.0)) >> ull--; >> else >> ull++; >> >> return *(double *) &ull; >> } >> $ gcc -m64 -o- -O3 -S repro.c >> ... >> nextafter: [...] >> .L4: >> comisd %xmm0, %xmm1 # sets CF if first comparand < second >> comparand >> movq %xmm0, %rdx >> leaq -1(%rdx), %rax # superfluous >> seta %r8b # sbb %rcx, %rcx >> comisd %xmm0, %xmm2 >> seta %cl # sbb %rax, %rax >> addq $1, %rdx # xor %rcx, %rax >> cmpb %cl, %r8b # or $1, %rax >> cmovne %rdx, %rax # add %rdx, %rax >> movq %rax, %xmm0 Stefan