Debian Codegraph @

src/algo/runeindex_x86.go

raw
1 //go:build 386 || amd64 || arm64
2
3 package algo
4
5 import (
6 "bytes"
7 "unsafe"
8 )
9
10 // On these architectures a []rune is a little-endian array of 4-byte lanes, so
11 // an ASCII rune is the byte itself followed by three zero bytes at a 4-byte
12 // aligned offset. That lets the SIMD byte scanners run over the rune array
13 // directly: find the low byte, then confirm alignment and the three zeroes.
14 // A byte equal to the needle can also appear as the low byte of a multi-byte
15 // rune (0x0165 has low byte 'e'), which those two checks reject.
16
17 func runeBytes(runes []rune) []byte {
18 return unsafe.Slice((*byte)(unsafe.Pointer(unsafe.SliceData(runes))), len(runes)*4)
19 }
20
21 // indexAsciiRune returns the index of the first rune equal to b, or to its
22 // uppercase form when ignoring case, at or after rune index from.
23 func indexAsciiRune(runes []rune, caseSensitive bool, b byte, from int) int {
24 view := runeBytes(runes)
25 both := !caseSensitive && b >= 'a' && b <= 'z'
26 for off := from * 4; off < len(view); {
27 var idx int
28 if both {
29 idx = IndexByteTwo(view[off:], b, b-32)
30 } else {
31 idx = bytes.IndexByte(view[off:], b)
32 }
33 if idx < 0 {
34 return -1
35 }
36 pos := off + idx
37 if pos&3 == 0 && view[pos+1]|view[pos+2]|view[pos+3] == 0 {
38 return pos >> 2
39 }
40 off = pos + 1
41 }
42 return -1
43 }
44
45 // runeNeedle picks which of the rune's four bytes to scan for, and returns its
46 // lane index and value. A zero byte is a useless needle because every ASCII
47 // rune contributes three of them, so U+AE00 scanned by its low byte would hit
48 // on almost every character of an ASCII-heavy line. Prefer a byte that cannot
49 // occur in an ASCII rune at all, then any non-zero byte.
50 func runeNeedle(r rune) (int, byte) {
51 var b [4]byte
52 b[0], b[1], b[2], b[3] = byte(r), byte(r>>8), byte(r>>16), byte(r>>24)
53 for i, v := range b {
54 if v >= 0x80 {
55 return i, v
56 }
57 }
58 for i, v := range b {
59 if v != 0 {
60 return i, v
61 }
62 }
63 return 0, 0
64 }
65
66 func runeAt(view []byte, start int) rune {
67 return rune(view[start]) | rune(view[start+1])<<8 | rune(view[start+2])<<16 | rune(view[start+3])<<24
68 }
69
70 // indexRune returns the index of the first rune equal to r at or after rune
71 // index from. Case is not folded, so the caller must have established that no
72 // other rune can transform into r.
73 func indexRune(runes []rune, r rune, from int) int {
74 view := runeBytes(runes)
75 lane, needle := runeNeedle(r)
76 for off := from*4 + lane; off < len(view); {
77 idx := bytes.IndexByte(view[off:], needle)
78 if idx < 0 {
79 return -1
80 }
81 pos := off + idx
82 if start := pos - lane; start&3 == 0 && runeAt(view, start) == r {
83 return start >> 2
84 }
85 off = pos + 1
86 }
87 return -1
88 }
89
90 // lastIndexRune is indexRune scanning backwards from the end.
91 func lastIndexRune(runes []rune, r rune, from int) int {
92 view := runeBytes(runes)
93 lane, needle := runeNeedle(r)
94 for end := len(view); end > from*4+lane; {
95 idx := bytes.LastIndexByte(view[from*4+lane:end], needle)
96 if idx < 0 {
97 return -1
98 }
99 pos := from*4 + lane + idx
100 if start := pos - lane; start&3 == 0 && runeAt(view, start) == r {
101 return start >> 2
102 }
103 end = pos
104 }
105 return -1
106 }
107
108 // lastIndexAsciiRune is indexAsciiRune scanning backwards from the end.
109 func lastIndexAsciiRune(runes []rune, caseSensitive bool, b byte, from int) int {
110 view := runeBytes(runes)[from*4:]
111 both := !caseSensitive && b >= 'a' && b <= 'z'
112 for end := len(view); end > 0; {
113 var idx int
114 if both {
115 idx = lastIndexByteTwo(view[:end], b, b-32)
116 } else {
117 idx = bytes.LastIndexByte(view[:end], b)
118 }
119 if idx < 0 {
120 return -1
121 }
122 if idx&3 == 0 && view[idx+1]|view[idx+2]|view[idx+3] == 0 {
123 return from + idx>>2
124 }
125 end = idx
126 }
127 return -1
128 }