 ! (c)2009 Slava Pestov, Joe Groff bsd license
-USING: accessors alien alien.data combinators
-sequences.cords cpu.architecture fry generalizations grouping
-kernel libc locals macros math math.libm math.order
-math.ranges math.vectors sequences sequences.generalizations
-sequences.private sequences.unrolled sequences.unrolled.private
-specialized-arrays vocabs words effects.parser locals.parser
-math.bitwise ;
+USING: accessors alien alien.data combinators cpu.architecture fry
+grouping kernel libc locals math math.libm math.order math.ranges
+sequences sequences.cords sequences.generalizations sequences.private
+sequences.unrolled sequences.unrolled.private specialized-arrays
+vocabs ;
 QUALIFIED-WITH: alien.c-types c
 SPECIALIZED-ARRAYS:
     c:char c:short c:int c:longlong
     c:uchar c:ushort c:uint c:ulonglong
     c:float c:double ;
 IN: math.vectors.simd.intrinsics
-
-<<
-: simd-intrinsic-body ( def effect -- def' )
-    '[ _ _ call-effect ] ;
-
-: define-simd-intrinsic ( word def effect -- )
-    [ simd-intrinsic-body ] keep define-declared ;
-
-SYNTAX: SIMD-INTRINSIC:
-    (:) define-declared ;
-SYNTAX: SIMD-INTRINSIC::
-    (::) define-declared ;
-
->>
 
 : assert-positive ( x -- y ) ;
 

        [ [ second ] bi@ * ] 2bi +
        wide-type c:c-type-clamp
    ] wide-rep <rep-array> unrolled-2map-as-unsafe underlying>> ;
SIMD-INTRINSIC: (simd-v/)                ( a b rep -- c ) [ native/ ] components-2map ;
SIMD-INTRINSIC: (simd-vavg)              ( a b rep -- c )
    [ + dup integer? [ 1 + -1 shift ] [ 0.5 * ] if ] components-2map ;
SIMD-INTRINSIC: (simd-vmin)              ( a b rep -- c ) [ min ] components-2map ;
SIMD-INTRINSIC: (simd-vmax)              ( a b rep -- c ) [ max ] components-2map ;
! XXX
SIMD-INTRINSIC: (simd-v.)                ( a b rep -- n )
    [ 2byte>rep-array [ [ first ] bi@ * ] 2keep ] keep
    1 swap rep-length [a,b) [ '[ _ swap nth-unsafe ] bi@ * + ] 2with each ;
SIMD-INTRINSIC: (simd-vsqrt)             ( a   rep -- c ) [ fsqrt ] components-map ;
SIMD-INTRINSIC: (simd-vsad)              ( a b rep -- c ) 2byte>rep-array [ - abs ] [ + ] 2map-reduce ;
SIMD-INTRINSIC: (simd-sum)               ( a   rep -- n ) [ + ] components-reduce ;
SIMD-INTRINSIC: (simd-vabs)              ( a   rep -- c ) [ abs ] components-map ;
SIMD-INTRINSIC: (simd-vbitand)           ( a b rep -- c ) [ bitand ] bitwise-components-2map ;
SIMD-INTRINSIC: (simd-vbitandn)          ( a b rep -- c ) [ [ bitnot ] dip bitand ] bitwise-components-2map ;
SIMD-INTRINSIC: (simd-vbitor)            ( a b rep -- c ) [ bitor ] bitwise-components-2map ;
SIMD-INTRINSIC: (simd-vbitxor)           ( a b rep -- c ) [ bitxor ] bitwise-components-2map ;
SIMD-INTRINSIC: (simd-vbitnot)           ( a   rep -- c ) [ bitnot ] bitwise-components-map ;
SIMD-INTRINSIC: (simd-vand)              ( a b rep -- c ) [ bitand ] bitwise-components-2map ;
SIMD-INTRINSIC: (simd-vandn)             ( a b rep -- c ) [ [ bitnot ] dip bitand ] bitwise-components-2map ;
SIMD-INTRINSIC: (simd-vor)               ( a b rep -- c ) [ bitor ] bitwise-components-2map ;
SIMD-INTRINSIC: (simd-vxor)              ( a b rep -- c ) [ bitxor ] bitwise-components-2map ;
SIMD-INTRINSIC: (simd-vnot)              ( a   rep -- c ) [ bitnot ] bitwise-components-map ;
SIMD-INTRINSIC: (simd-vlshift)           ( a n rep -- c ) swap '[ _ shift ] bitwise-components-map ;
SIMD-INTRINSIC: (simd-vrshift)           ( a n rep -- c ) swap '[ _ neg shift ] bitwise-components-map ;
! XXX
SIMD-INTRINSIC: (simd-hlshift)           ( a n rep -- c )
    drop head-slice* 16 0 pad-head ;
! XXX
SIMD-INTRINSIC: (simd-hrshift)           ( a n rep -- c )
    drop tail-slice 16 0 pad-tail ;
SIMD-INTRINSIC: (simd-vshuffle-elements) ( a n rep -- c ) [ rep-length 0 pad-tail ] keep (vshuffle) ;
SIMD-INTRINSIC: (simd-vshuffle2-elements) ( a b n rep -- c ) [ rep-length 0 pad-tail ] keep (vshuffle2) ;
SIMD-INTRINSIC: (simd-vshuffle-bytes)    ( a b rep -- c ) drop uchar-16-rep (vshuffle) ;
SIMD-INTRINSIC:: (simd-vmerge-head)      ( a b rep -- c )
    a b rep 2byte>rep-array :> ( a' b' )
    rep <rep-array> :> c'
    rep rep-length 2 /i [| n |

        n b' nth-unsafe n 2 * 1 + c' set-nth-unsafe
    ] unrolled-each-integer
    c' underlying>> ;
:: (simd-vmerge-tail)      ( a b rep -- c )
    a b rep 2byte>rep-array :> ( a' b' )
    rep <rep-array> :> c'
    rep rep-length 2 /i :> len
