Source file src/simd/internal/spec/reductions.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  //specgen:category Reductions
     6  
     7  package spec
     8  
     9  // ReduceSumInts returns the scalar sum of the elements of x.
    10  //
    11  //	z = x[0] + x[1] + ...
    12  //
    13  //specgen:name ReduceSum
    14  func ReduceSumInts[E Ints | Uints, W Width](x Vec[E, W]) (z E) {
    15  	// Integer addition wraps and is associative, so the order doesn't matter.
    16  	for _, xi := range x {
    17  		z += xi
    18  	}
    19  	return z
    20  }
    21  
    22  // ReduceSumFloats returns the scalar sum of the elements of x.
    23  //
    24  // {{if eq .xL 2}}
    25  //
    26  //	z = x[0] + x[1]
    27  //
    28  // {{else}}
    29  //
    30  // This uses repeated pairwise summation, which may affect the final precision
    31  // of the result.
    32  //
    33  // {{if eq .xL 4}}
    34  //
    35  //	z = (x[0] + x[1]) + (x[2] + x[3])
    36  //
    37  // {{else}}
    38  //
    39  //	z = ((x[0] + x[1]) + (x[2] + x[3])) + ...
    40  //
    41  // {{end}}
    42  //
    43  // {{end}}
    44  //
    45  //specgen:name ReduceSum
    46  func ReduceSumFloats[E Floats, W Width](x Vec[E, W]) (z E) {
    47  	// Vector lengths are powers of two, so every level pairs up evenly.
    48  	tmp := make([]E, x.len()/2, maxScalableWidth/elemBits[E]()/2)
    49  	for i := range tmp {
    50  		tmp[i] = x[2*i] + x[2*i+1]
    51  	}
    52  	for len(tmp) > 1 {
    53  		half := len(tmp) / 2
    54  		for i := range half {
    55  			tmp[i] = tmp[2*i] + tmp[2*i+1]
    56  		}
    57  		tmp = tmp[:half]
    58  	}
    59  	return tmp[0]
    60  }
    61  
    62  // ReduceMax returns the largest element of x.
    63  //
    64  //	z = max(x[0], x[1], ...)
    65  //
    66  // {{if eq .xB "float"}}
    67  // If any element is NaN, the result is NaN, and -0 is considered less than +0.
    68  // This matches Go's max built-in and the IEEE 754-2019 maximum operation. Note
    69  // that on AMD64 (and only AMD64), it differs from [Max].
    70  // {{end}}
    71  func ReduceMax[E Elt, W Width](x Vec[E, W]) (z E) {
    72  	// For floats, unlike [MaxFloats], this is exact on all platforms. A
    73  	// reduction with a non-commutative operation like AMD64's VMAXP[SD] makes
    74  	// little sense: its result would depend on the order in which an
    75  	// implementation happens to combine elements, which isn't something users
    76  	// can reasonably control or reason about.
    77  	//
    78  	// ARM64 (FMAXV, FMAXP) natively implements IEEE 754-2019 maximum. AMD64
    79  	// and WASM emulate it. Go's max is IEEE 754-2019 maximum (up to NaN payload,
    80  	// which equalExact ignores), which is commutative and associative, so the
    81  	// reduction order doesn't matter.
    82  	z = x[0]
    83  	for _, xi := range x[1:] {
    84  		z = max(z, xi)
    85  	}
    86  	return z
    87  }
    88  
    89  // ReduceMin returns the smallest element of x.
    90  //
    91  //	z = min(x[0], x[1], ...)
    92  //
    93  // {{if eq .xB "float"}}
    94  // If any element is NaN, the result is NaN, and -0 is considered less than +0.
    95  // This matches Go's min built-in and the IEEE 754-2019 minimum operation. Note
    96  // that on AMD64 (and only AMD64), it differs from [Min].
    97  // {{end}}
    98  func ReduceMin[E Elt, W Width](x Vec[E, W]) (z E) {
    99  	// See ReduceMax about the handling of non-finite values.
   100  	z = x[0]
   101  	for _, xi := range x[1:] {
   102  		z = min(z, xi)
   103  	}
   104  	return z
   105  }
   106  

View as plain text