summaryrefslogtreecommitdiffstats
path: root/src/kmk/inlined_memchr.h
blob: 9666635671faeca2f7265f506c1c7ddb6c61d86d (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#define _GNU_SOURCE 1
#include <string.h>

#ifdef _MSC_VER
_inline void *
#else
static __inline__ void *
#endif
my_inline_memchr(const void *pv, int ch, register size_t cb)
{
    register const unsigned int   uch = (unsigned)ch;
    register const unsigned char *pb = (const unsigned char *)pv;
#if 0 /* 8-byte loop unroll */
    while (cb >= 8)
    {
        if (*pb == uch)
            return (unsigned char *)pb;
        if (pb[1] == uch)
            return (unsigned char *)pb + 1;
        if (pb[2] == uch)
            return (unsigned char *)pb + 2;
        if (pb[3] == uch)
            return (unsigned char *)pb + 3;
        if (pb[4] == uch)
            return (unsigned char *)pb + 4;
        if (pb[5] == uch)
            return (unsigned char *)pb + 5;
        if (pb[6] == uch)
            return (unsigned char *)pb + 6;
        if (pb[7] == uch)
            return (unsigned char *)pb + 7;
        cb -= 8;
        pb += 8;
    }
    switch (cb & 7)
    {
        case 0:
            break;
        case 1:
            if (*pb == uch)
                return (unsigned char *)pb;
            break;
        case 2:
            if (*pb == uch)
                return (unsigned char *)pb;
            if (pb[1] == uch)
                return (unsigned char *)pb + 1;
            break;
        case 3:
            if (*pb == uch)
                return (unsigned char *)pb;
            if (pb[1] == uch)
                return (unsigned char *)pb + 1;
            if (pb[2] == uch)
                return (unsigned char *)pb + 2;
            break;
        case 4:
            if (*pb == uch)
                return (unsigned char *)pb;
            if (pb[1] == uch)
                return (unsigned char *)pb + 1;
            if (pb[2] == uch)
                return (unsigned char *)pb + 2;
            if (pb[3] == uch)
                return (unsigned char *)pb + 3;
            break;
        case 5:
            if (*pb == uch)
                return (unsigned char *)pb;
            if (pb[1] == uch)
                return (unsigned char *)pb + 1;
            if (pb[2] == uch)
                return (unsigned char *)pb + 2;
            if (pb[3] == uch)
                return (unsigned char *)pb + 3;
            if (pb[4] == uch)
                return (unsigned char *)pb + 4;
            break;
        case 6:
            if (*pb == uch)
                return (unsigned char *)pb;
            if (pb[1] == uch)
                return (unsigned char *)pb + 1;
            if (pb[2] == uch)
                return (unsigned char *)pb + 2;
            if (pb[3] == uch)
                return (unsigned char *)pb + 3;
            if (pb[4] == uch)
                return (unsigned char *)pb + 4;
            if (pb[5] == uch)
                return (unsigned char *)pb + 5;
            break;
        case 7:
            if (*pb == uch)
                return (unsigned char *)pb;
            if (pb[1] == uch)
                return (unsigned char *)pb + 1;
            if (pb[2] == uch)
                return (unsigned char *)pb + 2;
            if (pb[3] == uch)
                return (unsigned char *)pb + 3;
            if (pb[4] == uch)
                return (unsigned char *)pb + 4;
            if (pb[5] == uch)
                return (unsigned char *)pb + 5;
            if (pb[6] == uch)
                return (unsigned char *)pb + 6;
            break;
    }

#elif 1 /* 4 byte loop unroll */
    while (cb >= 4)
    {
        if (*pb == uch)
            return (unsigned char *)pb;
        if (pb[1] == uch)
            return (unsigned char *)pb + 1;
        if (pb[2] == uch)
            return (unsigned char *)pb + 2;
        if (pb[3] == uch)
            return (unsigned char *)pb + 3;
        cb -= 4;
        pb += 4;
    }
    switch (cb & 3)
    {
        case 0:
            break;
        case 1:
            if (*pb == uch)
                return (unsigned char *)pb;
            break;
        case 2:
            if (*pb == uch)
                return (unsigned char *)pb;
            if (pb[1] == uch)
                return (unsigned char *)pb + 1;
            break;
        case 3:
            if (*pb == uch)
                return (unsigned char *)pb;
            if (pb[1] == uch)
                return (unsigned char *)pb + 1;
            if (pb[2] == uch)
                return (unsigned char *)pb + 2;
            break;
    }

#else /* the basic loop */
    while (cb > 0)
    {
        if (*pb == uch)
            return (void *)pb;
        cb--;
        pb++;
    }
#endif
    return 0;
}

#define memchr my_inline_memchr