1 ! (c)2009 Slava Pestov, Joe Groff bsd license
2 USING: accessors alien alien.c-types alien.data combinators
3 cords cpu.architecture fry generalizations kernel libc locals
4 math math.libm math.order math.ranges math.vectors sequences
5 sequences.private specialized-arrays vocabs.loader ;
6 QUALIFIED-WITH: alien.c-types c
8 c:char c:short c:int c:longlong
9 c:uchar c:ushort c:uint c:ulonglong
11 IN: math.vectors.simd.intrinsics
13 : assert-positive ( x -- y ) ;
17 : >bitwise-vector-rep ( rep -- rep' )
19 { float-4-rep [ uint-4-rep ] }
20 { double-2-rep [ ulonglong-2-rep ] }
24 : >uint-vector-rep ( rep -- rep' )
26 { longlong-2-rep [ ulonglong-2-rep ] }
27 { int-4-rep [ uint-4-rep ] }
28 { short-8-rep [ ushort-8-rep ] }
29 { char-16-rep [ uchar-16-rep ] }
33 : >int-vector-rep ( rep -- rep' )
35 { float-4-rep [ int-4-rep ] }
36 { double-2-rep [ longlong-2-rep ] }
39 : >float-vector-rep ( rep -- rep' )
41 { int-4-rep [ float-4-rep ] }
42 { longlong-2-rep [ double-2-rep ] }
45 : [byte>rep-array] ( rep -- class )
47 { char-16-rep [ [ byte-array>char-array ] ] }
48 { uchar-16-rep [ [ byte-array>uchar-array ] ] }
49 { short-8-rep [ [ byte-array>short-array ] ] }
50 { ushort-8-rep [ [ byte-array>ushort-array ] ] }
51 { int-4-rep [ [ byte-array>int-array ] ] }
52 { uint-4-rep [ [ byte-array>uint-array ] ] }
53 { longlong-2-rep [ [ byte-array>longlong-array ] ] }
54 { ulonglong-2-rep [ [ byte-array>ulonglong-array ] ] }
55 { float-4-rep [ [ byte-array>float-array ] ] }
56 { double-2-rep [ [ byte-array>double-array ] ] }
59 : [>rep-array] ( rep -- class )
61 { char-16-rep [ [ >char-array ] ] }
62 { uchar-16-rep [ [ >uchar-array ] ] }
63 { short-8-rep [ [ >short-array ] ] }
64 { ushort-8-rep [ [ >ushort-array ] ] }
65 { int-4-rep [ [ >int-array ] ] }
66 { uint-4-rep [ [ >uint-array ] ] }
67 { longlong-2-rep [ [ >longlong-array ] ] }
68 { ulonglong-2-rep [ [ >ulonglong-array ] ] }
69 { float-4-rep [ [ >float-array ] ] }
70 { double-2-rep [ [ >double-array ] ] }
73 : [<rep-array>] ( rep -- class )
75 { char-16-rep [ [ 16 (char-array) ] ] }
76 { uchar-16-rep [ [ 16 (uchar-array) ] ] }
77 { short-8-rep [ [ 8 (short-array) ] ] }
78 { ushort-8-rep [ [ 8 (ushort-array) ] ] }
79 { int-4-rep [ [ 4 (int-array) ] ] }
80 { uint-4-rep [ [ 4 (uint-array) ] ] }
81 { longlong-2-rep [ [ 2 (longlong-array) ] ] }
82 { ulonglong-2-rep [ [ 2 (ulonglong-array) ] ] }
83 { float-4-rep [ [ 4 (float-array) ] ] }
84 { double-2-rep [ [ 2 (double-array) ] ] }
87 : rep-tf-values ( rep -- t f )
88 float-vector-rep? [ -1 bits>double 0.0 ] [ -1 0 ] if ;
90 : >rep-array ( a rep -- a' )
91 [byte>rep-array] call( a -- a' ) ; inline
92 : 2>rep-array ( a b rep -- a' b' )
93 [byte>rep-array] '[ _ call( a -- a' ) ] bi@ ; inline
94 : <rep-array> ( rep -- a' )
95 [<rep-array>] call( -- a' ) ; inline
97 : components-map ( a rep quot -- c )
98 [ >rep-array ] dip map underlying>> ; inline
99 : components-2map ( a b rep quot -- c )
100 [ 2>rep-array ] dip 2map underlying>> ; inline
101 : components-reduce ( a rep quot -- x )
102 [ >rep-array [ ] ] dip map-reduce ; inline
104 : bitwise-components-map ( a rep quot -- c )
105 [ >bitwise-vector-rep >rep-array ] dip map underlying>> ; inline
106 : bitwise-components-2map ( a b rep quot -- c )
107 [ >bitwise-vector-rep 2>rep-array ] dip 2map underlying>> ; inline
108 : bitwise-components-reduce ( a rep quot -- x )
109 [ >bitwise-vector-rep >rep-array [ ] ] dip map-reduce ; inline
111 :: (vshuffle) ( a elts rep -- c )
112 a rep >rep-array :> a'
113 rep <rep-array> :> c'
116 rep rep-length 1 - bitand
119 c' underlying>> ; inline
123 : (simd-v+) ( a b rep -- c ) [ + ] components-2map ;
124 : (simd-v-) ( a b rep -- c ) [ - ] components-2map ;
125 : (simd-vneg) ( a rep -- c ) [ neg ] components-map ;
126 :: (simd-v+-) ( a b rep -- c )
127 a b rep 2>rep-array :> ( a' b' )
128 rep <rep-array> :> c'
129 0 rep length 1 - 2 <range> [| n |
130 n a' nth-unsafe n b' nth-unsafe -
133 n 1 + a' nth-unsafe n 1 + b' nth-unsafe +
134 n 1 + c' set-nth-unsafe
137 : (simd-vs+) ( a b rep -- c ) dup '[ + _ c-type-clamp ] components-2map ;
138 : (simd-vs-) ( a b rep -- c ) dup '[ - _ c-type-clamp ] components-2map ;
139 : (simd-vs*) ( a b rep -- c ) dup '[ - _ c-type-clamp ] components-2map ;
140 : (simd-v*) ( a b rep -- c ) [ * ] components-2map ;
141 : (simd-v/) ( a b rep -- c ) [ / ] components-2map ;
142 : (simd-vmin) ( a b rep -- c ) [ min ] components-2map ;
143 : (simd-vmax) ( a b rep -- c ) [ max ] components-2map ;
144 : (simd-v.) ( a b rep -- n )
145 [ 2>rep-array [ [ first ] bi@ * ] 2keep ] keep
146 1 swap rep-length [a,b) [ '[ _ swap nth-unsafe ] bi@ * + ] with with each ;
147 : (simd-vsqrt) ( a rep -- c ) [ fsqrt ] components-map ;
148 : (simd-sum) ( a rep -- n ) [ + ] components-reduce ;
149 : (simd-vabs) ( a rep -- c ) [ abs ] components-map ;
150 : (simd-vbitand) ( a b rep -- c ) [ bitand ] bitwise-components-2map ;
151 : (simd-vbitandn) ( a b rep -- c ) [ [ not ] dip bitand ] bitwise-components-2map ;
152 : (simd-vbitor) ( a b rep -- c ) [ bitor ] bitwise-components-2map ;
153 : (simd-vbitxor) ( a b rep -- c ) [ bitxor ] bitwise-components-2map ;
154 : (simd-vbitnot) ( a rep -- c ) [ bitnot ] bitwise-components-map ;
155 : (simd-vand) ( a b rep -- c ) [ bitand ] bitwise-components-2map ;
156 : (simd-vandn) ( a b rep -- c ) [ [ not ] dip bitand ] bitwise-components-2map ;
157 : (simd-vor) ( a b rep -- c ) [ bitor ] bitwise-components-2map ;
158 : (simd-vxor) ( a b rep -- c ) [ bitxor ] bitwise-components-2map ;
159 : (simd-vnot) ( a rep -- c ) [ bitnot ] bitwise-components-map ;
160 : (simd-vlshift) ( a n rep -- c ) swap '[ _ shift ] bitwise-components-map ;
161 : (simd-vrshift) ( a n rep -- c ) swap '[ _ neg shift ] bitwise-components-map ;
162 : (simd-hlshift) ( a n rep -- c )
163 drop tail-slice 16 0 pad-tail ;
164 : (simd-hrshift) ( a n rep -- c )
165 drop head-slice 16 0 pad-head ;
166 : (simd-vshuffle-elements) ( a n rep -- c ) [ rep-length 0 pad-tail ] keep (vshuffle) ;
167 : (simd-vshuffle-bytes) ( a b rep -- c ) drop uchar-16-rep (vshuffle) ;
168 :: (simd-vmerge-head) ( a b rep -- c )
169 a b rep 2>rep-array :> ( a' b' )
170 rep <rep-array> :> c'
171 rep rep-length 2 /i iota [| n |
172 n a' nth-unsafe n 2 * c' set-nth-unsafe
173 n b' nth-unsafe n 2 * 1 + c' set-nth-unsafe
176 :: (simd-vmerge-tail) ( a b rep -- c )
177 a b rep 2>rep-array :> ( a' b' )
178 rep <rep-array> :> c'
179 rep rep-length 2 /i :> len
181 n len + a' nth-unsafe n 2 * c' set-nth-unsafe
182 n len + b' nth-unsafe n 2 * 1 + c' set-nth-unsafe
185 : (simd-v<=) ( a b rep -- c )
186 dup rep-tf-values '[ <= _ _ ? ] components-2map ;
187 : (simd-v<) ( a b rep -- c )
188 dup rep-tf-values '[ < _ _ ? ] components-2map ;
189 : (simd-v=) ( a b rep -- c )
190 dup rep-tf-values '[ = _ _ ? ] components-2map ;
191 : (simd-v>) ( a b rep -- c )
192 dup rep-tf-values '[ > _ _ ? ] components-2map ;
193 : (simd-v>=) ( a b rep -- c )
194 dup rep-tf-values '[ >= _ _ ? ] components-2map ;
195 : (simd-vunordered?) ( a b rep -- c )
196 dup rep-tf-values '[ unordered? _ _ ? ] components-2map ;
197 : (simd-vany?) ( a rep -- ? ) [ bitor ] bitwise-components-reduce zero? not ;
198 : (simd-vall?) ( a rep -- ? ) [ bitand ] bitwise-components-reduce zero? not ;
199 : (simd-vnone?) ( a rep -- ? ) [ bitor ] bitwise-components-reduce zero? ;
200 : (simd-v>float) ( a rep -- c )
201 [ >rep-array ] [ >float-vector-rep [>rep-array] ] bi call( i -- f ) ;
202 : (simd-v>integer) ( a rep -- c )
203 [ >rep-array ] [ >int-vector-rep [>rep-array] ] bi call( i -- f ) ;
204 : (simd-vpack-signed) ( a b rep -- c )
205 [ 2>rep-array cord-append ]
206 [ narrow-vector-rep [ [<rep-array>] ] [ rep-component-type ] bi ] bi
207 '[ _ c-type-clamp ] swap map-as ;
208 : (simd-vpack-unsigned) ( a b rep -- c )
209 [ 2>rep-array cord-append ]
210 [ narrow-vector-rep >uint-vector-rep [ [<rep-array>] ] [ rep-component-type ] bi ] bi
211 '[ _ c-type-clamp ] swap map-as ;
212 : (simd-vunpack-head) ( a rep -- c )
213 [ >rep-array ] [ widen-vector-rep [ rep-length ] [ [>rep-array] ] bi ] bi
214 [ head-slice ] dip call( a' -- c' ) underlying>> ;
215 : (simd-vunpack-tail) ( a rep -- c )
216 [ >rep-array ] [ widen-vector-rep [ rep-length ] [ [>rep-array] ] bi ] bi
217 [ tail-slice ] dip call( a' -- c' ) underlying>> ;
218 : (simd-with) ( n rep -- v )
219 [ rep-length iota swap '[ _ ] ] [ <rep-array> ] bi replicate-as ;
220 : (simd-gather-2) ( m n rep -- v ) <rep-array> [ 2 set-firstn ] keep underlying>> ;
221 : (simd-gather-4) ( m n o p rep -- v ) <rep-array> [ 4 set-firstn ] keep underlying>> ;
222 : (simd-select) ( a n rep -- x ) [ swap ] dip >rep-array nth-unsafe ;
224 : alien-vector ( c-ptr n rep -- value )
225 [ swap <displaced-alien> ] dip rep-size memory>byte-array ;
226 : set-alien-vector ( value c-ptr n rep -- )
227 [ swap <displaced-alien> swap ] dip rep-size memcpy ;
229 "compiler.cfg.intrinsics.simd" require
230 "compiler.tree.propagation.simd" require
231 "compiler.cfg.value-numbering.simd" require