1 ! Copyright (C) 2009 Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs biassocs byte-arrays classes
4 compiler.cfg compiler.cfg.comparisons compiler.cfg.instructions
5 compiler.cfg.intrinsics.simd compiler.cfg.intrinsics.simd.backend
6 compiler.cfg.stacks.local compiler.test compiler.tree
7 compiler.tree.propagation.info cpu.architecture fry kernel locals make
8 namespaces sequences 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 )
53 T{ basic-block } 0 0 0 0 height-state boa >>height
54 \ basic-block pick set-at
56 0 0 0 0 height-state boa \ height-state pick set-at
57 HS{ } clone \ local-peek-set pick set-at
58 H{ } clone \ replaces pick set-at
59 H{ } <biassoc> \ locs>vregs pick set-at ;
61 : make-classes ( quot -- seq )
62 { } make [ class-of ] map ; inline
64 : test-emit ( cpu rep quot -- node )
67 test-compiler-env [ _ test-node @ ] with-variables
69 ] make-classes ; inline
71 : test-emit-literal ( cpu lit rep quot -- node )
74 test-compiler-env [ _ _ test-node-literal @ ] with-variables
76 ] make-classes ; inline
78 : test-emit-nonliteral-rep ( cpu quot -- node )
81 test-compiler-env [ test-node-nonliteral-rep @ ] with-variables
83 ] make-classes ; inline
86 { char-16-rep short-8-rep int-4-rep longlong-2-rep float-4-rep double-2-rep }
89 char-16-rep short-8-rep int-4-rep longlong-2-rep float-4-rep double-2-rep
90 uchar-16-rep ushort-8-rep uint-4-rep ulonglong-2-rep
95 TUPLE: simple-ops-cpu ;
96 M: simple-ops-cpu %zero-vector-reps all-reps ;
97 M: simple-ops-cpu %fill-vector-reps all-reps ;
98 M: simple-ops-cpu %add-vector-reps all-reps ;
99 M: simple-ops-cpu %sub-vector-reps all-reps ;
100 M: simple-ops-cpu %mul-vector-reps all-reps ;
101 M: simple-ops-cpu %div-vector-reps all-reps ;
102 M: simple-ops-cpu %andn-vector-reps all-reps ;
103 M: simple-ops-cpu %and-vector-reps all-reps ;
104 M: simple-ops-cpu %or-vector-reps all-reps ;
105 M: simple-ops-cpu %xor-vector-reps all-reps ;
106 M: simple-ops-cpu %merge-vector-reps all-reps ;
107 M: simple-ops-cpu %sqrt-vector-reps all-reps ;
108 M: simple-ops-cpu %move-vector-mask-reps all-reps ;
109 M: simple-ops-cpu %test-vector-reps all-reps ;
110 M: simple-ops-cpu %signed-pack-vector-reps all-reps ;
111 M: simple-ops-cpu %unsigned-pack-vector-reps all-reps ;
112 M: simple-ops-cpu %gather-vector-2-reps { longlong-2-rep ulonglong-2-rep double-2-rep } ;
113 M: simple-ops-cpu %gather-vector-4-reps { int-4-rep uint-4-rep float-4-rep } ;
114 M: simple-ops-cpu %alien-vector-reps all-reps ;
118 [ simple-ops-cpu float-4-rep [ emit-simd-v+ ] test-emit ]
123 [ simple-ops-cpu float-4-rep [ emit-simd-v- ] test-emit ]
127 { { ##load-reference ##sub-vector } }
128 [ simple-ops-cpu float-4-rep [ emit-simd-vneg ] test-emit ]
131 { { ##zero-vector ##sub-vector } }
132 [ simple-ops-cpu int-4-rep [ emit-simd-vneg ] test-emit ]
137 [ simple-ops-cpu float-4-rep [ emit-simd-v* ] test-emit ]
142 [ simple-ops-cpu float-4-rep [ emit-simd-v/ ] test-emit ]
145 TUPLE: addsub-cpu < simple-ops-cpu ;
146 M: addsub-cpu %add-sub-vector-reps { int-4-rep float-4-rep } ;
149 { { ##add-sub-vector } }
150 [ addsub-cpu float-4-rep [ emit-simd-v+- ] test-emit ]
153 { { ##load-reference ##xor-vector ##add-vector } }
154 [ simple-ops-cpu float-4-rep [ emit-simd-v+- ] test-emit ]
157 { { ##load-reference ##xor-vector ##sub-vector ##add-vector } }
158 [ simple-ops-cpu int-4-rep [ emit-simd-v+- ] test-emit ]
161 TUPLE: saturating-cpu < simple-ops-cpu ;
162 M: saturating-cpu %saturated-add-vector-reps { int-4-rep } ;
163 M: saturating-cpu %saturated-sub-vector-reps { int-4-rep } ;
164 M: saturating-cpu %saturated-mul-vector-reps { int-4-rep } ;
168 [ simple-ops-cpu float-4-rep [ emit-simd-vs+ ] test-emit ]
172 [ saturating-cpu float-4-rep [ emit-simd-vs+ ] test-emit ]
175 { { ##saturated-add-vector } }
176 [ saturating-cpu int-4-rep [ emit-simd-vs+ ] test-emit ]
181 [ simple-ops-cpu float-4-rep [ emit-simd-vs- ] test-emit ]
185 [ saturating-cpu float-4-rep [ emit-simd-vs- ] test-emit ]
188 { { ##saturated-sub-vector } }
189 [ saturating-cpu int-4-rep [ emit-simd-vs- ] test-emit ]
194 [ simple-ops-cpu float-4-rep [ emit-simd-vs* ] test-emit ]
198 [ saturating-cpu float-4-rep [ emit-simd-vs* ] test-emit ]
201 { { ##saturated-mul-vector } }
202 [ saturating-cpu int-4-rep [ emit-simd-vs* ] test-emit ]
205 TUPLE: minmax-cpu < simple-ops-cpu ;
206 M: minmax-cpu %min-vector-reps signed-reps ;
207 M: minmax-cpu %max-vector-reps signed-reps ;
208 M: minmax-cpu %compare-vector-reps { cc= cc/= } member? [ signed-reps ] [ { } ] if ;
209 M: minmax-cpu %compare-vector-ccs nip f 2array 1array f ;
211 TUPLE: compare-cpu < simple-ops-cpu ;
212 M: compare-cpu %compare-vector-reps drop signed-reps ;
213 M: compare-cpu %compare-vector-ccs nip f 2array 1array f ;
217 [ minmax-cpu float-4-rep [ emit-simd-vmin ] test-emit ]
220 { { ##compare-vector ##and-vector ##andn-vector ##or-vector } }
221 [ compare-cpu float-4-rep [ emit-simd-vmin ] test-emit ]
226 [ minmax-cpu float-4-rep [ emit-simd-vmax ] test-emit ]
229 { { ##compare-vector ##and-vector ##andn-vector ##or-vector } }
230 [ compare-cpu float-4-rep [ emit-simd-vmax ] test-emit ]
233 TUPLE: dot-cpu < simple-ops-cpu ;
234 M: dot-cpu %dot-vector-reps { float-4-rep } ;
236 TUPLE: horizontal-cpu < simple-ops-cpu ;
237 M: horizontal-cpu %horizontal-add-vector-reps signed-reps ;
238 M: horizontal-cpu %unpack-vector-head-reps signed-reps ;
239 M: horizontal-cpu %unpack-vector-tail-reps signed-reps ;
243 [ dot-cpu float-4-rep [ emit-simd-v. ] test-emit ]
246 { { ##mul-vector ##horizontal-add-vector ##horizontal-add-vector ##vector>scalar } }
247 [ horizontal-cpu float-4-rep [ emit-simd-v. ] test-emit ]
252 ##merge-vector-head ##merge-vector-tail ##add-vector
253 ##merge-vector-head ##merge-vector-tail ##add-vector
256 [ simple-ops-cpu float-4-rep [ emit-simd-v. ] test-emit ]
260 { { ##sqrt-vector } }
261 [ simple-ops-cpu float-4-rep [ emit-simd-vsqrt ] test-emit ]
265 { { ##horizontal-add-vector ##vector>scalar } }
266 [ horizontal-cpu double-2-rep [ emit-simd-sum ] test-emit ]
269 { { ##horizontal-add-vector ##horizontal-add-vector ##vector>scalar } }
270 [ horizontal-cpu float-4-rep [ emit-simd-sum ] test-emit ]
274 ##unpack-vector-head ##unpack-vector-tail ##add-vector
275 ##horizontal-add-vector ##horizontal-add-vector
278 [ horizontal-cpu short-8-rep [ emit-simd-sum ] test-emit ]
282 ##unpack-vector-head ##unpack-vector-tail ##add-vector
283 ##horizontal-add-vector ##horizontal-add-vector ##horizontal-add-vector
286 [ horizontal-cpu char-16-rep [ emit-simd-sum ] test-emit ]
289 TUPLE: abs-cpu < simple-ops-cpu ;
290 M: abs-cpu %abs-vector-reps signed-reps ;
294 [ simple-ops-cpu uint-4-rep [ emit-simd-vabs ] test-emit ]
298 [ abs-cpu float-4-rep [ emit-simd-vabs ] test-emit ]
301 { { ##load-reference ##andn-vector } }
302 [ simple-ops-cpu float-4-rep [ emit-simd-vabs ] test-emit ]
305 { { ##zero-vector ##sub-vector ##compare-vector ##and-vector ##andn-vector ##or-vector } }
306 [ compare-cpu int-4-rep [ emit-simd-vabs ] test-emit ]
311 [ simple-ops-cpu float-4-rep [ emit-simd-vand ] test-emit ]
315 { { ##andn-vector } }
316 [ simple-ops-cpu float-4-rep [ emit-simd-vandn ] test-emit ]
321 [ simple-ops-cpu float-4-rep [ emit-simd-vor ] test-emit ]
326 [ simple-ops-cpu float-4-rep [ emit-simd-vxor ] test-emit ]
329 TUPLE: not-cpu < simple-ops-cpu ;
330 M: not-cpu %not-vector-reps signed-reps ;
334 [ not-cpu float-4-rep [ emit-simd-vnot ] test-emit ]
337 { { ##fill-vector ##xor-vector } }
338 [ simple-ops-cpu float-4-rep [ emit-simd-vnot ] test-emit ]
341 TUPLE: shift-cpu < simple-ops-cpu ;
342 M: shift-cpu %shl-vector-reps signed-reps ;
343 M: shift-cpu %shr-vector-reps signed-reps ;
345 TUPLE: shift-imm-cpu < simple-ops-cpu ;
346 M: shift-imm-cpu %shl-vector-imm-reps signed-reps ;
347 M: shift-imm-cpu %shr-vector-imm-reps signed-reps ;
349 TUPLE: horizontal-shift-cpu < simple-ops-cpu ;
350 M: horizontal-shift-cpu %horizontal-shl-vector-imm-reps signed-reps ;
351 M: horizontal-shift-cpu %horizontal-shr-vector-imm-reps signed-reps ;
354 { { ##shl-vector-imm } }
355 [ shift-imm-cpu 2 int-4-rep [ emit-simd-vlshift ] test-emit-literal ]
359 [ shift-cpu int-4-rep [ emit-simd-vlshift ] test-emit ]
363 { { ##shr-vector-imm } }
364 [ shift-imm-cpu 2 int-4-rep [ emit-simd-vrshift ] test-emit-literal ]
368 [ shift-cpu int-4-rep [ emit-simd-vrshift ] test-emit ]
372 { { ##horizontal-shl-vector-imm } }
373 [ horizontal-shift-cpu 2 int-4-rep [ emit-simd-hlshift ] test-emit-literal ]
377 { { ##horizontal-shr-vector-imm } }
378 [ horizontal-shift-cpu 2 int-4-rep [ emit-simd-hrshift ] test-emit-literal ]
381 TUPLE: shuffle-imm-cpu < simple-ops-cpu ;
382 M: shuffle-imm-cpu %shuffle-vector-imm-reps signed-reps ;
384 TUPLE: shuffle-cpu < simple-ops-cpu ;
385 M: shuffle-cpu %shuffle-vector-reps signed-reps ;
388 { { ##load-reference ##shuffle-vector } }
389 [ shuffle-cpu { 0 1 2 3 } int-4-rep [ emit-simd-vshuffle-elements ] test-emit-literal ]
392 { { ##shuffle-vector-imm } }
393 [ shuffle-imm-cpu { 0 1 2 3 } int-4-rep [ emit-simd-vshuffle-elements ] test-emit-literal ]
397 { { ##shuffle-vector } }
398 [ shuffle-cpu int-4-rep [ emit-simd-vshuffle-bytes ] test-emit ]
402 { { ##merge-vector-head } }
403 [ simple-ops-cpu float-4-rep [ emit-simd-vmerge-head ] test-emit ]
407 { { ##merge-vector-tail } }
408 [ simple-ops-cpu float-4-rep [ emit-simd-vmerge-tail ] test-emit ]
412 { { ##compare-vector } }
413 [ compare-cpu int-4-rep [ emit-simd-v<= ] test-emit ]
416 { { ##min-vector ##compare-vector } }
417 [ minmax-cpu int-4-rep [ emit-simd-v<= ] test-emit ]
420 { { ##load-reference ##xor-vector ##xor-vector ##compare-vector } }
421 [ compare-cpu uint-4-rep [ emit-simd-v<= ] test-emit ]
425 { { ##test-vector } }
426 [ simple-ops-cpu int-4-rep [ emit-simd-vany? ] test-emit ]
429 TUPLE: convert-cpu < simple-ops-cpu ;
430 M: convert-cpu %integer>float-vector-reps { int-4-rep } ;
431 M: convert-cpu %float>integer-vector-reps { float-4-rep } ;
435 [ convert-cpu float-4-rep [ emit-simd-v>float ] test-emit ]
438 { { ##integer>float-vector } }
439 [ convert-cpu int-4-rep [ emit-simd-v>float ] test-emit ]
444 [ convert-cpu int-4-rep [ emit-simd-v>integer ] test-emit ]
447 { { ##float>integer-vector } }
448 [ convert-cpu float-4-rep [ emit-simd-v>integer ] test-emit ]
452 { { ##signed-pack-vector } }
453 [ simple-ops-cpu int-4-rep [ emit-simd-vpack-signed ] test-emit ]
457 { { ##unsigned-pack-vector } }
458 [ simple-ops-cpu int-4-rep [ emit-simd-vpack-unsigned ] test-emit ]
461 TUPLE: unpack-head-cpu < simple-ops-cpu ;
462 M: unpack-head-cpu %unpack-vector-head-reps all-reps ;
463 TUPLE: unpack-cpu < unpack-head-cpu ;
464 M: unpack-cpu %unpack-vector-tail-reps all-reps ;
467 { { ##unpack-vector-head } }
468 [ unpack-head-cpu int-4-rep [ emit-simd-vunpack-head ] test-emit ]
471 { { ##zero-vector ##merge-vector-head } }
472 [ simple-ops-cpu uint-4-rep [ emit-simd-vunpack-head ] test-emit ]
475 { { ##merge-vector-head ##shr-vector-imm } }
476 [ shift-imm-cpu int-4-rep [ emit-simd-vunpack-head ] test-emit ]
479 { { ##zero-vector ##compare-vector ##merge-vector-head } }
480 [ compare-cpu int-4-rep [ emit-simd-vunpack-head ] test-emit ]
484 { { ##unpack-vector-tail } }
485 [ unpack-cpu int-4-rep [ emit-simd-vunpack-tail ] test-emit ]
488 { { ##tail>head-vector ##unpack-vector-head } }
489 [ unpack-head-cpu int-4-rep [ emit-simd-vunpack-tail ] test-emit ]
492 { { ##zero-vector ##merge-vector-tail } }
493 [ simple-ops-cpu uint-4-rep [ emit-simd-vunpack-tail ] test-emit ]
496 { { ##merge-vector-tail ##shr-vector-imm } }
497 [ shift-imm-cpu int-4-rep [ emit-simd-vunpack-tail ] test-emit ]
500 { { ##zero-vector ##compare-vector ##merge-vector-tail } }
501 [ compare-cpu int-4-rep [ emit-simd-vunpack-tail ] test-emit ]
505 { { ##scalar>vector ##shuffle-vector-imm } }
506 [ shuffle-imm-cpu float-4-rep [ emit-simd-with ] test-emit ]
510 { { ##gather-vector-2 } }
511 [ simple-ops-cpu double-2-rep [ emit-simd-gather-2 ] test-emit ]
515 { { ##gather-vector-4 } }
516 [ simple-ops-cpu float-4-rep [ emit-simd-gather-4 ] test-emit ]
520 { { ##shuffle-vector-imm ##vector>scalar } }
521 [ shuffle-imm-cpu 1 float-4-rep [ emit-simd-select ] test-emit-literal ]
524 ! ^load-neg-zero-vector
529 { obj B{ 0 0 0 128 0 0 0 128 0 0 0 128 0 0 0 128 } }
533 { obj B{ 0 0 0 0 0 0 0 128 0 0 0 0 0 0 0 128 } }
538 { float-4-rep double-2-rep } [ ^load-neg-zero-vector drop ] each
542 ! ^load-add-sub-vector
547 { obj B{ 0 0 0 128 0 0 0 0 0 0 0 128 0 0 0 0 } }
551 { obj B{ 0 0 0 0 0 0 0 128 0 0 0 0 0 0 0 0 } }
556 B{ 255 0 255 0 255 0 255 0 255 0 255 0 255 0 255 0 }
562 B{ 255 255 0 0 255 255 0 0 255 255 0 0 255 255 0 0 }
568 B{ 255 255 255 255 0 0 0 0 255 255 255 255 0 0 0 0 }
574 B{ 255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 }
587 } [ ^load-add-sub-vector drop ] each
596 { obj B{ 0 0 0 63 0 0 0 63 0 0 0 63 0 0 0 63 } }
600 { obj B{ 0 0 0 0 0 0 224 63 0 0 0 0 0 0 224 63 } }
605 { float-4-rep double-2-rep } [ ^load-half-vector drop ] each
612 B{ 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 }
613 B{ 0 128 0 128 0 128 0 128 0 128 0 128 0 128 0 128 }
614 B{ 0 0 0 128 0 0 0 128 0 0 0 128 0 0 0 128 }
615 B{ 0 0 0 0 0 0 0 128 0 0 0 0 0 0 0 128 }
618 { char-16-rep short-8-rep int-4-rep longlong-2-rep } [ sign-bit-mask ] map
622 ! test with nonliteral/invalid reps
623 [ simple-ops-cpu [ emit-simd-v+ ] test-emit-nonliteral-rep ]
624 [ bad-simd-intrinsic? ] must-fail-with
626 [ simple-ops-cpu f [ emit-simd-v+ ] test-emit ]
627 [ bad-simd-intrinsic? ] must-fail-with
629 [ simple-ops-cpu 3 [ emit-simd-v+ ] test-emit ]
630 [ bad-simd-intrinsic? ] must-fail-with