Hi,

targeting AMD64 alias x86_64 with -O3, GCC 10.2.0 generates the
following code (12 instructions using 51 bytes, plus 4 quadwords
using 32 bytes) for __builtin_rint() when -msse4.1 is NOT given:

                                .text
   0:   f2 0f 10 15 10 00 00 00 movsd   .LC1(%rip), %xmm2
                        4: R_X86_64_PC32        .rdata
   8:   f2 0f 10 1d 00 00 00 00 movsd   .LC0(%rip), %xmm3
                        c: R_X86_64_PC32        .rdata
  10:   66 0f 28 c8             movapd  %xmm0, %xmm1
  14:   66 0f 54 ca             andpd   %xmm2, %xmm1
  18:   66 0f 2f d9             comisd  %xmm1, %xmm3
  1c:   76 14                   jbe     32 <rint+0x32>
  1e:   f2 0f 58 cb             addsd   %xmm3, %xmm1
  22:   66 0f 55 d0             andnpd  %xmm0, %xmm2
  26:   f2 0f 5c cb             subsd   %xmm3, %xmm1
  2a:   66 0f 56 ca             orpd    %xmm2, %xmm1
  2e:   66 0f 28 c1             movapd  %xmm1, %xmm0
  32:   c3                      retq

                                .rdata
                                .align 8
   0:   00 00 00 00     .LC0:   .quad  0x1.0p52
        00 00 30 43
        00 00 00 00
        00 00 00 00
                                .align 16
  10:   ff ff ff ff     .LC1:   .quad  ~(-0.0)
        ff ff ff 7f
  18:   00 00 00 00             .quad  0.0
        00 00 00 00
                                .end

JFTR: in the best case, the memory accesses cost several cycles,
      while in the worst case they yield a page fault!


Properly optimized, faster and shorter code, using just 9 instructions
in only 33 bytes, WITHOUT superfluous constants, thus avoiding costly
memory accesses and saving at least 16 + 32 bytes, follows:

                              .intel_syntax
                              .text
   0:   f2 48 0f 2c c0        cvtsd2si rax, xmm0  # rax = llrint(argument)
   5:   48 f7 d8              neg     rax
                        #     jz      .L0         # argument zero?
   8:   70 16                 jo      .L0         # argument indefinite?
                                                  # argument overflows 64-bit 
integer?
   a:   48 f7 d8              neg     rax
   d:   f2 48 0f 2a c8        cvtsi2sd xmm1, rax  # xmm1 = rint(argument)
  12:   66 0f 73 d0 3f        psrlq   xmm0, 63
  17:   66 0f 73 f0 3f        psllq   xmm0, 63    # xmm0 = (argument & -0.0) ? 
-0.0 : 0.0
  1c:   66 0f 56 c1           orpd    xmm0, xmm1  # xmm0 = round(argument)
  20:   c3              .L0:  ret
                             .end

regards
Stefan

Reply via email to