Use only aligned loads for index funcs

Also obviates the necessity for safe slice wrappers
This commit is contained in:
Kovid Goyal
2024-02-11 06:10:33 +05:30
parent 31a5fcf297
commit f9fd6ffd46
3 changed files with 110 additions and 158 deletions

View File

@@ -4,7 +4,6 @@ package simdstring
import (
"runtime"
"unsafe"
"golang.org/x/sys/cpu"
)
@@ -14,78 +13,18 @@ var Have256bit = false
var VectorSize = 1
// Return the index at which either a or b first occurs in data. If neither is
// found -1 is returned. Can read upto VectorSize-1 bytes beyond the end of data.
var UnsafeIndexByte2 func(data []byte, a, b byte) int = index_byte2_scalar
// found -1 is returned.
var IndexByte2 func(data []byte, a, b byte) int = index_byte2_scalar
// Return the index at which either a or b first occurs in text. If neither is
// found -1 is returned. Can read upto VectorSize-1 bytes beyond the end of text.
var UnsafeIndexByte2String func(text string, a, b byte) int = index_byte2_string_scalar
func get_safe_slice(addr uintptr, mask, datalen int) (align_len, vecsafe_len int) {
vecsize := mask + 1
if datalen < vecsize {
return datalen, 0
}
align_len = vecsize - int(addr&uintptr(mask))
datalen -= align_len
vecsafe_len = datalen - datalen&mask
return
}
func get_safe_slice_with_cap(addr uintptr, mask, datalen, datacap int) (align_len, vecsafe_len int) {
if datacap > datalen+mask {
return 0, datalen
}
return get_safe_slice(addr, mask, datalen)
}
// Return the index at which either a or b first occurs in data. If neither is
// found -1 is returned.
func IndexByte2(data []byte, a, b byte) int {
align_len, vecsafe_len := get_safe_slice_with_cap(uintptr(unsafe.Pointer(&data)), VectorSize-1, len(data), cap(data))
if align_len > 0 {
if ans := index_byte2_scalar(data[:align_len], a, b); ans > -1 {
return ans
}
data = data[align_len:]
}
if vecsafe_len > 0 {
if ans := UnsafeIndexByte2(data[:vecsafe_len], a, b); ans > -1 {
return align_len + ans
}
data = data[vecsafe_len:]
}
if len(data) > 0 {
if ans := index_byte2_scalar(data, a, b); ans > -1 {
return ans + align_len + vecsafe_len
}
}
return -1
}
var IndexByte2String func(text string, a, b byte) int = index_byte2_string_scalar
// Return the index at which either a or b first occurs in data. If neither is
// found -1 is returned.
func IndexByte2String(data string, a, b byte) int {
align_len, vecsafe_len := get_safe_slice(uintptr(unsafe.Pointer(&data)), VectorSize-1, len(data))
if align_len > 0 {
if ans := index_byte2_string_scalar(data[:align_len], a, b); ans > -1 {
return ans
}
data = data[align_len:]
}
if vecsafe_len > 0 {
if ans := UnsafeIndexByte2String(data[:vecsafe_len], a, b); ans > -1 {
return align_len + ans
}
data = data[vecsafe_len:]
}
if len(data) > 0 {
if ans := index_byte2_string_scalar(data, a, b); ans > -1 {
return ans + align_len + vecsafe_len
}
}
return -1
}
// Return the index at which the first C0 byte is found or -1 when no such bytes are present.
var IndexC0 func(data []byte) int = index_c0_scalar
// Return the index at which the first C0 byte is found or -1 when no such bytes are present.
var IndexC0String func(data string) int = index_c0_string_scalar
func init() {
switch runtime.GOARCH {
@@ -99,12 +38,16 @@ func init() {
Have256bit = HasSIMD256Code
}
if Have256bit {
UnsafeIndexByte2 = index_byte2_asm_256
UnsafeIndexByte2String = index_byte2_string_asm_256
IndexByte2 = index_byte2_asm_256
IndexByte2String = index_byte2_string_asm_256
IndexC0 = index_c0_asm_256
IndexC0String = index_c0_string_asm_256
VectorSize = 32
} else if Have128bit {
UnsafeIndexByte2 = index_byte2_asm_128
UnsafeIndexByte2String = index_byte2_string_asm_128
IndexByte2 = index_byte2_asm_128
IndexByte2String = index_byte2_string_asm_128
IndexC0 = index_c0_asm_128
IndexC0String = index_c0_string_asm_128
VectorSize = 16
}
}