Source file src/simd/example_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
     6  
     7  package simd_test
     8  
     9  import (
    10  	"fmt"
    11  	"simd"
    12  )
    13  
    14  func ExampleInt8s_Add() {
    15  	// Initialize slice of 64 int8s (max vector size under AVX512).
    16  	in1 := []int8{
    17  		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
    18  		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    19  		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    20  		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    21  	}
    22  	in2 := []int8{
    23  		10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 100, 10, 10, 10, 16,
    24  		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    25  		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    26  		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    27  	}
    28  
    29  	// Load slices into vectors.
    30  	v1 := simd.LoadInt8s(in1)
    31  	v2 := simd.LoadInt8s(in2)
    32  
    33  	// Add the vectors.
    34  	sum := v1.Add(v2)
    35  
    36  	// Store the result back to a slice.
    37  	out := make([]int8, sum.Len())
    38  	sum.Store(out)
    39  
    40  	// Print the first 16 elements (minimum vector width across architectures).
    41  	fmt.Println(out[:16])
    42  	// Output: [11 22 33 44 55 66 77 88 99 110 121 112 23 24 25 32]
    43  }
    44  
    45  func ExampleInt8s_Masked() {
    46  	// Load vectors of 64 elements.
    47  	v1 := simd.LoadInt8s([]int8{
    48  		1, -2, 3, -4, 5, -6, 7, -8, 9, -10, 11, -12, 13, -14, 15, -16,
    49  		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    50  		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    51  		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    52  	})
    53  	var v2 simd.Int8s // zero value
    54  
    55  	// Create a mask where elements in v1 are greater than zero.
    56  	mask := v1.Greater(v2)
    57  
    58  	// Keep elements of v1 where the mask is true, zero out elsewhere.
    59  	res := v1.Masked(mask)
    60  
    61  	out := make([]int8, res.Len())
    62  	res.Store(out)
    63  
    64  	// Print the first 16 elements.
    65  	fmt.Println(out[:16])
    66  	// Output: [1 0 3 0 5 0 7 0 9 0 11 0 13 0 15 0]
    67  }
    68  
    69  func ExampleLoadInt8sPart() {
    70  	// Slice smaller than the full vector length.
    71  	s := []int8{1, 2, 3, 4, 5}
    72  
    73  	// Load partial slice.
    74  	v, n := simd.LoadInt8sPart(s)
    75  	fmt.Printf("Loaded %d elements\n", n)
    76  
    77  	// Store only the loaded elements.
    78  	out := make([]int8, n)
    79  	v.StorePart(out)
    80  	fmt.Println(out)
    81  
    82  	// Output:
    83  	// Loaded 5 elements
    84  	// [1 2 3 4 5]
    85  }
    86  
    87  func ExampleFloat32s_MulAdd() {
    88  	// Float32s on 512-bit vector has 16 elements.
    89  	v1 := simd.LoadFloat32s([]float32{
    90  		1.5, 2.5, 3.5, 4.5,
    91  		0, 0, 0, 0,
    92  		0, 0, 0, 0,
    93  		0, 0, 0, 0,
    94  	})
    95  	v2 := simd.LoadFloat32s([]float32{
    96  		2.0, 2.0, 2.0, 2.0,
    97  		0, 0, 0, 0,
    98  		0, 0, 0, 0,
    99  		0, 0, 0, 0,
   100  	})
   101  	v3 := simd.LoadFloat32s([]float32{
   102  		1.0, 2.0, 3.0, 4.0,
   103  		0, 0, 0, 0,
   104  		0, 0, 0, 0,
   105  		0, 0, 0, 0,
   106  	})
   107  
   108  	// Perform element-wise v1 * v2 + v3.
   109  	res := v1.MulAdd(v2, v3)
   110  
   111  	out := make([]float32, res.Len())
   112  	res.Store(out)
   113  
   114  	// Print the first 4 elements.
   115  	fmt.Println(out[:4])
   116  	// Output: [4 7 10 13]
   117  }
   118  
   119  func ExampleInt16s_ShiftAllLeft() {
   120  	// Int16s on 512-bit vector has 32 elements.
   121  	in := []int16{
   122  		1, 2, 4, 8, 16, 32, 64, 128,
   123  		0, 0, 0, 0, 0, 0, 0, 0,
   124  		0, 0, 0, 0, 0, 0, 0, 0,
   125  		0, 0, 0, 0, 0, 0, 0, 0,
   126  	}
   127  	v := simd.LoadInt16s(in)
   128  
   129  	// Shift all elements left by 2 bits.
   130  	res := v.ShiftAllLeft(2)
   131  
   132  	out := make([]int16, res.Len())
   133  	res.Store(out)
   134  
   135  	// Print the first 8 elements.
   136  	fmt.Println(out[:8])
   137  	// Output: [4 8 16 32 64 128 256 512]
   138  }
   139  
   140  func ExampleInt16s_RotateAllLeft() {
   141  	// Int16s on 512-bit vector has 32 elements.
   142  	in := []int16{
   143  		0x00f0, 0x1234, 0, 0, 0, 0, 0, 0x7000,
   144  		0, 0, 0, 0, 0, 0, 0, 0,
   145  		0, 0, 0, 0, 0, 0, 0, 0,
   146  		0, 0, 0, 0, 0, 0, 0, 0,
   147  	}
   148  	v := simd.LoadInt16s(in)
   149  
   150  	// Rotate all elements left by 4 bits.
   151  	res := v.RotateAllLeft(4)
   152  
   153  	out := make([]int16, res.Len())
   154  	res.Store(out)
   155  
   156  	fmt.Printf("%#04x\n", out[:8])
   157  	// Output: [0x0f00 0x2341 0x0000 0x0000 0x0000 0x0000 0x0000 0x0007]
   158  }
   159  

View as plain text