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