1 ! (c)2009 Joe Groff bsd license
2 USING: arrays assocs biassocs byte-arrays byte-arrays.hex
3 classes compiler.cfg compiler.cfg.comparisons compiler.cfg.instructions
4 compiler.cfg.intrinsics.simd compiler.cfg.intrinsics.simd.backend
5 compiler.cfg.registers compiler.cfg.stacks.height compiler.cfg.stacks.local
6 compiler.test compiler.tree compiler.tree.propagation.info
7 cpu.architecture fry hashtables kernel locals make namespaces sequences
8 system tools.test words ;
9 IN: compiler.cfg.intrinsics.simd.tests
11 :: test-node ( rep -- node )
16 { 1 T{ value-info-state { class byte-array } } }
17 { 2 T{ value-info-state { class byte-array } } }
18 { 3 T{ value-info-state { class byte-array } } }
19 { 4 T{ value-info-state { class word } { literal? t } { literal rep } } }
20 { 5 T{ value-info-state { class byte-array } } }
24 :: test-node-literal ( lit rep -- node )
25 lit class-of :> lit-class
30 { 1 T{ value-info-state { class byte-array } } }
31 { 2 T{ value-info-state { class byte-array } } }
32 { 3 T{ value-info-state { class lit-class } { literal? t } { literal lit } } }
33 { 4 T{ value-info-state { class word } { literal? t } { literal rep } } }
34 { 5 T{ value-info-state { class byte-array } } }
38 : test-node-nonliteral-rep ( -- node )
43 { 1 T{ value-info-state { class byte-array } } }
44 { 2 T{ value-info-state { class byte-array } } }
45 { 3 T{ value-info-state { class byte-array } } }
46 { 4 T{ value-info-state { class object } } }
47 { 5 T{ value-info-state { class byte-array } } }
51 : test-compiler-env ( -- x )
54 [ \ basic-block pick set-at ]
55 [ 0 swap associate \ ds-heights pick set-at ]
56 [ 0 swap associate \ rs-heights pick set-at ] tri
57 initial-height-state \ height-state pick set-at
58 HS{ } clone \ local-peek-set pick set-at
59 H{ } clone \ replaces pick set-at
60 H{ } <biassoc> \ locs>vregs pick set-at
61 H{ } clone \ peek-sets pick set-at
62 H{ } clone \ replace-sets pick set-at
63 H{ } clone \ kill-sets pick set-at ;
65 : make-classes ( quot -- seq )
66 { } make [ class-of ] map ; inline
68 : test-emit ( cpu rep quot -- node )
71 test-compiler-env [ _ test-node @ ] with-variables
73 ] make-classes ; inline
75 : test-emit-literal ( cpu lit rep quot -- node )
78 test-compiler-env [ _ _ test-node-literal @ ] with-variables
80 ] make-classes ; inline
82 : test-emit-nonliteral-rep ( cpu quot -- node )
85 test-compiler-env [ test-node-nonliteral-rep @ ] with-variables
87 ] make-classes ; inline
90 { char-16-rep short-8-rep int-4-rep longlong-2-rep float-4-rep double-2-rep }
93 char-16-rep short-8-rep int-4-rep longlong-2-rep float-4-rep double-2-rep
94 uchar-16-rep ushort-8-rep uint-4-rep ulonglong-2-rep
99 TUPLE: simple-ops-cpu ;
100 M: simple-ops-cpu %zero-vector-reps all-reps ;
101 M: simple-ops-cpu %fill-vector-reps all-reps ;
102 M: simple-ops-cpu %add-vector-reps all-reps ;
103 M: simple-ops-cpu %sub-vector-reps all-reps ;
104 M: simple-ops-cpu %mul-vector-reps all-reps ;
105 M: simple-ops-cpu %div-vector-reps all-reps ;
106 M: simple-ops-cpu %andn-vector-reps all-reps ;
107 M: simple-ops-cpu %and-vector-reps all-reps ;
108 M: simple-ops-cpu %or-vector-reps all-reps ;
109 M: simple-ops-cpu %xor-vector-reps all-reps ;
110 M: simple-ops-cpu %merge-vector-reps all-reps ;
111 M: simple-ops-cpu %sqrt-vector-reps all-reps ;
112 M: simple-ops-cpu %move-vector-mask-reps all-reps ;
113 M: simple-ops-cpu %test-vector-reps all-reps ;
114 M: simple-ops-cpu %signed-pack-vector-reps all-reps ;
115 M: simple-ops-cpu %unsigned-pack-vector-reps all-reps ;
116 M: simple-ops-cpu %gather-vector-2-reps { longlong-2-rep ulonglong-2-rep double-2-rep } ;
117 M: simple-ops-cpu %gather-vector-4-reps { int-4-rep uint-4-rep float-4-rep } ;
118 M: simple-ops-cpu %alien-vector-reps all-reps ;
122 [ simple-ops-cpu float-4-rep [ emit-simd-v+ ] test-emit ]
127 [ simple-ops-cpu float-4-rep [ emit-simd-v- ] test-emit ]
131 { { ##load-reference ##sub-vector } }
132 [ simple-ops-cpu float-4-rep [ emit-simd-vneg ] test-emit ]
135 { { ##zero-vector ##sub-vector } }
136 [ simple-ops-cpu int-4-rep [ emit-simd-vneg ] test-emit ]
141 [ simple-ops-cpu float-4-rep [ emit-simd-v* ] test-emit ]
146 [ simple-ops-cpu float-4-rep [ emit-simd-v/ ] test-emit ]
149 TUPLE: addsub-cpu < simple-ops-cpu ;
150 M: addsub-cpu %add-sub-vector-reps { int-4-rep float-4-rep } ;
153 { { ##add-sub-vector } }
154 [ addsub-cpu float-4-rep [ emit-simd-v+- ] test-emit ]
157 { { ##load-reference ##xor-vector ##add-vector } }
158 [ simple-ops-cpu float-4-rep [ emit-simd-v+- ] test-emit ]
161 { { ##load-reference ##xor-vector ##sub-vector ##add-vector } }
162 [ simple-ops-cpu int-4-rep [ emit-simd-v+- ] test-emit ]
165 TUPLE: saturating-cpu < simple-ops-cpu ;
166 M: saturating-cpu %saturated-add-vector-reps { int-4-rep } ;
167 M: saturating-cpu %saturated-sub-vector-reps { int-4-rep } ;
168 M: saturating-cpu %saturated-mul-vector-reps { int-4-rep } ;
172 [ simple-ops-cpu float-4-rep [ emit-simd-vs+ ] test-emit ]
176 [ saturating-cpu float-4-rep [ emit-simd-vs+ ] test-emit ]
179 { { ##saturated-add-vector } }
180 [ saturating-cpu int-4-rep [ emit-simd-vs+ ] test-emit ]
185 [ simple-ops-cpu float-4-rep [ emit-simd-vs- ] test-emit ]
189 [ saturating-cpu float-4-rep [ emit-simd-vs- ] test-emit ]
192 { { ##saturated-sub-vector } }
193 [ saturating-cpu int-4-rep [ emit-simd-vs- ] test-emit ]
198 [ simple-ops-cpu float-4-rep [ emit-simd-vs* ] test-emit ]
202 [ saturating-cpu float-4-rep [ emit-simd-vs* ] test-emit ]
205 { { ##saturated-mul-vector } }
206 [ saturating-cpu int-4-rep [ emit-simd-vs* ] test-emit ]
209 TUPLE: minmax-cpu < simple-ops-cpu ;
210 M: minmax-cpu %min-vector-reps signed-reps ;
211 M: minmax-cpu %max-vector-reps signed-reps ;
212 M: minmax-cpu %compare-vector-reps { cc= cc/= } member? [ signed-reps ] [ { } ] if ;
213 M: minmax-cpu %compare-vector-ccs nip f 2array 1array f ;
215 TUPLE: compare-cpu < simple-ops-cpu ;
216 M: compare-cpu %compare-vector-reps drop signed-reps ;
217 M: compare-cpu %compare-vector-ccs nip f 2array 1array f ;
221 [ minmax-cpu float-4-rep [ emit-simd-vmin ] test-emit ]
224 { { ##compare-vector ##and-vector ##andn-vector ##or-vector } }
225 [ compare-cpu float-4-rep [ emit-simd-vmin ] test-emit ]
230 [ minmax-cpu float-4-rep [ emit-simd-vmax ] test-emit ]
233 { { ##compare-vector ##and-vector ##andn-vector ##or-vector } }
234 [ compare-cpu float-4-rep [ emit-simd-vmax ] test-emit ]
237 TUPLE: dot-cpu < simple-ops-cpu ;
238 M: dot-cpu %dot-vector-reps { float-4-rep } ;
240 TUPLE: horizontal-cpu < simple-ops-cpu ;
241 M: horizontal-cpu %horizontal-add-vector-reps signed-reps ;
242 M: horizontal-cpu %unpack-vector-head-reps signed-reps ;
243 M: horizontal-cpu %unpack-vector-tail-reps signed-reps ;
247 [ dot-cpu float-4-rep [ emit-simd-v. ] test-emit ]
250 { { ##mul-vector ##horizontal-add-vector ##horizontal-add-vector ##vector>scalar } }
251 [ horizontal-cpu float-4-rep [ emit-simd-v. ] test-emit ]
256 ##merge-vector-head ##merge-vector-tail ##add-vector
257 ##merge-vector-head ##merge-vector-tail ##add-vector
260 [ simple-ops-cpu float-4-rep [ emit-simd-v. ] test-emit ]
264 { { ##sqrt-vector } }
265 [ simple-ops-cpu float-4-rep [ emit-simd-vsqrt ] test-emit ]
269 { { ##horizontal-add-vector ##vector>scalar } }
270 [ horizontal-cpu double-2-rep [ emit-simd-sum ] test-emit ]
273 { { ##horizontal-add-vector ##horizontal-add-vector ##vector>scalar } }
274 [ horizontal-cpu float-4-rep [ emit-simd-sum ] test-emit ]
278 ##unpack-vector-head ##unpack-vector-tail ##add-vector
279 ##horizontal-add-vector ##horizontal-add-vector
282 [ horizontal-cpu short-8-rep [ emit-simd-sum ] test-emit ]
286 ##unpack-vector-head ##unpack-vector-tail ##add-vector
287 ##horizontal-add-vector ##horizontal-add-vector ##horizontal-add-vector
290 [ horizontal-cpu char-16-rep [ emit-simd-sum ] test-emit ]
293 TUPLE: abs-cpu < simple-ops-cpu ;
294 M: abs-cpu %abs-vector-reps signed-reps ;
298 [ simple-ops-cpu uint-4-rep [ emit-simd-vabs ] test-emit ]
302 [ abs-cpu float-4-rep [ emit-simd-vabs ] test-emit ]
305 { { ##load-reference ##andn-vector } }
306 [ simple-ops-cpu float-4-rep [ emit-simd-vabs ] test-emit ]
309 { { ##zero-vector ##sub-vector ##compare-vector ##and-vector ##andn-vector ##or-vector } }
310 [ compare-cpu int-4-rep [ emit-simd-vabs ] test-emit ]
315 [ simple-ops-cpu float-4-rep [ emit-simd-vand ] test-emit ]
319 { { ##andn-vector } }
320 [ simple-ops-cpu float-4-rep [ emit-simd-vandn ] test-emit ]
325 [ simple-ops-cpu float-4-rep [ emit-simd-vor ] test-emit ]
330 [ simple-ops-cpu float-4-rep [ emit-simd-vxor ] test-emit ]
333 TUPLE: not-cpu < simple-ops-cpu ;
334 M: not-cpu %not-vector-reps signed-reps ;
338 [ not-cpu float-4-rep [ emit-simd-vnot ] test-emit ]
341 { { ##fill-vector ##xor-vector } }
342 [ simple-ops-cpu float-4-rep [ emit-simd-vnot ] test-emit ]
345 TUPLE: shift-cpu < simple-ops-cpu ;
346 M: shift-cpu %shl-vector-reps signed-reps ;
347 M: shift-cpu %shr-vector-reps signed-reps ;
349 TUPLE: shift-imm-cpu < simple-ops-cpu ;
350 M: shift-imm-cpu %shl-vector-imm-reps signed-reps ;
351 M: shift-imm-cpu %shr-vector-imm-reps signed-reps ;
353 TUPLE: horizontal-shift-cpu < simple-ops-cpu ;
354 M: horizontal-shift-cpu %horizontal-shl-vector-imm-reps signed-reps ;
355 M: horizontal-shift-cpu %horizontal-shr-vector-imm-reps signed-reps ;
358 { { ##shl-vector-imm } }
359 [ shift-imm-cpu 2 int-4-rep [ emit-simd-vlshift ] test-emit-literal ]
363 [ shift-cpu int-4-rep [ emit-simd-vlshift ] test-emit ]
367 { { ##shr-vector-imm } }
368 [ shift-imm-cpu 2 int-4-rep [ emit-simd-vrshift ] test-emit-literal ]
372 [ shift-cpu int-4-rep [ emit-simd-vrshift ] test-emit ]
376 { { ##horizontal-shl-vector-imm } }
377 [ horizontal-shift-cpu 2 int-4-rep [ emit-simd-hlshift ] test-emit-literal ]
381 { { ##horizontal-shr-vector-imm } }
382 [ horizontal-shift-cpu 2 int-4-rep [ emit-simd-hrshift ] test-emit-literal ]
385 TUPLE: shuffle-imm-cpu < simple-ops-cpu ;
386 M: shuffle-imm-cpu %shuffle-vector-imm-reps signed-reps ;
388 TUPLE: shuffle-cpu < simple-ops-cpu ;
389 M: shuffle-cpu %shuffle-vector-reps signed-reps ;
392 { { ##load-reference ##shuffle-vector } }
393 [ shuffle-cpu { 0 1 2 3 } int-4-rep [ emit-simd-vshuffle-elements ] test-emit-literal ]
396 { { ##shuffle-vector-imm } }
397 [ shuffle-imm-cpu { 0 1 2 3 } int-4-rep [ emit-simd-vshuffle-elements ] test-emit-literal ]
401 { { ##shuffle-vector } }
402 [ shuffle-cpu int-4-rep [ emit-simd-vshuffle-bytes ] test-emit ]
406 { { ##merge-vector-head } }
407 [ simple-ops-cpu float-4-rep [ emit-simd-vmerge-head ] test-emit ]
411 { { ##merge-vector-tail } }
412 [ simple-ops-cpu float-4-rep [ emit-simd-vmerge-tail ] test-emit ]
416 { { ##compare-vector } }
417 [ compare-cpu int-4-rep [ emit-simd-v<= ] test-emit ]
420 { { ##min-vector ##compare-vector } }
421 [ minmax-cpu int-4-rep [ emit-simd-v<= ] test-emit ]
424 { { ##load-reference ##xor-vector ##xor-vector ##compare-vector } }
425 [ compare-cpu uint-4-rep [ emit-simd-v<= ] test-emit ]
429 { { ##test-vector } }
430 [ simple-ops-cpu int-4-rep [ emit-simd-vany? ] test-emit ]
433 TUPLE: convert-cpu < simple-ops-cpu ;
434 M: convert-cpu %integer>float-vector-reps { int-4-rep } ;
435 M: convert-cpu %float>integer-vector-reps { float-4-rep } ;
439 [ convert-cpu float-4-rep [ emit-simd-v>float ] test-emit ]
442 { { ##integer>float-vector } }
443 [ convert-cpu int-4-rep [ emit-simd-v>float ] test-emit ]
448 [ convert-cpu int-4-rep [ emit-simd-v>integer ] test-emit ]
451 { { ##float>integer-vector } }
452 [ convert-cpu float-4-rep [ emit-simd-v>integer ] test-emit ]
456 { { ##signed-pack-vector } }
457 [ simple-ops-cpu int-4-rep [ emit-simd-vpack-signed ] test-emit ]
461 { { ##unsigned-pack-vector } }
462 [ simple-ops-cpu int-4-rep [ emit-simd-vpack-unsigned ] test-emit ]
465 TUPLE: unpack-head-cpu < simple-ops-cpu ;
466 M: unpack-head-cpu %unpack-vector-head-reps all-reps ;
467 TUPLE: unpack-cpu < unpack-head-cpu ;
468 M: unpack-cpu %unpack-vector-tail-reps all-reps ;
471 { { ##unpack-vector-head } }
472 [ unpack-head-cpu int-4-rep [ emit-simd-vunpack-head ] test-emit ]
475 { { ##zero-vector ##merge-vector-head } }
476 [ simple-ops-cpu uint-4-rep [ emit-simd-vunpack-head ] test-emit ]
479 { { ##merge-vector-head ##shr-vector-imm } }
480 [ shift-imm-cpu int-4-rep [ emit-simd-vunpack-head ] test-emit ]
483 { { ##zero-vector ##compare-vector ##merge-vector-head } }
484 [ compare-cpu int-4-rep [ emit-simd-vunpack-head ] test-emit ]
488 { { ##unpack-vector-tail } }
489 [ unpack-cpu int-4-rep [ emit-simd-vunpack-tail ] test-emit ]
492 { { ##tail>head-vector ##unpack-vector-head } }
493 [ unpack-head-cpu int-4-rep [ emit-simd-vunpack-tail ] test-emit ]
496 { { ##zero-vector ##merge-vector-tail } }
497 [ simple-ops-cpu uint-4-rep [ emit-simd-vunpack-tail ] test-emit ]
500 { { ##merge-vector-tail ##shr-vector-imm } }
501 [ shift-imm-cpu int-4-rep [ emit-simd-vunpack-tail ] test-emit ]
504 { { ##zero-vector ##compare-vector ##merge-vector-tail } }
505 [ compare-cpu int-4-rep [ emit-simd-vunpack-tail ] test-emit ]
509 { { ##scalar>vector ##shuffle-vector-imm } }
510 [ shuffle-imm-cpu float-4-rep [ emit-simd-with ] test-emit ]
514 { { ##gather-vector-2 } }
515 [ simple-ops-cpu double-2-rep [ emit-simd-gather-2 ] test-emit ]
519 { { ##gather-vector-4 } }
520 [ simple-ops-cpu float-4-rep [ emit-simd-gather-4 ] test-emit ]
524 { { ##shuffle-vector-imm ##vector>scalar } }
525 [ shuffle-imm-cpu 1 float-4-rep [ emit-simd-select ] test-emit-literal ]
528 ! ^load-neg-zero-vector
533 { obj B{ 0 0 0 128 0 0 0 128 0 0 0 128 0 0 0 128 } }
537 { obj B{ 0 0 0 0 0 0 0 128 0 0 0 0 0 0 0 128 } }
542 { float-4-rep double-2-rep } [ ^load-neg-zero-vector drop ] each
546 ! ^load-add-sub-vector
551 { obj B{ 0 0 0 128 0 0 0 0 0 0 0 128 0 0 0 0 } }
555 { obj B{ 0 0 0 0 0 0 0 128 0 0 0 0 0 0 0 0 } }
560 B{ 255 0 255 0 255 0 255 0 255 0 255 0 255 0 255 0 }
566 B{ 255 255 0 0 255 255 0 0 255 255 0 0 255 255 0 0 }
572 B{ 255 255 255 255 0 0 0 0 255 255 255 255 0 0 0 0 }
578 B{ 255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 }
591 } [ ^load-add-sub-vector drop ] each
600 { obj B{ 0 0 0 63 0 0 0 63 0 0 0 63 0 0 0 63 } }
604 { obj B{ 0 0 0 0 0 0 224 63 0 0 0 0 0 0 224 63 } }
609 { float-4-rep double-2-rep } [ ^load-half-vector drop ] each
616 B{ 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 }
617 B{ 0 128 0 128 0 128 0 128 0 128 0 128 0 128 0 128 }
618 B{ 0 0 0 128 0 0 0 128 0 0 0 128 0 0 0 128 }
619 B{ 0 0 0 0 0 0 0 128 0 0 0 0 0 0 0 128 }
622 { char-16-rep short-8-rep int-4-rep longlong-2-rep } [ sign-bit-mask ] map
626 ! test with nonliteral/invalid reps
627 [ simple-ops-cpu [ emit-simd-v+ ] test-emit-nonliteral-rep ]
628 [ bad-simd-intrinsic? ] must-fail-with
630 [ simple-ops-cpu f [ emit-simd-v+ ] test-emit ]
631 [ bad-simd-intrinsic? ] must-fail-with
633 [ simple-ops-cpu 3 [ emit-simd-v+ ] test-emit ]
634 [ bad-simd-intrinsic? ] must-fail-with