]> gitweb.factorcode.org Git - factor.git/blob - basis/math/vectors/simd/simd.factor
1714cfb0c8623a9c1d8bbc03f9fd6c22a76411f3
[factor.git] / basis / math / vectors / simd / simd.factor
1 USING: accessors alien arrays byte-arrays classes combinators
2 cpu.architecture effects fry functors generalizations generic
3 generic.parser kernel lexer literals locals macros math math.functions
4 math.vectors math.vectors.private math.vectors.simd.intrinsics
5 namespaces parser prettyprint.custom quotations sequences
6 sequences.generalizations sequences.private vocabs vocabs.loader
7 words math.bitwise ;
8 QUALIFIED-WITH: alien.c-types c
9 IN: math.vectors.simd
10
11 ERROR: bad-simd-length got expected ;
12
13 ERROR: bad-simd-vector obj ;
14
15 <<
16 <PRIVATE
17 ! Primitive SIMD constructors
18
19 GENERIC: new-underlying ( underlying seq -- seq' )
20
21 : make-underlying ( seq quot -- seq' )
22     dip new-underlying ; inline
23 : change-underlying ( seq quot -- seq' )
24     '[ underlying>> @ ] keep new-underlying ; inline
25 PRIVATE>
26 >>
27
28 <PRIVATE
29
30 ! Helper for boolean vector literals
31
32 : vector-true-value ( class -- value )
33     { c:float c:double } member? [ -1 bits>double ] [ -1 ] if ; foldable
34
35 : vector-false-value ( type -- value )
36     { c:float c:double } member? [ 0.0 ] [ 0 ] if ; foldable
37
38 : boolean>element ( bool/elt type -- elt )
39     swap {
40         { t [ vector-true-value  ] }
41         { f [ vector-false-value ] }
42         [ nip ]
43     } case ; inline
44
45 PRIVATE>
46
47 ! SIMD base type
48
49 TUPLE: simd-128
50     { underlying byte-array read-only initial: $[ 16 <byte-array> ] } ;
51
52 GENERIC: simd-element-type ( obj -- c-type )
53 GENERIC: simd-rep ( simd -- rep )
54 GENERIC: simd-with ( n exemplar -- v )
55
56 M: object simd-element-type drop f ;
57 M: object simd-rep drop f ;
58
59 <<
60 <PRIVATE
61
62 DEFER: simd-construct-op
63
64 ! Unboxers for SIMD operations
65 : if-both-vectors ( a b rep t f -- )
66     [ 2over [ simd-128? ] both? ] 2dip if ; inline
67
68 : if-both-vectors-match ( a b rep t f -- )
69     [ 3dup [ drop [ simd-128? ] both? ] [ '[ simd-rep _ eq? ] both? ] 3bi and ]
70     2dip if ; inline
71
72 : simd-unbox ( a -- a (a) )
73     [ ] [ underlying>> ] bi ; inline
74
75 : v->v-op ( a rep quot: ( (a) rep -- (c) ) fallback-quot -- c )
76     drop [ simd-unbox ] 2dip 2curry make-underlying ; inline
77
78 : vx->v-op ( a obj rep quot: ( (a) obj rep -- (c) ) fallback-quot -- c )
79     drop [ simd-unbox ] 3dip 3curry make-underlying ; inline
80
81 : vn->v-op ( a n rep quot: ( (a) n rep -- (c) ) fallback-quot -- c )
82     drop [ [ simd-unbox ] [ >fixnum ] bi* ] 2dip 3curry make-underlying ; inline
83
84 : vx->x-op ( a obj rep quot: ( (a) obj rep -- obj ) fallback-quot -- obj )
85     drop [ underlying>> ] 3dip call ; inline
86
87 : v->x-op ( a rep quot: ( (a) rep -- obj ) fallback-quot -- obj )
88     drop [ underlying>> ] 2dip call ; inline
89
90 : (vv->v-op) ( a b rep quot: ( (a) (b) rep -- (c) ) -- c )
91     [ [ simd-unbox ] [ underlying>> ] bi* ] 2dip 3curry make-underlying ; inline
92
93 : (vv->x-op) ( a b rep quot: ( (a) (b) rep -- n ) -- n )
94     [ [ underlying>> ] bi@ ] 2dip 3curry call ; inline
95
96 : (vvx->v-op) ( a b obj rep quot: ( (a) (b) obj rep -- (c) ) -- c )
97     [ [ simd-unbox ] [ underlying>> ] bi* ] 3dip 2curry 2curry make-underlying ; inline
98     
99 : vv->v-op ( a b rep quot: ( (a) (b) rep -- (c) ) fallback-quot -- c )
100     [ '[ _ (vv->v-op) ] ] [ '[ drop @ ] ] bi* if-both-vectors-match ; inline
101
102 :: vvx->v-op ( a b obj rep quot: ( (a) (b) obj rep -- (c) ) fallback-quot -- c )
103     a b rep
104     [ obj swap quot (vvx->v-op) ]
105     [ drop obj fallback-quot call ] if-both-vectors-match ; inline
106
107 : vv'->v-op ( a b rep quot: ( (a) (b) rep -- (c) ) fallback-quot -- c )
108     [ '[ _ (vv->v-op) ] ] [ '[ drop @ ] ] bi* if-both-vectors ; inline
109
110 : vv->x-op ( a b rep quot: ( (a) (b) rep -- obj ) fallback-quot -- obj )
111     [ '[ _ (vv->x-op) ] ] [ '[ drop @ ] ] bi* if-both-vectors-match ; inline
112
113 : mask>count ( n rep -- n' )
114     [ bit-count ] dip {
115         { float-4-rep     [ ] }
116         { double-2-rep    [ -1 shift ] }
117         { uchar-16-rep    [ ] }
118         { char-16-rep     [ ] }
119         { ushort-8-rep    [ -1 shift ] }
120         { short-8-rep     [ -1 shift ] }
121         { ushort-8-rep    [ -1 shift ] }
122         { int-4-rep       [ -2 shift ] }
123         { uint-4-rep      [ -2 shift ] }
124         { longlong-2-rep  [ -3 shift ] }
125         { ulonglong-2-rep [ -3 shift ] }
126     } case ; inline
127
128 PRIVATE>
129 >>
130
131 <<
132
133 ! SIMD vectors as sequences
134
135 M: simd-128 hashcode* underlying>> hashcode* ; inline
136 M: simd-128 clone [ clone ] change-underlying ; inline
137 M: simd-128 byte-length drop 16 ; inline
138
139 M: simd-128 new-sequence
140     2dup length =
141     [ nip [ 16 (byte-array) ] make-underlying ]
142     [ length bad-simd-length ] if ; inline
143
144 M: simd-128 equal?
145     dup simd-rep [ drop v= vall? ] [ 3drop f ] if-both-vectors-match ; inline
146
147 ! SIMD primitive operations
148
149 M: simd-128 v+
150     dup simd-rep [ (simd-v+) ] [ call-next-method ] vv->v-op ; inline
151 M: simd-128 v-
152     dup simd-rep [ (simd-v-) ] [ call-next-method ] vv->v-op ; inline
153 M: simd-128 vneg
154     dup simd-rep [ (simd-vneg) ] [ call-next-method ] v->v-op  ; inline
155 M: simd-128 v+-
156     dup simd-rep [ (simd-v+-) ] [ call-next-method ] vv->v-op ; inline
157 M: simd-128 vs+
158     dup simd-rep [ (simd-vs+) ] [ call-next-method ] vv->v-op ; inline
159 M: simd-128 vs-
160     dup simd-rep [ (simd-vs-) ] [ call-next-method ] vv->v-op ; inline
161 M: simd-128 vs*
162     dup simd-rep [ (simd-vs*) ] [ call-next-method ] vv->v-op ; inline
163 M: simd-128 v*
164     dup simd-rep [ (simd-v*) ] [ call-next-method ] vv->v-op ; inline
165 M: simd-128 v*high
166     dup simd-rep [ (simd-v*high) ] [ call-next-method ] vv->v-op ; inline
167 M: simd-128 v/
168     dup simd-rep [ (simd-v/) ] [ call-next-method ] vv->v-op ; inline
169 M: simd-128 vavg
170     dup simd-rep [ (simd-vavg) ] [ call-next-method ] vv->v-op ; inline
171 M: simd-128 vmin
172     dup simd-rep [ (simd-vmin) ] [ call-next-method ] vv->v-op ; inline
173 M: simd-128 vmax
174     dup simd-rep [ (simd-vmax) ] [ call-next-method ] vv->v-op ; inline
175 M: simd-128 v.
176     dup simd-rep [ (simd-v.) ] [ call-next-method ] vv->x-op ; inline
177 M: simd-128 vsad
178     dup simd-rep [ (simd-vsad) ] [ call-next-method ] vv->x-op ; inline
179 M: simd-128 vsqrt
180     dup simd-rep [ (simd-vsqrt) ] [ call-next-method ] v->v-op  ; inline
181 M: simd-128 sum
182     dup simd-rep [ (simd-sum) ] [ call-next-method ] v->x-op  ; inline
183 M: simd-128 vabs
184     dup simd-rep [ (simd-vabs) ] [ call-next-method ] v->v-op  ; inline
185 M: simd-128 vbitand
186     dup simd-rep [ (simd-vbitand) ] [ call-next-method ] vv->v-op ; inline
187 M: simd-128 vbitandn
188     dup simd-rep [ (simd-vbitandn) ] [ call-next-method ] vv->v-op ; inline
189 M: simd-128 vbitor
190     dup simd-rep [ (simd-vbitor) ] [ call-next-method ] vv->v-op ; inline
191 M: simd-128 vbitxor
192     dup simd-rep [ (simd-vbitxor) ] [ call-next-method ] vv->v-op ; inline
193 M: simd-128 vbitnot
194     dup simd-rep [ (simd-vbitnot) ] [ call-next-method ] v->v-op  ; inline
195 M: simd-128 vand
196     dup simd-rep [ (simd-vand) ] [ call-next-method ] vv->v-op ; inline
197 M: simd-128 vandn
198     dup simd-rep [ (simd-vandn) ] [ call-next-method ] vv->v-op ; inline
199 M: simd-128 vor
200     dup simd-rep [ (simd-vor) ] [ call-next-method ] vv->v-op ; inline
201 M: simd-128 vxor
202     dup simd-rep [ (simd-vxor) ] [ call-next-method ] vv->v-op ; inline
203 M: simd-128 vnot
204     dup simd-rep [ (simd-vnot) ] [ call-next-method ] v->v-op  ; inline
205 M: simd-128 vlshift
206     over simd-rep [ (simd-vlshift) ] [ call-next-method ] vn->v-op ; inline
207 M: simd-128 vrshift
208     over simd-rep [ (simd-vrshift) ] [ call-next-method ] vn->v-op ; inline
209 M: simd-128 hlshift
210     over simd-rep [ (simd-hlshift) ] [ call-next-method ] vn->v-op ; inline
211 M: simd-128 hrshift
212     over simd-rep [ (simd-hrshift) ] [ call-next-method ] vn->v-op ; inline
213 M: simd-128 vshuffle-elements
214     over simd-rep [ (simd-vshuffle-elements) ] [ call-next-method ] vx->v-op ; inline
215 M: simd-128 vshuffle2-elements
216     over simd-rep [ (simd-vshuffle2-elements) ] [ call-next-method ] vvx->v-op ; inline
217 M: simd-128 vshuffle-bytes
218     dup simd-rep [ (simd-vshuffle-bytes) ] [ call-next-method ] vv'->v-op ; inline
219 M: simd-128 (vmerge-head)
220     dup simd-rep [ (simd-vmerge-head) ] [ call-next-method ] vv->v-op ; inline
221 M: simd-128 (vmerge-tail)
222     dup simd-rep [ (simd-vmerge-tail) ] [ call-next-method ] vv->v-op ; inline
223 M: simd-128 v<=
224     dup simd-rep [ (simd-v<=) ] [ call-next-method ] vv->v-op ; inline
225 M: simd-128 v<
226     dup simd-rep [ (simd-v<) ] [ call-next-method ] vv->v-op ; inline
227 M: simd-128 v=
228     dup simd-rep [ (simd-v=) ] [ call-next-method ] vv->v-op ; inline
229 M: simd-128 v>
230     dup simd-rep [ (simd-v>) ] [ call-next-method ] vv->v-op ; inline
231 M: simd-128 v>=
232     dup simd-rep [ (simd-v>=) ] [ call-next-method ] vv->v-op ; inline
233 M: simd-128 vunordered?
234     dup simd-rep [ (simd-vunordered?) ] [ call-next-method ] vv->v-op ; inline
235 M: simd-128 vany?
236     dup simd-rep [ (simd-vany?) ] [ call-next-method ] v->x-op  ; inline
237 M: simd-128 vall?
238     dup simd-rep [ (simd-vall?) ] [ call-next-method ] v->x-op  ; inline
239 M: simd-128 vnone?
240     dup simd-rep [ (simd-vnone?) ] [ call-next-method ] v->x-op  ; inline
241 M: simd-128 vcount
242     dup simd-rep
243     [ [ (simd-vgetmask) assert-positive ] [ call-next-method ] v->x-op ]
244     [ mask>count ] bi ; inline
245
246 ! SIMD high-level specializations
247
248 M: simd-128 vbroadcast swap [ nth ] [ simd-with ] bi ; inline
249 M: simd-128 n+v [ simd-with ] keep v+ ; inline
250 M: simd-128 n-v [ simd-with ] keep v- ; inline
251 M: simd-128 n*v [ simd-with ] keep v* ; inline
252 M: simd-128 n/v [ simd-with ] keep v/ ; inline
253 M: simd-128 v+n over simd-with v+ ; inline
254 M: simd-128 v-n over simd-with v- ; inline
255 M: simd-128 v*n over simd-with v* ; inline
256 M: simd-128 v/n over simd-with v/ ; inline
257 M: simd-128 norm-sq dup v. assert-positive ; inline
258 M: simd-128 distance v- norm ; inline
259
260 M: simd-128 >pprint-sequence ;
261 M: simd-128 pprint* pprint-object ;
262
263 <PRIVATE
264
265 ! SIMD concrete type functor
266
267 FUNCTOR: define-simd-128 ( T -- )
268
269 A      DEFINES-CLASS ${T}
270 A-rep  IS            ${T}-rep
271 >A     DEFINES       >${T}
272 A-boa  DEFINES       ${T}-boa
273 A-with DEFINES       ${T}-with
274 A-cast DEFINES       ${T}-cast
275 A{     DEFINES       ${T}{
276
277 ELT     [ A-rep rep-component-type ]
278 N       [ A-rep rep-length ]
279 COERCER [ ELT c:c-type-class "coercer" word-prop [ ] or ]
280
281 BOA-EFFECT [ N "n" <array> { "v" } <effect> ]
282
283 WHERE
284
285 TUPLE: A < simd-128 ; final
286
287 M: A new-underlying    drop \ A boa ; inline
288 M: A simd-rep          drop A-rep ; inline
289 M: A simd-element-type drop ELT ; inline
290 M: A simd-with         drop A-with ; inline
291
292 M: A nth-unsafe
293     swap \ A-rep [ (simd-select) ] [ call-next-method ] vx->x-op ; inline
294 M: A set-nth-unsafe
295     [ ELT boolean>element ] 2dip
296     underlying>> ELT c:set-alien-element ; inline
297
298 : >A ( seq -- simd ) \ A new clone-like ; inline
299
300 M: A like drop dup \ A instance? [ >A ] unless ; inline
301
302 : A-with ( n -- v ) COERCER call \ A-rep (simd-with) \ A boa ; inline
303 : A-cast ( v -- v' ) underlying>> \ A boa ; inline
304
305 M: A length drop N ; inline
306
307 \ A-boa
308 [ COERCER N napply ] N {
309     { 2 [ [ A-rep (simd-gather-2) A boa ] ] }
310     { 4 [ [ A-rep (simd-gather-4) A boa ] ] }
311     [ \ A new '[ _ _ nsequence ] ]
312 } case compose
313 BOA-EFFECT define-inline
314
315 M: A pprint-delims drop \ A{ \ } ;
316 SYNTAX: A{ \ } [ >A ] parse-literal ;
317
318 INSTANCE: A sequence
319
320 c:<c-type>
321     byte-array >>class
322     A >>boxed-class
323     { A-rep alien-vector A boa } >quotation >>getter
324     {
325         [ dup simd-128? [ bad-simd-vector ] unless underlying>> ] 2dip
326         A-rep set-alien-vector
327     } >quotation >>setter
328     16 >>size
329     16 >>align
330     A-rep >>rep
331 \ A c:typedef
332
333 ;FUNCTOR
334
335 SYNTAX: SIMD-128:
336     scan-token define-simd-128 ;
337
338 PRIVATE>
339
340 >>
341
342 ! SIMD instances
343
344 SIMD-128: char-16
345 SIMD-128: uchar-16
346 SIMD-128: short-8
347 SIMD-128: ushort-8
348 SIMD-128: int-4
349 SIMD-128: uint-4
350 SIMD-128: longlong-2
351 SIMD-128: ulonglong-2
352 SIMD-128: float-4
353 SIMD-128: double-2
354
355 ! misc
356
357 M: simd-128 vshuffle ( u perm -- v )
358     vshuffle-bytes ; inline
359
360 M: uchar-16 v*hs+
361     uchar-16-rep [ (simd-v*hs+) ] [ call-next-method ] vv->v-op short-8-cast ; inline
362 M: ushort-8 v*hs+
363     ushort-8-rep [ (simd-v*hs+) ] [ call-next-method ] vv->v-op uint-4-cast ; inline
364 M: uint-4 v*hs+
365     uint-4-rep [ (simd-v*hs+) ] [ call-next-method ] vv->v-op ulonglong-2-cast ; inline
366 M: char-16 v*hs+
367     char-16-rep [ (simd-v*hs+) ] [ call-next-method ] vv->v-op short-8-cast ; inline
368 M: short-8 v*hs+
369     short-8-rep [ (simd-v*hs+) ] [ call-next-method ] vv->v-op int-4-cast ; inline
370 M: int-4 v*hs+
371     int-4-rep [ (simd-v*hs+) ] [ call-next-method ] vv->v-op longlong-2-cast ; inline
372
373 { "math.vectors.simd" "mirrors" } "math.vectors.simd.mirrors" require-when