Source file src/simd/internal/spec/rounding.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 Rounding
     6  
     7  package spec
     8  
     9  import "math"
    10  
    11  // Ceil returns the least integer value greater than or equal to each element
    12  // of x.
    13  //
    14  //	z[i] = ceil(x[i])
    15  //
    16  // Special cases follow IEEE 754: Ceil(±0) = ±0, Ceil(±Inf) = ±Inf, Ceil(x) = -0
    17  // for -1 < x < -0, and Ceil(NaN) = NaN. The sign and payload of a NaN result
    18  // are unspecified.
    19  func Ceil[E Floats, W Width](x Vec[E, W]) (z Vec[E, W]) {
    20  	return map1[E, W, E, W](x, func(x E) E {
    21  		return E(math.Ceil(float64(x)))
    22  	})
    23  }
    24  
    25  // Floor returns the greatest integer value less than or equal to each element
    26  // of x.
    27  //
    28  //	z[i] = floor(x[i])
    29  //
    30  // Special cases follow IEEE 754: Floor(±0) = ±0, Floor(±Inf) = ±Inf, and
    31  // Floor(NaN) = NaN. The sign and payload of a NaN result are unspecified.
    32  func Floor[E Floats, W Width](x Vec[E, W]) (z Vec[E, W]) {
    33  	return map1[E, W, E, W](x, func(x E) E {
    34  		return E(math.Floor(float64(x)))
    35  	})
    36  }
    37  
    38  // Round returns the nearest integer value to each element of x, rounding ties
    39  // to even.
    40  //
    41  //	z[i] = roundToEven(x[i])
    42  //
    43  // Special cases follow IEEE 754: Round(±0) = ±0, Round(±Inf) = ±Inf,
    44  // Round(x) = -0 for -0.5 <= x < -0, and Round(NaN) = NaN. The sign and payload
    45  // of a NaN result are unspecified.
    46  func Round[E Floats, W Width](x Vec[E, W]) (z Vec[E, W]) {
    47  	return map1[E, W, E, W](x, func(x E) E {
    48  		return E(math.RoundToEven(float64(x)))
    49  	})
    50  }
    51  
    52  // Trunc returns the integer value of each element of x, rounded toward zero.
    53  //
    54  //	z[i] = trunc(x[i])
    55  //
    56  // Special cases follow IEEE 754: Trunc(±0) = ±0, Trunc(±Inf) = ±Inf,
    57  // Trunc(x) = -0 for -1 < x < -0, and Trunc(NaN) = NaN. The sign and payload of
    58  // a NaN result are unspecified.
    59  func Trunc[E Floats, W Width](x Vec[E, W]) (z Vec[E, W]) {
    60  	return map1[E, W, E, W](x, func(x E) E {
    61  		return E(math.Trunc(float64(x)))
    62  	})
    63  }
    64  

View as plain text