From c75965b666edf8399fc0c56ba0b94c2a4f5e7070 Mon Sep 17 00:00:00 2001 From: David Chase Date: Tue, 14 Oct 2025 12:25:07 -0400 Subject: [dev.simd] simd: added String() method to SIMD vectors. this required a little plumbing to get access to the "good" floating point formatting. Change-Id: Iebec157c28a39df59351bade53b09a3729fc49c0 Reviewed-on: https://go-review.googlesource.com/c/go/+/711781 Reviewed-by: Junyang Shao LUCI-TryBot-Result: Go LUCI --- src/simd/string.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/simd/string.go (limited to 'src/simd/string.go') diff --git a/src/simd/string.go b/src/simd/string.go new file mode 100644 index 0000000000..35584da021 --- /dev/null +++ b/src/simd/string.go @@ -0,0 +1,49 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build goexperiment.simd && amd64 + +package simd + +import ( + "internal/ftoa" + "internal/itoa" +) + +type number interface { + ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64 +} + +func sliceToString[T number](x []T) string { + s := "" + pfx := "{" + for _, y := range x { + s += pfx + pfx = "," + switch e := any(y).(type) { + case int8: + s += itoa.Itoa(int(e)) + case int16: + s += itoa.Itoa(int(e)) + case int32: + s += itoa.Itoa(int(e)) + case int64: + s += itoa.Itoa(int(e)) + case uint8: + s += itoa.Uitoa(uint(e)) + case uint16: + s += itoa.Uitoa(uint(e)) + case uint32: + s += itoa.Uitoa(uint(e)) + case uint64: + s += itoa.Uitoa(uint(e)) + case float32: + s += ftoa.FormatFloat(float64(e), 'g', -1, 32) + case float64: + s += ftoa.FormatFloat(e, 'g', -1, 64) + } + } + s += "}" + return s +} -- cgit v1.3