Source file src/simd/archsimd/internal/simd_test/reduce_wasm_test.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build goexperiment.simdstable && wasm
     6  
     7  package simd_test
     8  
     9  import (
    10  	"math"
    11  	"simd/archsimd"
    12  	"testing"
    13  )
    14  
    15  func TestReduceMaxWasm(t *testing.T) {
    16  	testV2S(t, int8s, archsimd.LoadInt8x16, archsimd.Int8x16.ReduceMax, func(x []int8) int8 { return reduceSlice(x, max_[int8]) })
    17  	testV2S(t, int16s, archsimd.LoadInt16x8, archsimd.Int16x8.ReduceMax, func(x []int16) int16 { return reduceSlice(x, max_[int16]) })
    18  	testV2S(t, int32s, archsimd.LoadInt32x4, archsimd.Int32x4.ReduceMax, func(x []int32) int32 { return reduceSlice(x, max_[int32]) })
    19  	testV2S(t, int64s, archsimd.LoadInt64x2, archsimd.Int64x2.ReduceMax, func(x []int64) int64 { return reduceSlice(x, max_[int64]) })
    20  	testV2S(t, uint8s, archsimd.LoadUint8x16, archsimd.Uint8x16.ReduceMax, func(x []uint8) uint8 { return reduceSlice(x, max_[uint8]) })
    21  	testV2S(t, uint16s, archsimd.LoadUint16x8, archsimd.Uint16x8.ReduceMax, func(x []uint16) uint16 { return reduceSlice(x, max_[uint16]) })
    22  	testV2S(t, uint32s, archsimd.LoadUint32x4, archsimd.Uint32x4.ReduceMax, func(x []uint32) uint32 { return reduceSlice(x, max_[uint32]) })
    23  	testV2S(t, uint64s, archsimd.LoadUint64x2, archsimd.Uint64x2.ReduceMax, func(x []uint64) uint64 { return reduceSlice(x, max_[uint64]) })
    24  	testV2S(t, float32s, archsimd.LoadFloat32x4, archsimd.Float32x4.ReduceMax, func(x []float32) float32 { return reduceSlice(x, max_[float32]) })
    25  	testV2S(t, float64s, archsimd.LoadFloat64x2, archsimd.Float64x2.ReduceMax, func(x []float64) float64 { return reduceSlice(x, max_[float64]) })
    26  }
    27  
    28  func TestReduceMinWasm(t *testing.T) {
    29  	testV2S(t, int8s, archsimd.LoadInt8x16, archsimd.Int8x16.ReduceMin, func(x []int8) int8 { return reduceSlice(x, min_[int8]) })
    30  	testV2S(t, int16s, archsimd.LoadInt16x8, archsimd.Int16x8.ReduceMin, func(x []int16) int16 { return reduceSlice(x, min_[int16]) })
    31  	testV2S(t, int32s, archsimd.LoadInt32x4, archsimd.Int32x4.ReduceMin, func(x []int32) int32 { return reduceSlice(x, min_[int32]) })
    32  	testV2S(t, int64s, archsimd.LoadInt64x2, archsimd.Int64x2.ReduceMin, func(x []int64) int64 { return reduceSlice(x, min_[int64]) })
    33  	testV2S(t, uint8s, archsimd.LoadUint8x16, archsimd.Uint8x16.ReduceMin, func(x []uint8) uint8 { return reduceSlice(x, min_[uint8]) })
    34  	testV2S(t, uint16s, archsimd.LoadUint16x8, archsimd.Uint16x8.ReduceMin, func(x []uint16) uint16 { return reduceSlice(x, min_[uint16]) })
    35  	testV2S(t, uint32s, archsimd.LoadUint32x4, archsimd.Uint32x4.ReduceMin, func(x []uint32) uint32 { return reduceSlice(x, min_[uint32]) })
    36  	testV2S(t, uint64s, archsimd.LoadUint64x2, archsimd.Uint64x2.ReduceMin, func(x []uint64) uint64 { return reduceSlice(x, min_[uint64]) })
    37  	testV2S(t, float32s, archsimd.LoadFloat32x4, archsimd.Float32x4.ReduceMin, func(x []float32) float32 { return reduceSlice(x, min_[float32]) })
    38  	testV2S(t, float64s, archsimd.LoadFloat64x2, archsimd.Float64x2.ReduceMin, func(x []float64) float64 { return reduceSlice(x, min_[float64]) })
    39  }
    40  
    41  func TestReduceMinMaxFloatEdgeWasm(t *testing.T) {
    42  	negZero32 := float32(math.Copysign(0, -1))
    43  	nan32 := float32(math.NaN())
    44  	for lane := range 4 {
    45  		a := [4]float32{negZero32, negZero32, negZero32, negZero32}
    46  		a[lane] = 0
    47  		if got := archsimd.LoadFloat32x4Array(&a).ReduceMax(); got != 0 || math.Signbit(float64(got)) {
    48  			t.Errorf("Float32x4.ReduceMax(%v) = %v, want +0", a, got)
    49  		}
    50  		b := [4]float32{0, 0, 0, 0}
    51  		b[lane] = negZero32
    52  		if got := archsimd.LoadFloat32x4Array(&b).ReduceMin(); got != 0 || !math.Signbit(float64(got)) {
    53  			t.Errorf("Float32x4.ReduceMin(%v) = %v, want -0", b, got)
    54  		}
    55  		c := [4]float32{1, 2, 3, 4}
    56  		c[lane] = nan32
    57  		if got := archsimd.LoadFloat32x4Array(&c).ReduceMax(); got == got {
    58  			t.Errorf("Float32x4.ReduceMax(%v) = %v, want NaN", c, got)
    59  		}
    60  		if got := archsimd.LoadFloat32x4Array(&c).ReduceMin(); got == got {
    61  			t.Errorf("Float32x4.ReduceMin(%v) = %v, want NaN", c, got)
    62  		}
    63  	}
    64  
    65  	negZero64 := math.Copysign(0, -1)
    66  	nan64 := math.NaN()
    67  	for lane := range 2 {
    68  		a := [2]float64{negZero64, negZero64}
    69  		a[lane] = 0
    70  		if got := archsimd.LoadFloat64x2Array(&a).ReduceMax(); got != 0 || math.Signbit(got) {
    71  			t.Errorf("Float64x2.ReduceMax(%v) = %v, want +0", a, got)
    72  		}
    73  		b := [2]float64{0, 0}
    74  		b[lane] = negZero64
    75  		if got := archsimd.LoadFloat64x2Array(&b).ReduceMin(); got != 0 || !math.Signbit(got) {
    76  			t.Errorf("Float64x2.ReduceMin(%v) = %v, want -0", b, got)
    77  		}
    78  		c := [2]float64{1, 2}
    79  		c[lane] = nan64
    80  		if got := archsimd.LoadFloat64x2Array(&c).ReduceMax(); got == got {
    81  			t.Errorf("Float64x2.ReduceMax(%v) = %v, want NaN", c, got)
    82  		}
    83  		if got := archsimd.LoadFloat64x2Array(&c).ReduceMin(); got == got {
    84  			t.Errorf("Float64x2.ReduceMin(%v) = %v, want NaN", c, got)
    85  		}
    86  	}
    87  }
    88  

View as plain text