From: David Chase Date: Tue, 19 Aug 2025 20:17:58 +0000 (-0400) Subject: [dev.simd] simd: template field name cleanup in genfiles X-Git-Tag: go1.26rc1~147^2~111 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=1334285862;p=gostls13.git [dev.simd] simd: template field name cleanup in genfiles things were getting a little too ad hoc Change-Id: I4298002ae57f5b75159703ceed30a117804eb844 Reviewed-on: https://go-review.googlesource.com/c/go/+/697495 Commit-Queue: David Chase Reviewed-by: Junyang Shao TryBot-Bypass: David Chase --- diff --git a/src/simd/genfiles.go b/src/simd/genfiles.go index 3d9b26a6b0..592391f83b 100644 --- a/src/simd/genfiles.go +++ b/src/simd/genfiles.go @@ -113,25 +113,25 @@ var avx2UnsignedComparisons = &shapes{ } type templateData struct { - Vec string // the type of the vector, e.g. Float32x4 + VType string // the type of the vector, e.g. Float32x4 AOrAn string // for documentation, the article "a" or "an" - Width int // the bit width of the element type, e.g. 32 + EWidth int // the bit width of the element type, e.g. 32 Vwidth int // the width of the vector type, e.g. 128 Count int // the number of elements, e.g. 4 WxC string // the width-by-type string, e.g., "32x4" BxC string // as if bytes, in the proper count, e.g., "8x16" (W==8) - Base string // the capitalized Base Type of the vector, e.g., "Float" - Type string // the element type, e.g. "float32" + Base string // the title-case Base Type of the vector, e.g., "Float" + Etype string // the element type, e.g. "float32" OxFF string // a mask for the lowest 'count' bits - Ovec string - Otype string - OType string - Ocount int + OVType string // type of output vector + OEtype string // output element type + OEType string // output element type, title-case + OCount int // output element count } func (t templateData) As128BitVec() string { - return fmt.Sprintf("%s%dx%d", t.Base, t.Width, 128/t.Width) + return fmt.Sprintf("%s%dx%d", t.Base, t.EWidth, 128/t.EWidth) } func oneTemplate(t *template.Template, baseType string, width, count int, out io.Writer, rtf resultTypeFunc) { @@ -167,20 +167,20 @@ func oneTemplate(t *template.Template, baseType string, width, count int, out io } oxFF := fmt.Sprintf("0x%x", uint64((1<= {{.Count}} { - return Load{{.Vec}}Slice(s) + return Load{{.VType}}Slice(s) } if l == 0 { - var x {{.Vec}} + var x {{.VType}} return x } mask := Mask{{.WxC}}FromBits({{.OxFF}} >> ({{.Count}} - l)) - return LoadMasked{{.Vec}}(pa{{.Vec}}(s), mask) + return LoadMasked{{.VType}}(pa{{.VType}}(s), mask) } // StoreSlicePart stores the {{.Count}} elements of x into the slice s. // It stores as many elements as will fit in s. // If s has {{.Count}} or more elements, the method is equivalent to x.StoreSlice. -func (x {{.Vec}}) StoreSlicePart(s []{{.Type}}) { +func (x {{.VType}}) StoreSlicePart(s []{{.Etype}}) { l := len(s) if l >= {{.Count}} { x.StoreSlice(s) @@ -461,31 +461,31 @@ func (x {{.Vec}}) StoreSlicePart(s []{{.Type}}) { return } mask := Mask{{.WxC}}FromBits({{.OxFF}} >> ({{.Count}} - l)) - x.StoreMasked(pa{{.Vec}}(s), mask) + x.StoreMasked(pa{{.VType}}(s), mask) } `) var avx2MaskedLoadSlicePartTemplate = shapedTemplateOf(avx2MaskedLoadShapes, "avx 2 load slice part", ` -// Load{{.Vec}}SlicePart loads a {{.Vec}} from the slice s. +// Load{{.VType}}SlicePart loads a {{.VType}} from the slice s. // If s has fewer than {{.Count}} elements, the remaining elements of the vector are filled with zeroes. -// If s has {{.Count}} or more elements, the function is equivalent to Load{{.Vec}}Slice. -func Load{{.Vec}}SlicePart(s []{{.Type}}) {{.Vec}} { +// If s has {{.Count}} or more elements, the function is equivalent to Load{{.VType}}Slice. +func Load{{.VType}}SlicePart(s []{{.Etype}}) {{.VType}} { l := len(s) if l >= {{.Count}} { - return Load{{.Vec}}Slice(s) + return Load{{.VType}}Slice(s) } if l == 0 { - var x {{.Vec}} + var x {{.VType}} return x } - mask := vecMask{{.Width}}[len(vecMask{{.Width}})/2-l:] - return LoadMasked{{.Vec}}(pa{{.Vec}}(s), LoadInt{{.WxC}}Slice(mask).asMask()) + mask := vecMask{{.EWidth}}[len(vecMask{{.EWidth}})/2-l:] + return LoadMasked{{.VType}}(pa{{.VType}}(s), LoadInt{{.WxC}}Slice(mask).asMask()) } // StoreSlicePart stores the {{.Count}} elements of x into the slice s. // It stores as many elements as will fit in s. // If s has {{.Count}} or more elements, the method is equivalent to x.StoreSlice. -func (x {{.Vec}}) StoreSlicePart(s []{{.Type}}) { +func (x {{.VType}}) StoreSlicePart(s []{{.Etype}}) { l := len(s) if l >= {{.Count}} { x.StoreSlice(s) @@ -494,32 +494,32 @@ func (x {{.Vec}}) StoreSlicePart(s []{{.Type}}) { if l == 0 { return } - mask := vecMask{{.Width}}[len(vecMask{{.Width}})/2-l:] - x.StoreMasked(pa{{.Vec}}(s), LoadInt{{.WxC}}Slice(mask).asMask()) + mask := vecMask{{.EWidth}}[len(vecMask{{.EWidth}})/2-l:] + x.StoreMasked(pa{{.VType}}(s), LoadInt{{.WxC}}Slice(mask).asMask()) } `) var avx2SmallLoadSlicePartTemplate = shapedTemplateOf(avx2SmallLoadPunShapes, "avx 2 small load slice part", ` -// Load{{.Vec}}SlicePart loads a {{.Vec}} from the slice s. +// Load{{.VType}}SlicePart loads a {{.VType}} from the slice s. // If s has fewer than {{.Count}} elements, the remaining elements of the vector are filled with zeroes. -// If s has {{.Count}} or more elements, the function is equivalent to Load{{.Vec}}Slice. -func Load{{.Vec}}SlicePart(s []{{.Type}}) {{.Vec}} { +// If s has {{.Count}} or more elements, the function is equivalent to Load{{.VType}}Slice. +func Load{{.VType}}SlicePart(s []{{.Etype}}) {{.VType}} { if len(s) == 0 { - var zero {{.Vec}} + var zero {{.VType}} return zero } - t := unsafe.Slice((*int{{.Width}})(unsafe.Pointer(&s[0])), len(s)) - return LoadInt{{.WxC}}SlicePart(t).As{{.Vec}}() + t := unsafe.Slice((*int{{.EWidth}})(unsafe.Pointer(&s[0])), len(s)) + return LoadInt{{.WxC}}SlicePart(t).As{{.VType}}() } // StoreSlicePart stores the {{.Count}} elements of x into the slice s. // It stores as many elements as will fit in s. // If s has {{.Count}} or more elements, the method is equivalent to x.StoreSlice. -func (x {{.Vec}}) StoreSlicePart(s []{{.Type}}) { +func (x {{.VType}}) StoreSlicePart(s []{{.Etype}}) { if len(s) == 0 { return } - t := unsafe.Slice((*int{{.Width}})(unsafe.Pointer(&s[0])), len(s)) + t := unsafe.Slice((*int{{.EWidth}})(unsafe.Pointer(&s[0])), len(s)) x.AsInt{{.WxC}}().StoreSlicePart(t) } `) @@ -540,14 +540,14 @@ var avx2SignedComparisonsTemplate = shapedTemplateOf(avx2SignedComparisons, "avx // Less returns a mask whose elements indicate whether x < y // // Emulated, CPU Feature {{.CPUfeature}} -func (x {{.Vec}}) Less(y {{.Vec}}) Mask{{.WxC}} { +func (x {{.VType}}) Less(y {{.VType}}) Mask{{.WxC}} { return y.Greater(x) } // GreaterEqual returns a mask whose elements indicate whether x >= y // // Emulated, CPU Feature {{.CPUfeature}} -func (x {{.Vec}}) GreaterEqual(y {{.Vec}}) Mask{{.WxC}} { +func (x {{.VType}}) GreaterEqual(y {{.VType}}) Mask{{.WxC}} { ones := x.Equal(x).AsInt{{.WxC}}() return y.Greater(x).AsInt{{.WxC}}().Xor(ones).asMask() } @@ -555,7 +555,7 @@ func (x {{.Vec}}) GreaterEqual(y {{.Vec}}) Mask{{.WxC}} { // LessEqual returns a mask whose elements indicate whether x <= y // // Emulated, CPU Feature {{.CPUfeature}} -func (x {{.Vec}}) LessEqual(y {{.Vec}}) Mask{{.WxC}} { +func (x {{.VType}}) LessEqual(y {{.VType}}) Mask{{.WxC}} { ones := x.Equal(x).AsInt{{.WxC}}() return x.Greater(y).AsInt{{.WxC}}().Xor(ones).asMask() } @@ -563,7 +563,7 @@ func (x {{.Vec}}) LessEqual(y {{.Vec}}) Mask{{.WxC}} { // NotEqual returns a mask whose elements indicate whether x != y // // Emulated, CPU Feature {{.CPUfeature}} -func (x {{.Vec}}) NotEqual(y {{.Vec}}) Mask{{.WxC}} { +func (x {{.VType}}) NotEqual(y {{.VType}}) Mask{{.WxC}} { ones := x.Equal(x).AsInt{{.WxC}}() return x.Equal(y).AsInt{{.WxC}}().Xor(ones).asMask() } @@ -575,7 +575,7 @@ func (x {{.Vec}}) NotEqual(y {{.Vec}}) Mask{{.WxC}} { // the sizes > 8 (shifts are AVX) but must use broadcast (AVX2) // for bytes. func (t templateData) CPUfeatureAVX2if8() string { - if t.Width == 8 { + if t.EWidth == 8 { return "AVX2" } return t.CPUfeature() @@ -585,13 +585,13 @@ var avx2UnsignedComparisonsTemplate = shapedTemplateOf(avx2UnsignedComparisons, // Greater returns a mask whose elements indicate whether x > y // // Emulated, CPU Feature {{.CPUfeatureAVX2if8}} -func (x {{.Vec}}) Greater(y {{.Vec}}) Mask{{.WxC}} { +func (x {{.VType}}) Greater(y {{.VType}}) Mask{{.WxC}} { a, b := x.AsInt{{.WxC}}(), y.AsInt{{.WxC}}() -{{- if eq .Width 8}} - signs := BroadcastInt{{.WxC}}(-1 << ({{.Width}}-1)) +{{- if eq .EWidth 8}} + signs := BroadcastInt{{.WxC}}(-1 << ({{.EWidth}}-1)) {{- else}} ones := x.Equal(x).AsInt{{.WxC}}() - signs := ones.ShiftAllLeft({{.Width}}-1) + signs := ones.ShiftAllLeft({{.EWidth}}-1) {{- end }} return a.Xor(signs).Greater(b.Xor(signs)) } @@ -599,13 +599,13 @@ func (x {{.Vec}}) Greater(y {{.Vec}}) Mask{{.WxC}} { // Less returns a mask whose elements indicate whether x < y // // Emulated, CPU Feature {{.CPUfeatureAVX2if8}} -func (x {{.Vec}}) Less(y {{.Vec}}) Mask{{.WxC}} { +func (x {{.VType}}) Less(y {{.VType}}) Mask{{.WxC}} { a, b := x.AsInt{{.WxC}}(), y.AsInt{{.WxC}}() -{{- if eq .Width 8}} - signs := BroadcastInt{{.WxC}}(-1 << ({{.Width}}-1)) +{{- if eq .EWidth 8}} + signs := BroadcastInt{{.WxC}}(-1 << ({{.EWidth}}-1)) {{- else}} ones := x.Equal(x).AsInt{{.WxC}}() - signs := ones.ShiftAllLeft({{.Width}}-1) + signs := ones.ShiftAllLeft({{.EWidth}}-1) {{- end }} return b.Xor(signs).Greater(a.Xor(signs)) } @@ -613,13 +613,13 @@ func (x {{.Vec}}) Less(y {{.Vec}}) Mask{{.WxC}} { // GreaterEqual returns a mask whose elements indicate whether x >= y // // Emulated, CPU Feature {{.CPUfeatureAVX2if8}} -func (x {{.Vec}}) GreaterEqual(y {{.Vec}}) Mask{{.WxC}} { +func (x {{.VType}}) GreaterEqual(y {{.VType}}) Mask{{.WxC}} { a, b := x.AsInt{{.WxC}}(), y.AsInt{{.WxC}}() ones := x.Equal(x).AsInt{{.WxC}}() -{{- if eq .Width 8}} - signs := BroadcastInt{{.WxC}}(-1 << ({{.Width}}-1)) +{{- if eq .EWidth 8}} + signs := BroadcastInt{{.WxC}}(-1 << ({{.EWidth}}-1)) {{- else}} - signs := ones.ShiftAllLeft({{.Width}}-1) + signs := ones.ShiftAllLeft({{.EWidth}}-1) {{- end }} return b.Xor(signs).Greater(a.Xor(signs)).AsInt{{.WxC}}().Xor(ones).asMask() } @@ -627,13 +627,13 @@ func (x {{.Vec}}) GreaterEqual(y {{.Vec}}) Mask{{.WxC}} { // LessEqual returns a mask whose elements indicate whether x <= y // // Emulated, CPU Feature {{.CPUfeatureAVX2if8}} -func (x {{.Vec}}) LessEqual(y {{.Vec}}) Mask{{.WxC}} { +func (x {{.VType}}) LessEqual(y {{.VType}}) Mask{{.WxC}} { a, b := x.AsInt{{.WxC}}(), y.AsInt{{.WxC}}() ones := x.Equal(x).AsInt{{.WxC}}() -{{- if eq .Width 8}} - signs := BroadcastInt{{.WxC}}(-1 << ({{.Width}}-1)) +{{- if eq .EWidth 8}} + signs := BroadcastInt{{.WxC}}(-1 << ({{.EWidth}}-1)) {{- else}} - signs := ones.ShiftAllLeft({{.Width}}-1) + signs := ones.ShiftAllLeft({{.EWidth}}-1) {{- end }} return a.Xor(signs).Greater(b.Xor(signs)).AsInt{{.WxC}}().Xor(ones).asMask() } @@ -641,7 +641,7 @@ func (x {{.Vec}}) LessEqual(y {{.Vec}}) Mask{{.WxC}} { // NotEqual returns a mask whose elements indicate whether x != y // // Emulated, CPU Feature {{.CPUfeature}} -func (x {{.Vec}}) NotEqual(y {{.Vec}}) Mask{{.WxC}} { +func (x {{.VType}}) NotEqual(y {{.VType}}) Mask{{.WxC}} { a, b := x.AsInt{{.WxC}}(), y.AsInt{{.WxC}}() ones := x.Equal(x).AsInt{{.WxC}}() return a.Equal(b).AsInt{{.WxC}}().Xor(ones).asMask() @@ -649,27 +649,27 @@ func (x {{.Vec}}) NotEqual(y {{.Vec}}) Mask{{.WxC}} { `) var unsafePATemplate = templateOf("unsafe PA helper", ` -// pa{{.Vec}} returns a type-unsafe pointer to array that can +// pa{{.VType}} returns a type-unsafe pointer to array that can // only be used with partial load/store operations that only // access the known-safe portions of the array. -func pa{{.Vec}}(s []{{.Type}}) *[{{.Count}}]{{.Type}} { - return (*[{{.Count}}]{{.Type}})(unsafe.Pointer(&s[0])) +func pa{{.VType}}(s []{{.Etype}}) *[{{.Count}}]{{.Etype}} { + return (*[{{.Count}}]{{.Etype}})(unsafe.Pointer(&s[0])) } `) var avx2MaskedTemplate = shapedTemplateOf(avx2Shapes, "avx2 .Masked methods", ` // Masked returns x but with elements zeroed where mask is false. -func (x {{.Vec}}) Masked(mask Mask{{.WxC}}) {{.Vec}} { +func (x {{.VType}}) Masked(mask Mask{{.WxC}}) {{.VType}} { im := mask.AsInt{{.WxC}}() {{- if eq .Base "Int" }} return im.And(x) {{- else}} - return x.AsInt{{.WxC}}().And(im).As{{.Vec}}() + return x.AsInt{{.WxC}}().And(im).As{{.VType}}() {{- end -}} } // Merge returns x but with elements set to y where mask is false. -func (x {{.Vec}}) Merge(y {{.Vec}}, mask Mask{{.WxC}}) {{.Vec}} { +func (x {{.VType}}) Merge(y {{.VType}}, mask Mask{{.WxC}}) {{.VType}} { {{- if eq .BxC .WxC -}} im := mask.AsInt{{.BxC}}() {{- else}} @@ -680,7 +680,7 @@ func (x {{.Vec}}) Merge(y {{.Vec}}, mask Mask{{.WxC}}) {{.Vec}} { {{- else}} ix := x.AsInt{{.BxC}}() iy := y.AsInt{{.BxC}}() - return iy.blend(ix, im).As{{.Vec}}() + return iy.blend(ix, im).As{{.VType}}() {{- end -}} } `) @@ -688,23 +688,23 @@ func (x {{.Vec}}) Merge(y {{.Vec}}, mask Mask{{.WxC}}) {{.Vec}} { // TODO perhaps write these in ways that work better on AVX512 var avx512MaskedTemplate = shapedTemplateOf(avx512Shapes, "avx512 .Masked methods", ` // Masked returns x but with elements zeroed where mask is false. -func (x {{.Vec}}) Masked(mask Mask{{.WxC}}) {{.Vec}} { +func (x {{.VType}}) Masked(mask Mask{{.WxC}}) {{.VType}} { im := mask.AsInt{{.WxC}}() {{- if eq .Base "Int" }} return im.And(x) {{- else}} - return x.AsInt{{.WxC}}().And(im).As{{.Vec}}() + return x.AsInt{{.WxC}}().And(im).As{{.VType}}() {{- end -}} } // Merge returns x but with elements set to y where m is false. -func (x {{.Vec}}) Merge(y {{.Vec}}, mask Mask{{.WxC}}) {{.Vec}} { +func (x {{.VType}}) Merge(y {{.VType}}, mask Mask{{.WxC}}) {{.VType}} { {{- if eq .Base "Int" }} return y.blendMasked(x, mask) {{- else}} ix := x.AsInt{{.WxC}}() iy := y.AsInt{{.WxC}}() - return iy.blendMasked(ix, mask).As{{.Vec}}() + return iy.blendMasked(ix, mask).As{{.VType}}() {{- end -}} } `) @@ -716,7 +716,7 @@ func (t templateData) CPUfeatureBC() string { case 256: return "AVX2" case 512: - if t.Width <= 16 { + if t.EWidth <= 16 { return "AVX512BW" } return "AVX512F" @@ -725,11 +725,11 @@ func (t templateData) CPUfeatureBC() string { } var broadcastTemplate = templateOf("Broadcast functions", ` -// Broadcast{{.Vec}} returns a vector with the input +// Broadcast{{.VType}} returns a vector with the input // x assigned to all elements of the output. // // Emulated, CPU Feature {{.CPUfeatureBC}} -func Broadcast{{.Vec}}(x {{.Type}}) {{.Vec}} { +func Broadcast{{.VType}}(x {{.Etype}}) {{.VType}} { var z {{.As128BitVec }} return z.SetElem(0, x).Broadcast{{.Vwidth}}() }