1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types assocs byte-arrays classes
4 effects fry functors generalizations kernel literals locals
5 math math.functions math.vectors math.vectors.simd.intrinsics
6 math.vectors.specialization parser prettyprint.custom sequences
7 sequences.private strings words definitions macros cpu.architecture ;
9 IN: math.vectors.simd.functor
11 ERROR: bad-length got expected ;
13 MACRO: simd-boa ( rep class -- simd-array )
14 [ rep-components ] [ new ] bi* '[ _ _ nsequence ] ;
16 :: define-boa-custom-inlining ( word rep class -- )
19 rep rep rep-gather-word supported-simd-op? [
20 [ rep (simd-boa) class boa ]
22 ] "custom-inlining" set-word-prop ;
24 : simd-with ( rep class x -- simd-array )
25 [ rep-components ] [ new ] [ '[ _ ] ] tri* swap replicate-as ; inline
27 :: define-with-custom-inlining ( word rep class -- )
30 rep \ (simd-broadcast) supported-simd-op? [
31 [ rep rep-coerce rep (simd-broadcast) class boa ]
33 ] "custom-inlining" set-word-prop ;
35 : boa-effect ( rep n -- effect )
36 [ rep-components ] dip *
37 [ CHAR: a + 1string ] map
38 { "simd-vector" } <effect> ;
40 : supported-simd-ops ( assoc rep -- assoc' )
52 '[ nip _ swap supported-simd-op? ] assoc-filter
53 '[ drop _ key? ] assoc-filter ;
55 :: high-level-ops ( ctor elt-class -- assoc )
56 ! Some SIMD operations are defined in terms of others.
58 { vneg [ [ dup v- ] keep v- ] }
59 { n+v [ [ ctor execute ] dip v+ ] }
60 { v+n [ ctor execute v+ ] }
61 { n-v [ [ ctor execute ] dip v- ] }
62 { v-n [ ctor execute v- ] }
63 { n*v [ [ ctor execute ] dip v* ] }
64 { v*n [ ctor execute v* ] }
65 { n/v [ [ ctor execute ] dip v/ ] }
66 { v/n [ ctor execute v/ ] }
67 { norm-sq [ dup v. assert-positive ] }
68 { norm [ norm-sq sqrt ] }
69 { normalize [ dup norm v/n ] }
71 ! To compute dot product and distance with integer vectors, we
72 ! have to do things less efficiently, with integer overflow checks,
73 ! in the general case.
76 { distance [ v- norm ] }
81 :: simd-vector-words ( class ctor rep assoc -- )
82 rep rep-component-type c-type-boxed-class :> elt-class
85 assoc rep supported-simd-ops
86 ctor elt-class high-level-ops assoc-union
87 specialize-vector-words ;
89 :: define-simd-128-type ( class rep -- )
93 [ rep alien-vector class boa ] >>getter
94 [ [ underlying>> ] 2dip rep set-alien-vector ] >>setter
100 FUNCTOR: define-simd-128 ( T -- )
102 N [ 16 T heap-size /i ]
104 A DEFINES-CLASS ${T}-${N}
105 A-boa DEFINES ${A}-boa
106 A-with DEFINES ${A}-with
110 NTH [ T dup c-type-getter-boxer array-accessor ]
111 SET-NTH [ T dup c-setter array-accessor ]
114 A-vv->v-op DEFINES-PRIVATE ${A}-vv->v-op
115 A-v->n-op DEFINES-PRIVATE ${A}-v->n-op
120 { underlying byte-array read-only initial: $[ 16 <byte-array> ] } ;
122 M: A clone underlying>> clone \ A boa ; inline
124 M: A length drop N ; inline
126 M: A nth-unsafe underlying>> NTH call ; inline
128 M: A set-nth-unsafe underlying>> SET-NTH call ; inline
130 : >A ( seq -- simd-array ) \ A new clone-like ;
132 M: A like drop dup \ A instance? [ >A ] unless ; inline
136 [ drop 16 <byte-array> \ A boa ]
140 M: A equal? over \ A instance? [ sequence= ] [ 2drop f ] if ;
142 M: A byte-length underlying>> length ; inline
144 M: A pprint-delims drop \ A{ \ } ;
146 M: A >pprint-sequence ;
148 M: A pprint* pprint-object ;
150 SYNTAX: A{ \ } [ >A ] parse-literal ;
152 : A-with ( x -- simd-array ) [ A-rep A ] dip simd-with ;
154 \ A-with \ A-rep \ A define-with-custom-inlining
156 \ A-boa [ \ A-rep \ A simd-boa ] \ A-rep 1 boa-effect define-declared
158 \ A-rep rep-gather-word [
159 \ A-boa \ A-rep \ A define-boa-custom-inlining
166 : A-vv->v-op ( v1 v2 quot -- v3 )
167 [ [ underlying>> ] bi@ A-rep ] dip call \ A boa ; inline
169 : A-v->n-op ( v quot -- n )
170 [ underlying>> A-rep ] dip call ; inline
172 \ A \ A-with \ A-rep H{
173 { v+ [ [ (simd-v+) ] \ A-vv->v-op execute ] }
174 { v- [ [ (simd-v-) ] \ A-vv->v-op execute ] }
175 { v* [ [ (simd-v*) ] \ A-vv->v-op execute ] }
176 { v/ [ [ (simd-v/) ] \ A-vv->v-op execute ] }
177 { vmin [ [ (simd-vmin) ] \ A-vv->v-op execute ] }
178 { vmax [ [ (simd-vmax) ] \ A-vv->v-op execute ] }
179 { sum [ [ (simd-sum) ] \ A-v->n-op execute ] }
182 \ A \ A-rep define-simd-128-type
188 ! Synthesize 256-bit vectors from a pair of 128-bit vectors
192 :: define-simd-256-type ( class rep -- )
198 [ 16 + >fixnum rep alien-vector ] 2bi
202 [ [ underlying1>> ] 2dip rep set-alien-vector ]
203 [ [ underlying2>> ] 2dip 16 + >fixnum rep set-alien-vector ]
211 FUNCTOR: define-simd-256 ( T -- )
213 N [ 32 T heap-size /i ]
217 A/2-boa IS ${A/2}-boa
218 A/2-with IS ${A/2}-with
220 A DEFINES-CLASS ${T}-${N}
221 A-boa DEFINES ${A}-boa
222 A-with DEFINES ${A}-with
226 A-deref DEFINES-PRIVATE ${A}-deref
229 A-vv->v-op DEFINES-PRIVATE ${A}-vv->v-op
230 A-v->n-op DEFINES-PRIVATE ${A}-v->n-op
238 { underlying1 byte-array initial: $[ 16 <byte-array> ] read-only }
239 { underlying2 byte-array initial: $[ 16 <byte-array> ] read-only } ;
242 [ underlying1>> clone ] [ underlying2>> clone ] bi
245 M: A length drop N ; inline
247 : A-deref ( n seq -- n' seq' )
248 over N/2 < [ underlying1>> ] [ [ N/2 - ] dip underlying2>> ] if \ A/2 boa ; inline
250 M: A nth-unsafe A-deref nth-unsafe ; inline
252 M: A set-nth-unsafe A-deref set-nth-unsafe ; inline
254 : >A ( seq -- simd-array ) \ A new clone-like ;
256 M: A like drop dup \ A instance? [ >A ] unless ; inline
260 [ drop 16 <byte-array> 16 <byte-array> \ A boa ]
264 M: A equal? over \ A instance? [ sequence= ] [ 2drop f ] if ;
266 M: A byte-length drop 32 ; inline
268 SYNTAX: A{ \ } [ >A ] parse-literal ;
270 M: A pprint-delims drop \ A{ \ } ;
272 M: A >pprint-sequence ;
274 M: A pprint* pprint-object ;
276 : A-with ( x -- simd-array )
277 [ A/2-with ] [ A/2-with ] bi [ underlying>> ] bi@
280 : A-boa ( ... -- simd-array )
281 [ A/2-boa ] N/2 ndip A/2-boa [ underlying>> ] bi@
284 \ A-rep 2 boa-effect \ A-boa set-stack-effect
288 : A-vv->v-op ( v1 v2 quot -- v3 )
289 [ [ [ underlying1>> ] bi@ A-rep ] dip call ]
290 [ [ [ underlying2>> ] bi@ A-rep ] dip call ] 3bi
293 : A-v->n-op ( v1 combine-quot reduce-quot -- v2 )
294 [ [ [ underlying1>> ] [ underlying2>> ] bi A-rep ] dip call A-rep ]
297 \ A \ A-with \ A-rep H{
298 { v+ [ [ (simd-v+) ] \ A-vv->v-op execute ] }
299 { v- [ [ (simd-v-) ] \ A-vv->v-op execute ] }
300 { v* [ [ (simd-v*) ] \ A-vv->v-op execute ] }
301 { v/ [ [ (simd-v/) ] \ A-vv->v-op execute ] }
302 { vmin [ [ (simd-vmin) ] \ A-vv->v-op execute ] }
303 { vmax [ [ (simd-vmax) ] \ A-vv->v-op execute ] }
304 { sum [ [ (simd-v+) ] [ (simd-sum) ] \ A-v->n-op execute ] }
307 \ A \ A-rep define-simd-256-type