1 ! Copyright (C) 2009 Slava Pestov, Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types byte-arrays fry
4 classes.algebra cpu.architecture kernel layouts math sequences
5 math.vectors math.vectors.simd.intrinsics
6 macros generalizations combinators combinators.short-circuit
7 arrays locals compiler.tree.propagation.info
8 compiler.cfg.builder.blocks
9 compiler.cfg.comparisons
10 compiler.cfg.stacks compiler.cfg.stacks.local compiler.cfg.hats
11 compiler.cfg.instructions compiler.cfg.registers
12 compiler.cfg.intrinsics
13 compiler.cfg.intrinsics.alien
14 compiler.cfg.intrinsics.simd.backend
16 FROM: alien.c-types => heap-size char short int longlong float double ;
17 SPECIALIZED-ARRAYS: char uchar short ushort int uint longlong ulonglong float double ;
18 IN: compiler.cfg.intrinsics.simd
22 : sign-bit-mask ( rep -- byte-array )
24 { char-16-rep [ uchar-array{
25 HEX: 80 HEX: 80 HEX: 80 HEX: 80
26 HEX: 80 HEX: 80 HEX: 80 HEX: 80
27 HEX: 80 HEX: 80 HEX: 80 HEX: 80
28 HEX: 80 HEX: 80 HEX: 80 HEX: 80
30 { short-8-rep [ ushort-array{
31 HEX: 8000 HEX: 8000 HEX: 8000 HEX: 8000
32 HEX: 8000 HEX: 8000 HEX: 8000 HEX: 8000
34 { int-4-rep [ uint-array{
35 HEX: 8000,0000 HEX: 8000,0000
36 HEX: 8000,0000 HEX: 8000,0000
38 { longlong-2-rep [ ulonglong-array{
39 HEX: 8000,0000,0000,0000
40 HEX: 8000,0000,0000,0000
44 : ^load-neg-zero-vector ( rep -- dst )
46 { float-4-rep [ float-array{ -0.0 -0.0 -0.0 -0.0 } underlying>> ^^load-constant ] }
47 { double-2-rep [ double-array{ -0.0 -0.0 } underlying>> ^^load-constant ] }
50 : ^load-add-sub-vector ( rep -- dst )
52 { float-4-rep [ float-array{ -0.0 0.0 -0.0 0.0 } underlying>> ^^load-constant ] }
53 { double-2-rep [ double-array{ -0.0 0.0 } underlying>> ^^load-constant ] }
54 { char-16-rep [ char-array{ -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 } underlying>> ^^load-constant ] }
55 { short-8-rep [ short-array{ -1 0 -1 0 -1 0 -1 0 } underlying>> ^^load-constant ] }
56 { int-4-rep [ int-array{ -1 0 -1 0 } underlying>> ^^load-constant ] }
57 { longlong-2-rep [ longlong-array{ -1 0 } underlying>> ^^load-constant ] }
60 : ^load-half-vector ( rep -- dst )
62 { float-4-rep [ float-array{ 0.5 0.5 0.5 0.5 } underlying>> ^^load-constant ] }
63 { double-2-rep [ double-array{ 0.5 0.5 } underlying>> ^^load-constant ] }
66 : >variable-shuffle ( shuffle rep -- shuffle' )
67 rep-component-type heap-size
68 [ dup <repetition> >byte-array ]
69 [ iota >byte-array ] bi
70 '[ _ n*v _ v+ ] map concat ;
72 : ^load-immediate-shuffle ( shuffle rep -- dst )
73 >variable-shuffle ^^load-constant ;
75 :: ^blend-vector ( mask true false rep -- dst )
76 true mask rep ^^and-vector
77 mask false rep ^^andn-vector
80 : ^not-vector ( src rep -- dst )
83 [ [ ^^fill-vector ] [ ^^xor-vector ] bi ]
86 :: ^((compare-vector)) ( src1 src2 rep {cc,swap} -- dst )
87 {cc,swap} first2 :> ( cc swap? )
89 [ src2 src1 rep cc ^^compare-vector ]
90 [ src1 src2 rep cc ^^compare-vector ] if ;
92 :: ^(compare-vector) ( src1 src2 rep orig-cc -- dst )
93 rep orig-cc %compare-vector-ccs :> ( ccs not? )
96 [ rep not? [ ^^fill-vector ] [ ^^zero-vector ] if ]
98 ccs unclip :> ( rest-ccs first-cc )
99 src1 src2 rep first-cc ^((compare-vector)) :> first-dst
102 [ [ src1 src2 rep ] dip ^((compare-vector)) rep ^^or-vector ]
105 not? [ rep ^not-vector ] when
108 :: ^minmax-compare-vector ( src1 src2 rep cc -- dst )
110 { cc< [ src1 src2 rep ^^max-vector src1 rep cc/= ^(compare-vector) ] }
111 { cc<= [ src1 src2 rep ^^min-vector src1 rep cc= ^(compare-vector) ] }
112 { cc> [ src1 src2 rep ^^min-vector src1 rep cc/= ^(compare-vector) ] }
113 { cc>= [ src1 src2 rep ^^max-vector src1 rep cc= ^(compare-vector) ] }
116 : ^compare-vector ( src1 src2 rep cc -- dst )
118 [ ^(compare-vector) ]
119 [ ^minmax-compare-vector ]
120 { unsigned-int-vector-rep [| src1 src2 rep cc |
121 rep sign-bit-mask ^^load-constant :> sign-bits
122 src1 sign-bits rep ^^xor-vector
123 src2 sign-bits rep ^^xor-vector
124 rep signed-rep cc ^(compare-vector)
128 : ^unpack-vector-head ( src rep -- dst )
130 [ ^^unpack-vector-head ]
131 { unsigned-int-vector-rep [ [ ^^zero-vector ] [ ^^merge-vector-head ] bi ] }
132 { signed-int-vector-rep [| src rep |
133 src src rep ^^merge-vector-head :> merged
134 rep rep-component-type heap-size 8 * :> bits
135 merged bits rep widen-vector-rep ^^shr-vector-imm
137 { signed-int-vector-rep [| src rep |
138 rep ^^zero-vector :> zero
139 zero src rep cc> ^compare-vector :> sign
140 src sign rep ^^merge-vector-head
144 : ^unpack-vector-tail ( src rep -- dst )
146 [ ^^unpack-vector-tail ]
147 [ [ ^^tail>head-vector ] [ ^^unpack-vector-head ] bi ]
148 { unsigned-int-vector-rep [ [ ^^zero-vector ] [ ^^merge-vector-tail ] bi ] }
149 { signed-int-vector-rep [| src rep |
150 src src rep ^^merge-vector-tail :> merged
151 rep rep-component-type heap-size 8 * :> bits
152 merged bits rep widen-vector-rep ^^shr-vector-imm
154 { signed-int-vector-rep [| src rep |
155 rep ^^zero-vector :> zero
156 zero src rep cc> ^compare-vector :> sign
157 src sign rep ^^merge-vector-tail
161 PREDICATE: fixnum-vector-rep < int-vector-rep
162 rep-component-type heap-size cell < ;
164 : ^(sum-vector-2) ( src rep -- dst )
166 [ dupd ^^horizontal-add-vector ]
168 src src rep ^^merge-vector-head :> head
169 src src rep ^^merge-vector-tail :> tail
170 head tail rep ^^add-vector
174 : ^(sum-vector-4) ( src rep -- dst )
177 [ dupd ^^horizontal-add-vector ]
178 [ dupd ^^horizontal-add-vector ] bi
181 src src rep ^^merge-vector-head :> head
182 src src rep ^^merge-vector-tail :> tail
183 head tail rep ^^add-vector :> src'
185 rep widen-vector-rep :> rep'
186 src' src' rep' ^^merge-vector-head :> head'
187 src' src' rep' ^^merge-vector-tail :> tail'
188 head' tail' rep ^^add-vector
192 : ^(sum-vector-8) ( src rep -- dst )
195 [ dupd ^^horizontal-add-vector ]
196 [ dupd ^^horizontal-add-vector ]
197 [ dupd ^^horizontal-add-vector ] tri
200 src src rep ^^merge-vector-head :> head
201 src src rep ^^merge-vector-tail :> tail
202 head tail rep ^^add-vector :> src'
204 rep widen-vector-rep :> rep'
205 src' src' rep' ^^merge-vector-head :> head'
206 src' src' rep' ^^merge-vector-tail :> tail'
207 head' tail' rep ^^add-vector :> src''
209 rep' widen-vector-rep :> rep''
210 src'' src'' rep'' ^^merge-vector-head :> head''
211 src'' src'' rep'' ^^merge-vector-tail :> tail''
212 head'' tail'' rep ^^add-vector
216 : ^(sum-vector-16) ( src rep -- dst )
220 [ dupd ^^horizontal-add-vector ]
221 [ dupd ^^horizontal-add-vector ]
222 [ dupd ^^horizontal-add-vector ]
223 [ dupd ^^horizontal-add-vector ]
227 src src rep ^^merge-vector-head :> head
228 src src rep ^^merge-vector-tail :> tail
229 head tail rep ^^add-vector :> src'
231 rep widen-vector-rep :> rep'
232 src' src' rep' ^^merge-vector-head :> head'
233 src' src' rep' ^^merge-vector-tail :> tail'
234 head' tail' rep ^^add-vector :> src''
236 rep' widen-vector-rep :> rep''
237 src'' src'' rep'' ^^merge-vector-head :> head''
238 src'' src'' rep'' ^^merge-vector-tail :> tail''
239 head'' tail'' rep ^^add-vector :> src'''
241 rep'' widen-vector-rep :> rep'''
242 src''' src''' rep''' ^^merge-vector-head :> head'''
243 src''' src''' rep''' ^^merge-vector-tail :> tail'''
244 head''' tail''' rep ^^add-vector
248 : ^(sum-vector) ( src rep -- dst )
251 { 2 [ ^(sum-vector-2) ] }
252 { 4 [ ^(sum-vector-4) ] }
253 { 8 [ ^(sum-vector-8) ] }
254 { 16 [ ^(sum-vector-16) ] }
256 ] [ ^^vector>scalar ] bi ;
258 : ^sum-vector ( src rep -- dst )
260 { float-vector-rep [ ^(sum-vector) ] }
261 { fixnum-vector-rep [| src rep |
262 src rep ^unpack-vector-head :> head
263 src rep ^unpack-vector-tail :> tail
264 rep widen-vector-rep :> wide-rep
265 head tail wide-rep ^^add-vector wide-rep
270 : shuffle? ( obj -- ? ) { [ array? ] [ [ integer? ] all? ] } 1&& ;
272 : ^shuffle-vector-imm ( src1 shuffle rep -- dst )
273 [ rep-length 0 pad-tail ] keep {
274 [ ^^shuffle-vector-imm ]
275 [ [ ^load-immediate-shuffle ] [ ^^shuffle-vector ] bi ]
278 : ^broadcast-vector ( src n rep -- dst )
279 [ rep-length swap <array> ] keep
280 ^shuffle-vector-imm ;
282 : ^with-vector ( src rep -- dst )
283 [ ^^scalar>vector ] keep [ 0 ] dip ^broadcast-vector ;
285 : ^select-vector ( src n rep -- dst )
286 [ ^broadcast-vector ] keep ^^vector>scalar ;
290 : emit-simd-v+ ( node -- )
293 } emit-vv-vector-op ;
295 : emit-simd-v- ( node -- )
298 } emit-vv-vector-op ;
300 : emit-simd-vneg ( node -- )
302 { float-vector-rep [ [ ^load-neg-zero-vector swap ] [ ^^sub-vector ] bi ] }
303 { int-vector-rep [ [ ^^zero-vector swap ] [ ^^sub-vector ] bi ] }
306 : emit-simd-v+- ( node -- )
309 { float-vector-rep [| src1 src2 rep |
310 rep ^load-add-sub-vector :> signs
311 src2 signs rep ^^xor-vector :> src2'
312 src1 src2' rep ^^add-vector
314 { int-vector-rep [| src1 src2 rep |
315 rep ^load-add-sub-vector :> signs
316 src2 signs rep ^^xor-vector :> src2'
317 src2' signs rep ^^sub-vector :> src2''
318 src1 src2'' rep ^^add-vector
320 } emit-vv-vector-op ;
322 : emit-simd-vs+ ( node -- )
324 { float-vector-rep [ ^^add-vector ] }
325 { int-vector-rep [ ^^saturated-add-vector ] }
326 } emit-vv-vector-op ;
328 : emit-simd-vs- ( node -- )
330 { float-vector-rep [ ^^sub-vector ] }
331 { int-vector-rep [ ^^saturated-sub-vector ] }
332 } emit-vv-vector-op ;
334 : emit-simd-vs* ( node -- )
336 { float-vector-rep [ ^^mul-vector ] }
337 { int-vector-rep [ ^^saturated-mul-vector ] }
338 } emit-vv-vector-op ;
340 : emit-simd-v* ( node -- )
343 } emit-vv-vector-op ;
345 : emit-simd-v*high ( node -- )
347 [ ^^mul-high-vector ]
348 } emit-vv-vector-op ;
350 : emit-simd-v*hs+ ( node -- )
352 [ ^^mul-horizontal-add-vector ]
353 } emit-vv-vector-op ;
355 : emit-simd-v/ ( node -- )
358 } emit-vv-vector-op ;
360 : emit-simd-vmin ( node -- )
364 [ cc< ^compare-vector ]
365 [ ^blend-vector ] 3bi
367 } emit-vv-vector-op ;
369 : emit-simd-vmax ( node -- )
373 [ cc> ^compare-vector ]
374 [ ^blend-vector ] 3bi
376 } emit-vv-vector-op ;
378 : emit-simd-vavg ( node -- )
381 { float-vector-rep [| src1 src2 rep |
382 src1 src2 rep ^^add-vector
383 rep ^load-half-vector rep ^^mul-vector
385 } emit-vv-vector-op ;
387 : emit-simd-v. ( node -- )
390 { float-vector-rep [ [ ^^mul-vector ] [ ^sum-vector ] bi ] }
391 } emit-vv-vector-op ;
393 : emit-simd-vsad ( node -- )
396 [ ^^sad-vector dup { 2 3 0 1 } int-4-rep ^^shuffle-vector-imm int-4-rep ^^add-vector ]
397 [ widen-vector-rep ^^vector>scalar ] bi
399 } emit-vv-vector-op ;
401 : emit-simd-vsqrt ( node -- )
406 : emit-simd-sum ( node -- )
411 : emit-simd-vabs ( node -- )
413 { unsigned-int-vector-rep [ drop ] }
415 { float-vector-rep [ [ ^load-neg-zero-vector ] [ swapd ^^andn-vector ] bi ] }
416 { int-vector-rep [| src rep |
417 rep ^^zero-vector :> zero
418 zero src rep ^^sub-vector :> -src
419 zero src rep cc> ^compare-vector :> sign
420 sign -src src rep ^blend-vector
424 : emit-simd-vand ( node -- )
427 } emit-vv-vector-op ;
429 : emit-simd-vandn ( node -- )
432 } emit-vv-vector-op ;
434 : emit-simd-vor ( node -- )
437 } emit-vv-vector-op ;
439 : emit-simd-vxor ( node -- )
442 } emit-vv-vector-op ;
444 : emit-simd-vnot ( node -- )
449 : emit-simd-vlshift ( node -- )
454 } [ integer? ] emit-vv-or-vl-vector-op ;
456 : emit-simd-vrshift ( node -- )
461 } [ integer? ] emit-vv-or-vl-vector-op ;
463 : emit-simd-hlshift ( node -- )
465 [ ^^horizontal-shl-vector-imm ]
466 } [ integer? ] emit-vl-vector-op ;
468 : emit-simd-hrshift ( node -- )
470 [ ^^horizontal-shr-vector-imm ]
471 } [ integer? ] emit-vl-vector-op ;
473 : emit-simd-vshuffle-elements ( node -- )
475 [ ^shuffle-vector-imm ]
476 } [ shuffle? ] emit-vl-vector-op ;
478 : emit-simd-vshuffle-bytes ( node -- )
481 } emit-vv-vector-op ;
483 : emit-simd-vmerge-head ( node -- )
485 [ ^^merge-vector-head ]
486 } emit-vv-vector-op ;
488 : emit-simd-vmerge-tail ( node -- )
490 [ ^^merge-vector-tail ]
491 } emit-vv-vector-op ;
493 : emit-simd-v<= ( node -- )
495 [ cc<= ^compare-vector ]
496 } emit-vv-vector-op ;
497 : emit-simd-v< ( node -- )
499 [ cc< ^compare-vector ]
500 } emit-vv-vector-op ;
501 : emit-simd-v= ( node -- )
503 [ cc= ^compare-vector ]
504 } emit-vv-vector-op ;
505 : emit-simd-v> ( node -- )
507 [ cc> ^compare-vector ]
508 } emit-vv-vector-op ;
509 : emit-simd-v>= ( node -- )
511 [ cc>= ^compare-vector ]
512 } emit-vv-vector-op ;
513 : emit-simd-vunordered? ( node -- )
515 [ cc/<>= ^compare-vector ]
516 } emit-vv-vector-op ;
518 : emit-simd-vany? ( node -- )
520 [ vcc-any ^^test-vector ]
522 : emit-simd-vall? ( node -- )
524 [ vcc-all ^^test-vector ]
526 : emit-simd-vnone? ( node -- )
528 [ vcc-none ^^test-vector ]
531 : emit-simd-v>float ( node -- )
533 { float-vector-rep [ drop ] }
534 { int-vector-rep [ ^^integer>float-vector ] }
537 : emit-simd-v>integer ( node -- )
539 { float-vector-rep [ ^^float>integer-vector ] }
540 { int-vector-rep [ drop ] }
543 : emit-simd-vpack-signed ( node -- )
545 [ ^^signed-pack-vector ]
546 } emit-vv-vector-op ;
548 : emit-simd-vpack-unsigned ( node -- )
550 [ ^^unsigned-pack-vector ]
551 } emit-vv-vector-op ;
553 : emit-simd-vunpack-head ( node -- )
555 [ ^unpack-vector-head ]
558 : emit-simd-vunpack-tail ( node -- )
560 [ ^unpack-vector-tail ]
563 : emit-simd-with ( node -- )
565 { fixnum-vector-rep [ ^with-vector ] }
566 { float-vector-rep [ ^with-vector ] }
569 : emit-simd-gather-2 ( node -- )
571 { fixnum-vector-rep [ ^^gather-vector-2 ] }
572 { float-vector-rep [ ^^gather-vector-2 ] }
573 } emit-vv-vector-op ;
575 : emit-simd-gather-4 ( node -- )
577 { fixnum-vector-rep [ ^^gather-vector-4 ] }
578 { float-vector-rep [ ^^gather-vector-4 ] }
579 } emit-vvvv-vector-op ;
581 : emit-simd-select ( node -- )
583 { fixnum-vector-rep [ ^select-vector ] }
584 { float-vector-rep [ ^select-vector ] }
585 } [ integer? ] emit-vl-vector-op ;
587 : emit-alien-vector ( node -- )
590 ds-drop prepare-alien-getter
591 _ ^^alien-vector ds-push
593 [ inline-alien-getter? ] inline-alien
594 ] with { [ %alien-vector-reps member? ] } if-literals-match ;
596 : emit-set-alien-vector ( node -- )
599 ds-drop prepare-alien-setter ds-pop
602 [ byte-array inline-alien-setter? ]
604 ] with { [ %alien-vector-reps member? ] } if-literals-match ;
608 { (simd-v+) [ emit-simd-v+ ] }
609 { (simd-v-) [ emit-simd-v- ] }
610 { (simd-vneg) [ emit-simd-vneg ] }
611 { (simd-v+-) [ emit-simd-v+- ] }
612 { (simd-vs+) [ emit-simd-vs+ ] }
613 { (simd-vs-) [ emit-simd-vs- ] }
614 { (simd-vs*) [ emit-simd-vs* ] }
615 { (simd-v*) [ emit-simd-v* ] }
616 { (simd-v*high) [ emit-simd-v*high ] }
617 { (simd-v*hs+) [ emit-simd-v*hs+ ] }
618 { (simd-v/) [ emit-simd-v/ ] }
619 { (simd-vmin) [ emit-simd-vmin ] }
620 { (simd-vmax) [ emit-simd-vmax ] }
621 { (simd-vavg) [ emit-simd-vavg ] }
622 { (simd-v.) [ emit-simd-v. ] }
623 { (simd-vsad) [ emit-simd-vsad ] }
624 { (simd-vsqrt) [ emit-simd-vsqrt ] }
625 { (simd-sum) [ emit-simd-sum ] }
626 { (simd-vabs) [ emit-simd-vabs ] }
627 { (simd-vbitand) [ emit-simd-vand ] }
628 { (simd-vbitandn) [ emit-simd-vandn ] }
629 { (simd-vbitor) [ emit-simd-vor ] }
630 { (simd-vbitxor) [ emit-simd-vxor ] }
631 { (simd-vbitnot) [ emit-simd-vnot ] }
632 { (simd-vand) [ emit-simd-vand ] }
633 { (simd-vandn) [ emit-simd-vandn ] }
634 { (simd-vor) [ emit-simd-vor ] }
635 { (simd-vxor) [ emit-simd-vxor ] }
636 { (simd-vnot) [ emit-simd-vnot ] }
637 { (simd-vlshift) [ emit-simd-vlshift ] }
638 { (simd-vrshift) [ emit-simd-vrshift ] }
639 { (simd-hlshift) [ emit-simd-hlshift ] }
640 { (simd-hrshift) [ emit-simd-hrshift ] }
641 { (simd-vshuffle-elements) [ emit-simd-vshuffle-elements ] }
642 { (simd-vshuffle-bytes) [ emit-simd-vshuffle-bytes ] }
643 { (simd-vmerge-head) [ emit-simd-vmerge-head ] }
644 { (simd-vmerge-tail) [ emit-simd-vmerge-tail ] }
645 { (simd-v<=) [ emit-simd-v<= ] }
646 { (simd-v<) [ emit-simd-v< ] }
647 { (simd-v=) [ emit-simd-v= ] }
648 { (simd-v>) [ emit-simd-v> ] }
649 { (simd-v>=) [ emit-simd-v>= ] }
650 { (simd-vunordered?) [ emit-simd-vunordered? ] }
651 { (simd-vany?) [ emit-simd-vany? ] }
652 { (simd-vall?) [ emit-simd-vall? ] }
653 { (simd-vnone?) [ emit-simd-vnone? ] }
654 { (simd-v>float) [ emit-simd-v>float ] }
655 { (simd-v>integer) [ emit-simd-v>integer ] }
656 { (simd-vpack-signed) [ emit-simd-vpack-signed ] }
657 { (simd-vpack-unsigned) [ emit-simd-vpack-unsigned ] }
658 { (simd-vunpack-head) [ emit-simd-vunpack-head ] }
659 { (simd-vunpack-tail) [ emit-simd-vunpack-tail ] }
660 { (simd-with) [ emit-simd-with ] }
661 { (simd-gather-2) [ emit-simd-gather-2 ] }
662 { (simd-gather-4) [ emit-simd-gather-4 ] }
663 { (simd-select) [ emit-simd-select ] }
664 { alien-vector [ emit-alien-vector ] }
665 { set-alien-vector [ emit-set-alien-vector ] }
666 { assert-positive [ drop ] }
667 } enable-intrinsics ;