Debian Codegraph @

simd/memchr_class.go

raw symbols SCIP
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 package simd
6
7 import (
8 "github.com/gofiber/utils/v2/swar"
9 )
10
11 // MemchrWord returns the index of the first word character — 'A'..'Z',
12 // 'a'..'z', '0'..'9', or '_' (the regex \w class) — in haystack, or -1 if
13 // none is present.
14 func MemchrWord(haystack []byte) int {
15 if len(haystack) == 0 {
16 return -1
17 }
18 return memchrWordImpl(haystack)
19 }
20
21 // MemchrNotWord is the complement of MemchrWord: it returns the index of the
22 // first byte that is NOT a word character, or -1 if every byte is one.
23 func MemchrNotWord(haystack []byte) int {
24 if len(haystack) == 0 {
25 return -1
26 }
27 return memchrNotWordImpl(haystack)
28 }
29
30 // MemchrInTable returns the index of the first byte b of haystack with
31 // table[b] true, or -1 if none matches or table is nil.
32 //
33 // This is a plain scalar table scan on every architecture — an arbitrary
34 // 256-entry membership test has no cheap SIMD or SWAR form — so unlike the
35 // rest of this package it carries no acceleration. For the \w class
36 // specifically, MemchrWord is much faster.
37 func MemchrInTable(haystack []byte, table *[256]bool) int {
38 if table == nil {
39 return -1
40 }
41 for i, b := range haystack {
42 if table[b] {
43 return i
44 }
45 }
46 return -1
47 }
48
49 // MemchrNotInTable is the complement of MemchrInTable: it returns the index
50 // of the first byte b with table[b] false, or -1 if every byte is in the
51 // table or table is nil. Like MemchrInTable it is a plain scalar loop on
52 // every architecture.
53 func MemchrNotInTable(haystack []byte, table *[256]bool) int {
54 if table == nil {
55 return -1
56 }
57 for i, b := range haystack {
58 if !table[b] {
59 return i
60 }
61 }
62 return -1
63 }
64
65 // isWordChar reports whether b is in [A-Za-z0-9_].
66 func isWordChar(b byte) bool {
67 return (b >= 'A' && b <= 'Z') ||
68 (b >= 'a' && b <= 'z') ||
69 (b >= '0' && b <= '9') ||
70 b == '_'
71 }
72
73 // wordMask flags the lanes of w holding word characters. The range masks
74 // are exact per lane and bytes >= 0x80 never match, so the composition is
75 // exact too.
76 func wordMask(w uint64) uint64 {
77 return swar.MatchRangeMask(w, 'A', 'Z') |
78 swar.MatchRangeMask(w, 'a', 'z') |
79 swar.MatchRangeMask(w, '0', '9') |
80 swar.MatchByteMask(w, '_')
81 }
82
83 // memchrWordGeneric is the portable SWAR fallback for MemchrWord, finishing
84 // 8+ byte inputs with one overlapping word at n-8.
85 func memchrWordGeneric(haystack []byte) int {
86 n := len(haystack)
87 if n < swar.WordLen {
88 for i := range n {
89 if isWordChar(haystack[i]) {
90 return i
91 }
92 }
93 return -1
94 }
95 i := 0
96 for ; i+2*swar.WordLen <= n; i += 2 * swar.WordLen {
97 // Pinned window: see the note above isASCIIGeneric in ascii.go for
98 // why the loads use constant offsets into it.
99 w := haystack[i : i+2*swar.WordLen : i+2*swar.WordLen]
100 m0 := wordMask(swar.Load8(w, 0))
101 m1 := wordMask(swar.Load8(w, swar.WordLen))
102 if m0|m1 != 0 {
103 if m0 != 0 {
104 return i + swar.FirstLane(m0)
105 }
106 return i + swar.WordLen + swar.FirstLane(m1)
107 }
108 }
109 for ; i+swar.WordLen <= n; i += swar.WordLen {
110 if m := wordMask(swar.Load8(haystack, i)); m != 0 {
111 return i + swar.FirstLane(m)
112 }
113 }
114 if i == n {
115 return -1
116 }
117 if m := wordMask(swar.Load8(haystack, n-swar.WordLen)); m != 0 {
118 return n - swar.WordLen + swar.FirstLane(m)
119 }
120 return -1
121 }
122
123 // memchrNotWordGeneric is the portable SWAR fallback for MemchrNotWord.
124 // wordMask is exact per lane, so its complement within the 0x80 lane
125 // markers is exact as well.
126 func memchrNotWordGeneric(haystack []byte) int {
127 n := len(haystack)
128 if n < swar.WordLen {
129 for i := range n {
130 if !isWordChar(haystack[i]) {
131 return i
132 }
133 }
134 return -1
135 }
136 i := 0
137 for ; i+2*swar.WordLen <= n; i += 2 * swar.WordLen {
138 w := haystack[i : i+2*swar.WordLen : i+2*swar.WordLen]
139 m0 := swar.HighBits &^ wordMask(swar.Load8(w, 0))
140 m1 := swar.HighBits &^ wordMask(swar.Load8(w, swar.WordLen))
141 if m0|m1 != 0 {
142 if m0 != 0 {
143 return i + swar.FirstLane(m0)
144 }
145 return i + swar.WordLen + swar.FirstLane(m1)
146 }
147 }
148 for ; i+swar.WordLen <= n; i += swar.WordLen {
149 if m := swar.HighBits &^ wordMask(swar.Load8(haystack, i)); m != 0 {
150 return i + swar.FirstLane(m)
151 }
152 }
153 if i == n {
154 return -1
155 }
156 if m := swar.HighBits &^ wordMask(swar.Load8(haystack, n-swar.WordLen)); m != 0 {
157 return n - swar.WordLen + swar.FirstLane(m)
158 }
159 return -1
160 }