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

            Bug ID: 117799
           Summary: __builtin_memcmp not optimized for size
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lh_mouse at 126 dot com
  Target Milestone: ---

This is observed in code that compares two UUIDs (that's why it's so useful):

(https://gcc.godbolt.org/z/1cY9GeaPb)
```
template<typename T>
bool
bytewise_equal(const T& x, const T& y) noexcept
  { return __builtin_memcmp(&x, &y, sizeof(x)); }

struct uuid 
  {
    unsigned int a;
    unsigned short b, c, d;
    unsigned char e[6]; 
  };

bool 
long_eq(const long* x, const long* y) noexcept
  { return bytewise_equal(*x, *y); }

bool
uuid_eq(const uuid* x, const uuid* y) noexcept
  { return bytewise_equal(*x, *y); }
```

Clang:
```
long_eq(long const*, long const*):
        mov     rax, qword ptr [rdi]
        cmp     rax, qword ptr [rsi]
        setne   al
        ret

uuid_eq(uuid const*, uuid const*):
        movdqu  xmm0, xmmword ptr [rdi]
        movdqu  xmm1, xmmword ptr [rsi]
        pcmpeqb xmm1, xmm0
        pmovmskb        eax, xmm1
        cmp     eax, 65535
        setne   al
        ret
```

GCC:
```
long_eq(long const*, long const*):
        push    rax
        mov     edx, 8
        call    memcmp
        pop     rdx
        test    eax, eax
        setne   al
        ret
uuid_eq(uuid const*, uuid const*):
        push    rax
        mov     edx, 16
        call    memcmp
        pop     rdx
        test    eax, eax
        setne   al
        ret
```

Reply via email to