| 1 |
// Package swar provides SWAR (SIMD within a register) primitives for |
| 2 |
// processing eight ASCII bytes per uint64 word. The building blocks here are |
| 3 |
// the ones the rest of gofiber/utils composes into case conversion, |
| 4 |
// multi-needle scanning, and validation loops, and they are exported so that |
| 5 |
// downstream packages (fiber itself, middleware) can fuse their own scans — |
| 6 |
// e.g. finding a delimiter while simultaneously classifying the bytes before |
| 7 |
// it — without re-deriving the bit tricks. |
| 8 |
// |
| 9 |
// Every operation yields per-lane results (exactly for the Match* masks, |
| 10 |
// approximately for ZeroLanes, whose contract allows false positives above |
| 11 |
// the first hit), so results are identical on little- and big-endian |
| 12 |
// platforms. Load8 and Store8 always use little-endian lane order: lane 0 |
| 13 |
// (the least significant byte) is the lowest byte index, which is what |
| 14 |
// FirstLane and LastLane assume. The empty-mask sentinels differ on |
| 15 |
// purpose: FirstLane(0) == 8 lets forward scans step past the current word |
| 16 |
// arithmetically, while LastLane(0) == -1 is the conventional not-found |
| 17 |
// result for reverse scans. |
| 18 |
// |
| 19 |
// No unsafe is used anywhere in this package. |
| 20 |
package swar |
| 21 |
|
| 22 |
import ( |
| 23 |
"math/bits" |
| 24 |
) |
| 25 |
|
| 26 |
const ( |
| 27 |
// WordLen is the SWAR word width in bytes. Word loops should iterate |
| 28 |
// while i+WordLen <= len(s) and route shorter inputs to scalar code. |
| 29 |
WordLen = 8 |
| 30 |
|
| 31 |
// Ones has 0x01 in every byte lane. |
| 32 |
Ones = 0x0101010101010101 |
| 33 |
// HighBits has 0x80 in every byte lane; w&HighBits != 0 iff some byte |
| 34 |
// has its high bit set, and Match* masks equal HighBits iff every lane |
| 35 |
// matched. |
| 36 |
HighBits = 0x8080808080808080 |
| 37 |
// LowSeven has 0x7F in every byte lane. |
| 38 |
LowSeven = 0x7f7f7f7f7f7f7f7f |
| 39 |
) |
| 40 |
|
| 41 |
// Load8 assembles s[i:i+8] into a little-endian uint64: s[i] becomes the |
| 42 |
// least significant byte (lane 0). The caller must guarantee i+8 <= len(s); |
| 43 |
// the reslice below turns a violation into a bounds panic. The 8-byte |
| 44 |
// reslice pins the length so the constant-index reads are bounds-check free, |
| 45 |
// and the compiler fuses them into a single 8-byte load on little-endian |
| 46 |
// targets (verified for both the string and []byte instantiations). |
| 47 |
// |
| 48 |
// The reslice itself is still checked, against the backing array's |
| 49 |
// capacity, which a loop condition written in terms of len does not imply. |
| 50 |
// A loop that loads several words per iteration should therefore pin the |
| 51 |
// whole group once and index into it with constants rather than call Load8 |
| 52 |
// at a variable offset each time: |
| 53 |
// |
| 54 |
// for ; i+4*WordLen <= n; i += 4 * WordLen { |
| 55 |
// w := b[i : i+4*WordLen : i+4*WordLen] |
| 56 |
// acc := Load8(w, 0) | Load8(w, WordLen) | Load8(w, 2*WordLen) | Load8(w, 3*WordLen) |
| 57 |
// // ... |
| 58 |
// } |
| 59 |
// |
| 60 |
// That moves four checks and their empty-slice pointer clamps down to one |
| 61 |
// per group; on []byte the three-index form pins the capacity too and |
| 62 |
// removes the rest. It is worth 20-60% on the word loops in package simd. |
| 63 |
func Load8[S ~string | ~[]byte](s S, i int) uint64 { |
| 64 |
w := s[i : i+8] |
| 65 |
return uint64(w[0]) | |
| 66 |
uint64(w[1])<<8 | |
| 67 |
uint64(w[2])<<16 | |
| 68 |
uint64(w[3])<<24 | |
| 69 |
uint64(w[4])<<32 | |
| 70 |
uint64(w[5])<<40 | |
| 71 |
uint64(w[6])<<48 | |
| 72 |
uint64(w[7])<<56 |
| 73 |
} |
| 74 |
|
| 75 |
// Store8 writes w into b[i:i+8] in Load8's lane order: lane 0 (the least |
| 76 |
// significant byte) lands at the lowest byte index, so a Load8/Store8 round |
| 77 |
// trip is the identity. The caller must guarantee i+8 <= len(b); the |
| 78 |
// reslice below turns a violation into a bounds panic. Like Load8, the |
| 79 |
// constant-index writes are bounds-check free and fuse into a single 8-byte |
| 80 |
// store on little-endian targets. |
| 81 |
func Store8(b []byte, i int, w uint64) { |
| 82 |
d := b[i : i+8] |
| 83 |
d[0] = byte(w) |
| 84 |
d[1] = byte(w >> 8) |
| 85 |
d[2] = byte(w >> 16) |
| 86 |
d[3] = byte(w >> 24) |
| 87 |
d[4] = byte(w >> 32) |
| 88 |
d[5] = byte(w >> 40) |
| 89 |
d[6] = byte(w >> 48) |
| 90 |
d[7] = byte(w >> 56) |
| 91 |
} |
| 92 |
|
| 93 |
// Broadcast returns a word with c in every byte lane, the needle form that |
| 94 |
// ZeroLanes scans consume. Hoist the result out of word loops; it compiles |
| 95 |
// to a single multiply. |
| 96 |
func Broadcast(c byte) uint64 { |
| 97 |
return uint64(c) * Ones |
| 98 |
} |
| 99 |
|
| 100 |
// ToLowerWord lower-cases every 'A'..'Z' lane in w. All other bytes, |
| 101 |
// including those >= 0x80, pass through unchanged — bit-identical to the |
| 102 |
// ASCII case-conversion tables used elsewhere in this module. |
| 103 |
func ToLowerWord(w uint64) uint64 { |
| 104 |
return w | MatchRangeMask(w, 'A', 'Z')>>2 // 0x80>>2 == 0x20: set bit 5 |
| 105 |
} |
| 106 |
|
| 107 |
// ToUpperWord upper-cases every 'a'..'z' lane in w. All other bytes, |
| 108 |
// including those >= 0x80, pass through unchanged. |
| 109 |
func ToUpperWord(w uint64) uint64 { |
| 110 |
return w &^ (MatchRangeMask(w, 'a', 'z') >> 2) // clear bit 5 on matched lanes |
| 111 |
} |
| 112 |
|
| 113 |
// ZeroLanes returns a mask flagging the zero-byte lanes of x. Three |
| 114 |
// properties are part of the contract: the mask is zero iff x has no zero |
| 115 |
// byte; the lowest set lane is always the lowest true zero lane; and every |
| 116 |
// true zero lane has its high bit set — a 0x00 lane leaves the subtraction |
| 117 |
// as 0xFF or 0xFE regardless of borrow-in, and &^ x keeps its high bit — |
| 118 |
// so combining masks (OR for multi-needle scans, AND for pair scans, as |
| 119 |
// simd.Memchr2 and simd.MemchrPair's fallbacks do) never loses a true |
| 120 |
// match. Lanes above the first true zero may carry false positives |
| 121 |
// (borrows propagate strictly upward), so anything beyond the lowest set |
| 122 |
// lane must be re-verified. It is two ops cheaper than MatchByteMask, |
| 123 |
// which makes it the right primitive for first-match scans, typically as |
| 124 |
// ZeroLanes(w ^ Broadcast(c)) with the broadcast hoisted out of the word |
| 125 |
// loop. Use MatchByteMask when every lane must be exact. |
| 126 |
func ZeroLanes(x uint64) uint64 { |
| 127 |
return (x - Ones) &^ x & HighBits |
| 128 |
} |
| 129 |
|
| 130 |
// MatchByteMask returns a word with 0x80 set in exactly the lanes of w whose |
| 131 |
// byte equals c, and zero in all other lanes. The result is per-lane exact |
| 132 |
// (no false positives in lanes above a match), so it is safe to feed to |
| 133 |
// LastLane or to arbitrary lane masking, not just first-match scans; for |
| 134 |
// pure first-match scanning ZeroLanes is cheaper. |
| 135 |
func MatchByteMask(w uint64, c byte) uint64 { |
| 136 |
x := w ^ (uint64(c) * Ones) |
| 137 |
// Setting the high bit before the per-lane subtraction keeps every lane |
| 138 |
// >= 0x80, so borrows cannot cross lanes and the test stays exact: |
| 139 |
// after the subtraction the high bit is clear only where the lane was 0, |
| 140 |
// and OR-ing x back in rejects lanes that had their own high bit set. |
| 141 |
return ^(((x | HighBits) - Ones) | x) & HighBits |
| 142 |
} |
| 143 |
|
| 144 |
// MatchRangeMask returns a word with 0x80 set in exactly the lanes of w |
| 145 |
// whose byte is within [lo, hi]. It requires lo <= hi <= 0x7F; lanes with |
| 146 |
// the high bit set (bytes >= 0x80) never match. |
| 147 |
func MatchRangeMask(w uint64, lo, hi byte) uint64 { |
| 148 |
b := w & LowSeven |
| 149 |
// After masking, every lane is <= 0x7F, so the biased additions below |
| 150 |
// cannot carry into a neighboring lane. |
| 151 |
ge := b + (0x80-uint64(lo))*Ones // high bit set where lane >= lo |
| 152 |
gt := b + (0x80-uint64(hi)-1)*Ones // high bit set where lane > hi |
| 153 |
return ge &^ gt &^ w & HighBits |
| 154 |
} |
| 155 |
|
| 156 |
// FirstLane returns the index (0..7) of the lowest-addressed set lane in |
| 157 |
// mask — the smallest byte index, given Load8's lane order — or 8 if mask |
| 158 |
// is zero. mask must contain only 0x80 lane markers, as produced by the |
| 159 |
// Match* functions. |
| 160 |
func FirstLane(mask uint64) int { |
| 161 |
return bits.TrailingZeros64(mask) >> 3 |
| 162 |
} |
| 163 |
|
| 164 |
// LastLane returns the index (0..7) of the highest-addressed set lane in |
| 165 |
// mask, or -1 if mask is zero. mask must contain only 0x80 lane markers. |
| 166 |
func LastLane(mask uint64) int { |
| 167 |
return (63 - bits.LeadingZeros64(mask)) >> 3 |
| 168 |
} |