Skip to content

Commit 853dad4

Browse files
committed
Optional SIMD memchr
1 parent 553305f commit 853dad4

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

libc-top-half/musl/src/string/memchr.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
#include <stdint.h>
33
#include <limits.h>
44

5+
#ifdef __wasm_simd128__
6+
#include <wasm_simd128.h>
7+
#endif
8+
59
#define SS (sizeof(size_t))
610
#define ALIGN (sizeof(size_t)-1)
711
#define ONES ((size_t)-1/UCHAR_MAX)
@@ -10,6 +14,52 @@
1014

1115
void *memchr(const void *src, int c, size_t n)
1216
{
17+
#if defined(__wasm_simd128__) && defined(__wasilibc_simd_string)
18+
// When n is zero, a function that locates a character finds no occurrence.
19+
// Otherwise, decrement n to ensure sub_overflow overflows
20+
// when n would go equal-to-or-below zero.
21+
if (!n--) {
22+
return NULL;
23+
}
24+
25+
// memchr must behave as if it reads characters sequentially
26+
// and stops as soon as a match is found.
27+
// Aligning ensures loads beyond the first match are safe.
28+
// Casting through uintptr_t makes this implementation-defined,
29+
// rather than undefined behavior.
30+
uintptr_t align = (uintptr_t)src % sizeof(v128_t);
31+
const v128_t *v = (v128_t *)((uintptr_t)src - align);
32+
const v128_t vc = wasm_i8x16_splat(c);
33+
34+
for (;;) {
35+
const v128_t cmp = wasm_i8x16_eq(*v, vc);
36+
// Bitmask is slow on AArch64, any_true is much faster.
37+
if (wasm_v128_any_true(cmp)) {
38+
// Clear the bits corresponding to align (little-endian)
39+
// so we can count trailing zeros.
40+
int mask = wasm_i8x16_bitmask(cmp) >> align << align;
41+
// At least one bit will be set, unless align cleared them.
42+
// Knowing this helps the compiler if it unrolls the loop.
43+
__builtin_assume(mask || align);
44+
// If the mask became zero because of align,
45+
// it's as if we didn't find anything.
46+
if (mask) {
47+
// Find the offset of the first one bit (little-endian).
48+
// That's a match, unless it is beyond the end of the object.
49+
// Recall that we decremented n, so less-than-or-equal-to is correct.
50+
size_t ctz = __builtin_ctz(mask);
51+
return ctz - align <= n ? (char *)v + ctz : NULL;
52+
}
53+
}
54+
// Decrement n; if it overflows we're done.
55+
if (__builtin_sub_overflow(n, sizeof(v128_t) - align, &n)) {
56+
return NULL;
57+
}
58+
align = 0;
59+
v++;
60+
}
61+
#endif
62+
1363
const unsigned char *s = src;
1464
c = (unsigned char)c;
1565
#ifdef __GNUC__

test/src/misc/memchr.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//! add-flags.py(LDFLAGS): -Wl,--stack-first -Wl,--initial-memory=327680
2+
3+
#include <__macro_PAGESIZE.h>
4+
#include <stddef.h>
5+
#include <stdio.h>
6+
#include <string.h>
7+
8+
void test(char *ptr, size_t length, void *want) {
9+
void *got = memchr(ptr, 7, length);
10+
if (got != want) {
11+
printf("memchr(%p, 7, %lu) = %p, want %p\n", ptr, length, got, want);
12+
}
13+
}
14+
15+
int main(void) {
16+
char *const LIMIT = (char *)(__builtin_wasm_memory_size(0) * PAGESIZE);
17+
18+
for (size_t length = 0; length < 64; length++) {
19+
for (size_t alignment = 0; alignment < 24; alignment++) {
20+
for (ptrdiff_t pos = -2; pos < length + 2; pos++) {
21+
// Create a buffer with the given length, at a pointer with the given
22+
// alignment. Using the offset LIMIT - PAGESIZE - 8 means many buffers
23+
// will straddle a (Wasm, and likely OS) page boundary. Place the
24+
// character to find at every position in the buffer, including just
25+
// prior to it and after its end.
26+
char *ptr = LIMIT - PAGESIZE - 8 + alignment;
27+
memset(LIMIT - 2 * PAGESIZE, 0, 2 * PAGESIZE);
28+
memset(ptr, 5, length);
29+
ptr[pos] = 7;
30+
31+
// The first instance of the character is found.
32+
if (pos >= 0) ptr[pos + 2] = 7;
33+
34+
// The character is found if it's within range.
35+
test(ptr, length, 0 <= pos && pos < length ? &ptr[pos] : NULL);
36+
}
37+
}
38+
39+
// Ensure we never read past the end of memory.
40+
char *ptr = LIMIT - length;
41+
memset(LIMIT - 2 * PAGESIZE, 0, 2 * PAGESIZE);
42+
memset(ptr, 5, length);
43+
ptr[length - 1] = 7;
44+
45+
// Nothing found on an empty buffer.
46+
test(ptr, length, length != 0 ? &ptr[length - 1] : NULL);
47+
48+
// Test for length overflow.
49+
if (length > 0) test(ptr, SIZE_MAX, &ptr[length - 1]);
50+
}
51+
52+
return 0;
53+
}

0 commit comments

Comments
 (0)