summaryrefslogtreecommitdiffstats
path: root/contrib/restricted/emscripten/system/lib/libc/musl/src/string/memcmp.c
blob: c93cf8cc97330a9d14cccc40c9303c710488bc22 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#if __EMSCRIPTEN__
#include <stdint.h>
#endif
#include <string.h>

int memcmp(const void *vl, const void *vr, size_t n)
{
	const unsigned char *l=vl, *r=vr;

// XXX EMSCRIPTEN: add an optimized version.
#if !defined(EMSCRIPTEN_OPTIMIZE_FOR_OZ) && !__has_feature(address_sanitizer)
	// If we have enough bytes, and everything is aligned, loop on words instead
	// of single bytes.
	if (n >= 4 && !((((uintptr_t)l) & 3) | (((uintptr_t)r) & 3))) {
		while (n >= 4) {
			if (*((uint32_t *)l) != *((uint32_t *)r)) {
				// Go to the single-byte loop to find the specific byte.
				break;
			}
			l += 4;
			r += 4;
			n -= 4;
		}
	}
#endif

#if defined(EMSCRIPTEN_OPTIMIZE_FOR_OZ)
#pragma clang loop unroll(disable)
#endif
	for (; n && *l == *r; n--, l++, r++);
	return n ? *l-*r : 0;
}