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 ;
8 IN: math.vectors.simd.functor
10 ERROR: bad-length got expected ;
12 MACRO: simd-boa ( rep class -- simd-array )
13 [ rep-components ] [ new ] bi* '[ _ _ nsequence ] ;
15 :: define-boa-custom-inlining ( word rep class -- )
18 rep rep rep-gather-word supported-simd-op? [
19 [ rep (simd-boa) class boa ]
21 ] "custom-inlining" set-word-prop ;
23 : simd-with ( rep class x -- simd-array )
24 [ rep-components ] [ new ] [ '[ _ ] ] tri* swap replicate-as ; inline
26 :: define-with-custom-inlining ( word rep class -- )
29 rep \ (simd-broadcast) supported-simd-op? [
30 [ rep rep-coerce rep (simd-broadcast) class boa ]
32 ] "custom-inlining" set-word-prop ;
34 : boa-effect ( rep n -- effect )
35 [ rep-components ] dip *
36 [ CHAR: a + 1string ] map
37 { "simd-vector" } <effect> ;
39 : supported-simd-ops ( assoc rep -- assoc' )
51 '[ nip _ swap supported-simd-op? ] assoc-filter
52 '[ drop _ key? ] assoc-filter ;
54 :: high-level-ops ( ctor -- assoc )
55 ! Some SIMD operations are defined in terms of others.
57 { 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 ] }
70 { distance [ v- norm ] }
73 :: simd-vector-words ( class ctor rep assoc -- )
75 rep rep-component-type c-type-boxed-class
76 assoc rep supported-simd-ops
77 ctor high-level-ops assoc-union
78 specialize-vector-words ;
80 FUNCTOR: define-simd-128 ( T -- )
82 N [ 16 T heap-size /i ]
84 A DEFINES-CLASS ${T}-${N}
85 A-boa DEFINES ${A}-boa
86 A-with DEFINES ${A}-with
90 NTH [ T dup c-type-getter-boxer array-accessor ]
91 SET-NTH [ T dup c-setter array-accessor ]
94 A-vv->v-op DEFINES-PRIVATE ${A}-vv->v-op
95 A-v->n-op DEFINES-PRIVATE ${A}-v->n-op
100 { underlying byte-array read-only initial: $[ 16 <byte-array> ] } ;
102 M: A clone underlying>> clone \ A boa ; inline
104 M: A length drop N ; inline
106 M: A nth-unsafe underlying>> NTH call ; inline
108 M: A set-nth-unsafe underlying>> SET-NTH call ; inline
110 : >A ( seq -- simd-array ) \ A new clone-like ;
112 M: A like drop dup \ A instance? [ >A ] unless ; inline
116 [ drop 16 <byte-array> \ A boa ]
120 M: A equal? over \ A instance? [ sequence= ] [ 2drop f ] if ;
122 M: A byte-length underlying>> length ; inline
124 M: A pprint-delims drop \ A{ \ } ;
126 M: A >pprint-sequence ;
128 M: A pprint* pprint-object ;
130 SYNTAX: A{ \ } [ >A ] parse-literal ;
132 : A-with ( x -- simd-array ) [ A-rep A ] dip simd-with ;
134 \ A-with \ A-rep \ A define-with-custom-inlining
136 \ A-boa [ \ A-rep \ A simd-boa ] \ A-rep 1 boa-effect define-declared
138 \ A-rep rep-gather-word [
139 \ A-boa \ A-rep \ A define-boa-custom-inlining
146 : A-vv->v-op ( v1 v2 quot -- v3 )
147 [ [ underlying>> ] bi@ A-rep ] dip call \ A boa ; inline
149 : A-v->n-op ( v quot -- n )
150 [ underlying>> A-rep ] dip call ; inline
152 \ A \ A-with \ A-rep H{
153 { v+ [ [ (simd-v+) ] \ A-vv->v-op execute ] }
154 { v- [ [ (simd-v-) ] \ A-vv->v-op execute ] }
155 { v* [ [ (simd-v*) ] \ A-vv->v-op execute ] }
156 { v/ [ [ (simd-v/) ] \ A-vv->v-op execute ] }
157 { vmin [ [ (simd-vmin) ] \ A-vv->v-op execute ] }
158 { vmax [ [ (simd-vmax) ] \ A-vv->v-op execute ] }
159 { sum [ [ (simd-sum) ] \ A-v->n-op execute ] }
166 ! Synthesize 256-bit vectors from a pair of 128-bit vectors
167 FUNCTOR: define-simd-256 ( T -- )
169 N [ 32 T heap-size /i ]
173 A/2-boa IS ${A/2}-boa
174 A/2-with IS ${A/2}-with
176 A DEFINES-CLASS ${T}-${N}
177 A-boa DEFINES ${A}-boa
178 A-with DEFINES ${A}-with
182 A-deref DEFINES-PRIVATE ${A}-deref
185 A-vv->v-op DEFINES-PRIVATE ${A}-vv->v-op
186 A-v->n-op DEFINES-PRIVATE ${A}-v->n-op
194 { underlying1 byte-array initial: $[ 16 <byte-array> ] read-only }
195 { underlying2 byte-array initial: $[ 16 <byte-array> ] read-only } ;
198 [ underlying1>> clone ] [ underlying2>> clone ] bi
201 M: A length drop N ; inline
203 : A-deref ( n seq -- n' seq' )
204 over N/2 < [ underlying1>> ] [ [ N/2 - ] dip underlying2>> ] if \ A/2 boa ; inline
206 M: A nth-unsafe A-deref nth-unsafe ; inline
208 M: A set-nth-unsafe A-deref set-nth-unsafe ; inline
210 : >A ( seq -- simd-array ) \ A new clone-like ;
212 M: A like drop dup \ A instance? [ >A ] unless ; inline
216 [ drop 16 <byte-array> 16 <byte-array> \ A boa ]
220 M: A equal? over \ A instance? [ sequence= ] [ 2drop f ] if ;
222 M: A byte-length drop 32 ; inline
224 SYNTAX: A{ \ } [ >A ] parse-literal ;
226 M: A pprint-delims drop \ A{ \ } ;
228 M: A >pprint-sequence ;
230 M: A pprint* pprint-object ;
232 : A-with ( x -- simd-array )
233 [ A/2-with ] [ A/2-with ] bi [ underlying>> ] bi@
236 : A-boa ( ... -- simd-array )
237 [ A/2-boa ] N/2 ndip A/2-boa [ underlying>> ] bi@
240 \ A-rep 2 boa-effect \ A-boa set-stack-effect
244 : A-vv->v-op ( v1 v2 quot -- v3 )
245 [ [ [ underlying1>> ] bi@ A-rep ] dip call ]
246 [ [ [ underlying2>> ] bi@ A-rep ] dip call ] 3bi
249 : A-v->n-op ( v1 combine-quot reduce-quot -- v2 )
250 [ [ [ underlying1>> ] [ underlying2>> ] bi A-rep ] dip call A-rep ]
253 \ A \ A-with \ A-rep H{
254 { v+ [ [ (simd-v+) ] \ A-vv->v-op execute ] }
255 { v- [ [ (simd-v-) ] \ A-vv->v-op execute ] }
256 { v* [ [ (simd-v*) ] \ A-vv->v-op execute ] }
257 { v/ [ [ (simd-v/) ] \ A-vv->v-op execute ] }
258 { vmin [ [ (simd-vmin) ] \ A-vv->v-op execute ] }
259 { vmax [ [ (simd-vmax) ] \ A-vv->v-op execute ] }
260 { sum [ [ (simd-v+) ] [ (simd-sum) ] \ A-v->n-op execute ] }