Which libc are you thinking of? I wouldn't mind taking a look at their implementation. GCC 8.2 created this convoluted memcmp from what I thought was a pretty straightforward C func. It was long, so I didn't look too closely, I just decided to avoid it.
Code: Select all
int memcmp(const void *s1, const void *s2, size_t n)
{
147c: 2f02 movel %d2,%sp@-
147e: 206f 0008 moveal %sp@(8),%a0
1482: 226f 000c moveal %sp@(12),%a1
while (n--) {
1486: 4aaf 0010 tstl %sp@(16)
148a: 6730 beqs 14bc <memcmp+0x40>
if (*(uint8_t *)s1 != *(uint8_t *)s2)
148c: 1010 moveb %a0@,%d0
148e: 1211 moveb %a1@,%d1
1490: b001 cmpb %d1,%d0
1492: 6616 bnes 14aa <memcmp+0x2e>
1494: 5289 addql #1,%a1
1496: 242f 0010 movel %sp@(16),%d2
149a: d488 addl %a0,%d2
return *(uint8_t *)s1 - *(uint8_t *)s2;
s1++;
149c: 5288 addql #1,%a0
while (n--) {
149e: b1c2 cmpal %d2,%a0
14a0: 671a beqs 14bc <memcmp+0x40>
if (*(uint8_t *)s1 != *(uint8_t *)s2)
14a2: 1010 moveb %a0@,%d0
14a4: 1219 moveb %a1@+,%d1
14a6: b200 cmpb %d0,%d1
14a8: 67f2 beqs 149c <memcmp+0x20>
return *(uint8_t *)s1 - *(uint8_t *)s2;
14aa: 0280 0000 00ff andil #255,%d0
14b0: 0281 0000 00ff andil #255,%d1
14b6: 9081 subl %d1,%d0
s2++;
}
return 0;
}
14b8: 241f movel %sp@+,%d2
14ba: 4e75 rts
return 0;
14bc: 7000 moveq #0,%d0
}
14be: 241f movel %sp@+,%d2
14c0: 4e75 rts