Help us shape the future of Go! Take the 2026 Go Developer Survey

Source file src/simd/internal/simdfmt/simdfmt.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  // Package simdfmt contains formatting helpers shared by the simd packages.
     6  package simdfmt
     7  
     8  import "internal/strconv"
     9  
    10  // Number is the set of element types that SliceToString can format.
    11  type Number interface {
    12  	int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | float32 | float64
    13  }
    14  
    15  // SliceToString returns a string representation of x of the form "{a,b,c}".
    16  func SliceToString[T Number](x []T) string {
    17  	var s []byte
    18  	s = append(s, '{')
    19  	for i, y := range x {
    20  		if i > 0 {
    21  			s = append(s, ',')
    22  		}
    23  		switch e := any(y).(type) {
    24  		case int8:
    25  			s = strconv.AppendInt(s, int64(e), 10)
    26  		case int16:
    27  			s = strconv.AppendInt(s, int64(e), 10)
    28  		case int32:
    29  			s = strconv.AppendInt(s, int64(e), 10)
    30  		case int64:
    31  			s = strconv.AppendInt(s, e, 10)
    32  		case uint8:
    33  			s = strconv.AppendUint(s, uint64(e), 10)
    34  		case uint16:
    35  			s = strconv.AppendUint(s, uint64(e), 10)
    36  		case uint32:
    37  			s = strconv.AppendUint(s, uint64(e), 10)
    38  		case uint64:
    39  			s = strconv.AppendUint(s, e, 10)
    40  		case float32:
    41  			s = strconv.AppendFloat(s, float64(e), 'g', -1, 32)
    42  		case float64:
    43  			s = strconv.AppendFloat(s, e, 'g', -1, 64)
    44  		}
    45  	}
    46  	s = append(s, '}')
    47  	return string(s)
    48  }
    49  

View as plain text