chore: import official kitty sources from upstream

Bring the kovidgoyal/kitty codebase in as the base of FWS-Kitty,
vendored at upstream commit 91fb38a without its git history.
This commit is contained in:
BlackAngelTV committed 2026-11-04 11:20:47 +01:00
commit 7e7d249099
1134 files changed
+433629

No files matched your search

+11
View File
@@ -0,0 +1,11 @@
#!/bin/sh
#
# test.sh
# Copyright (C) 2024 Kovid Goyal <kovid at kovidgoyal.net>
#
# Distributed under terms of the MIT license.
#
go run generate.go && go test -bench=.
+108
View File
@@ -0,0 +1,108 @@
// License: GPLv3 Copyright: 2024, Kovid Goyal, <kovid at kovidgoyal.net>
package simdstring
import (
"bytes"
"fmt"
"testing"
)
var _ = fmt.Print
func haystack(filler, needle byte, pos int) []byte {
var data []byte
if pos > 0 {
data = append(bytes.Repeat([]byte{filler}, pos-1), needle)
} else {
data = []byte{needle}
}
return data
}
var sizes = []int{6, 327, 9875, 1198673}
func BenchmarkIndexByte(b *testing.B) {
t := func(pos int, which string) {
data := haystack('a', 'q', pos)
f := IndexByte
switch which {
case "scalar":
f = index_byte_scalar
case "stdlib":
f = bytes.IndexByte
}
b.Run(fmt.Sprintf("%s_sz=%d", which, pos), func(b *testing.B) {
for b.Loop() {
f(data, 'q')
}
})
}
for _, pos := range sizes {
t(pos, "simdstring")
t(pos, "scalar")
t(pos, "stdlib")
}
}
func BenchmarkIndexByte2(b *testing.B) {
t := func(pos int, which string) {
data := haystack('a', 'q', pos)
f := IndexByte2
switch which {
case "scalar":
f = index_byte2_scalar
}
b.Run(fmt.Sprintf("%s_sz=%d", which, pos), func(b *testing.B) {
for b.Loop() {
f(data, 'q', 'x')
}
})
}
for _, pos := range sizes {
t(pos, "simdstring")
t(pos, "scalar")
}
}
func BenchmarkNotIndexByte(b *testing.B) {
t := func(pos int, which string) {
// Fill with 'a' and place 'q' (a non-matching byte) at the target position
data := haystack('a', 'q', pos)
f := NotIndexByte
switch which {
case "scalar":
f = not_index_byte_scalar
}
b.Run(fmt.Sprintf("%s_sz=%d", which, pos), func(b *testing.B) {
for b.Loop() {
f(data, 'a')
}
})
}
for _, pos := range sizes {
t(pos, "simdstring")
t(pos, "scalar")
}
}
func BenchmarkNotIndexByte2(b *testing.B) {
t := func(pos int, which string) {
// Fill with 'a' and place 'q' (neither 'a' nor 'x') at the target position
data := haystack('a', 'q', pos)
f := NotIndexByte2
switch which {
case "scalar":
f = not_index_byte2_scalar
}
b.Run(fmt.Sprintf("%s_sz=%d", which, pos), func(b *testing.B) {
for b.Loop() {
f(data, 'a', 'x')
}
})
}
for _, pos := range sizes {
t(pos, "simdstring")
t(pos, "scalar")
}
}
File diff suppressed because it is too large. Load diff
+8
View File
@@ -0,0 +1,8 @@
#!/bin/sh
#
# generate.sh
# Copyright (C) 2024 Kovid Goyal <kovid at kovidgoyal.net>
#
# Distributed under terms of the MIT license.
#
go run generate.go && GOARCH=amd64 go vet ./... && GOARCH=arm64 go vet ./...
+83
View File
@@ -0,0 +1,83 @@
// License: GPLv3 Copyright: 2024, Kovid Goyal, <kovid at kovidgoyal.net>
package simdstring
import (
"runtime"
"golang.org/x/sys/cpu"
)
var Have128bit = false
var Have256bit = false
var VectorSize = 1
// Return the index at which b first occurs in data. If not found -1 is returned.
var IndexByte func(data []byte, b byte) int = index_byte_scalar
// Return the index at which b first occurs in text. If not found -1 is returned.
var IndexByteString func(text string, b byte) int = index_byte_string_scalar
// Return the index at which either a or b first occurs in data. If neither is
// 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.
var IndexByte2String func(text string, a, b byte) int = index_byte2_string_scalar
// 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
// Return the index of the first byte in data that is not equal to b. If all bytes equal b, -1 is returned.
var NotIndexByte func(data []byte, b byte) int = not_index_byte_scalar
// Return the index of the first byte in text that is not equal to b. If all bytes equal b, -1 is returned.
var NotIndexByteString func(text string, b byte) int = not_index_byte_string_scalar
// Return the index of the first byte in data that is neither a nor b. If all bytes are a or b, -1 is returned.
var NotIndexByte2 func(data []byte, a, b byte) int = not_index_byte2_scalar
// Return the index of the first byte in text that is neither a nor b. If all bytes are a or b, -1 is returned.
var NotIndexByte2String func(text string, a, b byte) int = not_index_byte2_string_scalar
func init() {
switch runtime.GOARCH {
case "amd64":
if cpu.Initialized {
Have128bit = cpu.X86.HasSSE42 && HasSIMD128Code
Have256bit = cpu.X86.HasAVX2 && HasSIMD256Code
}
case "arm64":
Have128bit = HasSIMD128Code
Have256bit = HasSIMD256Code
}
if Have256bit {
IndexByte = index_byte_asm_256
IndexByteString = index_byte_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
NotIndexByte = not_index_byte_asm_256
NotIndexByteString = not_index_byte_string_asm_256
NotIndexByte2 = not_index_byte2_asm_256
NotIndexByte2String = not_index_byte2_string_asm_256
VectorSize = 32
} else if Have128bit {
IndexByte = index_byte_asm_128
IndexByteString = index_byte_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
NotIndexByte = not_index_byte_asm_128
NotIndexByteString = not_index_byte_string_asm_128
NotIndexByte2 = not_index_byte2_asm_128
NotIndexByte2String = not_index_byte2_string_asm_128
VectorSize = 16
}
}
+420
View File
@@ -0,0 +1,420 @@
// License: GPLv3 Copyright: 2024, Kovid Goyal, <kovid at kovidgoyal.net>
package simdstring
import (
"bytes"
"fmt"
"runtime"
"strings"
"testing"
"unsafe"
"github.com/google/go-cmp/cmp"
)
var _ = fmt.Print
func test_load(src []byte) []byte {
ans := make([]byte, len(src))
if len(src) == 16 {
test_load_asm_128(src, ans)
} else {
test_load_asm_256(src, ans)
}
return ans
}
func test_set1_epi8(b byte, sz int) []byte {
ans := make([]byte, sz)
if sz == 16 {
test_set1_epi8_asm_128(b, ans)
} else {
test_set1_epi8_asm_256(b, ans)
}
return ans
}
func test_cmpeq_epi8(a, b []byte) []byte {
ans := make([]byte, len(a))
if len(ans) == 16 {
test_cmpeq_epi8_asm_128(a, b, ans)
} else {
test_cmpeq_epi8_asm_256(a, b, ans)
}
return ans
}
func test_cmplt_epi8(t *testing.T, a, b []byte) []byte {
ans := make([]byte, len(a))
var prev []byte
for which := range 3 {
if len(ans) == 16 {
test_cmplt_epi8_asm_128(a, b, which, ans)
} else {
test_cmplt_epi8_asm_256(a, b, which, ans)
}
if prev != nil {
if s := cmp.Diff(prev, ans); s != "" {
t.Fatalf("cmplt returned different result for which=%d\n%s", which, s)
}
}
prev = bytes.Clone(ans)
}
return ans
}
func test_or(a, b []byte) []byte {
ans := make([]byte, len(a))
if len(ans) == 16 {
test_or_asm_128(a, b, ans)
} else {
test_or_asm_256(a, b, ans)
}
return ans
}
func test_jump_if_zero(a []byte) int {
if len(a) == 16 {
return test_jump_if_zero_asm_128(a)
}
return test_jump_if_zero_asm_256(a)
}
func test_count_to_match(a []byte, b byte) int {
if len(a) == 16 {
return test_count_to_match_asm_128(a, b)
}
return test_count_to_match_asm_256(a, b)
}
func ordered_bytes(size int) []byte {
ans := make([]byte, size)
for i := range ans {
ans[i] = byte(i)
}
return ans
}
func broadcast_byte(b byte, size int) []byte {
return bytes.Repeat([]byte{b}, size)
}
func get_sizes(t *testing.T) []int {
sizes := []int{}
if Have128bit {
sizes = append(sizes, 16)
}
if Have256bit {
sizes = append(sizes, 32)
}
if len(sizes) == 0 {
t.Skip("skipping as no SIMD available at runtime")
}
return sizes
}
func addressof_data(b []byte) uintptr {
return uintptr(unsafe.Pointer(&b[0]))
}
func memset(ans []byte, val byte) {
for i := range ans {
ans[i] = val
}
}
func aligned_slice(sz, alignment int) ([]byte, []byte) {
ans := make([]byte, sz+alignment+512)
a := addressof_data(ans)
a &= uintptr(alignment - 1)
extra := uintptr(alignment) - a
memset(ans, '<')
memset(ans[extra+uintptr(sz):], '>')
return ans[extra : extra+uintptr(sz)], ans
}
func TestSIMDStringOps(t *testing.T) {
sizes := get_sizes(t)
test := func(haystack []byte, a, b byte, align_offset int) {
var actual int
sh, _ := aligned_slice(len(haystack)+align_offset, 64)
sh = sh[align_offset:]
copy(sh, haystack)
haystack = sh
expected := index_byte2_scalar(haystack, a, b)
for _, sz := range sizes {
switch sz {
case 16:
actual = index_byte2_asm_128(haystack, a, b)
case 32:
actual = index_byte2_asm_256(haystack, a, b)
}
if actual != expected {
t.Fatalf("Failed to find '%c' or '%c' in: %#v at align: %d (expected: %d != actual: %d) at size: %d",
a, b, string(haystack), addressof_data(haystack)&uintptr(sz-1), expected, actual, sz)
}
}
}
// test alignment issues
q := []byte("abc")
for sz := range 32 {
test(q, '<', '>', sz)
test(q, ' ', 'b', sz)
test(q, '<', 'a', sz)
test(q, '<', 'b', sz)
test(q, 'c', '>', sz)
}
tests := func(h string, a, b byte) {
for _, sz := range []int{0, 16, 32, 64, 79} {
q := strings.Repeat(" ", sz) + h
for sz := range 32 {
test([]byte(q), a, b, sz)
}
}
}
test(nil, '<', '>', 1)
test([]byte{}, '<', '>', 1)
tests("", '<', '>')
tests("a", 0, 0)
tests("a", '<', '>')
tests("dsdfsfa", '1', 'a')
tests("xa", 'a', 'a')
tests("bbb", 'a', '1')
tests("bba", 'a', '<')
tests("baa", '>', 'a')
c0test := func(haystack []byte) {
var actual int
safe_haystack := append(bytes.Repeat([]byte{'<'}, 64), haystack...)
safe_haystack = append(safe_haystack, bytes.Repeat([]byte{'>'}, 64)...)
haystack = safe_haystack[64 : 64+len(haystack)]
expected := index_c0_scalar(haystack)
for _, sz := range sizes {
switch sz {
case 16:
actual = index_c0_asm_128(haystack)
case 32:
actual = index_c0_asm_256(haystack)
}
if actual != expected {
t.Fatalf("C0 char index failed in: %#v (%d != %d) at size: %d", string(haystack), expected, actual, sz)
}
}
}
c0tests := func(h string) {
c0test([]byte(h))
for _, sz := range []int{16, 32, 64, 79} {
q := strings.Repeat(" ", sz) + h
c0test([]byte(q))
}
}
c0tests("a\nfgdfgd\r")
c0tests("")
c0tests("abcdef")
c0tests("afsgdfg\x7f")
c0tests("afgd\x1bfgd\t")
c0tests("a\x00")
index_test := func(haystack []byte, needle byte) {
var actual int
expected := index_byte_scalar(haystack, needle)
for _, sz := range sizes {
switch sz {
case 16:
actual = index_byte_asm_128(haystack, needle)
case 32:
actual = index_byte_asm_256(haystack, needle)
}
if actual != expected {
t.Fatalf("index failed in: %#v (%d != %d) at size: %d with needle: %#v", string(haystack), expected, actual, sz, needle)
}
}
}
index_test([]byte("abc"), 'x')
index_test([]byte("abc"), 'b')
not_index_test := func(haystack []byte, needle byte) {
var actual int
expected := not_index_byte_scalar(haystack, needle)
for _, sz := range sizes {
switch sz {
case 16:
actual = not_index_byte_asm_128(haystack, needle)
case 32:
actual = not_index_byte_asm_256(haystack, needle)
}
if actual != expected {
t.Fatalf("not_index failed in: %#v (%d != %d) at size: %d with needle: %#v", string(haystack), expected, actual, sz, needle)
}
}
}
not_index_test(nil, 'a')
not_index_test([]byte{}, 'a')
not_index_test([]byte("aaa"), 'a')
not_index_test([]byte("aaab"), 'a')
not_index_test([]byte("baaa"), 'a')
not_index_test([]byte("abc"), 'a')
for _, sz := range []int{0, 16, 32, 64, 79} {
q := strings.Repeat("a", sz) + "b"
not_index_test([]byte(q), 'a')
not_index_test([]byte(q), 'b')
not_index_test([]byte(strings.Repeat("a", sz)), 'a')
}
not_index2_test := func(haystack []byte, a, b byte) {
var actual int
expected := not_index_byte2_scalar(haystack, a, b)
for _, sz := range sizes {
switch sz {
case 16:
actual = not_index_byte2_asm_128(haystack, a, b)
case 32:
actual = not_index_byte2_asm_256(haystack, a, b)
}
if actual != expected {
t.Fatalf("not_index2 failed in: %#v (%d != %d) at size: %d with needles: %#v %#v", string(haystack), expected, actual, sz, a, b)
}
}
}
not_index2_test(nil, 'a', 'b')
not_index2_test([]byte{}, 'a', 'b')
not_index2_test([]byte("aabb"), 'a', 'b')
not_index2_test([]byte("aabbc"), 'a', 'b')
not_index2_test([]byte("caabb"), 'a', 'b')
for _, sz := range []int{0, 16, 32, 64, 79} {
q := strings.Repeat("ab", sz) + "c"
not_index2_test([]byte(q), 'a', 'b')
not_index2_test([]byte(strings.Repeat("ab", sz)), 'a', 'b')
for align := range 32 {
not_index2_test([]byte(strings.Repeat(" ", align)+q), 'a', 'b')
}
}
}
func TestIntrinsics(t *testing.T) {
switch runtime.GOARCH {
case "amd64":
if !HasSIMD128Code {
t.Fatal("SIMD 128bit code not built")
}
if !HasSIMD256Code {
t.Fatal("SIMD 256bit code not built")
}
case "arm64":
if !HasSIMD128Code {
t.Fatal("SIMD 128bit code not built")
}
if !Have128bit {
t.Fatal("SIMD 128bit support not available at runtime")
}
}
ae := func(sz int, func_name string, a, b any) {
if s := cmp.Diff(a, b); s != "" {
t.Fatalf("%s failed with size: %d\n%s", func_name, sz, s)
}
}
tests := []func(int){}
tests = append(tests, func(sz int) {
a := ordered_bytes(sz)
ae(sz, `load_test`, a, test_load(a))
})
tests = append(tests, func(sz int) {
for _, b := range []byte{1, 0b110111, 0xff, 0, ' '} {
ae(sz, `set1_epi8_test`, broadcast_byte(b, sz), test_set1_epi8(b, sz))
}
ae(sz, `set1_epi8_test`, broadcast_byte(0xff, sz), test_set1_epi8(11, sz))
})
tests = append(tests, func(sz int) {
a := ordered_bytes(sz)
b := ordered_bytes(sz)
ans := test_cmpeq_epi8(a, b)
ae(sz, `cmpeq_epi8_test`, broadcast_byte(0xff, sz), ans)
lt := func(a, b []byte) []byte {
ans := make([]byte, len(a))
for i := range ans {
if int8(a[i]) < int8(b[i]) {
ans[i] = 0xff
}
}
return ans
}
ae(sz, "cmplt_epi8_test with equal vecs of non-negative numbers", lt(a, b), test_cmplt_epi8(t, a, b))
a = broadcast_byte(1, sz)
b = broadcast_byte(2, sz)
ae(sz, "cmplt_epi8_test with 1 and 2", lt(a, b), test_cmplt_epi8(t, a, b))
ae(sz, "cmplt_epi8_test with 2 and 1", lt(b, a), test_cmplt_epi8(t, b, a))
a = broadcast_byte(0xff, sz)
b = broadcast_byte(0, sz)
ae(sz, "cmplt_epi8_test with -1 and 0", lt(a, b), test_cmplt_epi8(t, a, b))
})
tests = append(tests, func(sz int) {
a := make([]byte, sz)
b := make([]byte, sz)
c := make([]byte, sz)
a[0] = 0xff
b[0] = 0xff
b[1] = 0xff
a[sz-1] = 1
b[sz-1] = 2
for i := range c {
c[i] = a[i] | b[i]
}
ans := test_or(a, b)
ae(sz, `or_test`, c, ans)
})
tests = append(tests, func(sz int) {
a := make([]byte, sz)
if e := test_jump_if_zero(a); e != 0 {
t.Fatalf("Did not detect zero register")
}
for i := range sz {
a = make([]byte, sz)
a[i] = 1
if e := test_jump_if_zero(a); e != 1 {
t.Fatalf("Did not detect non-zero register")
}
}
})
tests = append(tests, func(sz int) {
a := ordered_bytes(sz)
if e := test_count_to_match(a, 77); e != -1 {
t.Fatalf("Unexpectedly found byte at: %d", e)
}
for i := range sz {
if e := test_count_to_match(a, byte(i)); e != i {
t.Fatalf("Failed to find the byte: %d (%d != %d)", i, i, e)
}
}
a[7] = 0x34
if e := test_count_to_match(a, 0x34); e != 7 {
t.Fatalf("Failed to find the byte: %d (%d != %d)", 0x34, 7, e)
}
})
sizes := get_sizes(t)
for _, sz := range sizes {
for _, test := range tests {
test(sz)
}
}
}
+95
View File
@@ -0,0 +1,95 @@
// License: GPLv3 Copyright: 2024, Kovid Goyal, <kovid at kovidgoyal.net>
package simdstring
func index_byte_scalar(data []byte, b byte) int {
for i, ch := range data {
if ch == b {
return i
}
}
return -1
}
func index_byte_string_scalar(data string, b byte) int {
for i := 0; i < len(data); i++ {
if data[i] == b {
return i
}
}
return -1
}
func index_byte2_scalar(data []byte, a, b byte) int {
for i, ch := range data {
switch ch {
case a, b:
return i
}
}
return -1
}
func index_byte2_string_scalar(data string, a, b byte) int {
for i := 0; i < len(data); i++ {
switch data[i] {
case a, b:
return i
}
}
return -1
}
func index_c0_scalar(data []byte) int {
for i := range data {
if data[i] == 0x7f || data[i] < ' ' {
return i
}
}
return -1
}
func index_c0_string_scalar(data string) int {
for i := 0; i < len(data); i++ {
if data[i] == 0x7f || data[i] < ' ' {
return i
}
}
return -1
}
func not_index_byte_scalar(data []byte, b byte) int {
for i, ch := range data {
if ch != b {
return i
}
}
return -1
}
func not_index_byte_string_scalar(data string, b byte) int {
for i := 0; i < len(data); i++ {
if data[i] != b {
return i
}
}
return -1
}
func not_index_byte2_scalar(data []byte, a, b byte) int {
for i, ch := range data {
if ch != a && ch != b {
return i
}
}
return -1
}
func not_index_byte2_string_scalar(data string, a, b byte) int {
for i := 0; i < len(data); i++ {
if data[i] != a && data[i] != b {
return i
}
}
return -1
}
+12
View File
@@ -0,0 +1,12 @@
#!/bin/sh
#
# test.sh
# Copyright (C) 2024 Kovid Goyal <kovid at kovidgoyal.net>
#
# Distributed under terms of the MIT license.
#
echo -e "\x1b[32mtesting amd64\x1b[m" && go test -v &&
echo -e "\x1b[32mtesting arm64\x1b[m" && GOARCH=arm64 go test -v -exec qemu-aarch64-static