Skip to content

Commit 251a92f

Browse files
committed
Weak symbols.
1 parent f5206ea commit 251a92f

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

sqlite3/libc/libc.wat

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -964,11 +964,11 @@
964964
)
965965
)
966966
(v128.store
967-
(i32.const 65520)
967+
(i32.const 65568)
968968
(v128.const i32x4 0x00000000 0x00000000 0x00000000 0x00000000)
969969
)
970970
(v128.store
971-
(i32.const 65504)
971+
(i32.const 65552)
972972
(v128.const i32x4 0x00000000 0x00000000 0x00000000 0x00000000)
973973
)
974974
(local.set $1
@@ -988,7 +988,7 @@
988988
)
989989
(i32.const 28)
990990
)
991-
(i32.const 65504)
991+
(i32.const 65552)
992992
)
993993
)
994994
(i32.or
@@ -1046,7 +1046,7 @@
10461046
)
10471047
(i32.const 28)
10481048
)
1049-
(i32.const 65504)
1049+
(i32.const 65552)
10501050
)
10511051
)
10521052
(local.get $3)
@@ -1206,11 +1206,11 @@
12061206
)
12071207
)
12081208
(v128.store
1209-
(i32.const 65520)
1209+
(i32.const 65600)
12101210
(v128.const i32x4 0x00000000 0x00000000 0x00000000 0x00000000)
12111211
)
12121212
(v128.store
1213-
(i32.const 65504)
1213+
(i32.const 65584)
12141214
(v128.const i32x4 0x00000000 0x00000000 0x00000000 0x00000000)
12151215
)
12161216
(local.set $1
@@ -1230,7 +1230,7 @@
12301230
)
12311231
(i32.const 28)
12321232
)
1233-
(i32.const 65504)
1233+
(i32.const 65584)
12341234
)
12351235
)
12361236
(i32.or
@@ -1280,7 +1280,7 @@
12801280
)
12811281
(i32.const 28)
12821282
)
1283-
(i32.const 65504)
1283+
(i32.const 65584)
12841284
)
12851285
)
12861286
(local.get $2)

