]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/intrinsics/simd/simd.factor
implement vneg as an intrinsic in terms of load -0, subtract
[factor.git] / basis / compiler / cfg / intrinsics / simd / simd.factor
1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors byte-arrays fry cpu.architecture kernel math
4 sequences math.vectors.simd.intrinsics macros generalizations
5 combinators combinators.short-circuit arrays locals
6 compiler.tree.propagation.info compiler.cfg.builder.blocks
7 compiler.cfg.comparisons
8 compiler.cfg.stacks compiler.cfg.stacks.local compiler.cfg.hats
9 compiler.cfg.instructions compiler.cfg.registers
10 compiler.cfg.intrinsics.alien
11 specialized-arrays ;
12 FROM: alien.c-types => float double ;
13 SPECIALIZED-ARRAYS: float double ;
14 IN: compiler.cfg.intrinsics.simd
15
16 MACRO: check-elements ( quots -- )
17     [ length '[ _ firstn ] ]
18     [ '[ _ spread ] ]
19     [ length 1 - \ and <repetition> [ ] like ]
20     tri 3append ;
21
22 MACRO: if-literals-match ( quots -- )
23     [ length ] [ ] [ length ] tri
24     ! n quots n n
25     '[
26         ! node quot
27         [
28             dup node-input-infos
29             _ tail-slice* [ literal>> ] map
30             dup _ check-elements
31         ] dip
32         swap [
33             ! node literals quot
34             [ _ firstn ] dip call
35             drop
36         ] [ 2drop emit-primitive ] if
37     ] ;
38
39 : emit-vector-op ( node quot: ( rep -- ) -- )
40     { [ representation? ] } if-literals-match ; inline
41
42 : [binary] ( quot -- quot' )
43     '[ [ ds-drop 2inputs ] dip @ ds-push ] ; inline
44
45 : emit-binary-vector-op ( node quot -- )
46     [binary] emit-vector-op ; inline
47
48 : [unary] ( quot -- quot' )
49     '[ [ ds-drop ds-pop ] dip @ ds-push ] ; inline
50
51 : emit-unary-vector-op ( node quot -- )
52     [unary] emit-vector-op ; inline
53
54 : [unary/param] ( quot -- quot' )
55     '[ [ -2 inc-d ds-pop ] 2dip @ ds-push ] ; inline
56
57 : emit-horizontal-shift ( node quot -- )
58     [unary/param]
59     { [ integer? ] [ representation? ] } if-literals-match ; inline
60
61 : emit-gather-vector-2 ( node -- )
62     [ ^^gather-vector-2 ] emit-binary-vector-op ;
63
64 : emit-gather-vector-4 ( node -- )
65     [
66         ds-drop
67         [
68             D 3 peek-loc
69             D 2 peek-loc
70             D 1 peek-loc
71             D 0 peek-loc
72             -4 inc-d
73         ] dip
74         ^^gather-vector-4
75         ds-push
76     ] emit-vector-op ;
77
78 : shuffle? ( obj -- ? ) { [ array? ] [ [ integer? ] all? ] } 1&& ;
79
80 : emit-shuffle-vector ( node -- )
81     ! Pad the permutation with zeroes if its too short, since we
82     ! can't throw an error at this point.
83     [ [ rep-components 0 pad-tail ] keep ^^shuffle-vector ] [unary/param]
84     { [ shuffle? ] [ representation? ] } if-literals-match ;
85
86 : ^^broadcast-vector ( src n rep -- dst )
87     [ rep-components swap <array> ] keep
88     ^^shuffle-vector ;
89
90 : emit-broadcast-vector ( node -- )
91     [ ^^broadcast-vector ] [unary/param]
92     { [ integer? ] [ representation? ] } if-literals-match ;
93
94 : ^^with-vector ( src rep -- dst )
95     [ ^^scalar>vector ] keep [ 0 ] dip ^^broadcast-vector ;
96
97 : ^^select-vector ( src n rep -- dst )
98     [ ^^broadcast-vector ] keep ^^vector>scalar ;
99
100 : emit-select-vector ( node -- )
101     [ ^^select-vector ] [unary/param]
102     { [ integer? ] [ representation? ] } if-literals-match ; inline
103
104 : emit-alien-vector ( node -- )
105     dup [
106         '[
107             ds-drop prepare-alien-getter
108             _ ^^alien-vector ds-push
109         ]
110         [ inline-alien-getter? ] inline-alien
111     ] with emit-vector-op ;
112
113 : emit-set-alien-vector ( node -- )
114     dup [
115         '[
116             ds-drop prepare-alien-setter ds-pop
117             _ ##set-alien-vector
118         ]
119         [ byte-array inline-alien-setter? ]
120         inline-alien
121     ] with emit-vector-op ;
122
123 : generate-not-vector ( src rep -- dst )
124     dup %not-vector-reps member?
125     [ ^^not-vector ]
126     [ [ ^^fill-vector ] [ ^^xor-vector ] bi ] if ;
127
128 :: (generate-compare-vector) ( src1 src2 rep {cc,swap} -- dst )
129     {cc,swap} first2 :> swap? :> cc
130     swap?
131     [ src2 src1 rep cc ^^compare-vector ]
132     [ src1 src2 rep cc ^^compare-vector ] if ;
133
134 :: generate-compare-vector ( src1 src2 rep orig-cc -- dst )
135     rep orig-cc %compare-vector-ccs :> not? :> ccs
136
137     ccs empty?
138     [ rep not? [ ^^fill-vector ] [ ^^zero-vector ] if ]
139     [
140         ccs unclip :> first-cc :> rest-ccs
141         src1 src2 rep first-cc (generate-compare-vector) :> first-dst
142
143         rest-ccs first-dst
144         [ [ src1 src2 rep ] dip (generate-compare-vector) rep ^^or-vector ]
145         reduce
146
147         not? [ rep generate-not-vector ] when
148     ] if ;
149
150 :: generate-unpack-vector-head ( src rep -- dst )
151     {
152         {
153             [ rep %unpack-vector-head-reps member? ]
154             [ src rep ^^unpack-vector-head ]
155         }
156         [
157             rep ^^zero-vector :> zero
158             zero src rep cc> ^^compare-vector :> sign
159             src sign rep ^^merge-vector-head
160         ] 
161     } cond ;
162
163 :: generate-unpack-vector-tail ( src rep -- dst )
164     {
165         {
166             [ rep %unpack-vector-tail-reps member? ]
167             [ src rep ^^unpack-vector-tail ]
168         }
169         {
170             [ rep %unpack-vector-head-reps member? ]
171             [
172                 src rep ^^tail>head-vector :> tail
173                 tail rep ^^unpack-vector-head
174             ]
175         }
176         [
177             rep ^^zero-vector :> zero
178             zero src rep cc> ^^compare-vector :> sign
179             src sign rep ^^merge-vector-tail
180         ] 
181     } cond ;
182
183 :: generate-neg-vector ( src rep -- dst )
184     rep {
185         { float-4-rep [ float-array{ -0.0 -0.0 -0.0 -0.0 } underlying>> ^^load-constant ] }
186         { double-2-rep [ double-array{ -0.0 -0.0 } underlying>> ^^load-constant ] }
187         [ drop rep ^^zero-vector ]
188     } case
189     src rep ^^sub-vector ;