Debian Codegraph @

libc-top-half/musl/src/string/memcmp.c

raw
1 #include <string.h>
2
3 #ifdef __wasm_simd128__
4 #include <wasm_simd128.h>
5 #endif
6
7 int memcmp(const void *vl, const void *vr, size_t n)
8 {
9 #if defined(__wasm_simd128__) && defined(__wasilibc_simd_string)
10 if (n >= sizeof(v128_t)) {
11 // memcmp is allowed to read up to n bytes from each object.
12 // Find the first different character in the objects.
13 // Unaligned loads handle the case where the objects
14 // have mismatching alignments.
15 const v128_t *v1 = (v128_t *)vl;
16 const v128_t *v2 = (v128_t *)vr;
17 while (n) {
18 const v128_t cmp = wasm_i8x16_eq(wasm_v128_load(v1), wasm_v128_load(v2));
19 // Bitmask is slow on AArch64, all_true is much faster.
20 if (!wasm_i8x16_all_true(cmp)) {
21 // Find the offset of the first zero bit (little-endian).
22 size_t ctz = __builtin_ctz(~wasm_i8x16_bitmask(cmp));
23 const unsigned char *u1 = (unsigned char *)v1 + ctz;
24 const unsigned char *u2 = (unsigned char *)v2 + ctz;
25 // This may help the compiler if the function is inlined.
26 __builtin_assume(*u1 - *u2 != 0);
27 return *u1 - *u2;
28 }
29 // This makes n a multiple of sizeof(v128_t)
30 // for every iteration except the first.
31 size_t align = (n - 1) % sizeof(v128_t) + 1;
32 v1 = (v128_t *)((char *)v1 + align);
33 v2 = (v128_t *)((char *)v2 + align);
34 n -= align;
35 }
36 return 0;
37 }
38 #endif
39
40 const unsigned char *l=vl, *r=vr;
41 for (; n && *l == *r; n--, l++, r++);
42 return n ? *l-*r : 0;
43 }