5 #define ALIGN (sizeof(size_t)-1)
6 #define ONES ((size_t)-1/UCHAR_MAX)
7 #define HIGHS (ONES * (UCHAR_MAX/2+1))
8 #define HASZERO(x) ((x)-ONES & ~(x) & HIGHS)
10 void *memccpy(void *restrict dest
, const void *restrict src
, int c
, size_t n
)
12 unsigned char *d
= dest
;
13 const unsigned char *s
= src
;
17 typedef size_t __attribute__((__may_alias__
)) word
;
20 if (((uintptr_t)s
& ALIGN
) == ((uintptr_t)d
& ALIGN
)) {
21 for (; ((uintptr_t)s
& ALIGN
) && n
&& (*d
=*s
)!=c
; n
--, s
++, d
++);
22 if ((uintptr_t)s
& ALIGN
) goto tail
;
24 wd
=(void *)d
; ws
=(const void *)s
;
25 for (; n
>=sizeof(size_t) && !HASZERO(*ws
^k
);
26 n
-=sizeof(size_t), ws
++, wd
++) *wd
= *ws
;
27 d
=(void *)wd
; s
=(const void *)ws
;
30 for (; n
&& (*d
=*s
)!=c
; n
--, s
++, d
++);