Source file src/simd/archsimd/internal/simd_test/convert_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.simd && wasm
     6  
     7  package simd_test
     8  
     9  import (
    10  	"simd/archsimd"
    11  	"testing"
    12  )
    13  
    14  func TestConvertWasm(t *testing.T) {
    15  	testFloat64x2ConvertToFloat32(t, archsimd.Float64x2.ConvertToFloat32, map1[float64](toFloat32))
    16  	testFloat32x4ConvertToFloat64(t, archsimd.Float32x4.ConvertToFloat64, map1n[float32](toFloat64, 2))
    17  	testInt32x4ConvertToFloat64(t, archsimd.Int32x4.ConvertToFloat64, map1n[int32](toFloat64, 2))
    18  	testUint32x4ConvertToFloat64(t, archsimd.Uint32x4.ConvertToFloat64, map1n[uint32](toFloat64, 2))
    19  	// Wasm float-to-int conversions saturate, like arm64's.
    20  	testFloat64x2ConvertToInt32(t, archsimd.Float64x2.ConvertToInt32, map1[float64](floatToInt32_arm64))
    21  	testFloat64x2ConvertToUint32(t, archsimd.Float64x2.ConvertToUint32, map1[float64](floatToUint32_arm64))
    22  }
    23  
    24  func TestExtendWasm(t *testing.T) {
    25  	testInt8x16ConvertToInt16(t, archsimd.Int8x16.ExtendToInt16, map1n[int8](toInt16, 8))
    26  	testInt16x8ConvertToInt32(t, archsimd.Int16x8.ExtendToInt32, map1n[int16](toInt32, 4))
    27  	testInt32x4ConvertToInt64(t, archsimd.Int32x4.ExtendToInt64, map1n[int32](toInt64, 2))
    28  
    29  	testUint8x16ConvertToUint16(t, archsimd.Uint8x16.ExtendToUint16, map1n[uint8](toUint16, 8))
    30  	testUint16x8ConvertToUint32(t, archsimd.Uint16x8.ExtendToUint32, map1n[uint16](toUint32, 4))
    31  	testUint32x4ConvertToUint64(t, archsimd.Uint32x4.ExtendToUint64, map1n[uint32](toUint64, 2))
    32  }
    33  
    34  func TestTruncateWasm(t *testing.T) {
    35  	testInt16x8ConvertToInt8(t, archsimd.Int16x8.TruncToInt8, map1[int16](toInt8))
    36  	testInt32x4ConvertToInt16(t, archsimd.Int32x4.TruncToInt16, map1[int32](toInt16))
    37  	testInt64x2ConvertToInt32(t, archsimd.Int64x2.TruncToInt32, map1[int64](toInt32))
    38  
    39  	testUint16x8ConvertToUint8(t, archsimd.Uint16x8.TruncToUint8, map1[uint16](toUint8))
    40  	testUint32x4ConvertToUint16(t, archsimd.Uint32x4.TruncToUint16, map1[uint32](toUint16))
    41  	testUint64x2ConvertToUint32(t, archsimd.Uint64x2.TruncToUint32, map1[uint64](toUint32))
    42  }
    43  
    44  func TestSaturateWasm(t *testing.T) {
    45  	testInt16x8ConvertToInt8(t, archsimd.Int16x8.SaturateToInt8, map1[int16](satToInt8))
    46  	testInt32x4ConvertToInt16(t, archsimd.Int32x4.SaturateToInt16, map1[int32](satToInt16))
    47  	testInt64x2ConvertToInt32(t, archsimd.Int64x2.SaturateToInt32, map1[int64](satToInt32))
    48  
    49  	testUint16x8ConvertToUint8(t, archsimd.Uint16x8.SaturateToUint8, map1[uint16](satToUint8))
    50  	testUint32x4ConvertToUint16(t, archsimd.Uint32x4.SaturateToUint16, map1[uint32](satToUint16))
    51  	testUint64x2ConvertToUint32(t, archsimd.Uint64x2.SaturateToUint32, map1[uint64](satToUint32))
    52  
    53  	testInt16x8ConvertToUint8(t, archsimd.Int16x8.SaturateToUint8, map1[int16](satToUint8))
    54  	testInt32x4ConvertToUint16(t, archsimd.Int32x4.SaturateToUint16, map1[int32](satToUint16))
    55  	testInt64x2ConvertToUint32(t, archsimd.Int64x2.SaturateToUint32, map1[int64](satToUint32))
    56  }
    57  
    58  // TestExtendHiWasm tests x.HiToLo().ExtendTo*(), which compiles to a single
    59  // instruction that widens the high half of x.
    60  func TestExtendHiWasm(t *testing.T) {
    61  	testInt8x16ConvertToInt16(t, func(x archsimd.Int8x16) archsimd.Int16x8 { return x.HiToLo().ExtendToInt16() }, hi[int8](toInt16))
    62  	testInt16x8ConvertToInt32(t, func(x archsimd.Int16x8) archsimd.Int32x4 { return x.HiToLo().ExtendToInt32() }, hi[int16](toInt32))
    63  	testInt32x4ConvertToInt64(t, func(x archsimd.Int32x4) archsimd.Int64x2 { return x.HiToLo().ExtendToInt64() }, hi[int32](toInt64))
    64  
    65  	testUint8x16ConvertToUint16(t, func(x archsimd.Uint8x16) archsimd.Uint16x8 { return x.HiToLo().ExtendToUint16() }, hi[uint8](toUint16))
    66  	testUint16x8ConvertToUint32(t, func(x archsimd.Uint16x8) archsimd.Uint32x4 { return x.HiToLo().ExtendToUint32() }, hi[uint16](toUint32))
    67  	testUint32x4ConvertToUint64(t, func(x archsimd.Uint32x4) archsimd.Uint64x2 { return x.HiToLo().ExtendToUint64() }, hi[uint32](toUint64))
    68  }
    69  

View as plain text