| 1 |
// Adapted from the coregex project, https://github.com/coregx/coregex |
| 2 |
// (package simd), Copyright (c) 2025 Andrey Kolkov and contributors, |
| 3 |
// MIT License. See the LICENSE file in this directory for the full text. |
| 4 |
|
| 5 |
//go:build amd64 |
| 6 |
|
| 7 |
package simd |
| 8 |
|
| 9 |
// Assembly kernels implemented in memchr_amd64.s. Each consumes four |
| 10 |
// 32-byte AVX2 vectors per iteration (OR-ing the per-vector match masks so |
| 11 |
// a 128-byte block needs one extraction and one branch), drops to a |
| 12 |
// 32-byte loop for the remainder, and finishes the last 1-31 positions |
| 13 |
// with an overlapping rescan: memchr2 and memchr3 reload one 32-byte |
| 14 |
// vector ending at the buffer end, while memchrPair reloads both of its |
| 15 |
// windows — the byte1 side ending at len-offset, the byte2 side at the |
| 16 |
// buffer end. |
| 17 |
// |
| 18 |
//go:noescape |
| 19 |
func memchr2AVX2(haystack []byte, needle1, needle2 byte) int |
| 20 |
|
| 21 |
//go:noescape |
| 22 |
func memchr3AVX2(haystack []byte, needle1, needle2, needle3 byte) int |
| 23 |
|
| 24 |
//go:noescape |
| 25 |
func memchrPairAVX2(haystack []byte, byte1, byte2 byte, offset int) int |
| 26 |
|
| 27 |
// memchr2Impl dispatches to the AVX2 kernel for inputs large enough to amortize |
| 28 |
// the vector setup. The caller guarantees a non-empty haystack. |
| 29 |
func memchr2Impl(haystack []byte, needle1, needle2 byte) int { |
| 30 |
if hasAVX2 && len(haystack) >= MinLen { |
| 31 |
return memchr2AVX2(haystack, needle1, needle2) |
| 32 |
} |
| 33 |
return memchr2Generic(haystack, needle1, needle2) |
| 34 |
} |
| 35 |
|
| 36 |
// memchr3Impl dispatches like memchr2Impl. |
| 37 |
func memchr3Impl(haystack []byte, needle1, needle2, needle3 byte) int { |
| 38 |
if hasAVX2 && len(haystack) >= MinLen { |
| 39 |
return memchr3AVX2(haystack, needle1, needle2, needle3) |
| 40 |
} |
| 41 |
return memchr3Generic(haystack, needle1, needle2, needle3) |
| 42 |
} |
| 43 |
|
| 44 |
// memchrPairImpl dispatches to the AVX2 kernel when there is room for at least |
| 45 |
// one 32-byte vector of pair positions. The caller guarantees offset >= 1 |
| 46 |
// and len(haystack) > offset. |
| 47 |
func memchrPairImpl(haystack []byte, byte1, byte2 byte, offset int) int { |
| 48 |
if hasAVX2 && len(haystack) >= MinLen+offset { |
| 49 |
return memchrPairAVX2(haystack, byte1, byte2, offset) |
| 50 |
} |
| 51 |
return memchrPairGeneric(haystack, byte1, byte2, offset) |
| 52 |
} |