| // Copyright 2026 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| //go:build goexperiment.simd && arm64 |
| |
| package simd_test |
| |
| import ( |
| "simd/archsimd" |
| "testing" |
| ) |
| |
| func TestLookupOrZero(t *testing.T) { |
| // Out-of-range indices produce zero lane value. |
| x := []uint8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16} |
| indices := []uint8{7, 6, 5, 4, 3, 2, 1, 0, 0xff, 8, 16, 9, 128, 10, 20, 11} |
| want := []uint8{8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 0, 10, 0, 11, 0, 12} |
| got := make([]uint8, len(x)) |
| archsimd.LoadUint8x16(x).LookupOrZero(archsimd.LoadUint8x16(indices)).Store(got) |
| checkSlices(t, got, want) |
| } |
| |
| func TestClMul(t *testing.T) { |
| var x = archsimd.LoadUint64x2([]uint64{1, 5}) |
| var y = archsimd.LoadUint64x2([]uint64{3, 9}) |
| |
| foo := func(v archsimd.Uint64x2, s []uint64) { |
| r := make([]uint64, 2, 2) |
| v.Store(r) |
| checkSlices[uint64](t, r, s) |
| } |
| |
| foo(x.CarrylessMultiplyEven(y), []uint64{3, 0}) |
| foo(x.CarrylessMultiplyEvenOdd(y), []uint64{9, 0}) |
| foo(x.CarrylessMultiplyOddEven(y), []uint64{15, 0}) |
| foo(x.CarrylessMultiplyOdd(y), []uint64{45, 0}) |
| foo(y.CarrylessMultiplyEven(y), []uint64{5, 0}) |
| } |
| |
| func TestAddSlice(t *testing.T) { |
| if !archsimd.ARM64.SVE() { |
| t.Skip("no sve") |
| } |
| testSize := 100 |
| x := make([]int8, testSize) |
| y := make([]int8, testSize) |
| for i := 0; i < testSize; i++ { |
| x[i] = int8(i) |
| y[i] = int8(i + 1) |
| } |
| res := make([]int8, testSize) |
| expected := make([]int8, testSize) |
| for i := 0; i < testSize; i++ { |
| expected[i] = x[i] + y[i] |
| } |
| for i := 0; i < testSize; i += 5 { |
| // The user can actually choose whatever stride they like. |
| // as long as the stride is less than VL, the result |
| // won't miss anything. |
| p := archsimd.Mask8sFromCount(5) |
| xv0, _ := archsimd.LoadInt8sPart(x[i:]) |
| xv := xv0.Masked(p) |
| yv0, _ := archsimd.LoadInt8sPart(y[i:]) |
| yv := yv0.Masked(p) |
| zv := xv.Add(yv).Masked(p) |
| zv.Masked(p).StorePart(res[i:]) |
| } |
| for i := range res { |
| if res[i] != expected[i] { |
| t.Errorf("res[%d] = %d; want %d", i, res[i], expected[i]) |
| } |
| } |
| } |