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

            Bug ID: 105751
           Summary: std::array comparision does not inline memcmp
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc at maxbachmann dot de
  Target Milestone: ---

On trunk and gcc 12.1, with -O2/-O3 the following array comparison is only
optimized to a memcmp, but not to a direct comparision:

```
bool test(std::array<int8_t, 8>& a, std::array<int8_t, 8>& b)
{
    return a == b;
}
```
which is optimized to:
```
test(std::array<signed char, 8ul>&, std::array<signed char, 8ul>&):
        sub     rsp, 8
        mov     edx, 8
        call    memcmp
        test    eax, eax
        sete    al
        add     rsp, 8
        ret
```

However for a direct memcmp gcc is able to optimize the memcmp to a single 64
bit comparision:
```
bool test(int8_t* a, int8_t* b)
{
    return memcmp(a, b, sizeof(int8_t) * 8) == 0;
}
```
which is optimized to
```
test(signed char*, signed char*):
        mov     rax, QWORD PTR [rsi]
        cmp     QWORD PTR [rdi], rax
        sete    al
        ret
```

This is optimized to a single comparision both in clang and msvc:
https://godbolt.org/z/99reqf3Yz

Reply via email to