6
6
#include <wasm_simd128.h>
7
7
#include <__macro_PAGESIZE.h>
8
8
9
- #include_next <string.h> // the system string.h
9
+ #include_next <string.h> // the system string.h
10
10
11
11
#ifdef __cplusplus
12
12
extern "C" {
@@ -18,14 +18,17 @@ extern "C" {
18
18
// Clang will intrinsify using SIMD for small, constant N.
19
19
// For everything else, this helps inlining.
20
20
21
+ __attribute__((weak ))
21
22
void * memset (void * dest , int c , size_t n ) {
22
23
return __builtin_memset (dest , c , n );
23
24
}
24
25
26
+ __attribute__((weak ))
25
27
void * memcpy (void * restrict dest , const void * restrict src , size_t n ) {
26
28
return __builtin_memcpy (dest , src , n );
27
29
}
28
30
31
+ __attribute__((weak ))
29
32
void * memmove (void * dest , const void * src , size_t n ) {
30
33
return __builtin_memmove (dest , src , n );
31
34
}
@@ -43,6 +46,7 @@ void *memmove(void *dest, const void *src, size_t n) {
43
46
// These also assume unaligned access is not painfully slow,
44
47
// but that bitmask extraction is slow on AArch64.
45
48
49
+ __attribute__((weak ))
46
50
int memcmp (const void * v1 , const void * v2 , size_t n ) {
47
51
const v128_t * w1 = v1 ;
48
52
const v128_t * w2 = v2 ;
@@ -64,6 +68,7 @@ int memcmp(const void *v1, const void *v2, size_t n) {
64
68
return 0 ;
65
69
}
66
70
71
+ __attribute__((weak ))
67
72
void * memchr (const void * v , int c , size_t n ) {
68
73
uintptr_t align = (uintptr_t )v % sizeof (v128_t );
69
74
const v128_t * w = (void * )(v - align );
@@ -86,6 +91,7 @@ void *memchr(const void *v, int c, size_t n) {
86
91
}
87
92
}
88
93
94
+ __attribute__((weak ))
89
95
size_t strlen (const char * s ) {
90
96
uintptr_t align = (uintptr_t )s % sizeof (v128_t );
91
97
const v128_t * w = (void * )(s - align );
@@ -104,6 +110,7 @@ size_t strlen(const char *s) {
104
110
}
105
111
}
106
112
113
+ __attribute__((weak ))
107
114
int strcmp (const char * s1 , const char * s2 ) {
108
115
const v128_t * const limit =
109
116
(v128_t * )(__builtin_wasm_memory_size (0 ) * PAGESIZE ) - 1 ;
@@ -132,6 +139,7 @@ int strcmp(const char *s1, const char *s2) {
132
139
return 0 ;
133
140
}
134
141
142
+ __attribute__((weak ))
135
143
int strncmp (const char * s1 , const char * s2 , size_t n ) {
136
144
const v128_t * const limit =
137
145
(v128_t * )(__builtin_wasm_memory_size (0 ) * PAGESIZE ) - 1 ;
@@ -160,7 +168,8 @@ int strncmp(const char *s1, const char *s2, size_t n) {
160
168
return 0 ;
161
169
}
162
170
163
- char * strchrnul (const char * s , int c ) {
171
+ __attribute__((always_inline ))
172
+ static char * __strchrnul (const char * s , int c ) {
164
173
if (__builtin_constant_p (c ) && (char )c == 0 ) {
165
174
return (char * )s + strlen (s );
166
175
}
@@ -183,18 +192,34 @@ char *strchrnul(const char *s, int c) {
183
192
}
184
193
}
185
194
195
+ __attribute__((weak ))
196
+ char * strchrnul (const char * s , int c ) {
197
+ return __strchrnul (s , c );
198
+ }
199
+
200
+ __attribute__((weak ))
186
201
char * strchr (const char * s , int c ) {
187
- char * r = strchrnul (s , c );
202
+ char * r = __strchrnul (s , c );
188
203
return * (char * )r == (char )c ? r : NULL ;
189
204
}
190
205
206
+ #pragma push_macro("STATIC")
191
207
#pragma push_macro("BITOP")
192
208
209
+ // Avoid using the C stack.
210
+ #ifndef _REENTRANT
211
+ #define STATIC static
212
+ #else
213
+ #define STATIC
214
+ #endif
215
+
193
216
#define BITOP (a , b , op ) \
194
217
((a)[(b) / (8 * sizeof(size_t))] op((size_t)1) \
195
218
<< ((b) % (8 * sizeof(size_t))))
196
219
220
+ __attribute__((weak ))
197
221
size_t strspn (const char * s , const char * c ) {
222
+ STATIC size_t byteset [32 / sizeof (size_t )];
198
223
const char * const a = s ;
199
224
200
225
if (!c [0 ]) return 0 ;
@@ -216,25 +241,27 @@ size_t strspn(const char *s, const char *c) {
216
241
return s - a ;
217
242
}
218
243
219
- size_t byteset [32 / sizeof (size_t )] = {0 };
220
-
244
+ memset (byteset , 0 , sizeof (byteset ));
221
245
for (; * c && BITOP (byteset , * (uint8_t * )c , |=); c ++ );
222
246
for (; * s && BITOP (byteset , * (uint8_t * )s , & ); s ++ );
223
247
return s - a ;
224
248
}
225
249
250
+ __attribute__((weak ))
226
251
size_t strcspn (const char * s , const char * c ) {
227
- if (!c [0 ] || !c [1 ]) return strchrnul (s , * c ) - s ;
228
-
252
+ STATIC size_t byteset [32 / sizeof (size_t )];
229
253
const char * const a = s ;
230
- size_t byteset [32 / sizeof (size_t )] = {0 };
231
254
255
+ if (!c [0 ] || !c [1 ]) return __strchrnul (s , * c ) - s ;
256
+
257
+ memset (byteset , 0 , sizeof (byteset ));
232
258
for (; * c && BITOP (byteset , * (uint8_t * )c , |=); c ++ );
233
259
for (; * s && !BITOP (byteset , * (uint8_t * )s , & ); s ++ );
234
260
return s - a ;
235
261
}
236
262
237
263
#pragma pop_macro("BITOP")
264
+ #pragma pop_macro("STATIC")
238
265
239
266
#endif // __wasm_simd128__
240
267
0 commit comments