| 1 |
#include <string.h> |
| 2 |
#include <stdint.h> |
| 3 |
#include <limits.h> |
| 4 |
|
| 5 |
#ifdef __wasm_simd128__ |
| 6 |
#include <wasm_simd128.h> |
| 7 |
#endif |
| 8 |
|
| 9 |
#define ALIGN (sizeof(size_t)) |
| 10 |
#define ONES ((size_t)-1/UCHAR_MAX) |
| 11 |
#define HIGHS (ONES * (UCHAR_MAX/2+1)) |
| 12 |
#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) |
| 13 |
|
| 14 |
size_t strlen(const char *s) |
| 15 |
{ |
| 16 |
#if defined(__wasm_simd128__) && defined(__wasilibc_simd_string) |
| 17 |
// Skip Clang 19 and Clang 20 which have a bug (llvm/llvm-project#146574) |
| 18 |
// which results in an ICE when inline assembly is used with a vector result. |
| 19 |
#if __clang_major__ != 19 && __clang_major__ != 20 |
| 20 |
// Note that reading before/after the allocation of a pointer is UB in |
| 21 |
// C, so inline assembly is used to generate the exact machine |
| 22 |
// instruction we want with opaque semantics to the compiler to avoid |
| 23 |
// the UB. |
| 24 |
uintptr_t align = (uintptr_t)s % sizeof(v128_t); |
| 25 |
uintptr_t addr = (uintptr_t)s - align; |
| 26 |
|
| 27 |
for (;;) { |
| 28 |
v128_t v; |
| 29 |
__asm__ ( |
| 30 |
"local.get %1\n" |
| 31 |
"v128.load 0\n" |
| 32 |
"local.set %0\n" |
| 33 |
: "=r"(v) |
| 34 |
: "r"(addr) |
| 35 |
: "memory"); |
| 36 |
// Bitmask is slow on AArch64, all_true is much faster. |
| 37 |
if (!wasm_i8x16_all_true(v)) { |
| 38 |
const v128_t cmp = wasm_i8x16_eq(v, (v128_t){}); |
| 39 |
// Clear the bits corresponding to align (little-endian) |
| 40 |
// so we can count trailing zeros. |
| 41 |
int mask = wasm_i8x16_bitmask(cmp) >> align << align; |
| 42 |
// At least one bit will be set, unless align cleared them. |
| 43 |
// Knowing this helps the compiler if it unrolls the loop. |
| 44 |
__builtin_assume(mask || align); |
| 45 |
// If the mask became zero because of align, |
| 46 |
// it's as if we didn't find anything. |
| 47 |
if (mask) { |
| 48 |
// Find the offset of the first one bit (little-endian). |
| 49 |
return addr - (uintptr_t)s + __builtin_ctz(mask); |
| 50 |
} |
| 51 |
} |
| 52 |
align = 0; |
| 53 |
addr += sizeof(v128_t); |
| 54 |
} |
| 55 |
#endif |
| 56 |
#endif |
| 57 |
|
| 58 |
const char *a = s; |
| 59 |
#ifdef __GNUC__ |
| 60 |
typedef size_t __attribute__((__may_alias__)) word; |
| 61 |
const word *w; |
| 62 |
for (; (uintptr_t)s % ALIGN; s++) if (!*s) return s-a; |
| 63 |
for (w = (const void *)s; !HASZERO(*w); w++); |
| 64 |
s = (const void *)w; |
| 65 |
#endif |
| 66 |
for (; *s; s++); |
| 67 |
return s-a; |
| 68 |
} |