From 3bd11a0a86349e58c5de2dfacf0cebaebc1d6328 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Sat, 5 Apr 2025 11:15:16 +0100 Subject: [PATCH] More SIMD. --- sqlite3/libc/build.sh | 1 + sqlite3/libc/libc.wasm | Bin 858 -> 905 bytes sqlite3/libc/libc.wat | 240 ++++++++++++++++++++++---------------- sqlite3/libc/libc_test.go | 18 +++ sqlite3/strings.c | 58 +++++---- 5 files changed, 195 insertions(+), 122 deletions(-) diff --git a/sqlite3/libc/build.sh b/sqlite3/libc/build.sh index 3211d01..9dcd5be 100755 --- a/sqlite3/libc/build.sh +++ b/sqlite3/libc/build.sh @@ -20,6 +20,7 @@ trap 'rm -f libc.tmp' EXIT -Wl,--export=memset \ -Wl,--export=memcpy \ -Wl,--export=memcmp \ + -Wl,--export=strlen \ -Wl,--export=strcmp \ -Wl,--export=strncmp diff --git a/sqlite3/libc/libc.wasm b/sqlite3/libc/libc.wasm index ce4e30611e65b1cc26f4be82942621c92b7daade..1ed0412ff11723022580d149382a48375ab5b312 100755 GIT binary patch delta 360 zcmYk2u}T9$6h+^AGrNn~EG|e&@ntG4jNlKrQ$!F+A%eDvOAupr5jS9AF}8MwZTthV zva<3I`~?5OPgwBHBF5%%VeZ^>xnJR9vp9lht@IaA` zmOI;9^JxhxtQNOB6ShhK>_Z8Dk@fO+HMJ z(WWzlOrr$in8CV=an|=HuwF3kf*h|=EDZ4*5;?r5Y?aDzl!%G{$MZROiqXwUb1EP$ zJe^rsk6UIR1t4hfpQOfL1Hl<;sYV^@RgJcR)S~(w2gIuW#+Tkumy==&owfPZMLxLm UGwz4=D{XdVAN@N|P=C_X4UXYayYp(jcm$t(&;C=yJQu4p~s542nm5U!yu0fAU7j^+#=!QVln z(RjoKcFusAx0$^8+Nbm**@xMxLIA)mnwk&-hAC+@iPbDvMtG$3Bv~Kr^~>r8OuD*V z&h^C|#O#&r`tBusAyJ?Q3Ic*l-(B!X;1Ix(U_=LV!3G$1tw2j|PMfnRano1HMnuVI zq8#(cLpN{D!f{(!I4kIjD~AFK5F6FlW4sBu9RF*RJ0s+Oa3MzHL^tuM6{tWlj^dnF osXuX${ke6I-nNH4pH&x-ZCqcTmxFu#p95RJ(+ #include #include #include @@ -21,8 +22,6 @@ void *memmove(void *dest, const void *src, size_t n) { #ifdef __wasm_simd128__ -#define UNALIGNED(x) ((uintptr_t)x % sizeof(*x)) - int memcmp(const void *v1, const void *v2, size_t n) { const v128_t *w1 = v1; const v128_t *w2 = v2; @@ -44,20 +43,41 @@ int memcmp(const void *v1, const void *v2, size_t n) { return 0; } -int strcmp(const char *c1, const char *c2) { - const v128_t *w1 = (void *)c1; - const v128_t *w2 = (void *)c2; - if (!(UNALIGNED(w1) | UNALIGNED(w2))) { - while (true) { - if (wasm_v128_any_true(*w1 ^ *w2)) { - break; // *w1 != *w2 - } - if (!wasm_i8x16_all_true(*w1)) { - return 0; // *w1 == *w2 and have a NUL - } - w1++; - w2++; +size_t strlen(const char *s) { + const v128_t *const limit = + (v128_t *)(__builtin_wasm_memory_size(0) * PAGESIZE) - 1; + + const v128_t *w = (void *)s; + while (w <= limit) { + if (!wasm_i8x16_all_true(wasm_v128_load(w))) { + break; // *w has a NUL } + w++; + } + + const char *ss = (void *)w; + while (true) { + if (*ss == 0) break; + ss++; + } + return ss - s; +} + +int strcmp(const char *s1, const char *s2) { + const v128_t *const limit = + (v128_t *)(__builtin_wasm_memory_size(0) * PAGESIZE) - 1; + + const v128_t *w1 = (void *)s1; + const v128_t *w2 = (void *)s2; + while (w1 <= limit && w2 <= limit) { + if (wasm_v128_any_true(wasm_v128_load(w1) ^ wasm_v128_load(w2))) { + break; // *w1 != *w2 + } + if (!wasm_i8x16_all_true(wasm_v128_load(w1))) { + return 0; // *w1 == *w2 and have a NUL + } + w1++; + w2++; } const uint8_t *u1 = (void *)w1; @@ -71,9 +91,9 @@ int strcmp(const char *c1, const char *c2) { return 0; } -int strncmp(const char *c1, const char *c2, size_t n) { - const v128_t *w1 = (void *)c1; - const v128_t *w2 = (void *)c2; +int strncmp(const char *s1, const char *s2, size_t n) { + const v128_t *w1 = (void *)s1; + const v128_t *w2 = (void *)s2; for (; n >= sizeof(v128_t); n -= sizeof(v128_t)) { if (wasm_v128_any_true(wasm_v128_load(w1) ^ wasm_v128_load(w2))) { break; // *w1 != *w2 @@ -96,6 +116,4 @@ int strncmp(const char *c1, const char *c2, size_t n) { return 0; } -#undef UNALIGNED - #endif