From fc5ced209cb14e33fa5d082c2f96fc87112b9a6c Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Tue, 13 May 2025 14:27:26 +0100 Subject: [PATCH] Fix `bcmp`. --- embed/bcw2/build.sh | 12 +- embed/build.sh | 12 +- sqlite3/libc/build.sh | 12 +- sqlite3/libc/libc.wasm | Bin 5218 -> 5209 bytes sqlite3/libc/libc.wat | 270 ++++++++++++++--------------- sqlite3/libc/stdlib.h | 3 - sqlite3/libc/string.h | 47 ++++- sqlite3/libc/strings.h | 38 +--- util/sql3util/wasm/build.sh | 12 +- vfs/tests/mptest/wasm/build.sh | 12 +- vfs/tests/speedtest1/wasm/build.sh | 12 +- 11 files changed, 212 insertions(+), 218 deletions(-) diff --git a/embed/bcw2/build.sh b/embed/bcw2/build.sh index c2502db..0a1670a 100755 --- a/embed/bcw2/build.sh +++ b/embed/bcw2/build.sh @@ -46,9 +46,9 @@ cd ~- -o bcw2.wasm build/main.c \ -I"$ROOT/sqlite3/libc" -I"build" \ -mexec-model=reactor \ - -msimd128 -mmutable-globals -mmultivalue \ - -mbulk-memory -mreference-types \ - -mnontrapping-fptoint -msign-ext \ + -mmutable-globals -mnontrapping-fptoint \ + -msimd128 -mbulk-memory -msign-ext \ + -mreference-types -mmultivalue \ -fno-stack-protector -fno-stack-clash-protection \ -Wl,--stack-first \ -Wl,--import-undefined \ @@ -61,6 +61,6 @@ cd ~- "$BINARYEN/wasm-ctor-eval" -g -c _initialize bcw2.wasm -o bcw2.tmp "$BINARYEN/wasm-opt" -g --strip --strip-producers -c -O3 \ bcw2.tmp -o bcw2.wasm --low-memory-unused \ - --enable-simd --enable-mutable-globals --enable-multivalue \ - --enable-bulk-memory --enable-reference-types \ - --enable-nontrapping-float-to-int --enable-sign-ext \ No newline at end of file + --enable-mutable-globals --enable-nontrapping-float-to-int \ + --enable-simd --enable-bulk-memory --enable-sign-ext \ + --enable-reference-types --enable-multivalue \ No newline at end of file diff --git a/embed/build.sh b/embed/build.sh index 88e12be..c96a186 100755 --- a/embed/build.sh +++ b/embed/build.sh @@ -14,9 +14,9 @@ trap 'rm -f sqlite3.tmp' EXIT -o sqlite3.wasm "$ROOT/sqlite3/main.c" \ -I"$ROOT/sqlite3/libc" -I"$ROOT/sqlite3" \ -mexec-model=reactor \ - -msimd128 -mmutable-globals -mmultivalue \ - -mbulk-memory -mreference-types \ - -mnontrapping-fptoint -msign-ext \ + -mmutable-globals -mnontrapping-fptoint \ + -msimd128 -mbulk-memory -msign-ext \ + -mreference-types -mmultivalue \ -fno-stack-protector -fno-stack-clash-protection \ -Wl,--stack-first \ -Wl,--import-undefined \ @@ -28,6 +28,6 @@ trap 'rm -f sqlite3.tmp' EXIT "$BINARYEN/wasm-ctor-eval" -g -c _initialize sqlite3.wasm -o sqlite3.tmp "$BINARYEN/wasm-opt" -g --strip --strip-producers -c -O3 \ sqlite3.tmp -o sqlite3.wasm --low-memory-unused \ - --enable-simd --enable-mutable-globals --enable-multivalue \ - --enable-bulk-memory --enable-reference-types \ - --enable-nontrapping-float-to-int --enable-sign-ext \ No newline at end of file + --enable-mutable-globals --enable-nontrapping-float-to-int \ + --enable-simd --enable-bulk-memory --enable-sign-ext \ + --enable-reference-types --enable-multivalue \ No newline at end of file diff --git a/sqlite3/libc/build.sh b/sqlite3/libc/build.sh index 9893823..e28dd43 100755 --- a/sqlite3/libc/build.sh +++ b/sqlite3/libc/build.sh @@ -19,9 +19,9 @@ EOF -Wall -Wextra -Wno-unused-parameter -Wno-unused-function \ -o libc.wasm -I. "$SRCS" \ -mexec-model=reactor \ - -msimd128 -mmutable-globals -mmultivalue \ - -mbulk-memory -mreference-types \ - -mnontrapping-fptoint -msign-ext \ + -mmutable-globals -mnontrapping-fptoint \ + -msimd128 -mbulk-memory -msign-ext \ + -mreference-types -mmultivalue \ -fno-stack-protector -fno-stack-clash-protection \ -Wl,-z,stack-size=4096 \ -Wl,--stack-first \ @@ -54,8 +54,8 @@ EOF "$BINARYEN/wasm-ctor-eval" -g -c _initialize libc.wasm -o libc.tmp "$BINARYEN/wasm-opt" -g --strip --strip-producers -c -O3 \ libc.tmp -o libc.wasm \ - --enable-simd --enable-mutable-globals --enable-multivalue \ - --enable-bulk-memory --enable-reference-types \ - --enable-nontrapping-float-to-int --enable-sign-ext + --enable-mutable-globals --enable-nontrapping-float-to-int \ + --enable-simd --enable-bulk-memory --enable-sign-ext \ + --enable-reference-types --enable-multivalue "$BINARYEN/wasm-dis" -o libc.wat libc.wasm \ No newline at end of file diff --git a/sqlite3/libc/libc.wasm b/sqlite3/libc/libc.wasm index 1fa577ae131e06b67fb164c937a514a4c9beb5e4..3e5583b918c76d1a3b77aa1b8ccc361ea22c6ea8 100755 GIT binary patch delta 341 zcmXYsF;2uV5Jhb#cH-FHfM^gB(s!zD5ET+-T&%JwD9~PDOXRJfp`geeh{O@7k@hsS z+#?b*3(Z&_&wKOzv;JOxZKZraj`Fj){wS57$~UMRA!36f$wOC1qxzypGkbq!>&#CT z>iO2=vRT@AL1U{NTc{-S2|6F=y&oqCxToBCZd=vkY*Uhj*{I1^EkX!buFK=H3knr9 zEllk*5naeMX+ejDMmTgj2F3vQzps-Z zT&8J=Wzo~coT^7jX$}9alP?>}BBB271BDhW6)er2M*R?!FB}ROZkU0BIf&>T{IOl_ F%rDw!G%)}G delta 350 zcmXAly-ve06os!H+e!S>7BNu?={Qpt#Dvr_9;&nhLZ~mm21+F)SQ+vV85!7^k@_^O zyhkLi+vQlk_BrRXe^-C2pREv|$DR1L*I!EdL482p5Jin5i9=(fk!{gJkKQk9+vlSa zcD(ht(9=9#^0iqX=a5PC -#include - #include_next // the system stdlib.h #ifdef __cplusplus diff --git a/sqlite3/libc/string.h b/sqlite3/libc/string.h index 04764f8..7a1e857 100644 --- a/sqlite3/libc/string.h +++ b/sqlite3/libc/string.h @@ -1,9 +1,7 @@ #ifndef _WASM_SIMD128_STRING_H #define _WASM_SIMD128_STRING_H -#include #include -#include #include #include <__macro_PAGESIZE.h> @@ -82,6 +80,49 @@ int memcmp(const void *v1, const void *v2, size_t n) { return 0; } +#ifdef __OPTIMIZE_SIZE__ + +// __memcmpeq is the same as memcmp but only compares for equality. + +#define __memcmpeq(v1, v2, n) memcmp(v1, v2, n) + +#else // __OPTIMIZE_SIZE__ + +static int __memcmpeq(const void *v1, const void *v2, size_t n) { + // Baseline algorithm. + if (n < sizeof(v128_t)) { + const unsigned char *u1 = (unsigned char *)v1; + const unsigned char *u2 = (unsigned char *)v2; + while (n--) { + if (*u1 != *u2) return 1; + u1++; + u2++; + } + return 0; + } + + // memcmpeq is allowed to read up to n bytes from each object. + // Unaligned loads handle the case where the objects + // have mismatching alignments. + const v128_t *w1 = (v128_t *)v1; + const v128_t *w2 = (v128_t *)v2; + while (n) { + // Find any single bit difference. + if (wasm_v128_any_true(wasm_v128_load(w1) ^ wasm_v128_load(w2))) { + return 1; + } + // This makes n a multiple of sizeof(v128_t) + // for every iteration except the first. + size_t align = (n - 1) % sizeof(v128_t) + 1; + w1 = (v128_t *)((char *)w1 + align); + w2 = (v128_t *)((char *)w2 + align); + n -= align; + } + return 0; +} + +#endif // __OPTIMIZE_SIZE__ + __attribute__((weak)) void *memchr(const void *v, int c, size_t n) { // When n is zero, a function that locates a character finds no occurrence. @@ -510,7 +551,7 @@ static const char *__memmem_raita(const char *haystk, size_t sh, // Each iteration clears that bit, tries again. for (uint32_t mask = wasm_i8x16_bitmask(cmp); mask; mask &= mask - 1) { size_t ctz = __builtin_ctz(mask); - if (!bcmp(haystk + ctz + 1, needle + 1, sn - 1)) { + if (!__memcmpeq(haystk + ctz + 1, needle + 1, sn - 1)) { return haystk + ctz; } } diff --git a/sqlite3/libc/strings.h b/sqlite3/libc/strings.h index 811400d..373a33a 100644 --- a/sqlite3/libc/strings.h +++ b/sqlite3/libc/strings.h @@ -1,8 +1,7 @@ #ifndef _WASM_SIMD128_STRINGS_H #define _WASM_SIMD128_STRINGS_H -#include -#include +#include #include_next // the system strings.h @@ -11,45 +10,12 @@ extern "C" { #endif #ifdef __wasm_simd128__ -#ifndef __OPTIMIZE_SIZE__ __attribute__((weak)) int bcmp(const void *v1, const void *v2, size_t n) { - // bcmp is the same as memcmp but only compares for equality. - - // Baseline algorithm. - if (n < sizeof(v128_t)) { - const unsigned char *u1 = (unsigned char *)v1; - const unsigned char *u2 = (unsigned char *)v2; - while (n--) { - if (*u1 != *u2) return 1; - u1++; - u2++; - } - return 0; - } - - // bcmp is allowed to read up to n bytes from each object. - // Unaligned loads handle the case where the objects - // have mismatching alignments. - const v128_t *w1 = (v128_t *)v1; - const v128_t *w2 = (v128_t *)v2; - while (n) { - // Find any single bit difference. - if (wasm_v128_any_true(wasm_v128_load(w1) ^ wasm_v128_load(w2))) { - return 1; - } - // This makes n a multiple of sizeof(v128_t) - // for every iteration except the first. - size_t align = (n - 1) % sizeof(v128_t) + 1; - w1 = (v128_t *)((char *)w1 + align); - w2 = (v128_t *)((char *)w2 + align); - n -= align; - } - return 0; + return __memcmpeq(v1, v2, n); } -#endif // __OPTIMIZE_SIZE__ #endif // __wasm_simd128__ #ifdef __cplusplus diff --git a/util/sql3util/wasm/build.sh b/util/sql3util/wasm/build.sh index e445a5b..4146943 100755 --- a/util/sql3util/wasm/build.sh +++ b/util/sql3util/wasm/build.sh @@ -14,9 +14,9 @@ trap 'rm -f sql3parse_table.tmp' EXIT -o sql3parse_table.wasm main.c \ -I"$ROOT/sqlite3/libc" -I"$ROOT/sqlite3" \ -mexec-model=reactor \ - -msimd128 -mmutable-globals -mmultivalue \ - -mbulk-memory -mreference-types \ - -mnontrapping-fptoint -msign-ext \ + -mmutable-globals -mnontrapping-fptoint \ + -msimd128 -mbulk-memory -msign-ext \ + -mreference-types -mmultivalue \ -fno-stack-protector -fno-stack-clash-protection \ -Wl,--stack-first \ -Wl,--import-undefined \ @@ -25,6 +25,6 @@ trap 'rm -f sql3parse_table.tmp' EXIT "$BINARYEN/wasm-ctor-eval" -c _initialize sql3parse_table.wasm -o sql3parse_table.tmp "$BINARYEN/wasm-opt" --strip --strip-debug --strip-producers -c -Oz \ sql3parse_table.tmp -o sql3parse_table.wasm --low-memory-unused \ - --enable-simd --enable-mutable-globals --enable-multivalue \ - --enable-bulk-memory --enable-reference-types \ - --enable-nontrapping-float-to-int --enable-sign-ext \ No newline at end of file + --enable-mutable-globals --enable-nontrapping-float-to-int \ + --enable-simd --enable-bulk-memory --enable-sign-ext \ + --enable-reference-types --enable-multivalue \ No newline at end of file diff --git a/vfs/tests/mptest/wasm/build.sh b/vfs/tests/mptest/wasm/build.sh index 1596c60..151c8cc 100755 --- a/vfs/tests/mptest/wasm/build.sh +++ b/vfs/tests/mptest/wasm/build.sh @@ -10,9 +10,9 @@ WASI_SDK="$ROOT/tools/wasi-sdk/bin" "$WASI_SDK/clang" --target=wasm32-wasi -std=c23 -g0 -O2 \ -o mptest.wasm main.c \ -I"$ROOT/sqlite3/libc" -I"$ROOT/sqlite3" \ - -msimd128 -mmutable-globals -mmultivalue \ - -mbulk-memory -mreference-types \ - -mnontrapping-fptoint -msign-ext \ + -mmutable-globals -mnontrapping-fptoint \ + -msimd128 -mbulk-memory -msign-ext \ + -mreference-types -mmultivalue \ -fno-stack-protector -fno-stack-clash-protection \ -Wl,--stack-first \ -Wl,--import-undefined \ @@ -27,7 +27,7 @@ WASI_SDK="$ROOT/tools/wasi-sdk/bin" "$BINARYEN/wasm-opt" -g --strip --strip-producers -c -O3 \ mptest.wasm -o mptest.tmp --low-memory-unused \ - --enable-simd --enable-mutable-globals --enable-multivalue \ - --enable-bulk-memory --enable-reference-types \ - --enable-nontrapping-float-to-int --enable-sign-ext + --enable-mutable-globals --enable-nontrapping-float-to-int \ + --enable-simd --enable-bulk-memory --enable-sign-ext \ + --enable-reference-types --enable-multivalue mv mptest.tmp mptest.wasm \ No newline at end of file diff --git a/vfs/tests/speedtest1/wasm/build.sh b/vfs/tests/speedtest1/wasm/build.sh index 0a22ee9..1b2d548 100755 --- a/vfs/tests/speedtest1/wasm/build.sh +++ b/vfs/tests/speedtest1/wasm/build.sh @@ -10,9 +10,9 @@ WASI_SDK="$ROOT/tools/wasi-sdk/bin" "$WASI_SDK/clang" --target=wasm32-wasi -std=c23 -g0 -O2 \ -o speedtest1.wasm main.c \ -I"$ROOT/sqlite3/libc" -I"$ROOT/sqlite3" \ - -msimd128 -mmutable-globals -mmultivalue \ - -mbulk-memory -mreference-types \ - -mnontrapping-fptoint -msign-ext \ + -mmutable-globals -mnontrapping-fptoint \ + -msimd128 -mbulk-memory -msign-ext \ + -mreference-types -mmultivalue \ -fno-stack-protector -fno-stack-clash-protection \ -Wl,--stack-first \ -Wl,--import-undefined \ @@ -22,7 +22,7 @@ WASI_SDK="$ROOT/tools/wasi-sdk/bin" "$BINARYEN/wasm-opt" -g --strip --strip-producers -c -O3 \ speedtest1.wasm -o speedtest1.tmp --low-memory-unused \ - --enable-simd --enable-mutable-globals --enable-multivalue \ - --enable-bulk-memory --enable-reference-types \ - --enable-nontrapping-float-to-int --enable-sign-ext + --enable-mutable-globals --enable-nontrapping-float-to-int \ + --enable-simd --enable-bulk-memory --enable-sign-ext \ + --enable-reference-types --enable-multivalue mv speedtest1.tmp speedtest1.wasm \ No newline at end of file