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