sqlite3/libc/stdlib.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ extern "C" {
1212

1313
// Shellsort with Gonnet & Baeza-Yates gap sequence.
1414
// Simple, no recursion, doesn't use the C stack.
15-
// Clang auto-vectorizes the inner loop.
15+
// Clang auto-vectorizes the inner loop.
1616

17+
__attribute__((weak))
1718
void qsort(void *base, size_t nel, size_t width,
1819
int (*comp)(const void *, const void *)) {
1920
if (width == 0) return;

sqlite3/libc/string.h

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <wasm_simd128.h>
77
#include <__macro_PAGESIZE.h>
88

9-
#include_next <string.h> // the system string.h
9+
#include_next <string.h> // the system string.h
1010

1111
#ifdef __cplusplus
1212
extern "C" {
@@ -18,14 +18,17 @@ extern "C" {
1818
// Clang will intrinsify using SIMD for small, constant N.
1919
// For everything else, this helps inlining.
2020

21+
__attribute__((weak))
2122
void *memset(void *dest, int c, size_t n) {
2223
return __builtin_memset(dest, c, n);
2324
}
2425

26+
__attribute__((weak))
2527
void *memcpy(void *restrict dest, const void *restrict src, size_t n) {
2628
return __builtin_memcpy(dest, src, n);
2729
}
2830

31+
__attribute__((weak))
2932
void *memmove(void *dest, const void *src, size_t n) {
3033
return __builtin_memmove(dest, src, n);
3134
}
@@ -43,6 +46,7 @@ void *memmove(void *dest, const void *src, size_t n) {
4346
// These also assume unaligned access is not painfully slow,
4447
// but that bitmask extraction is slow on AArch64.
4548

49+
__attribute__((weak))
4650
int memcmp(const void *v1, const void *v2, size_t n) {
4751
const v128_t *w1 = v1;
4852
const v128_t *w2 = v2;
@@ -64,6 +68,7 @@ int memcmp(const void *v1, const void *v2, size_t n) {
6468
return 0;
6569
}
6670

71+
__attribute__((weak))
6772
void *memchr(const void *v, int c, size_t n) {
6873
uintptr_t align = (uintptr_t)v % sizeof(v128_t);
6974
const v128_t *w = (void *)(v - align);
@@ -86,6 +91,7 @@ void *memchr(const void *v, int c, size_t n) {
8691
}
8792
}
8893

94+
__attribute__((weak))
8995
size_t strlen(const char *s) {
9096
uintptr_t align = (uintptr_t)s % sizeof(v128_t);
9197
const v128_t *w = (void *)(s - align);
@@ -104,6 +110,7 @@ size_t strlen(const char *s) {
104110
}
105111
}
106112

113+
__attribute__((weak))
107114
int strcmp(const char *s1, const char *s2) {
108115
const v128_t *const limit =
109116
(v128_t *)(__builtin_wasm_memory_size(0) * PAGESIZE) - 1;
@@ -132,6 +139,7 @@ int strcmp(const char *s1, const char *s2) {
132139
return 0;
133140
}
134141

142+
__attribute__((weak))
135143
int strncmp(const char *s1, const char *s2, size_t n) {
136144
const v128_t *const limit =
137145
(v128_t *)(__builtin_wasm_memory_size(0) * PAGESIZE) - 1;
@@ -160,7 +168,8 @@ int strncmp(const char *s1, const char *s2, size_t n) {
160168
return 0;
161169
}
162170

163-
char *strchrnul(const char *s, int c) {
171+
__attribute__((always_inline))
172+
static char *__strchrnul(const char *s, int c) {
164173
if (__builtin_constant_p(c) && (char)c == 0) {
165174
return (char *)s + strlen(s);
166175
}
@@ -183,18 +192,34 @@ char *strchrnul(const char *s, int c) {
183192
}
184193
}
185194

195+
__attribute__((weak))
196+
char *strchrnul(const char *s, int c) {
197+
return __strchrnul(s, c);
198+
}
199+
200+
__attribute__((weak))
186201
char *strchr(const char *s, int c) {
187-
char *r = strchrnul(s, c);
202+
char *r = __strchrnul(s, c);
188203
return *(char *)r == (char)c ? r : NULL;
189204
}
190205

206+
#pragma push_macro("STATIC")
191207
#pragma push_macro("BITOP")
192208

209+
// Avoid using the C stack.
210+
#ifndef _REENTRANT
211+
#define STATIC static
212+
#else
213+
#define STATIC
214+
#endif
215+
193216
#define BITOP(a, b, op) \
194217
((a)[(b) / (8 * sizeof(size_t))] op((size_t)1) \
195218
<< ((b) % (8 * sizeof(size_t))))
196219

220+
__attribute__((weak))
197221
size_t strspn(const char *s, const char *c) {
222+
STATIC size_t byteset[32 / sizeof(size_t)];
198223
const char *const a = s;
199224

200225
if (!c[0]) return 0;
@@ -216,25 +241,27 @@ size_t strspn(const char *s, const char *c) {
216241
return s - a;
217242
}
218243

219-
size_t byteset[32 / sizeof(size_t)] = {0};
220-
244+
memset(byteset, 0, sizeof(byteset));
221245
for (; *c && BITOP(byteset, *(uint8_t *)c, |=); c++);
222246
for (; *s && BITOP(byteset, *(uint8_t *)s, &); s++);
223247
return s - a;
224248
}
225249

250+
__attribute__((weak))
226251
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)];
229253
const char *const a = s;
230-
size_t byteset[32 / sizeof(size_t)] = {0};
231254

255+
if (!c[0] || !c[1]) return __strchrnul(s, *c) - s;
256+
257+
memset(byteset, 0, sizeof(byteset));
232258
for (; *c && BITOP(byteset, *(uint8_t *)c, |=); c++);
233259
for (; *s && !BITOP(byteset, *(uint8_t *)s, &); s++);
234260
return s - a;
235261
}
236262

237263
#pragma pop_macro("BITOP")
264+
#pragma pop_macro("STATIC")
238265

239266
#endif // __wasm_simd128__
240267

0 commit comments

Comments
 (0)