https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115147

            Bug ID: 115147
           Summary: exp2 with integer arguments could be translated into
                    ldexp
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: janschultke at googlemail dot com
  Target Milestone: ---

https://godbolt.org/z/9141MdG6b

float e(int x) {
    return __builtin_exp2f(x);
}

GCC (-O2) optimizes this to:

> e(int):
>         pxor    xmm0, xmm0
>         cvtsi2ss        xmm0, edi
>         jmp     exp2f

Clang (-O2) optimizes this to:

> .LCPI0_0:
>         .long   0x3f800000
> e(int):
>         movss   xmm0, dword ptr [rip + .LCPI0_0]
>         jmp     ldexpf@PLT


I believe translating to ldexp is usually better, and this is a missed
optimization. ldexp takes an integer exponent. Converting x to float and then
going through exp2 seems wasteful in this case.

Reply via email to