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

Side by Side Diff: fusl/src/string/memchr.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/memccpy.c ('k') | fusl/src/string/memcmp.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 SS (sizeof(size_t))
6 #define ALIGN (sizeof(size_t)-1)
7 #define ONES ((size_t)-1/UCHAR_MAX)
8 #define HIGHS (ONES * (UCHAR_MAX/2+1))
9 #define HASZERO(x) ((x)-ONES & ~(x) & HIGHS)
10
11 void *memchr(const void *src, int c, size_t n)
12 {
13 const unsigned char *s = src;
14 c = (unsigned char)c;
15 for (; ((uintptr_t)s & ALIGN) && n && *s != c; s++, n--);
16 if (n && *s != c) {
17 const size_t *w;
18 size_t k = ONES * c;
19 for (w = (const void *)s; n>=SS && !HASZERO(*w^k); w++, n-=SS);
20 for (s = (const void *)w; n && *s != c; s++, n--);
21 }
22 return n ? (void *)s : 0;
23 }
OLDNEW
« no previous file with comments | « fusl/src/string/memccpy.c ('k') | fusl/src/string/memcmp.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698