-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstatic_strlen.c
209 lines (179 loc) · 4.55 KB
/
static_strlen.c
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* static_strlen - example compile-time strlen implementation
* Copyright (C) 2014 Daniel Santos <daniel.santos@pobox.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "compiler.h"
static __always_inline int is_nonzero_const(int v) {
return __builtin_constant_p(v) && v;
}
#define _STATIC_STRLEN_DEC(s, i, n) \
do { \
i >>= 1; \
if (is_nonzero_const(s[i - 1])) \
goto add_ ## n; \
} while (0)
#define _STATIC_STRLEN_INC(s, i, n) \
do { \
add_ ## n: \
if (!s[i] && s[i - 1]) \
return i; \
if (is_nonzero_const(s[i + n - 1])) \
i += n; \
} while (0)
static __always_inline size_t static_strlen(const char *s) {
size_t i = 65536;
/* if non-const or const, but longer than 64k bytes, use rt strlen */
if (!__builtin_constant_p(*s) || is_nonzero_const(s[i - 1])) {
BUILD_BUG();
//return strlen(s);
}
_STATIC_STRLEN_DEC(s, i, 16384);
_STATIC_STRLEN_DEC(s, i, 8192);
_STATIC_STRLEN_DEC(s, i, 4096);
_STATIC_STRLEN_DEC(s, i, 2048);
_STATIC_STRLEN_DEC(s, i, 1024);
_STATIC_STRLEN_DEC(s, i, 512);
_STATIC_STRLEN_DEC(s, i, 256);
_STATIC_STRLEN_DEC(s, i, 128);
_STATIC_STRLEN_DEC(s, i, 64);
_STATIC_STRLEN_DEC(s, i, 32);
_STATIC_STRLEN_DEC(s, i, 16);
_STATIC_STRLEN_DEC(s, i, 8);
_STATIC_STRLEN_DEC(s, i, 4);
_STATIC_STRLEN_DEC(s, i, 2);
_STATIC_STRLEN_DEC(s, i, 1);
_STATIC_STRLEN_DEC(s, i, zero);
i >>= 1;
BUILD_BUG_ON(i != 0 || s[i] != 0);
return 0;
_STATIC_STRLEN_INC(s, i, 16384);
_STATIC_STRLEN_INC(s, i, 8192);
_STATIC_STRLEN_INC(s, i, 4096);
_STATIC_STRLEN_INC(s, i, 2048);
_STATIC_STRLEN_INC(s, i, 1024);
_STATIC_STRLEN_INC(s, i, 512);
_STATIC_STRLEN_INC(s, i, 256);
_STATIC_STRLEN_INC(s, i, 128);
_STATIC_STRLEN_INC(s, i, 64);
_STATIC_STRLEN_INC(s, i, 32);
_STATIC_STRLEN_INC(s, i, 16);
_STATIC_STRLEN_INC(s, i, 8);
_STATIC_STRLEN_INC(s, i, 4);
_STATIC_STRLEN_INC(s, i, 2);
_STATIC_STRLEN_INC(s, i, 1);
add_zero:
if (!s[i] && s[i - 1])
return i;
if (!s[0])
return 0;
BUILD_BUG_ON(s[0]);
return 0;
}
#if 0
/* smaller version not using macros for illustrative purposes */
static __always_inline size_t static_strlen(const char *s) {
size_t i = 512;
/* if non-const or const, but longer than 4096 bytes, use rt strlen */
if (!__builtin_constant_p(*s) || is_nonzero_const(s[i - 1]))
return strlen(s);
i >>= 1; // 256
if (is_nonzero_const(s[i - 1]))
goto add_128;
i >>= 1; // 128
if (is_nonzero_const(s[i - 1]))
goto add_64;
i >>= 1; // 64
if (is_nonzero_const(s[i - 1]))
goto add_32;
i >>= 1; // 32
if (is_nonzero_const(s[i - 1]))
goto add_16;
i >>= 1; // 16
if (is_nonzero_const(s[i - 1]))
goto add_8;
i >>= 1; // 8
if (is_nonzero_const(s[i - 1]))
goto add_4;
i >>= 1; // 4
if (is_nonzero_const(s[i - 1]))
goto add_2;
i >>= 1; // 2
if (is_nonzero_const(s[i - 1]))
goto add_1;
i >>= 1; // 1
if (is_nonzero_const(s[i - 1]))
goto add_zero;
i >>= 1;
BUILD_BUG_ON(i != 0 || s[i] != 0);
return 0;
add_128:
if (!s[i] && s[i - 1])
return i;
if (is_nonzero_const(s[i + 127]))
i += 128;
add_64:
if (!s[i] && s[i - 1])
return i;
if (is_nonzero_const(s[i + 63]))
i += 64;
add_32:
if (!s[i] && s[i - 1])
return i;
if (is_nonzero_const(s[i + 31]))
i += 32;
add_16:
if (!s[i] && s[i - 1])
return i;
if (is_nonzero_const(s[i + 15]))
i += 16;
add_8:
if (!s[i] && s[i - 1])
return i;
if (is_nonzero_const(s[i + 7]))
i += 8;
add_4:
if (!s[i] && s[i - 1])
return i;
if (is_nonzero_const(s[i + 3]))
i += 4;
add_2:
if (!s[i] && s[i - 1])
return i;
if (is_nonzero_const(s[i + 1]))
i += 2;
add_1:
if (!s[i] && s[i - 1])
return i;
if (is_nonzero_const(s[i]))
i += 1;
add_zero:
if (!s[i] && s[i - 1])
return i;
if (!s[0])
return 0;
BUILD_BUG_ON(s[0]);
return 0;
}
#endif
int main(int argc, char **argv) {
size_t ret;
ret = strlen("123456789abcdef0123456789abcde");
return ret;
}