Debian Codegraph @

simd/memchr_class.go

raw
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, exactly per lane;
74 // bytes >= 0x80 never match. It is the swar.MatchRangeMask construction
75 // with the three range tests fused into one expression:
76 //
77 // - the ASCII gate (&^ w & HighBits) is applied once to the OR of the
78 // tests instead of once per test;
79 // - 'A'..'Z' and 'a'..'z' collapse into a single 'a'..'z' test on a
80 // case-folded copy (bit 5 set in every lane), which maps exactly the
81 // two letter ranges onto 0x61..0x7A;
82 // - '_' (0x5F) is the only byte besides DEL (0x7F) that folds to 0x7F,
83 // so "folded lane == 0x7F" is one biased add, and the DEL lanes are
84 // removed by w<<2, which moves each lane's bit 5 — clear for '_', set
85 // for DEL — into the bit-7 marker position.
86 //
87 // Every addition below adds a per-lane bias of at most 0x50 to lanes that
88 // are at most 0x7F, so no lane can carry into its neighbor and the bit-7
89 // markers stay exact. That is 15 ALU operations per word instead of 26
90 // for the four separate masks.
91 func wordMask(w uint64) uint64 {
92 b := w & swar.LowSeven
93 bf := b | 0x20*swar.Ones
94 letters := (bf + (0x80-'a')*swar.Ones) &^ (bf + (0x80-'z'-1)*swar.Ones)
95 digits := (b + (0x80-'0')*swar.Ones) &^ (b + (0x80-'9'-1)*swar.Ones)
96 underscore := (bf + swar.Ones) &^ (w << 2)
97 return (letters | digits | underscore) &^ w & swar.HighBits
98 }
99
100 // memchrWordGeneric is the portable SWAR fallback for MemchrWord, finishing
101 // 8+ byte inputs with one overlapping word at n-8.
102 func memchrWordGeneric(haystack []byte) int {
103 n := len(haystack)
104 if n < swar.WordLen {
105 for i := range n {
106 if isWordChar(haystack[i]) {
107 return i
108 }
109 }
110 return -1
111 }
112 i := 0
113 for ; i+2*swar.WordLen <= n; i += 2 * swar.WordLen {
114 // Pinned window: see the note above isASCIIGeneric in ascii.go for
115 // why the loads use constant offsets into it.
116 w := haystack[i : i+2*swar.WordLen : i+2*swar.WordLen]
117 m0 := wordMask(swar.Load8(w, 0))
118 m1 := wordMask(swar.Load8(w, swar.WordLen))
119 if m0|m1 != 0 {
120 if m0 != 0 {
121 return i + swar.FirstLane(m0)
122 }
123 return i + swar.WordLen + swar.FirstLane(m1)
124 }
125 }
126 for ; i+swar.WordLen <= n; i += swar.WordLen {
127 if m := wordMask(swar.Load8(haystack, i)); m != 0 {
128 return i + swar.FirstLane(m)
129 }
130 }
131 if i == n {
132 return -1
133 }
134 if m := wordMask(swar.Load8(haystack, n-swar.WordLen)); m != 0 {
135 return n - swar.WordLen + swar.FirstLane(m)
136 }
137 return -1
138 }
139
140 // memchrNotWordGeneric is the portable SWAR fallback for MemchrNotWord.
141 // wordMask is exact per lane, so its complement within the 0x80 lane
142 // markers is exact as well.
143 func memchrNotWordGeneric(haystack []byte) int {
144 n := len(haystack)
145 if n < swar.WordLen {
146 for i := range n {
147 if !isWordChar(haystack[i]) {
148 return i
149 }
150 }
151 return -1
152 }
153 i := 0
154 for ; i+2*swar.WordLen <= n; i += 2 * swar.WordLen {
155 w := haystack[i : i+2*swar.WordLen : i+2*swar.WordLen]
156 m0 := swar.HighBits &^ wordMask(swar.Load8(w, 0))
157 m1 := swar.HighBits &^ wordMask(swar.Load8(w, swar.WordLen))
158 if m0|m1 != 0 {
159 if m0 != 0 {
160 return i + swar.FirstLane(m0)
161 }
162 return i + swar.WordLen + swar.FirstLane(m1)
163 }
164 }
165 for ; i+swar.WordLen <= n; i += swar.WordLen {
166 if m := swar.HighBits &^ wordMask(swar.Load8(haystack, i)); m != 0 {
167 return i + swar.FirstLane(m)
168 }
169 }
170 if i == n {
171 return -1
172 }
173 if m := swar.HighBits &^ wordMask(swar.Load8(haystack, n-swar.WordLen)); m != 0 {
174 return n - swar.WordLen + swar.FirstLane(m)
175 }
176 return -1
177 }