Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: fusl/src/string/memccpy.c

Issue 1573973002: Add a "fork" of musl as //fusl. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « fusl/src/string/index.c ('k') | fusl/src/string/memchr.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #include <string.h>
2 #include <stdint.h>
3 #include <limits.h>
4
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)
9
10 void *memccpy(void *restrict dest, const void *restrict src, int c, size_t n)
11 {
12 unsigned char *d = dest;
13 const unsigned char *s = src;
14 size_t *wd, k;
15 const size_t *ws;
16
17 c = (unsigned char)c;
18 if (((uintptr_t)s & ALIGN) == ((uintptr_t)d & ALIGN)) {
19 for (; ((uintptr_t)s & ALIGN) && n && (*d=*s)!=c; n--, s++, d++) ;
20 if ((uintptr_t)s & ALIGN) goto tail;
21 k = ONES * c;
22 wd=(void *)d; ws=(const void *)s;
23 for (; n>=sizeof(size_t) && !HASZERO(*ws^k);
24 n-=sizeof(size_t), ws++, wd++) *wd = *ws;
25 d=(void *)wd; s=(const void *)ws;
26 }
27 for (; n && (*d=*s)!=c; n--, s++, d++);
28 tail:
29 if (*s==c) return d+1;
30 return 0;
31 }
OLDNEW
« no previous file with comments | « fusl/src/string/index.c ('k') | fusl/src/string/memchr.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698