From 746730720035fcdbec9eebecf2c1c4fc9a01d420 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 11 Feb 2024 07:25:04 +0530 Subject: [PATCH] Add some alignment tests --- kittens/pager/file_input.go | 13 +------------ tools/simdstring/intrinsics_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/kittens/pager/file_input.go b/kittens/pager/file_input.go index 04d06666c..b8039316c 100644 --- a/kittens/pager/file_input.go +++ b/kittens/pager/file_input.go @@ -10,7 +10,6 @@ import ( "os" "strings" "time" - "unsafe" "golang.org/x/sys/unix" @@ -34,19 +33,9 @@ func wait_for_file_to_grow(file_name string, limit int64) (err error) { return } -func aligned_slice(b []byte, alignment uintptr) []byte { - addr := uintptr(unsafe.Pointer(&b)) - extra := addr & (alignment - 1) - if extra > 0 { - return b[alignment-extra:] - } - return b -} - func read_input(input_file *os.File, input_file_name string, input_channel chan<- input_line_struct, follow bool, count_carriage_returns bool) { const buf_capacity = 8192 - var buf_storage [buf_capacity + 128]byte // ensure we have an 64 byte aligned slice with at least 64 bytes after it - buf := aligned_slice(buf_storage[:], 64)[:buf_capacity] + buf := make([]byte, buf_capacity) output_buf := strings.Builder{} output_buf.Grow(buf_capacity) var err error diff --git a/tools/simdstring/intrinsics_test.go b/tools/simdstring/intrinsics_test.go index b567ff9ea..ac8397aeb 100644 --- a/tools/simdstring/intrinsics_test.go +++ b/tools/simdstring/intrinsics_test.go @@ -8,6 +8,7 @@ import ( "runtime" "strings" "testing" + "unsafe" "github.com/google/go-cmp/cmp" ) @@ -114,6 +115,18 @@ func get_sizes(t *testing.T) []int { return sizes } +func addressof_data(b []byte) uintptr { + return uintptr(unsafe.Pointer(&b[0])) +} + +func aligned_slice(sz, alignment int) []byte { + ans := make([]byte, sz+alignment) + a := addressof_data(ans) + a &= uintptr(alignment - 1) + extra := uintptr(alignment) - a + return ans[extra : extra+uintptr(sz)] +} + func TestSIMDStringOps(t *testing.T) { sizes := get_sizes(t) test := func(haystack []byte, a, b byte) { @@ -136,6 +149,18 @@ func TestSIMDStringOps(t *testing.T) { } } + // test alignment issues + for sz := 0; sz < 32; sz++ { + q := aligned_slice(sz+3, 64)[sz:] + q[0] = 'a' + q[1] = 'b' + q[2] = 'c' + test(q, '<', '>') + test(q, '<', 'a') + test(q, '<', 'b') + test(q, 'c', '>') + } + tests := func(h string, a, b byte) { for _, sz := range []int{0, 16, 32, 64, 79} { q := strings.Repeat(" ", sz) + h