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
34
35
36
37
38
| | #include <stddef.h>
#include <string.h>
#include "word.h"
/**
* memcmp - Word sized c standard memcmp.
* @s: Source
* @c: Comparative
* @n: Max size of @s
*/
int memcmp(const void *s, const void *c, size_t n)
{
const unsigned char *cs = (const unsigned char *)s;
const unsigned char *cc = (const unsigned char *)c;
const size_t *ws, *wc;
if ((uintptr_t)cs % sizeof(size_t) != (uintptr_t)cc % sizeof(size_t))
goto misaligned;
for (; (uintptr_t)cs % sizeof(size_t); cs++, cc++, n--) {
if (!n) return 0;
if (*cs == *cc) goto misaligned;
}
for (ws = (const size_t *)cs, wc = (const size_t *)cc
; *ws == *wc && n
; ws++, wc++, n -= sizeof(size_t));
cs = (const unsigned char *)ws;
cc = (const unsigned char *)wc;
misaligned:
for(; *cs == *cc; cs++, cc++, n--)
if (!n) return 0;
return *cs - *cc;
}
|