xref: /haiku/src/system/libroot/posix/string/strcmp.c (revision 4fd62caa9acc437534c41bbb7d3fc9d53e915005)
1 /*
2  * Copyright 2008, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT license.
4  */
5 
6 
7 #include <stdbool.h>
8 #include <string.h>
9 #include <SupportDefs.h>
10 
11 
12 #define LACKS_ZERO_BYTE(value) \
13 	(((value - 0x01010101) & ~value & 0x80808080) == 0)
14 
15 int
16 strcmp(char const *a, char const *b)
17 {
18 	/* Make sure we don't pass page boundries on a or b when doing four byte
19 	   comparisons */
20 	if ((((addr_t)a) & 3) == 0 && (((addr_t)b) && 3) == 0) {
21 		uint32* a32 = (uint32*)a;
22 		uint32* b32 = (uint32*)b;
23 
24 		while (LACKS_ZERO_BYTE(*a32)) {
25 			int32 cmp = *a32++ - *b32++;
26 			if (cmp != 0)
27 				return cmp;
28 		}
29 		return *a32 - *b32;
30 	}
31 	while (true) {
32 		int cmp = (unsigned char)*a - (unsigned char)*b++;
33 		if (cmp != 0 || *a++ == '\0')
34 			return cmp;
35 	}
36 }
37