]> gitweb.factorcode.org Git - factor.git/blob - basis/math/vectors/simd/intrinsics/intrinsics.factor
Merge branch 'mongodb-changes' of git://github.com/x6j8x/factor
[factor.git] / basis / math / vectors / simd / intrinsics / intrinsics.factor
1 ! (c)2009 Slava Pestov, Joe Groff bsd license
2 USING: accessors alien alien.c-types alien.data combinators
3 sequences.cords cpu.architecture fry generalizations grouping
4 kernel libc locals math math.libm math.order math.ranges
5 math.vectors sequences sequences.private specialized-arrays
6 vocabs.loader ;
7 QUALIFIED-WITH: alien.c-types c
8 SPECIALIZED-ARRAYS:
9     c:char c:short c:int c:longlong
10     c:uchar c:ushort c:uint c:ulonglong
11     c:float c:double ;
12 IN: math.vectors.simd.intrinsics
13
14 : assert-positive ( x -- y ) ;
15
16 <PRIVATE
17
18 : >bitwise-vector-rep ( rep -- rep' )
19     {
20         { float-4-rep    [ uint-4-rep      ] }
21         { double-2-rep   [ ulonglong-2-rep ] }
22         [ ]
23     } case ; foldable
24
25 : >uint-vector-rep ( rep -- rep' )
26     {
27         { longlong-2-rep [ ulonglong-2-rep ] }
28         { int-4-rep      [ uint-4-rep      ] }
29         { short-8-rep    [ ushort-8-rep    ] }
30         { char-16-rep    [ uchar-16-rep    ] }
31         [ ]
32     } case ; foldable
33
34 : >int-vector-rep ( rep -- rep' )
35     {
36         { float-4-rep  [ int-4-rep      ] }
37         { double-2-rep [ longlong-2-rep ] }
38     } case ; foldable
39
40 : >float-vector-rep ( rep -- rep' )
41     {
42         { int-4-rep      [ float-4-rep  ] }
43         { longlong-2-rep [ double-2-rep ] }
44     } case ; foldable
45
46 : [byte>rep-array] ( rep -- class )
47     {
48         { char-16-rep      [ [ byte-array>char-array      ] ] }
49         { uchar-16-rep     [ [ byte-array>uchar-array     ] ] }
50         { short-8-rep      [ [ byte-array>short-array     ] ] }
51         { ushort-8-rep     [ [ byte-array>ushort-array    ] ] }
52         { int-4-rep        [ [ byte-array>int-array       ] ] }
53         { uint-4-rep       [ [ byte-array>uint-array      ] ] }
54         { longlong-2-rep   [ [ byte-array>longlong-array  ] ] }
55         { ulonglong-2-rep  [ [ byte-array>ulonglong-array ] ] }
56         { float-4-rep      [ [ byte-array>float-array     ] ] }
57         { double-2-rep     [ [ byte-array>double-array    ] ] }
58     } case ; foldable
59
60 : [>rep-array] ( rep -- class )
61     {
62         { char-16-rep      [ [ >char-array      ] ] }
63         { uchar-16-rep     [ [ >uchar-array     ] ] }
64         { short-8-rep      [ [ >short-array     ] ] }
65         { ushort-8-rep     [ [ >ushort-array    ] ] }
66         { int-4-rep        [ [ >int-array       ] ] }
67         { uint-4-rep       [ [ >uint-array      ] ] }
68         { longlong-2-rep   [ [ >longlong-array  ] ] }
69         { ulonglong-2-rep  [ [ >ulonglong-array ] ] }
70         { float-4-rep      [ [ >float-array     ] ] }
71         { double-2-rep     [ [ >double-array    ] ] }
72     } case ; foldable
73
74 : [<rep-array>] ( rep -- class )
75     {
76         { char-16-rep      [ [ 16 (char-array)      ] ] }
77         { uchar-16-rep     [ [ 16 (uchar-array)     ] ] }
78         { short-8-rep      [ [  8 (short-array)     ] ] }
79         { ushort-8-rep     [ [  8 (ushort-array)    ] ] }
80         { int-4-rep        [ [  4 (int-array)       ] ] }
81         { uint-4-rep       [ [  4 (uint-array)      ] ] }
82         { longlong-2-rep   [ [  2 (longlong-array)  ] ] }
83         { ulonglong-2-rep  [ [  2 (ulonglong-array) ] ] }
84         { float-4-rep      [ [  4 (float-array)     ] ] }
85         { double-2-rep     [ [  2 (double-array)    ] ] }
86     } case ; foldable
87
88 : rep-tf-values ( rep -- t f )
89     float-vector-rep? [ -1 bits>double 0.0 ] [ -1 0 ] if ;
90
91 : >rep-array ( a rep -- a' )
92     [byte>rep-array] call( a -- a' ) ; inline
93 : 2>rep-array ( a b rep -- a' b' )
94     [byte>rep-array] '[ _ call( a -- a' ) ] bi@ ; inline
95 : <rep-array> ( rep -- a' )
96     [<rep-array>] call( -- a' ) ; inline
97
98 : components-map ( a rep quot -- c )
99     [ >rep-array ] dip map underlying>> ; inline
100 : components-2map ( a b rep quot -- c )
101     [ 2>rep-array ] dip 2map underlying>> ; inline
102 : components-reduce ( a rep quot -- x )
103     [ >rep-array [ ] ] dip map-reduce ; inline
104
105 : bitwise-components-map ( a rep quot -- c )
106     [ >bitwise-vector-rep >rep-array ] dip map underlying>> ; inline
107 : bitwise-components-2map ( a b rep quot -- c )
108     [ >bitwise-vector-rep 2>rep-array ] dip 2map underlying>> ; inline
109 : bitwise-components-reduce ( a rep quot -- x )
110     [ >bitwise-vector-rep >rep-array [ ] ] dip map-reduce ; inline
111
112 :: (vshuffle) ( a elts rep -- c )
113     a rep >rep-array :> a'
114     rep <rep-array> :> c'
115     elts [| from to |
116         from rep rep-length 1 - bitand
117            a' nth-unsafe
118         to c' set-nth-unsafe
119     ] each-index
120     c' underlying>> ; inline
121
122 PRIVATE>
123
124 : (simd-v+)                ( a b rep -- c ) [ + ] components-2map ;
125 : (simd-v-)                ( a b rep -- c ) [ - ] components-2map ;
126 : (simd-vneg)              ( a   rep -- c ) [ neg ] components-map ;
127 :: (simd-v+-)              ( a b rep -- c ) 
128     a b rep 2>rep-array :> ( a' b' )
129     rep <rep-array> :> c'
130     0  rep rep-length 1 -  2 <range> [| n |
131         n     a' nth-unsafe n     b' nth-unsafe -
132         n     c' set-nth-unsafe
133
134         n 1 + a' nth-unsafe n 1 + b' nth-unsafe +
135         n 1 + c' set-nth-unsafe
136     ] each
137     c' underlying>> ;
138 : (simd-vs+)               ( a b rep -- c )
139     dup rep-component-type '[ + _ c-type-clamp ] components-2map ;
140 : (simd-vs-)               ( a b rep -- c )
141     dup rep-component-type '[ - _ c-type-clamp ] components-2map ;
142 : (simd-vs*)               ( a b rep -- c )
143     dup rep-component-type '[ * _ c-type-clamp ] components-2map ;
144 : (simd-v*)                ( a b rep -- c ) [ * ] components-2map ;
145 : (simd-v*high)            ( a b rep -- c )
146     dup rep-component-type heap-size -8 * '[ * _ shift ] components-2map ;
147 :: (simd-v*hs+)            ( a b rep -- c )
148     rep { char-16-rep uchar-16-rep } member-eq?
149     [ uchar-16-rep char-16-rep ]
150     [ rep rep ] if :> ( a-rep b-rep )
151     b-rep widen-vector-rep signed-rep :> wide-rep
152     wide-rep rep-component-type :> wide-type
153     a a-rep >rep-array 2 <groups> :> a'
154     b b-rep >rep-array 2 <groups> :> b'
155     a' b' [
156         [ [ first  ] bi@ * ]
157         [ [ second ] bi@ * ] 2bi +
158         wide-type c-type-clamp
159     ] wide-rep <rep-array> 2map-as underlying>> ;
160 : (simd-v/)                ( a b rep -- c ) [ / ] components-2map ;
161 : (simd-vavg)              ( a b rep -- c )
162     [ + dup integer? [ 1 + -1 shift ] [ 0.5 * ] if ] components-2map ;
163 : (simd-vmin)              ( a b rep -- c ) [ min ] components-2map ;
164 : (simd-vmax)              ( a b rep -- c ) [ max ] components-2map ;
165 : (simd-v.)                ( a b rep -- n )
166     [ 2>rep-array [ [ first ] bi@ * ] 2keep ] keep
167     1 swap rep-length [a,b) [ '[ _ swap nth-unsafe ] bi@ * + ] with with each ;
168 : (simd-vsqrt)             ( a   rep -- c ) [ fsqrt ] components-map ;
169 : (simd-vsad)              ( a b rep -- c ) 2>rep-array [ - abs ] [ + ] 2map-reduce ;
170 : (simd-sum)               ( a   rep -- n ) [ + ] components-reduce ;
171 : (simd-vabs)              ( a   rep -- c ) [ abs ] components-map ;
172 : (simd-vbitand)           ( a b rep -- c ) [ bitand ] bitwise-components-2map ;
173 : (simd-vbitandn)          ( a b rep -- c ) [ [ bitnot ] dip bitand ] bitwise-components-2map ;
174 : (simd-vbitor)            ( a b rep -- c ) [ bitor ] bitwise-components-2map ;
175 : (simd-vbitxor)           ( a b rep -- c ) [ bitxor ] bitwise-components-2map ;
176 : (simd-vbitnot)           ( a   rep -- c ) [ bitnot ] bitwise-components-map ;
177 : (simd-vand)              ( a b rep -- c ) [ bitand ] bitwise-components-2map ;
178 : (simd-vandn)             ( a b rep -- c ) [ [ bitnot ] dip bitand ] bitwise-components-2map ;
179 : (simd-vor)               ( a b rep -- c ) [ bitor ] bitwise-components-2map ;
180 : (simd-vxor)              ( a b rep -- c ) [ bitxor ] bitwise-components-2map ;
181 : (simd-vnot)              ( a   rep -- c ) [ bitnot ] bitwise-components-map ;
182 : (simd-vlshift)           ( a n rep -- c ) swap '[ _ shift ] bitwise-components-map ;
183 : (simd-vrshift)           ( a n rep -- c ) swap '[ _ neg shift ] bitwise-components-map ;
184 : (simd-hlshift)           ( a n rep -- c )
185     drop head-slice* 16 0 pad-head ;
186 : (simd-hrshift)           ( a n rep -- c )
187     drop tail-slice 16 0 pad-tail ;
188 : (simd-vshuffle-elements) ( a n rep -- c ) [ rep-length 0 pad-tail ] keep (vshuffle) ;
189 : (simd-vshuffle-bytes)    ( a b rep -- c ) drop uchar-16-rep (vshuffle) ;
190 :: (simd-vmerge-head)      ( a b rep -- c )
191     a b rep 2>rep-array :> ( a' b' )
192     rep <rep-array> :> c'
193     rep rep-length 2 /i iota [| n |
194         n a' nth-unsafe n 2 *     c' set-nth-unsafe
195         n b' nth-unsafe n 2 * 1 + c' set-nth-unsafe
196     ] each
197     c' underlying>> ;
198 :: (simd-vmerge-tail)      ( a b rep -- c )
199     a b rep 2>rep-array :> ( a' b' )
200     rep <rep-array> :> c'
201     rep rep-length 2 /i :> len
202     len iota [| n |
203         n len + a' nth-unsafe n 2 *     c' set-nth-unsafe
204         n len + b' nth-unsafe n 2 * 1 + c' set-nth-unsafe
205     ] each
206     c' underlying>> ;
207 : (simd-v<=)               ( a b rep -- c )
208     dup rep-tf-values '[ <= _ _ ? ] components-2map ; 
209 : (simd-v<)                ( a b rep -- c )
210     dup rep-tf-values '[ <  _ _ ? ] components-2map ;
211 : (simd-v=)                ( a b rep -- c )
212     dup rep-tf-values '[ =  _ _ ? ] components-2map ;
213 : (simd-v>)                ( a b rep -- c )
214     dup rep-tf-values '[ >  _ _ ? ] components-2map ;
215 : (simd-v>=)               ( a b rep -- c )
216     dup rep-tf-values '[ >= _ _ ? ] components-2map ;
217 : (simd-vunordered?)       ( a b rep -- c )
218     dup rep-tf-values '[ unordered? _ _ ? ] components-2map ;
219 : (simd-vany?)             ( a   rep -- ? ) [ bitor  ] bitwise-components-reduce zero? not ;
220 : (simd-vall?)             ( a   rep -- ? ) [ bitand ] bitwise-components-reduce zero? not ;
221 : (simd-vnone?)            ( a   rep -- ? ) [ bitor  ] bitwise-components-reduce zero?     ;
222 : (simd-v>float)           ( a   rep -- c )
223     [ >rep-array [ >float ] ] [ >float-vector-rep <rep-array> ] bi map-as underlying>> ;
224 : (simd-v>integer)         ( a   rep -- c )
225     [ >rep-array [ >integer ] ] [ >int-vector-rep <rep-array> ] bi map-as underlying>> ;
226 : (simd-vpack-signed)      ( a b rep -- c )
227     [ 2>rep-array cord-append ]
228     [ narrow-vector-rep [ <rep-array> ] [ rep-component-type ] bi ] bi
229     '[ _ c-type-clamp ] swap map-as underlying>> ;
230 : (simd-vpack-unsigned)    ( a b rep -- c )
231     [ 2>rep-array cord-append ]
232     [ narrow-vector-rep >uint-vector-rep [ <rep-array> ] [ rep-component-type ] bi ] bi
233     '[ _ c-type-clamp ] swap map-as underlying>> ;
234 : (simd-vunpack-head)      ( a   rep -- c ) 
235     [ >rep-array ] [ widen-vector-rep [ rep-length ] [ [>rep-array] ] bi ] bi
236     [ head-slice ] dip call( a' -- c' ) underlying>> ;
237 : (simd-vunpack-tail)      ( a   rep -- c )
238     [ >rep-array ] [ widen-vector-rep [ rep-length ] [ [>rep-array] ] bi ] bi
239     [ tail-slice ] dip call( a' -- c' ) underlying>> ;
240 : (simd-with)              (   n rep -- v )
241     [ rep-length swap '[ _ ] ] [ <rep-array> ] bi replicate-as 
242     underlying>> ;
243 : (simd-gather-2)          ( m n rep -- v ) <rep-array> [ 2 set-firstn ] keep underlying>> ;
244 : (simd-gather-4)          ( m n o p rep -- v ) <rep-array> [ 4 set-firstn ] keep underlying>> ;
245 : (simd-select)            ( a n rep -- x ) [ swap ] dip >rep-array nth-unsafe ;
246
247 : alien-vector     (       c-ptr n rep -- value )
248     [ swap <displaced-alien> ] dip rep-size memory>byte-array ;
249 : set-alien-vector ( value c-ptr n rep --       )
250     [ swap <displaced-alien> swap ] dip rep-size memcpy ;
251
252 "compiler.cfg.intrinsics.simd" require
253 "compiler.tree.propagation.simd" require
254 "compiler.cfg.value-numbering.simd" require
255