1 ! Copyright (C) 2009 Slava Pestov, Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.data combinators cpu.architecture fry
4 grouping kernel libc locals math math.libm math.order math.ranges
5 sequences sequences.cords sequences.generalizations sequences.private
6 sequences.unrolled sequences.unrolled.private specialized-arrays
8 QUALIFIED-WITH: alien.c-types c
10 c:char c:short c:int c:longlong
11 c:uchar c:ushort c:uint c:ulonglong
13 IN: math.vectors.simd.intrinsics
15 ! Word props are added later
16 : assert-positive ( x -- y ) ;
20 : >bitwise-vector-rep ( rep -- rep' )
22 { float-4-rep [ uint-4-rep ] }
23 { double-2-rep [ ulonglong-2-rep ] }
27 : >uint-vector-rep ( rep -- rep' )
29 { longlong-2-rep [ ulonglong-2-rep ] }
30 { int-4-rep [ uint-4-rep ] }
31 { short-8-rep [ ushort-8-rep ] }
32 { char-16-rep [ uchar-16-rep ] }
36 : >int-vector-rep ( rep -- rep' )
38 { float-4-rep [ int-4-rep ] }
39 { double-2-rep [ longlong-2-rep ] }
42 : >float-vector-rep ( rep -- rep' )
44 { int-4-rep [ float-4-rep ] }
45 { longlong-2-rep [ double-2-rep ] }
48 : byte>rep-array ( byte-array rep -- array )
50 { char-16-rep [ 16 c:char <c-direct-array> ] }
51 { uchar-16-rep [ 16 c:uchar <c-direct-array> ] }
52 { short-8-rep [ 8 c:short <c-direct-array> ] }
53 { ushort-8-rep [ 8 c:ushort <c-direct-array> ] }
54 { int-4-rep [ 4 c:int <c-direct-array> ] }
55 { uint-4-rep [ 4 c:uint <c-direct-array> ] }
56 { longlong-2-rep [ 2 c:longlong <c-direct-array> ] }
57 { ulonglong-2-rep [ 2 c:ulonglong <c-direct-array> ] }
58 { float-4-rep [ 4 c:float <c-direct-array> ] }
59 { double-2-rep [ 2 c:double <c-direct-array> ] }
62 : >rep-array ( seq rep -- array )
64 { char-16-rep [ c:char >c-array ] }
65 { uchar-16-rep [ c:uchar >c-array ] }
66 { short-8-rep [ c:short >c-array ] }
67 { ushort-8-rep [ c:ushort >c-array ] }
68 { int-4-rep [ c:int >c-array ] }
69 { uint-4-rep [ c:uint >c-array ] }
70 { longlong-2-rep [ c:longlong >c-array ] }
71 { ulonglong-2-rep [ c:ulonglong >c-array ] }
72 { float-4-rep [ c:float >c-array ] }
73 { double-2-rep [ c:double >c-array ] }
76 : <rep-array> ( rep -- array )
78 { char-16-rep [ 16 c:char (c-array) ] }
79 { uchar-16-rep [ 16 c:uchar (c-array) ] }
80 { short-8-rep [ 8 c:short (c-array) ] }
81 { ushort-8-rep [ 8 c:ushort (c-array) ] }
82 { int-4-rep [ 4 c:int (c-array) ] }
83 { uint-4-rep [ 4 c:uint (c-array) ] }
84 { longlong-2-rep [ 2 c:longlong (c-array) ] }
85 { ulonglong-2-rep [ 2 c:ulonglong (c-array) ] }
86 { float-4-rep [ 4 c:float (c-array) ] }
87 { double-2-rep [ 2 c:double (c-array) ] }
90 : rep-tf-values ( rep -- t f )
91 float-vector-rep? [ -1 bits>double 0.0 ] [ -1 0 ] if ;
93 : 2byte>rep-array ( a b rep -- a' b' )
94 '[ _ byte>rep-array ] bi@ ; inline
96 : components-map ( a rep quot -- c )
97 [ [ byte>rep-array ] [ rep-length ] bi ] dip unrolled-map-unsafe underlying>> ; inline
98 : components-2map ( a b rep quot -- c )
99 [ [ 2byte>rep-array ] [ rep-length ] bi ] dip unrolled-2map-unsafe underlying>> ; inline
100 : components-reduce ( a rep quot -- x )
101 [ byte>rep-array [ ] ] dip map-reduce ; inline
103 : bitwise-components-map ( a rep quot -- c )
104 [ >bitwise-vector-rep [ byte>rep-array ] [ rep-length ] bi ] dip
105 unrolled-map-unsafe underlying>> ; inline
106 : bitwise-components-2map ( a b rep quot -- c )
107 [ >bitwise-vector-rep [ 2byte>rep-array ] [ rep-length ] bi ] dip
108 unrolled-2map-unsafe underlying>> ; inline
109 : bitwise-components-reduce ( a rep quot -- x )
110 [ >bitwise-vector-rep byte>rep-array [ ] ] dip map-reduce ; inline
111 : bitwise-components-reduce* ( a rep identity quot -- x )
112 [ >bitwise-vector-rep byte>rep-array ] 2dip reduce ; inline
114 :: (vshuffle) ( a elts rep -- c )
115 a rep byte>rep-array :> a'
116 rep <rep-array> :> c'
117 elts rep rep-length [| from to |
118 from rep rep-length 1 - bitand
121 ] unrolled-each-index-unsafe
122 c' underlying>> ; inline
124 :: (vshuffle2) ( a b elts rep -- c )
125 a rep byte>rep-array :> a'
126 b rep byte>rep-array :> b'
127 a' b' cord-append :> ab'
128 rep <rep-array> :> c'
129 elts rep rep-length [| from to |
130 from rep rep-length dup + 1 - bitand
133 ] unrolled-each-index-unsafe
134 c' underlying>> ; inline
136 GENERIC: native/ ( x y -- x/y )
138 M: integer native/ /i ; inline
139 M: float native/ /f ; inline
141 : (vgetmask) ( a rep -- b )
142 0 [ [ 1 shift ] [ zero? 0 1 ? ] bi* bitor ] bitwise-components-reduce* ; inline
146 : (simd-v+) ( a b rep -- c ) [ + ] components-2map ;
147 : (simd-v-) ( a b rep -- c ) [ - ] components-2map ;
148 : (simd-vneg) ( a rep -- c ) [ neg ] components-map ;
149 :: (simd-v+-) ( a b rep -- c )
150 a b rep 2byte>rep-array :> ( a' b' )
151 rep <rep-array> :> c'
152 0 rep rep-length [ 1 - 2 <range> ] [ 2 /i ] bi [| n |
153 n a' nth-unsafe n b' nth-unsafe -
156 n 1 + a' nth-unsafe n 1 + b' nth-unsafe +
157 n 1 + c' set-nth-unsafe
158 ] unrolled-each-unsafe
160 : (simd-vs+) ( a b rep -- c )
161 dup rep-component-type '[ + _ c:c-type-clamp ] components-2map ;
162 : (simd-vs-) ( a b rep -- c )
163 dup rep-component-type '[ - _ c:c-type-clamp ] components-2map ;
164 : (simd-vs*) ( a b rep -- c )
165 dup rep-component-type '[ * _ c:c-type-clamp ] components-2map ;
166 : (simd-v*) ( a b rep -- c ) [ * ] components-2map ;
167 : (simd-v*high) ( a b rep -- c )
168 dup rep-component-type c:heap-size -8 * '[ * _ shift ] components-2map ;
169 :: (simd-v*hs+) ( a b rep -- c )
170 rep { char-16-rep uchar-16-rep } member-eq?
171 [ uchar-16-rep char-16-rep ]
172 [ rep rep ] if :> ( a-rep b-rep )
173 b-rep widen-vector-rep signed-rep :> wide-rep
174 wide-rep rep-component-type :> wide-type
175 a a-rep byte>rep-array 2 <groups> :> a'
176 b b-rep byte>rep-array 2 <groups> :> b'
177 a' b' rep rep-length 2 /i [
179 [ [ second ] bi@ * ] 2bi +
180 wide-type c:c-type-clamp
181 ] wide-rep <rep-array> unrolled-2map-as-unsafe underlying>> ;
182 : (simd-v/) ( a b rep -- c ) [ native/ ] components-2map ;
183 : (simd-vavg) ( a b rep -- c )
184 [ + dup integer? [ 1 + -1 shift ] [ 0.5 * ] if ] components-2map ;
185 : (simd-vmin) ( a b rep -- c ) [ min ] components-2map ;
186 : (simd-vmax) ( a b rep -- c ) [ max ] components-2map ;
188 : (simd-vdot) ( a b rep -- n )
189 [ 2byte>rep-array [ [ first ] bi@ * ] 2keep ] keep
190 1 swap rep-length [a,b) [ '[ _ swap nth-unsafe ] bi@ * + ] 2with each ;
191 : (simd-vsqrt) ( a rep -- c ) [ fsqrt ] components-map ;
192 : (simd-vsad) ( a b rep -- c ) 2byte>rep-array [ - abs ] [ + ] 2map-reduce ;
193 : (simd-sum) ( a rep -- n ) [ + ] components-reduce ;
194 : (simd-vabs) ( a rep -- c ) [ abs ] components-map ;
195 : (simd-vbitand) ( a b rep -- c ) [ bitand ] bitwise-components-2map ;
196 : (simd-vbitandn) ( a b rep -- c ) [ [ bitnot ] dip bitand ] bitwise-components-2map ;
197 : (simd-vbitor) ( a b rep -- c ) [ bitor ] bitwise-components-2map ;
198 : (simd-vbitxor) ( a b rep -- c ) [ bitxor ] bitwise-components-2map ;
199 : (simd-vbitnot) ( a rep -- c ) [ bitnot ] bitwise-components-map ;
200 : (simd-vand) ( a b rep -- c ) [ bitand ] bitwise-components-2map ;
201 : (simd-vandn) ( a b rep -- c ) [ [ bitnot ] dip bitand ] bitwise-components-2map ;
202 : (simd-vor) ( a b rep -- c ) [ bitor ] bitwise-components-2map ;
203 : (simd-vxor) ( a b rep -- c ) [ bitxor ] bitwise-components-2map ;
204 : (simd-vnot) ( a rep -- c ) [ bitnot ] bitwise-components-map ;
205 : (simd-vlshift) ( a n rep -- c ) swap '[ _ shift ] bitwise-components-map ;
206 : (simd-vrshift) ( a n rep -- c ) swap '[ _ neg shift ] bitwise-components-map ;
208 : (simd-hlshift) ( a n rep -- c )
209 drop head-slice* 16 0 pad-head ;
211 : (simd-hrshift) ( a n rep -- c )
212 drop tail-slice 16 0 pad-tail ;
213 : (simd-vshuffle-elements) ( a n rep -- c ) [ rep-length 0 pad-tail ] keep (vshuffle) ;
214 : (simd-vshuffle2-elements) ( a b n rep -- c ) [ rep-length 0 pad-tail ] keep (vshuffle2) ;
215 : (simd-vshuffle-bytes) ( a b rep -- c ) drop uchar-16-rep (vshuffle) ;
216 :: (simd-vmerge-head) ( a b rep -- c )
217 a b rep 2byte>rep-array :> ( a' b' )
218 rep <rep-array> :> c'
219 rep rep-length 2 /i [| n |
220 n a' nth-unsafe n 2 * c' set-nth-unsafe
221 n b' nth-unsafe n 2 * 1 + c' set-nth-unsafe
222 ] unrolled-each-integer
224 :: (simd-vmerge-tail) ( a b rep -- c )
225 a b rep 2byte>rep-array :> ( a' b' )
226 rep <rep-array> :> c'
227 rep rep-length 2 /i :> len
229 n len + a' nth-unsafe n 2 * c' set-nth-unsafe
230 n len + b' nth-unsafe n 2 * 1 + c' set-nth-unsafe
231 ] unrolled-each-integer
233 : (simd-v<=) ( a b rep -- c )
234 dup rep-tf-values '[ <= _ _ ? ] components-2map ;
235 : (simd-v<) ( a b rep -- c )
236 dup rep-tf-values '[ < _ _ ? ] components-2map ;
237 : (simd-v=) ( a b rep -- c )
238 dup rep-tf-values '[ = _ _ ? ] components-2map ;
239 : (simd-v>) ( a b rep -- c )
240 dup rep-tf-values '[ > _ _ ? ] components-2map ;
241 : (simd-v>=) ( a b rep -- c )
242 dup rep-tf-values '[ >= _ _ ? ] components-2map ;
243 : (simd-vunordered?) ( a b rep -- c )
244 dup rep-tf-values '[ unordered? _ _ ? ] components-2map ;
245 : (simd-vany?) ( a rep -- ? ) [ bitor ] bitwise-components-reduce zero? not ;
246 : (simd-vall?) ( a rep -- ? ) [ bitand ] bitwise-components-reduce zero? not ;
247 : (simd-vnone?) ( a rep -- ? ) [ bitor ] bitwise-components-reduce zero? ;
248 : (simd-vgetmask) ( a rep -- n )
249 { float-4-rep double-2-rep } member?
250 [ uint-4-rep (vgetmask) ] [ uchar-16-rep (vgetmask) ] if ;
251 : (simd-v>float) ( a rep -- c )
252 [ [ byte>rep-array ] [ rep-length ] bi [ >float ] ]
253 [ >float-vector-rep <rep-array> ] bi unrolled-map-as-unsafe underlying>> ;
254 : (simd-v>integer) ( a rep -- c )
255 [ [ byte>rep-array ] [ rep-length ] bi [ >integer ] ]
256 [ >int-vector-rep <rep-array> ] bi unrolled-map-as-unsafe underlying>> ;
257 : (simd-vpack-signed) ( a b rep -- c )
258 [ [ 2byte>rep-array cord-append ] [ rep-length 2 * ] bi ]
259 [ narrow-vector-rep [ <rep-array> ] [ rep-component-type ] bi ] bi
260 '[ _ c:c-type-clamp ] swap unrolled-map-as-unsafe underlying>> ;
261 : (simd-vpack-unsigned) ( a b rep -- c )
262 [ [ 2byte>rep-array cord-append ] [ rep-length 2 * ] bi ]
263 [ narrow-vector-rep >uint-vector-rep [ <rep-array> ] [ rep-component-type ] bi ] bi
264 '[ _ c:c-type-clamp ] swap unrolled-map-as-unsafe underlying>> ;
265 : (simd-vunpack-head) ( a rep -- c )
266 [ byte>rep-array ] [ widen-vector-rep [ rep-length ] [ '[ _ >rep-array ] ] bi ] bi
267 [ head-slice ] dip call( a' -- c' ) underlying>> ;
268 : (simd-vunpack-tail) ( a rep -- c )
269 [ byte>rep-array ] [ widen-vector-rep [ rep-length ] [ '[ _ >rep-array ] ] bi ] bi
270 [ tail-slice ] dip call( a' -- c' ) underlying>> ;
271 : (simd-with) ( n rep -- v )
272 [ rep-length swap '[ _ ] ] [ <rep-array> ] bi replicate-as
274 : (simd-gather-2) ( m n rep -- v ) <rep-array> [ 2 set-firstn-unsafe ] keep underlying>> ;
275 : (simd-gather-4) ( m n o p rep -- v ) <rep-array> [ 4 set-firstn-unsafe ] keep underlying>> ;
276 : (simd-select) ( a n rep -- x ) swapd byte>rep-array nth-unsafe ;
278 : alien-vector ( c-ptr n rep -- value )
279 [ swap <displaced-alien> ] dip rep-size memory>byte-array ;
280 : set-alien-vector ( value c-ptr n rep -- )
281 [ swap <displaced-alien> swap ] dip rep-size memcpy ;
283 "compiler.cfg.intrinsics.simd" require
284 "compiler.tree.propagation.simd" require
285 "compiler.cfg.value-numbering.simd" require