2009-09-03 04:43:43 -04:00
|
|
|
! Copyright (C) 2009 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-09-20 03:08:32 -04:00
|
|
|
USING: alien alien.c-types alien.data assocs combinators
|
2009-10-03 12:29:34 -04:00
|
|
|
cpu.architecture compiler.cfg.comparisons fry generalizations
|
2009-10-06 21:13:38 -04:00
|
|
|
kernel libc macros math
|
2009-10-06 22:28:33 -04:00
|
|
|
math.vectors.conversion.backend
|
2009-10-07 15:09:46 -04:00
|
|
|
sequences sets effects accessors namespaces
|
2009-10-03 12:29:34 -04:00
|
|
|
lexer parser vocabs.parser words arrays math.vectors ;
|
2009-09-03 04:43:43 -04:00
|
|
|
IN: math.vectors.simd.intrinsics
|
|
|
|
|
|
|
|
ERROR: bad-simd-call ;
|
|
|
|
|
2009-09-23 03:46:54 -04:00
|
|
|
<<
|
|
|
|
|
|
|
|
: simd-effect ( word -- effect )
|
|
|
|
stack-effect [ in>> "rep" suffix ] [ out>> ] bi <effect> ;
|
2009-10-06 21:13:38 -04:00
|
|
|
: simd-conversion-effect ( word -- effect )
|
|
|
|
stack-effect [ in>> but-last "rep" suffix ] [ out>> ] bi <effect> ;
|
2009-09-23 03:46:54 -04:00
|
|
|
|
|
|
|
SYMBOL: simd-ops
|
|
|
|
|
|
|
|
V{ } clone simd-ops set-global
|
|
|
|
|
2009-10-06 21:13:38 -04:00
|
|
|
: (SIMD-OP:) ( accum quot -- accum )
|
|
|
|
[
|
|
|
|
scan-word dup name>> "(simd-" ")" surround create-in
|
|
|
|
[ nip [ bad-simd-call ] define ]
|
|
|
|
] dip
|
|
|
|
'[ _ dip set-stack-effect ]
|
2009-09-23 03:46:54 -04:00
|
|
|
[ 2array simd-ops get push ]
|
2009-10-06 21:13:38 -04:00
|
|
|
2tri ; inline
|
|
|
|
|
|
|
|
SYNTAX: SIMD-OP:
|
|
|
|
[ simd-effect ] (SIMD-OP:) ;
|
|
|
|
|
|
|
|
SYNTAX: SIMD-CONVERSION-OP:
|
|
|
|
[ simd-conversion-effect ] (SIMD-OP:) ;
|
2009-09-23 03:46:54 -04:00
|
|
|
|
|
|
|
>>
|
|
|
|
|
|
|
|
SIMD-OP: v+
|
|
|
|
SIMD-OP: v-
|
2009-10-09 14:16:39 -04:00
|
|
|
SIMD-OP: vneg
|
2009-09-23 03:46:54 -04:00
|
|
|
SIMD-OP: v+-
|
|
|
|
SIMD-OP: vs+
|
|
|
|
SIMD-OP: vs-
|
|
|
|
SIMD-OP: vs*
|
|
|
|
SIMD-OP: v*
|
|
|
|
SIMD-OP: v/
|
|
|
|
SIMD-OP: vmin
|
|
|
|
SIMD-OP: vmax
|
2009-09-28 18:31:34 -04:00
|
|
|
SIMD-OP: v.
|
2009-09-23 03:46:54 -04:00
|
|
|
SIMD-OP: vsqrt
|
|
|
|
SIMD-OP: sum
|
|
|
|
SIMD-OP: vabs
|
|
|
|
SIMD-OP: vbitand
|
2009-09-28 03:17:46 -04:00
|
|
|
SIMD-OP: vbitandn
|
2009-09-23 03:46:54 -04:00
|
|
|
SIMD-OP: vbitor
|
|
|
|
SIMD-OP: vbitxor
|
2009-10-02 21:04:28 -04:00
|
|
|
SIMD-OP: vbitnot
|
2009-10-02 15:17:01 -04:00
|
|
|
SIMD-OP: vand
|
|
|
|
SIMD-OP: vandn
|
|
|
|
SIMD-OP: vor
|
|
|
|
SIMD-OP: vxor
|
2009-10-02 21:04:28 -04:00
|
|
|
SIMD-OP: vnot
|
2009-09-24 07:58:33 -04:00
|
|
|
SIMD-OP: vlshift
|
|
|
|
SIMD-OP: vrshift
|
2009-09-28 03:17:46 -04:00
|
|
|
SIMD-OP: hlshift
|
|
|
|
SIMD-OP: hrshift
|
2009-09-28 18:31:34 -04:00
|
|
|
SIMD-OP: vshuffle
|
2009-10-05 18:55:39 -04:00
|
|
|
SIMD-OP: (vmerge-head)
|
|
|
|
SIMD-OP: (vmerge-tail)
|
2009-10-03 12:29:34 -04:00
|
|
|
SIMD-OP: v<=
|
|
|
|
SIMD-OP: v<
|
2009-10-01 15:31:37 -04:00
|
|
|
SIMD-OP: v=
|
2009-10-03 12:29:34 -04:00
|
|
|
SIMD-OP: v>
|
|
|
|
SIMD-OP: v>=
|
|
|
|
SIMD-OP: vunordered?
|
2009-10-01 16:35:38 -04:00
|
|
|
SIMD-OP: vany?
|
|
|
|
SIMD-OP: vall?
|
|
|
|
SIMD-OP: vnone?
|
2009-09-23 03:46:54 -04:00
|
|
|
|
2009-10-06 21:13:38 -04:00
|
|
|
SIMD-CONVERSION-OP: (v>float)
|
|
|
|
SIMD-CONVERSION-OP: (v>integer)
|
|
|
|
SIMD-CONVERSION-OP: (vpack-signed)
|
|
|
|
SIMD-CONVERSION-OP: (vpack-unsigned)
|
|
|
|
SIMD-CONVERSION-OP: (vunpack-head)
|
|
|
|
SIMD-CONVERSION-OP: (vunpack-tail)
|
|
|
|
|
2009-09-29 23:58:20 -04:00
|
|
|
: (simd-with) ( x rep -- v ) bad-simd-call ;
|
2009-09-03 04:43:43 -04:00
|
|
|
: (simd-gather-2) ( a b rep -- v ) bad-simd-call ;
|
|
|
|
: (simd-gather-4) ( a b c d rep -- v ) bad-simd-call ;
|
2009-09-28 18:31:34 -04:00
|
|
|
: (simd-select) ( v n rep -- x ) bad-simd-call ;
|
2009-09-28 03:17:46 -04:00
|
|
|
|
2009-09-03 04:43:43 -04:00
|
|
|
: assert-positive ( x -- y ) ;
|
|
|
|
|
|
|
|
: alien-vector ( c-ptr n rep -- value )
|
|
|
|
! Inefficient version for when intrinsics are missing
|
|
|
|
[ swap <displaced-alien> ] dip rep-size memory>byte-array ;
|
|
|
|
|
|
|
|
: set-alien-vector ( value c-ptr n rep -- )
|
|
|
|
! Inefficient version for when intrinsics are missing
|
|
|
|
[ swap <displaced-alien> swap ] dip rep-size memcpy ;
|
|
|
|
|
2009-09-20 03:08:32 -04:00
|
|
|
<<
|
|
|
|
|
|
|
|
: rep-components ( rep -- n )
|
|
|
|
16 swap rep-component-type heap-size /i ; foldable
|
|
|
|
|
|
|
|
: rep-coercer ( rep -- quot )
|
|
|
|
{
|
|
|
|
{ [ dup int-vector-rep? ] [ [ >fixnum ] ] }
|
|
|
|
{ [ dup float-vector-rep? ] [ [ >float ] ] }
|
|
|
|
} cond nip ; foldable
|
|
|
|
|
|
|
|
: rep-coerce ( value rep -- value' )
|
|
|
|
rep-coercer call( value -- value' ) ; inline
|
|
|
|
|
|
|
|
CONSTANT: rep-gather-words
|
|
|
|
{
|
|
|
|
{ 2 (simd-gather-2) }
|
|
|
|
{ 4 (simd-gather-4) }
|
|
|
|
}
|
|
|
|
|
|
|
|
: rep-gather-word ( rep -- word )
|
|
|
|
rep-components rep-gather-words at ;
|
|
|
|
|
|
|
|
>>
|
|
|
|
|
|
|
|
MACRO: (simd-boa) ( rep -- quot )
|
|
|
|
{
|
|
|
|
[ rep-coercer ]
|
|
|
|
[ rep-components ]
|
|
|
|
[ ]
|
|
|
|
[ rep-gather-word ]
|
|
|
|
} cleave
|
|
|
|
'[ _ _ napply _ _ execute ] ;
|
|
|
|
|
|
|
|
GENERIC# supported-simd-op? 1 ( rep intrinsic -- ? )
|
|
|
|
|
2009-10-07 15:09:46 -04:00
|
|
|
: (%unpack-reps) ( -- reps )
|
|
|
|
%merge-vector-reps [ int-vector-rep? ] filter
|
|
|
|
%unpack-vector-head-reps union ;
|
|
|
|
|
2009-09-20 03:08:32 -04:00
|
|
|
M: vector-rep supported-simd-op?
|
|
|
|
{
|
2009-10-05 18:55:39 -04:00
|
|
|
{ \ (simd-v+) [ %add-vector-reps ] }
|
|
|
|
{ \ (simd-vs+) [ %saturated-add-vector-reps ] }
|
|
|
|
{ \ (simd-v+-) [ %add-sub-vector-reps ] }
|
|
|
|
{ \ (simd-v-) [ %sub-vector-reps ] }
|
|
|
|
{ \ (simd-vs-) [ %saturated-sub-vector-reps ] }
|
2009-10-09 14:16:39 -04:00
|
|
|
{ \ (simd-vneg) [ %sub-vector-reps ] }
|
2009-10-05 18:55:39 -04:00
|
|
|
{ \ (simd-v*) [ %mul-vector-reps ] }
|
|
|
|
{ \ (simd-vs*) [ %saturated-mul-vector-reps ] }
|
|
|
|
{ \ (simd-v/) [ %div-vector-reps ] }
|
|
|
|
{ \ (simd-vmin) [ %min-vector-reps ] }
|
|
|
|
{ \ (simd-vmax) [ %max-vector-reps ] }
|
|
|
|
{ \ (simd-v.) [ %dot-vector-reps ] }
|
|
|
|
{ \ (simd-vsqrt) [ %sqrt-vector-reps ] }
|
|
|
|
{ \ (simd-sum) [ %horizontal-add-vector-reps ] }
|
|
|
|
{ \ (simd-vabs) [ %abs-vector-reps ] }
|
|
|
|
{ \ (simd-vbitand) [ %and-vector-reps ] }
|
|
|
|
{ \ (simd-vbitandn) [ %andn-vector-reps ] }
|
|
|
|
{ \ (simd-vbitor) [ %or-vector-reps ] }
|
|
|
|
{ \ (simd-vbitxor) [ %xor-vector-reps ] }
|
2009-10-07 12:59:36 -04:00
|
|
|
{ \ (simd-vbitnot) [ %xor-vector-reps ] }
|
2009-10-05 18:55:39 -04:00
|
|
|
{ \ (simd-vand) [ %and-vector-reps ] }
|
|
|
|
{ \ (simd-vandn) [ %andn-vector-reps ] }
|
|
|
|
{ \ (simd-vor) [ %or-vector-reps ] }
|
|
|
|
{ \ (simd-vxor) [ %xor-vector-reps ] }
|
2009-10-07 12:59:36 -04:00
|
|
|
{ \ (simd-vnot) [ %xor-vector-reps ] }
|
2009-10-05 18:55:39 -04:00
|
|
|
{ \ (simd-vlshift) [ %shl-vector-reps ] }
|
|
|
|
{ \ (simd-vrshift) [ %shr-vector-reps ] }
|
|
|
|
{ \ (simd-hlshift) [ %horizontal-shl-vector-reps ] }
|
|
|
|
{ \ (simd-hrshift) [ %horizontal-shr-vector-reps ] }
|
|
|
|
{ \ (simd-vshuffle) [ %shuffle-vector-reps ] }
|
|
|
|
{ \ (simd-(vmerge-head)) [ %merge-vector-reps ] }
|
|
|
|
{ \ (simd-(vmerge-tail)) [ %merge-vector-reps ] }
|
2009-10-06 21:13:38 -04:00
|
|
|
{ \ (simd-(v>float)) [ %integer>float-vector-reps ] }
|
|
|
|
{ \ (simd-(v>integer)) [ %float>integer-vector-reps ] }
|
|
|
|
{ \ (simd-(vpack-signed)) [ %signed-pack-vector-reps ] }
|
|
|
|
{ \ (simd-(vpack-unsigned)) [ %unsigned-pack-vector-reps ] }
|
2009-10-07 15:09:46 -04:00
|
|
|
{ \ (simd-(vunpack-head)) [ (%unpack-reps) ] }
|
|
|
|
{ \ (simd-(vunpack-tail)) [ (%unpack-reps) ] }
|
2009-10-05 18:55:39 -04:00
|
|
|
{ \ (simd-v<=) [ cc<= %compare-vector-reps ] }
|
|
|
|
{ \ (simd-v<) [ cc< %compare-vector-reps ] }
|
|
|
|
{ \ (simd-v=) [ cc= %compare-vector-reps ] }
|
|
|
|
{ \ (simd-v>) [ cc> %compare-vector-reps ] }
|
|
|
|
{ \ (simd-v>=) [ cc>= %compare-vector-reps ] }
|
|
|
|
{ \ (simd-vunordered?) [ cc/<>= %compare-vector-reps ] }
|
|
|
|
{ \ (simd-gather-2) [ %gather-vector-2-reps ] }
|
|
|
|
{ \ (simd-gather-4) [ %gather-vector-4-reps ] }
|
|
|
|
{ \ (simd-vany?) [ %test-vector-reps ] }
|
|
|
|
{ \ (simd-vall?) [ %test-vector-reps ] }
|
|
|
|
{ \ (simd-vnone?) [ %test-vector-reps ] }
|
2009-09-20 03:08:32 -04:00
|
|
|
} case member? ;
|