5

The next code (with -O3 -ffast-math):

#include <cmath>

float a[4];

void sin1() {
    for(unsigned i = 0; i < 4; i++) a[i] = sinf(a[i]);
}

Compiles vectorized version of sinf (_ZGVbN4v_sinf):

sin1():
        sub     rsp, 8
        movaps  xmm0, XMMWORD PTR a[rip]
        call    _ZGVbN4v_sinf
        movaps  XMMWORD PTR a[rip], xmm0
        add     rsp, 8
        ret

But when i use c++ version of sinf (std::sin) no vectorization occurrs:

void sin2() {
    for(unsigned i = 0; i < 4; i++) a[i] = std::sin(a[i]);
}

sin2():
        sub     rsp, 8
        movss   xmm0, DWORD PTR a[rip]
        call    sinf
        movss   DWORD PTR a[rip], xmm0
        movss   xmm0, DWORD PTR a[rip+4]
        call    sinf
        movss   DWORD PTR a[rip+4], xmm0
        movss   xmm0, DWORD PTR a[rip+8]
        call    sinf
        movss   DWORD PTR a[rip+8], xmm0
        movss   xmm0, DWORD PTR a[rip+12]
        call    sinf
        movss   DWORD PTR a[rip+12], xmm0
        add     rsp, 8
        ret

Compiler Explorer Code

Diego91b
  • 121
  • 4
  • 1
    So what is your question? – Ben Steffan Aug 03 '17 at 08:38
  • GCC bug or something wrong in code that prevents vectorization? – Diego91b Aug 03 '17 at 08:40
  • You're calling `sin` on uninitialized data. The compiler doesn't have to emit code at all. The `-O0` setting should be judged on how well it deals with bad code, and `-O3` on how well it works with good code. – MSalters Aug 03 '17 at 09:09
  • Even so the emitted code should be the same in both cases (for sinf and std::sin), however modifying the code to avoid uninitialized data the difference persists (sinf vectorization but std::sin no vectorization): [Compiler Explorer Modified Code](https://godbolt.org/g/aVUW72) – Diego91b Aug 03 '17 at 09:39
  • 3
    Looks like a bug, you get the same thing replacing sinf with __builtin_sinf, please report to gcc's bugzilla. – Marc Glisse Aug 03 '17 at 09:39
  • @MSalters: It's not uninitialized, it's zero initialized, since it's working on global variables. Secondly, even if it wasn't initialized in this code, the compiler couldn't have any idea whether it would be initialized before these functions are called, as there's no main provided. – Benjamin Lindley Aug 03 '17 at 13:53
  • 2
    Thank you for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81706 :-) – Marc Glisse Aug 03 '17 at 21:35

0 Answers0