mailing list of musl libc
 help / color / mirror / code / Atom feed
667498e1a608a5cc2a8181a7b16262dd9e7f9b41 blob 924 bytes (raw)

 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
33
 
#include <string.h>
#include <stdint.h>

#define aliases __attribute__((__may_alias__))
#define byte_repeat(x) ((size_t)~0 / 0xff * (x))
#define word_has_zero(x) (((x) - byte_repeat(0x01)) & ~(x) & byte_repeat(0x80))

int strncmp(const char *_l, const char *_r, size_t n)
{
	const unsigned char *l = (const void *)_l, *r = (const void *)_r;
	const size_t aliases *wl, aliases *wr;

	if (!n--) return 0;

	if (n < sizeof(size_t) * 3 || ((uintptr_t)l | (uintptr_t)r)
	    & sizeof(size_t) - 1) goto bytewise;

	for (; (uintptr_t)l & sizeof(size_t) - 1 && *l && *l == *r
	     ; l++, r++, n--);
	if ((uintptr_t)l & sizeof(size_t) - 1) return *l - *r;

	wl = (const void *)l;
	wr = (const void *)r;
	for (; n >= sizeof(size_t) && !word_has_zero(*wl) && *wl == *wr
	     ; wl++, wr++, n -= sizeof(size_t));
	l = (const void *)wl;
	r = (const void *)wr;

bytewise:
	for (; n && *l && *l == *r; l++, r++, n--);

	return *l - *r;
}
debug log:

solving 667498e1 ...
found 667498e1 in https://inbox.vuxu.org/musl/20170715195541.3136-2-nwmcsween@gmail.com/
found e228843f in https://git.vuxu.org/mirror/musl/
preparing index
index prepared:
100644 e228843f0bb394a209b764296e1df52fc8663344	src/string/strncmp.c

applying [1/1] https://inbox.vuxu.org/musl/20170715195541.3136-2-nwmcsween@gmail.com/
diff --git a/src/string/strncmp.c b/src/string/strncmp.c
index e228843f..667498e1 100644

Checking patch src/string/strncmp.c...
Applied patch src/string/strncmp.c cleanly.

index at:
100644 667498e1a608a5cc2a8181a7b16262dd9e7f9b41	src/string/strncmp.c

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).