1
2
3
4
5 package main
6
7 import (
8 "fmt"
9 "simd/archsimd/_gen/sgutil"
10 "strings"
11 "text/template"
12 )
13
14
15
16 type emulatedShuffle struct {
17 Op string
18 Base string
19 EWidth int
20 Vwidth int
21 CPU string
22 }
23
24 func (s emulatedShuffle) Count() int {
25 return s.Vwidth / s.EWidth
26 }
27
28 func (s emulatedShuffle) VType() string {
29 return fmt.Sprintf("%s%dx%d", s.Base, s.EWidth, s.Count())
30 }
31
32 func (s emulatedShuffle) UType() string {
33 return fmt.Sprintf("Uint%dx%d", s.EWidth, s.Count())
34 }
35
36 func (s emulatedShuffle) LowerOp() string {
37 return strings.ToLower(s.Op[:1]) + s.Op[1:]
38 }
39
40 func (s emulatedShuffle) Select2() string {
41 if s.Op == "InterleaveHi" || s.Op == "ConcatOdd" {
42 return "1, 3"
43 }
44 return "0, 2"
45 }
46
47 func (s emulatedShuffle) Select4() string {
48 if s.Op == "ConcatOdd" {
49 return "1, 3, 5, 7"
50 }
51 return "0, 2, 4, 6"
52 }
53
54 func (s emulatedShuffle) InterleaveOp() string {
55 if s.Op == "ConcatOdd" {
56 return "InterleaveHi"
57 }
58 return "InterleaveLo"
59 }
60
61 func (s emulatedShuffle) InterleaveOpGrouped() string {
62 if s.Op == "ConcatOdd" {
63 return "interleaveHiGrouped"
64 }
65 return "interleaveLoGrouped"
66 }
67
68 var (
69 shuffleDelegateToUintTmpl = sgutil.TemplateNamed("shuffleDelegateToUintTmpl", `
70 // Emulated, CPU Feature: {{.CPU}}
71 func (x {{.VType}}) {{.Op}}(y {{.VType}}) (z {{.VType}}) {
72 return x.ToBits().{{.Op}}(y.ToBits()).BitsTo{{.Base}}{{.EWidth}}()
73 }
74 `)
75
76 shufflePermuteScalars2Tmpl = sgutil.TemplateNamed("shufflePermuteScalars2Tmpl", `
77 // Emulated, CPU Feature: {{.CPU}}
78 func (x {{.VType}}) {{.Op}}(y {{.VType}}) (z {{.VType}}) {
79 return x.concatPermuteScalars({{.Select2}}, y)
80 }
81 `)
82
83 shuffleConcatLookup512Tmpl = sgutil.TemplateNamed("shuffleConcatLookup512Tmpl", `
84 // Emulated, CPU Feature: {{.CPU}}
85 func (x {{.VType}}) {{.Op}}(y {{.VType}}) (z {{.VType}}) {
86 return x.ConcatLookupMod(y, Load{{.UType}}Array(&{{.LowerOp}}{{.EWidth}}x{{.Count}}Idx))
87 }
88 `)
89
90 interleave256IntTmpl = sgutil.TemplateNamed("interleave256IntTmpl", `
91 // Emulated, CPU Feature: {{.CPU}}
92 func (x {{.VType}}) {{.Op}}(y {{.VType}}) (z {{.VType}}) {
93 lo := x.interleaveLoGrouped(y)
94 hi := x.interleaveHiGrouped(y)
95 return lo.concatPermute128Scalars({{.Select2}}, hi)
96 }
97 `)
98
99 interleave256Float64Tmpl = sgutil.TemplateNamed("interleave256Float64Tmpl", `
100 // Emulated, CPU Feature: {{.CPU}}
101 func (x {{.VType}}) {{.Op}}(y {{.VType}}) (z {{.VType}}) {
102 lo := x.concatPermuteScalarsGrouped(0, 2, y)
103 hi := x.concatPermuteScalarsGrouped(1, 3, y)
104 return lo.concatPermute128Scalars({{.Select2}}, hi)
105 }
106 `)
107
108 interleave512Uint8Tmpl = sgutil.TemplateNamed("interleave512Uint8Tmpl", `
109 // Emulated, CPU Feature: {{.CPU}}
110 func (x Uint8x64) {{.Op}}(y Uint8x64) (z Uint8x64) {
111 if X86.AVX512VBMI() {
112 return x.ConcatLookupMod(y, LoadUint8x64Array(&{{.LowerOp}}8x64Idx))
113 }
114 lo := x.interleaveLoGrouped(y).ReshapeToUint64s()
115 hi := x.interleaveHiGrouped(y).ReshapeToUint64s()
116 return lo.ConcatLookupMod(hi, LoadUint64x8Array(&{{.LowerOp}}128x4Idx)).ReshapeToUint8s()
117 }
118 `)
119
120 concat128Int64Tmpl = sgutil.TemplateNamed("concat128Int64Tmpl", `
121 // Emulated, CPU Feature: {{.CPU}}
122 func (x {{.VType}}) {{.Op}}(y {{.VType}}) (z {{.VType}}) {
123 return x.{{.InterleaveOp}}(y)
124 }
125 `)
126
127 concat128Elem32Tmpl = sgutil.TemplateNamed("concat128Elem32Tmpl", `
128 // Emulated, CPU Feature: {{.CPU}}
129 func (x {{.VType}}) {{.Op}}(y {{.VType}}) (z {{.VType}}) {
130 return x.concatPermuteScalars({{.Select4}}, y)
131 }
132 `)
133
134 concatEven128Uint16Tmpl = sgutil.TemplateNamed("concatEven128Uint16Tmpl", `
135 // Emulated, CPU Feature: {{.CPU}}
136 func (x Uint16x8) ConcatEven(y Uint16x8) (z Uint16x8) {
137 idx := LoadInt8x16Array(&trunc32To16Idx)
138 a := x.ReshapeToUint8s().LookupModOrZero(idx).ReshapeToUint64s()
139 b := y.ReshapeToUint8s().LookupModOrZero(idx).ReshapeToUint64s()
140 return a.InterleaveLo(b).ReshapeToUint16s()
141 }
142 `)
143
144 concatOdd128Uint16Tmpl = sgutil.TemplateNamed("concatOdd128Uint16Tmpl", `
145 // Emulated, CPU Feature: {{.CPU}}
146 func (x Uint16x8) ConcatOdd(y Uint16x8) (z Uint16x8) {
147 xo := x.ReshapeToUint32s().BitsToInt32().ShiftAllRight(16)
148 yo := y.ReshapeToUint32s().BitsToInt32().ShiftAllRight(16)
149 return xo.ConcatSaturateToInt16(yo).ToBits()
150 }
151 `)
152
153 concatEven128Uint8Tmpl = sgutil.TemplateNamed("concatEven128Uint8Tmpl", `
154 // Emulated, CPU Feature: {{.CPU}}
155 func (x Uint8x16) ConcatEven(y Uint8x16) (z Uint8x16) {
156 idx := LoadInt8x16Array(&trunc16To8Idx)
157 a := x.LookupModOrZero(idx).ReshapeToUint64s()
158 b := y.LookupModOrZero(idx).ReshapeToUint64s()
159 return a.InterleaveLo(b).ReshapeToUint8s()
160 }
161 `)
162
163 concatOdd128Uint8Tmpl = sgutil.TemplateNamed("concatOdd128Uint8Tmpl", `
164 // Emulated, CPU Feature: {{.CPU}}
165 func (x Uint8x16) ConcatOdd(y Uint8x16) (z Uint8x16) {
166 xo := x.ReshapeToUint16s().BitsToInt16().ShiftAllRight(8)
167 yo := y.ReshapeToUint16s().BitsToInt16().ShiftAllRight(8)
168 return xo.concatSaturateToInt8(yo).ToBits()
169 }
170 `)
171
172 concat256Float64Tmpl = sgutil.TemplateNamed("concat256Float64Tmpl", `
173 // Emulated, CPU Feature: {{.CPU}}
174 func (x Float64x4) {{.Op}}(y Float64x4) (z Float64x4) {
175 lo := x.concatPermute128Scalars(0, 2, y)
176 hi := x.concatPermute128Scalars(1, 3, y)
177 return lo.concatPermuteScalarsGrouped({{.Select2}}, hi)
178 }
179 `)
180
181 concat256Float32Tmpl = sgutil.TemplateNamed("concat256Float32Tmpl", `
182 // Emulated, CPU Feature: {{.CPU}}
183 func (x Float32x8) {{.Op}}(y Float32x8) (z Float32x8) {
184 lo := x.concatPermute128Scalars(0, 2, y)
185 hi := x.concatPermute128Scalars(1, 3, y)
186 return lo.concatPermuteScalarsGrouped({{.Select4}}, hi)
187 }
188 `)
189
190 concat256Uint64Tmpl = sgutil.TemplateNamed("concat256Uint64Tmpl", `
191 // Emulated, CPU Feature: {{.CPU}}
192 func (x Uint64x4) {{.Op}}(y Uint64x4) (z Uint64x4) {
193 return gatherLo64(x.{{.InterleaveOpGrouped}}(y))
194 }
195 `)
196
197 concat256Uint32Tmpl = sgutil.TemplateNamed("concat256Uint32Tmpl", `
198 // Emulated, CPU Feature: {{.CPU}}
199 func (x Uint32x8) {{.Op}}(y Uint32x8) (z Uint32x8) {
200 return x.concatPermuteScalarsGrouped({{.Select4}}, y).LookupMod(LoadUint32x8Array(&gatherLo64Idx))
201 }
202 `)
203
204 concatEven256Uint16Tmpl = sgutil.TemplateNamed("concatEven256Uint16Tmpl", `
205 // Emulated, CPU Feature: {{.CPU}}
206 func (x Uint16x16) ConcatEven(y Uint16x16) (z Uint16x16) {
207 if X86.AVX512() {
208 return x.ConcatLookupMod(y, LoadUint16x16Array(&concatEven16x16Idx))
209 }
210 idx := LoadInt8x32Array(&trunc32To16GroupedIdx)
211 a := x.ReshapeToUint8s().LookupModOrZeroGrouped(idx).ReshapeToUint64s()
212 b := y.ReshapeToUint8s().LookupModOrZeroGrouped(idx).ReshapeToUint64s()
213 return gatherLo64(a.interleaveLoGrouped(b)).ReshapeToUint16s()
214 }
215 `)
216
217 concatOdd256Uint16Tmpl = sgutil.TemplateNamed("concatOdd256Uint16Tmpl", `
218 // Emulated, CPU Feature: {{.CPU}}
219 func (x Uint16x16) ConcatOdd(y Uint16x16) (z Uint16x16) {
220 if X86.AVX512() {
221 return x.ConcatLookupMod(y, LoadUint16x16Array(&concatOdd16x16Idx))
222 }
223 xo := x.ReshapeToUint32s().BitsToInt32().ShiftAllRight(16)
224 yo := y.ReshapeToUint32s().BitsToInt32().ShiftAllRight(16)
225 return gatherLo64(xo.ConcatSaturateToInt16Grouped(yo).ToBits().ReshapeToUint64s()).ReshapeToUint16s()
226 }
227 `)
228
229 concatEven256Uint8Tmpl = sgutil.TemplateNamed("concatEven256Uint8Tmpl", `
230 // Emulated, CPU Feature: {{.CPU}}
231 func (x Uint8x32) ConcatEven(y Uint8x32) (z Uint8x32) {
232 if X86.AVX512VBMI() {
233 return x.ConcatLookupMod(y, LoadUint8x32Array(&concatEven8x32Idx))
234 }
235 idx := LoadInt8x32Array(&trunc16To8GroupedIdx)
236 a := x.LookupModOrZeroGrouped(idx).ReshapeToUint64s()
237 b := y.LookupModOrZeroGrouped(idx).ReshapeToUint64s()
238 return gatherLo64(a.interleaveLoGrouped(b)).ReshapeToUint8s()
239 }
240 `)
241
242 concatOdd256Uint8Tmpl = sgutil.TemplateNamed("concatOdd256Uint8Tmpl", `
243 // Emulated, CPU Feature: {{.CPU}}
244 func (x Uint8x32) ConcatOdd(y Uint8x32) (z Uint8x32) {
245 if X86.AVX512VBMI() {
246 return x.ConcatLookupMod(y, LoadUint8x32Array(&concatOdd8x32Idx))
247 }
248 xo := x.ReshapeToUint16s().BitsToInt16().ShiftAllRight(8)
249 yo := y.ReshapeToUint16s().BitsToInt16().ShiftAllRight(8)
250 return gatherLo64(xo.concatSaturateToInt8Grouped(yo).ToBits().ReshapeToUint64s()).ReshapeToUint8s()
251 }
252 `)
253
254 concat512Uint8Tmpl = sgutil.TemplateNamed("concat512Uint8Tmpl", `
255 // Emulated, CPU Feature: {{.CPU}}
256 func (x Uint8x64) {{.Op}}(y Uint8x64) (z Uint8x64) {
257 if X86.AVX512VBMI() {
258 return x.ConcatLookupMod(y, LoadUint8x64Array(&{{.LowerOp}}8x64Idx))
259 }
260 idx := LoadInt8x64Array(&{{.LowerOp}}8x64GroupedIdx)
261 a := x.LookupModOrZeroGrouped(idx).ReshapeToUint64s()
262 b := y.LookupModOrZeroGrouped(idx).ReshapeToUint64s()
263 return a.ConcatLookupMod(b, LoadUint64x8Array(&concatEven64x8Idx)).ReshapeToUint8s()
264 }
265 `)
266 )
267
268 func interleaveTemplateFor(s emulatedShuffle) *template.Template {
269 switch s.Vwidth {
270 case 128:
271 if s.EWidth == 64 {
272 return shufflePermuteScalars2Tmpl
273 }
274 return shuffleDelegateToUintTmpl
275 case 256:
276 switch {
277 case s.Base == "Float" && s.EWidth == 64:
278 return interleave256Float64Tmpl
279 case s.Base == "Float":
280 return shuffleDelegateToUintTmpl
281 default:
282 return interleave256IntTmpl
283 }
284 case 512:
285 if s.EWidth == 8 {
286 if s.Base == "Int" {
287 return shuffleDelegateToUintTmpl
288 }
289 return interleave512Uint8Tmpl
290 }
291 return shuffleConcatLookup512Tmpl
292 }
293 panic(fmt.Sprintf("unexpected interleave shape: %+v", s))
294 }
295
296 func concatTemplateFor(s emulatedShuffle) *template.Template {
297 odd := s.Op == "ConcatOdd"
298 switch s.Vwidth {
299 case 128:
300 switch s.EWidth {
301 case 64:
302 if s.Base == "Float" {
303 return shufflePermuteScalars2Tmpl
304 }
305 return concat128Int64Tmpl
306 case 32:
307 return concat128Elem32Tmpl
308 case 16:
309 if s.Base == "Int" {
310 return shuffleDelegateToUintTmpl
311 }
312 if odd {
313 return concatOdd128Uint16Tmpl
314 }
315 return concatEven128Uint16Tmpl
316 case 8:
317 if s.Base == "Int" {
318 return shuffleDelegateToUintTmpl
319 }
320 if odd {
321 return concatOdd128Uint8Tmpl
322 }
323 return concatEven128Uint8Tmpl
324 }
325 case 256:
326 if s.Base == "Float" {
327 if s.EWidth == 64 {
328 return concat256Float64Tmpl
329 }
330 return concat256Float32Tmpl
331 }
332 if s.Base == "Int" {
333 return shuffleDelegateToUintTmpl
334 }
335 switch s.EWidth {
336 case 64:
337 return concat256Uint64Tmpl
338 case 32:
339 return concat256Uint32Tmpl
340 case 16:
341 if odd {
342 return concatOdd256Uint16Tmpl
343 }
344 return concatEven256Uint16Tmpl
345 case 8:
346 if odd {
347 return concatOdd256Uint8Tmpl
348 }
349 return concatEven256Uint8Tmpl
350 }
351 case 512:
352 if s.EWidth == 8 {
353 if s.Base == "Int" {
354 return shuffleDelegateToUintTmpl
355 }
356 return concat512Uint8Tmpl
357 }
358 return shuffleConcatLookup512Tmpl
359 }
360 panic(fmt.Sprintf("unexpected concat shape: %+v", s))
361 }
362
363
364
365
366 func emulatedShuffles(filename string) {
367 buf := files.NewGoFile(filename)
368 prologue("tmplgen", "amd64", buf)
369 exec := func(t *template.Template, s emulatedShuffle) {
370 if err := t.Execute(buf, s); err != nil {
371 panic(fmt.Errorf("template execute failed, %v", err))
372 }
373 }
374
375 types := []struct {
376 base string
377 w int
378 }{
379 {"Int", 8}, {"Uint", 8},
380 {"Int", 16}, {"Uint", 16},
381 {"Int", 32}, {"Uint", 32}, {"Float", 32},
382 {"Int", 64}, {"Uint", 64}, {"Float", 64},
383 }
384
385
386 for _, op := range []string{"InterleaveLo", "InterleaveHi"} {
387 for _, vw := range []int{128, 256, 512} {
388 for _, typ := range types {
389 if vw == 128 && typ.base != "Float" {
390 continue
391 }
392 cpu := "AVX512"
393 if vw == 128 || (vw == 256 && typ.base == "Float" && typ.w == 64) {
394 cpu = "AVX"
395 } else if vw == 256 {
396 cpu = "AVX2"
397 }
398 s := emulatedShuffle{op, typ.base, typ.w, vw, cpu}
399 exec(interleaveTemplateFor(s), s)
400 }
401 }
402 }
403
404
405 for _, op := range []string{"ConcatEven", "ConcatOdd"} {
406 for _, vw := range []int{128, 256, 512} {
407 for _, typ := range types {
408 cpu := "AVX512"
409 if vw == 128 || (vw == 256 && typ.base == "Float") {
410 cpu = "AVX"
411 } else if vw == 256 {
412 cpu = "AVX2"
413 }
414 s := emulatedShuffle{op, typ.base, typ.w, vw, cpu}
415 exec(concatTemplateFor(s), s)
416 }
417 }
418 }
419 }
420
View as plain text