1 USING: accessors arrays classes compiler compiler.tree.debugger
2 effects fry io kernel kernel.private math math.functions
3 math.private math.vectors math.vectors.simd
4 math.vectors.simd.private prettyprint random sequences system
5 tools.test vocabs assocs compiler.cfg.debugger words
6 locals math.vectors.specialization combinators cpu.architecture
7 math.vectors.conversion.backend
8 math.vectors.simd.intrinsics namespaces byte-arrays alien
9 specialized-arrays classes.struct eval classes.algebra sets
10 quotations math.constants compiler.units ;
11 QUALIFIED-WITH: alien.c-types c
12 SPECIALIZED-ARRAY: c:float
23 IN: math.vectors.simd.tests
25 ! Make sure the functor doesn't generate bogus vocabularies
26 2 [ [ "USE: math.vectors.simd SIMD: rubinius" eval( -- ) ] must-fail ] times
28 [ f ] [ "math.vectors.simd.instances.rubinius" vocab ] unit-test
30 ! Test type propagation
31 [ V{ float } ] [ [ { float-4 } declare norm-sq ] final-classes ] unit-test
33 [ V{ float } ] [ [ { float-4 } declare norm ] final-classes ] unit-test
35 [ V{ float-4 } ] [ [ { float-4 } declare normalize ] final-classes ] unit-test
37 [ V{ float-4 } ] [ [ { float-4 float-4 } declare v+ ] final-classes ] unit-test
39 [ V{ float } ] [ [ { float-4 } declare second ] final-classes ] unit-test
41 [ V{ int-4 } ] [ [ { int-4 int-4 } declare v+ ] final-classes ] unit-test
43 [ t ] [ [ { int-4 } declare second ] final-classes first integer class<= ] unit-test
45 [ V{ longlong-2 } ] [ [ { longlong-2 longlong-2 } declare v+ ] final-classes ] unit-test
47 [ V{ integer } ] [ [ { longlong-2 } declare second ] final-classes ] unit-test
49 [ V{ int-8 } ] [ [ { int-8 int-8 } declare v+ ] final-classes ] unit-test
51 [ t ] [ [ { int-8 } declare second ] final-classes first integer class<= ] unit-test
53 ! Test puns; only on x86
55 [ double-2{ 4 1024 } ] [
57 [ { float-4 } declare dup v+ underlying>> double-2 boa dup v+ ] compile-call
61 double-2{ 1 2 } double-2{ 10 20 }
62 [ { double-2 double-2 } declare v+ underlying>> 3.0 float* ] compile-call
67 CONSTANT: simd-classes
91 : with-ctors ( -- seq )
92 simd-classes [ [ name>> "-with" append ] [ vocabulary>> ] bi lookup ] map ;
94 : boa-ctors ( -- seq )
95 simd-classes [ [ name>> "-boa" append ] [ vocabulary>> ] bi lookup ] map ;
97 : check-optimizer ( seq quot eq-quot -- failures )
100 [ dup [ class ] { } map-as ] dip '[ _ declare @ ]
102 [ "print-mr" get [ nip test-mr mr. ] [ 2drop ] if ]
103 [ "print-checks" get [ [ . ] bi@ ] [ 2drop ] if ]
104 [ [ call ] dip call ]
105 [ [ call ] dip compile-call ]
110 "== Checking -new constructors" print
113 simd-classes [ [ [ ] ] dip '[ _ new ] ] [ = ] check-optimizer
117 simd-classes [ '[ _ new ] compile-call [ zero? ] all? not ] filter
120 "== Checking -with constructors" print
124 [ 1000 random '[ _ ] ] dip '[ _ execute ]
125 ] [ = ] check-optimizer
128 [ HEX: ffffffff ] [ HEX: ffffffff uint-4-with first ] unit-test
130 [ HEX: ffffffff ] [ HEX: ffffffff [ uint-4-with ] compile-call first ] unit-test
132 "== Checking -boa constructors" print
136 [ stack-effect in>> length [ 1000 random ] [ ] replicate-as ] keep
138 ] [ = ] check-optimizer
141 [ HEX: ffffffff ] [ HEX: ffffffff 2 3 4 [ uint-4-boa ] compile-call first ] unit-test
143 "== Checking vector operations" print
145 : random-int-vector ( class -- vec )
146 new [ drop 1,000 random ] map ;
147 : random-float-vector ( class -- vec )
151 10 swap <array> 0/0. suffix random
154 : random-vector ( class elt-class -- vec )
156 [ random-float-vector ]
157 [ random-int-vector ] if ;
159 :: check-vector-op ( word inputs class elt-class -- inputs quot )
162 { +vector+ [ class elt-class random-vector ] }
163 { +scalar+ [ 1000 random elt-class float = [ >float ] when ] }
166 word '[ _ execute ] ;
168 : remove-float-words ( alist -- alist' )
169 { vsqrt n/v v/n v/ normalize } unique assoc-diff ;
171 : remove-integer-words ( alist -- alist' )
172 { vlshift vrshift } unique assoc-diff ;
174 : boolean-ops ( -- words )
175 { vand vandn vor vxor vnot } ;
177 : remove-boolean-words ( alist -- alist' )
178 boolean-ops unique assoc-diff ;
180 : remove-special-words ( alist -- alist' )
181 ! These have their own tests later
183 hlshift hrshift vshuffle vbroadcast
185 (v>float) (v>integer)
186 (vpack-signed) (vpack-unsigned)
187 (vunpack-head) (vunpack-tail)
188 } unique assoc-diff ;
190 : ops-to-check ( elt-class -- alist )
191 [ vector-words >alist ] dip
192 float = [ remove-integer-words ] [ remove-float-words ] if
194 remove-special-words ;
196 : check-vector-ops ( class elt-class compare-quot -- )
198 [ nip ops-to-check ] 2keep
199 '[ first2 inputs _ _ check-vector-op ]
200 ] dip check-optimizer ; inline
202 : approx= ( x y -- ? )
204 { [ 2dup [ fp-nan? ] both? ] [ 2drop t ] }
205 { [ 2dup [ float? ] both? ] [ -1.e8 ~ ] }
206 { [ 2dup [ fp-infinity? ] either? ] [ fp-bitwise= ] }
207 { [ 2dup [ sequence? ] both? ] [
210 { [ 2dup [ fp-nan? ] both? ] [ 2drop t ] }
211 { [ 2dup [ fp-infinity? ] either? ] [ fp-bitwise= ] }
212 { [ 2dup [ fp-nan? ] either? not ] [ -1.e8 ~ ] }
218 : exact= ( x y -- ? )
220 { [ 2dup [ float? ] both? ] [ fp-bitwise= ] }
221 { [ 2dup [ sequence? ] both? ] [ [ fp-bitwise= ] 2all? ] }
224 : simd-classes&reps ( -- alist )
227 { [ dup name>> "float" head? ] [ float [ approx= ] ] }
228 { [ dup name>> "double" head? ] [ float [ exact= ] ] }
234 [ [ { } ] ] dip first3 '[ _ _ _ check-vector-ops ] unit-test
237 "== Checking boolean operations" print
239 : random-boolean-vector ( class -- vec )
240 new [ drop 2 random zero? ] map ;
242 :: check-boolean-op ( word inputs class elt-class -- inputs quot )
245 { +vector+ [ class random-boolean-vector ] }
246 { +scalar+ [ 1000 random elt-class float = [ >float ] when ] }
249 word '[ _ execute ] ;
251 : check-boolean-ops ( class elt-class compare-quot -- )
253 [ boolean-ops [ dup word-schema ] { } map>assoc ] 2dip
254 '[ first2 inputs _ _ check-boolean-op ]
255 ] dip check-optimizer ; inline
258 [ [ { } ] ] dip first3 '[ _ _ _ check-boolean-ops ] unit-test
261 "== Checking vector blend" print
263 [ char-16{ 0 1 22 33 4 5 6 77 8 99 110 121 12 143 14 15 } ]
265 char-16{ t t f f t t t f t f f f t f t t }
266 char-16{ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 }
267 char-16{ 0 11 22 33 44 55 66 77 88 99 110 121 132 143 154 165 } v?
270 [ char-16{ 0 1 22 33 4 5 6 77 8 99 110 121 12 143 14 15 } ]
272 char-16{ t t f f t t t f t f f f t f t t }
273 char-16{ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 }
274 char-16{ 0 11 22 33 44 55 66 77 88 99 110 121 132 143 154 165 }
275 [ { char-16 char-16 char-16 } declare v? ] compile-call
278 [ int-4{ 1 22 33 4 } ]
279 [ int-4{ t f f t } int-4{ 1 2 3 4 } int-4{ 11 22 33 44 } v? ] unit-test
281 [ int-4{ 1 22 33 4 } ]
283 int-4{ t f f t } int-4{ 1 2 3 4 } int-4{ 11 22 33 44 }
284 [ { int-4 int-4 int-4 } declare v? ] compile-call
287 [ float-4{ 1.0 22.0 33.0 4.0 } ]
288 [ float-4{ t f f t } float-4{ 1.0 2.0 3.0 4.0 } float-4{ 11.0 22.0 33.0 44.0 } v? ] unit-test
290 [ float-4{ 1.0 22.0 33.0 4.0 } ]
292 float-4{ t f f t } float-4{ 1.0 2.0 3.0 4.0 } float-4{ 11.0 22.0 33.0 44.0 }
293 [ { float-4 float-4 float-4 } declare v? ] compile-call
296 "== Checking shifts and permutations" print
298 [ int-4{ 256 512 1024 2048 } ]
299 [ int-4{ 1 2 4 8 } 1 hlshift ] unit-test
301 [ int-4{ 256 512 1024 2048 } ]
302 [ int-4{ 1 2 4 8 } [ { int-4 } declare 1 hlshift ] compile-call ] unit-test
304 [ int-4{ 256 512 1024 2048 } ]
305 [ int-4{ 1 2 4 8 } 1 [ { int-4 fixnum } declare hlshift ] compile-call ] unit-test
308 [ int-4{ 256 512 1024 2048 } 1 hrshift ] unit-test
311 [ int-4{ 256 512 1024 2048 } [ { int-4 } declare 1 hrshift ] compile-call ] unit-test
314 [ int-4{ 256 512 1024 2048 } 1 [ { int-4 fixnum } declare hrshift ] compile-call ] unit-test
316 ! Invalid inputs should not cause the compiler to throw errors
318 [ [ { int-4 } declare t hrshift ] (( a -- b )) define-temp drop ] with-compilation-unit
322 [ [ { int-4 } declare { 3 2 1 } vshuffle ] (( a -- b )) define-temp drop ] with-compilation-unit
326 : shuffles-for ( n -- shuffles )
357 [ dup '[ _ random ] replicate 1array ]
362 [ new length shuffles-for ] keep
364 _ [ [ _ new [ length iota ] keep like 1quotation ] dip '[ _ vshuffle ] ]
365 [ = ] check-optimizer
369 "== Checking vector tests" print
371 :: test-vector-tests-bool ( vector declaration -- none? any? all? )
373 [ [ declaration declare vnone? ] compile-call ]
374 [ [ declaration declare vany? ] compile-call ]
375 [ [ declaration declare vall? ] compile-call ] tri ; inline
380 :: test-vector-tests-branch ( vector declaration -- none? any? all? )
382 [ [ declaration declare vnone? [ yes ] [ no ] if ] compile-call ]
383 [ [ declaration declare vany? [ yes ] [ no ] if ] compile-call ]
384 [ [ declaration declare vall? [ yes ] [ no ] if ] compile-call ] tri ; inline
386 SYMBOL: !!inconsistent!!
388 : ?inconsistent ( a b -- ab/inconsistent )
389 2dup = [ drop ] [ 2drop !!inconsistent!! ] if ;
391 :: test-vector-tests ( vector decl -- none? any? all? )
392 vector decl test-vector-tests-bool :> bool-all :> bool-any :> bool-none
393 vector decl test-vector-tests-branch :> branch-all :> branch-any :> branch-none
395 bool-none branch-none ?inconsistent
396 bool-any branch-any ?inconsistent
397 bool-all branch-all ?inconsistent ; inline
400 [ float-4{ t t t t } { float-4 } test-vector-tests ] unit-test
402 [ float-4{ f t t t } { float-4 } test-vector-tests ] unit-test
404 [ float-4{ f f f f } { float-4 } test-vector-tests ] unit-test
407 [ double-2{ t t } { double-2 } test-vector-tests ] unit-test
409 [ double-2{ f t } { double-2 } test-vector-tests ] unit-test
411 [ double-2{ f f } { double-2 } test-vector-tests ] unit-test
414 [ int-4{ t t t t } { int-4 } test-vector-tests ] unit-test
416 [ int-4{ f t t t } { int-4 } test-vector-tests ] unit-test
418 [ int-4{ f f f f } { int-4 } test-vector-tests ] unit-test
421 [ float-8{ t t t t t t t t } { float-8 } test-vector-tests ] unit-test
423 [ float-8{ f t t t t f t t } { float-8 } test-vector-tests ] unit-test
425 [ float-8{ f f f f f f f f } { float-8 } test-vector-tests ] unit-test
428 [ double-4{ t t t t } { double-4 } test-vector-tests ] unit-test
430 [ double-4{ f t t f } { double-4 } test-vector-tests ] unit-test
432 [ double-4{ f f f f } { double-4 } test-vector-tests ] unit-test
435 [ int-8{ t t t t t t t t } { int-8 } test-vector-tests ] unit-test
437 [ int-8{ f t t t t f f f } { int-8 } test-vector-tests ] unit-test
439 [ int-8{ f f f f f f f f } { int-8 } test-vector-tests ] unit-test
441 "== Checking element access" print
443 ! Test element access -- it should box bignums for int-4 on x86
444 : test-accesses ( seq -- failures )
445 [ length >array ] keep
446 '[ [ _ 1quotation ] dip '[ _ swap nth ] ] [ = ] check-optimizer ; inline
448 [ { } ] [ float-4{ 1.0 2.0 3.0 4.0 } test-accesses ] unit-test
449 [ { } ] [ int-4{ HEX: 7fffffff 3 4 -8 } test-accesses ] unit-test
450 [ { } ] [ uint-4{ HEX: ffffffff 2 3 4 } test-accesses ] unit-test
452 [ HEX: 7fffffff ] [ int-4{ HEX: 7fffffff 3 4 -8 } first ] unit-test
453 [ -8 ] [ int-4{ HEX: 7fffffff 3 4 -8 } last ] unit-test
454 [ HEX: ffffffff ] [ uint-4{ HEX: ffffffff 2 3 4 } first ] unit-test
456 [ { } ] [ double-2{ 1.0 2.0 } test-accesses ] unit-test
457 [ { } ] [ longlong-2{ 1 2 } test-accesses ] unit-test
458 [ { } ] [ ulonglong-2{ 1 2 } test-accesses ] unit-test
460 [ { } ] [ float-8{ 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 } test-accesses ] unit-test
461 [ { } ] [ int-8{ 1 2 3 4 5 6 7 8 } test-accesses ] unit-test
462 [ { } ] [ uint-8{ 1 2 3 4 5 6 7 8 } test-accesses ] unit-test
464 [ { } ] [ double-4{ 1.0 2.0 3.0 4.0 } test-accesses ] unit-test
465 [ { } ] [ longlong-4{ 1 2 3 4 } test-accesses ] unit-test
466 [ { } ] [ ulonglong-4{ 1 2 3 4 } test-accesses ] unit-test
468 "== Checking broadcast" print
469 : test-broadcast ( seq -- failures )
470 [ length >array ] keep
471 '[ [ _ 1quotation ] dip '[ _ vbroadcast ] ] [ = ] check-optimizer ; inline
473 [ { } ] [ float-4{ 1.0 2.0 3.0 4.0 } test-broadcast ] unit-test
474 [ { } ] [ int-4{ HEX: 7fffffff 3 4 -8 } test-broadcast ] unit-test
475 [ { } ] [ uint-4{ HEX: ffffffff 2 3 4 } test-broadcast ] unit-test
477 [ { } ] [ double-2{ 1.0 2.0 } test-broadcast ] unit-test
478 [ { } ] [ longlong-2{ 1 2 } test-broadcast ] unit-test
479 [ { } ] [ ulonglong-2{ 1 2 } test-broadcast ] unit-test
481 [ { } ] [ float-8{ 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 } test-broadcast ] unit-test
482 [ { } ] [ int-8{ 1 2 3 4 5 6 7 8 } test-broadcast ] unit-test
483 [ { } ] [ uint-8{ 1 2 3 4 5 6 7 8 } test-broadcast ] unit-test
485 [ { } ] [ double-4{ 1.0 2.0 3.0 4.0 } test-broadcast ] unit-test
486 [ { } ] [ longlong-4{ 1 2 3 4 } test-broadcast ] unit-test
487 [ { } ] [ ulonglong-4{ 1 2 3 4 } test-broadcast ] unit-test
489 ! Make sure we use the fallback in the correct situations
490 [ int-4{ 3 3 3 3 } ] [ int-4{ 12 34 3 17 } 2 [ { int-4 fixnum } declare vbroadcast ] compile-call ] unit-test
492 "== Checking alien operations" print
494 [ float-4{ 1 2 3 4 } ] [
497 underlying>> 0 float-4-rep alien-vector
498 ] compile-call float-4 boa
501 [ B{ 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 } ] [
502 16 [ 1 ] B{ } replicate-as 16 <byte-array>
505 { byte-array c-ptr fixnum } declare
506 float-4-rep set-alien-vector
511 [ float-array{ 1 2 3 4 } ] [
513 float-array{ 1 2 3 4 } underlying>>
514 float-array{ 4 3 2 1 } clone
515 [ underlying>> 0 float-4-rep set-alien-vector ] keep
525 [ t ] [ [ simd-struct <struct> ] compile-call >c-ptr [ 0 = ] all? ] unit-test
531 float-8{ 1 2 3 4 5 6 7 8 }
534 float-4{ 1 2 3 4 } >>x
536 double-4{ 4 3 2 1 } >>z
537 float-8{ 1 2 3 4 5 6 7 8 } >>w
538 { [ x>> ] [ y>> ] [ z>> ] [ w>> ] } cleave
545 float-8{ 1 2 3 4 5 6 7 8 }
549 float-4{ 1 2 3 4 } >>x
551 double-4{ 4 3 2 1 } >>z
552 float-8{ 1 2 3 4 5 6 7 8 } >>w
553 { [ x>> ] [ y>> ] [ z>> ] [ w>> ] } cleave
557 "== Misc tests" print
559 [ ] [ char-16 new 1array stack. ] unit-test
563 int-8{ 1000 1000 1000 1000 1000 1000 1000 1000 }
564 [ { int-8 } declare dup [ * ] [ + ] 2map-reduce ] compile-call
567 ! Coalescing was too aggressive
568 :: broken ( axis theta -- a b c )
569 axis { float-4 } declare drop
570 theta { float } declare drop
572 theta cos float-4-with :> cc
573 theta sin float-4-with :> ss
575 axis cc v+ :> diagonal
577 diagonal cc ss ; inline
580 float-4{ 1.0 0.0 1.0 0.0 } pi [ broken 3array ]
581 [ compile-call ] [ call ] 3bi =