]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/math/vectors/simd/intrinsics/intrinsics.factor
factor: trim using lists
[factor.git] / basis / math / vectors / simd / intrinsics / intrinsics.factor
index 1bd5834f2cefa12eb17bf9b8c36607e063525bd7..21e691f3e2dd124bbeb003cabe21e2b22d5b14b9 100644 (file)
-! Copyright (C) 2009 Slava Pestov.
+! Copyright (C) 2009 Slava Pestov, Joe Groff.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: alien alien.c-types alien.data assocs combinators
-cpu.architecture compiler.cfg.comparisons fry generalizations
-kernel libc macros math
-math.vectors.conversion.backend
-sequences sets effects accessors namespaces
-lexer parser vocabs.parser words arrays math.vectors ;
+USING: accessors alien alien.data combinators cpu.architecture
+grouping kernel libc math math.libm math.order 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
 
-ERROR: bad-simd-call word ;
-
-<<
-
-: simd-effect ( word -- effect )
-    stack-effect [ in>> "rep" suffix ] [ out>> ] bi <effect> ;
-: simd-conversion-effect ( word -- effect )
-    stack-effect [ in>> but-last "rep" suffix ] [ out>> ] bi <effect> ;
-
-SYMBOL: simd-ops
-
-V{ } clone simd-ops set-global
-
-: (SIMD-OP:) ( accum quot -- accum )
-    [
-        scan-word dup name>> "(simd-" ")" surround create-in
-        [ nip dup '[ _ bad-simd-call ] define ]
-    ] dip
-    '[ _ dip set-stack-effect ]
-    [ 2array simd-ops get push ]
-    2tri ; inline
-
-SYNTAX: SIMD-OP:
-    [ simd-effect ] (SIMD-OP:) ;
-
-SYNTAX: SIMD-CONVERSION-OP:
-    [ simd-conversion-effect ] (SIMD-OP:) ;
-
->>
-
-SIMD-OP: v+
-SIMD-OP: v-
-SIMD-OP: vneg
-SIMD-OP: v+-
-SIMD-OP: vs+
-SIMD-OP: vs-
-SIMD-OP: vs*
-SIMD-OP: v*
-SIMD-OP: v/
-SIMD-OP: vmin
-SIMD-OP: vmax
-SIMD-OP: v.
-SIMD-OP: vsqrt
-SIMD-OP: sum
-SIMD-OP: vabs
-SIMD-OP: vbitand
-SIMD-OP: vbitandn
-SIMD-OP: vbitor
-SIMD-OP: vbitxor
-SIMD-OP: vbitnot
-SIMD-OP: vand
-SIMD-OP: vandn
-SIMD-OP: vor
-SIMD-OP: vxor
-SIMD-OP: vnot
-SIMD-OP: vlshift
-SIMD-OP: vrshift
-SIMD-OP: hlshift
-SIMD-OP: hrshift
-SIMD-OP: vshuffle-elements
-SIMD-OP: vshuffle-bytes
-SIMD-OP: (vmerge-head)
-SIMD-OP: (vmerge-tail)
-SIMD-OP: v<=
-SIMD-OP: v<
-SIMD-OP: v=
-SIMD-OP: v>
-SIMD-OP: v>=
-SIMD-OP: vunordered?
-SIMD-OP: vany?
-SIMD-OP: vall?
-SIMD-OP: vnone?
-
-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)
-
-: (simd-with) ( x rep -- v ) bad-simd-call ;
-: (simd-gather-2) ( a b rep -- v ) bad-simd-call ;
-: (simd-gather-4) ( a b c d rep -- v ) bad-simd-call ;
-: (simd-select) ( v n rep -- x ) bad-simd-call ;
-
+! Word props are added later
 : 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 ;
+<PRIVATE
 
-: set-alien-vector ( value c-ptr n rep -- )
-    ! Inefficient version for when intrinsics are missing
-    [ swap <displaced-alien> swap ] dip rep-size memcpy ;
+: >bitwise-vector-rep ( rep -- rep' )
+    {
+        { float-4-rep    [ uint-4-rep      ] }
+        { double-2-rep   [ ulonglong-2-rep ] }
+        [ ]
+    } case ; foldable
 
-<<
+: >uint-vector-rep ( rep -- rep' )
+    {
+        { longlong-2-rep [ ulonglong-2-rep ] }
+        { int-4-rep      [ uint-4-rep      ] }
+        { short-8-rep    [ ushort-8-rep    ] }
+        { char-16-rep    [ uchar-16-rep    ] }
+        [ ]
+    } case ; foldable
 
-: rep-components ( rep -- n )
-    16 swap rep-component-type heap-size /i ; foldable
+: >int-vector-rep ( rep -- rep' )
+    {
+        { float-4-rep  [ int-4-rep      ] }
+        { double-2-rep [ longlong-2-rep ] }
+    } case ; foldable
 
-: rep-coercer ( rep -- quot )
+: >float-vector-rep ( rep -- rep' )
     {
-        { [ dup int-vector-rep? ] [ [ >fixnum ] ] }
-        { [ dup float-vector-rep? ] [ [ >float ] ] }
-    } cond nip ; foldable
+        { int-4-rep      [ float-4-rep  ] }
+        { longlong-2-rep [ double-2-rep ] }
+    } case ; foldable
 
-: rep-coerce ( value rep -- value' )
-    rep-coercer call( value -- value' ) ; inline
+: byte>rep-array ( byte-array rep -- array )
+    {
+        { char-16-rep      [ 16 c:char <c-direct-array>      ] }
+        { uchar-16-rep     [ 16 c:uchar <c-direct-array>     ] }
+        { short-8-rep      [  8 c:short <c-direct-array>     ] }
+        { ushort-8-rep     [  8 c:ushort <c-direct-array>    ] }
+        { int-4-rep        [  4 c:int <c-direct-array>       ] }
+        { uint-4-rep       [  4 c:uint <c-direct-array>      ] }
+        { longlong-2-rep   [  2 c:longlong <c-direct-array>  ] }
+        { ulonglong-2-rep  [  2 c:ulonglong <c-direct-array> ] }
+        { float-4-rep      [  4 c:float <c-direct-array>     ] }
+        { double-2-rep     [  2 c:double <c-direct-array>    ] }
+    } case ; inline
 
-CONSTANT: rep-gather-words
+: >rep-array ( seq rep -- array )
     {
-        { 2 (simd-gather-2) }
-        { 4 (simd-gather-4) }
-    }
+        { char-16-rep      [ c:char >c-array      ] }
+        { uchar-16-rep     [ c:uchar >c-array     ] }
+        { short-8-rep      [ c:short >c-array     ] }
+        { ushort-8-rep     [ c:ushort >c-array    ] }
+        { int-4-rep        [ c:int >c-array       ] }
+        { uint-4-rep       [ c:uint >c-array      ] }
+        { longlong-2-rep   [ c:longlong >c-array  ] }
+        { ulonglong-2-rep  [ c:ulonglong >c-array ] }
+        { float-4-rep      [ c:float >c-array     ] }
+        { double-2-rep     [ c:double >c-array    ] }
+    } case ; inline
 
-: rep-gather-word ( rep -- word )
-    rep-components rep-gather-words at ;
+: <rep-array> ( rep -- array )
+    {
+        { char-16-rep      [ 16 c:char (c-array)      ] }
+        { uchar-16-rep     [ 16 c:uchar (c-array)     ] }
+        { short-8-rep      [  8 c:short (c-array)     ] }
+        { ushort-8-rep     [  8 c:ushort (c-array)    ] }
+        { int-4-rep        [  4 c:int (c-array)       ] }
+        { uint-4-rep       [  4 c:uint (c-array)      ] }
+        { longlong-2-rep   [  2 c:longlong (c-array)  ] }
+        { ulonglong-2-rep  [  2 c:ulonglong (c-array) ] }
+        { float-4-rep      [  4 c:float (c-array)     ] }
+        { double-2-rep     [  2 c:double (c-array)    ] }
+    } case ; inline
 
->>
+: rep-tf-values ( rep -- t f )
+    float-vector-rep? [ -1 bits>double 0.0 ] [ -1 0 ] if ;
 
-MACRO: (simd-boa) ( rep -- quot )
-    {
-        [ rep-coercer ]
-        [ rep-components ]
-        [ ]
-        [ rep-gather-word ]
-    } cleave
-    '[ _ _ napply _ _ execute ] ;
+: 2byte>rep-array ( a b rep -- a' b' )
+    '[ _ byte>rep-array ] bi@ ; inline
 
-GENERIC# supported-simd-op? 1 ( rep intrinsic -- ? )
+: components-map ( a rep quot -- c )
+    [ [ byte>rep-array ] [ rep-length ] bi ] dip unrolled-map-unsafe underlying>> ; inline
+: components-2map ( a b rep quot -- c )
+    [ [ 2byte>rep-array ] [ rep-length ] bi ] dip unrolled-2map-unsafe underlying>> ; inline
+: components-reduce ( a rep quot -- x )
+    [ byte>rep-array [ ] ] dip map-reduce ; inline
 
-: (%unpack-reps) ( -- reps )
-    %merge-vector-reps [ int-vector-rep? ] filter
-    %unpack-vector-head-reps union ;
+: bitwise-components-map ( a rep quot -- c )
+    [ >bitwise-vector-rep [ byte>rep-array ] [ rep-length ] bi ] dip
+    unrolled-map-unsafe underlying>> ; inline
+: bitwise-components-2map ( a b rep quot -- c )
+    [ >bitwise-vector-rep [ 2byte>rep-array ] [ rep-length ] bi ] dip
+    unrolled-2map-unsafe underlying>> ; inline
+: bitwise-components-reduce ( a rep quot -- x )
+    [ >bitwise-vector-rep byte>rep-array [ ] ] dip map-reduce ; inline
+: bitwise-components-reduce* ( a rep identity quot -- x )
+    [ >bitwise-vector-rep byte>rep-array ] 2dip reduce ; inline
 
-: (%abs-reps) ( -- reps )
-    cc> %compare-vector-reps [ int-vector-rep? ] filter
-    %xor-vector-reps [ float-vector-rep? ] filter
-    union
-    [ { } ] [ { uchar-16-rep ushort-8-rep uint-4-rep ulonglong-2-rep } union ] if-empty ;
+:: (vshuffle) ( a elts rep -- c )
+    a rep byte>rep-array :> a'
+    rep <rep-array> :> c'
+    elts rep rep-length [| from to |
+        from rep rep-length 1 - bitand
+           a' nth-unsafe
+        to c' set-nth-unsafe
+    ] unrolled-each-index-unsafe
+    c' underlying>> ; inline
 
-: (%shuffle-imm-reps) ( -- reps )
-    %shuffle-vector-reps %shuffle-vector-imm-reps union ;
+:: (vshuffle2) ( a b elts rep -- c )
+    a rep byte>rep-array :> a'
+    b rep byte>rep-array :> b'
+    a' b' cord-append :> ab'
+    rep <rep-array> :> c'
+    elts rep rep-length [| from to |
+        from rep rep-length dup + 1 - bitand
+           ab' nth-unsafe
+        to c' set-nth-unsafe
+    ] unrolled-each-index-unsafe
+    c' underlying>> ; inline
 
-M: vector-rep supported-simd-op?
-    {
-        { \ (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  ] }
-        { \ (simd-vneg)          [ %sub-vector-reps            ] }
-        { \ (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-reps)                 ] }
-        { \ (simd-vbitand)       [ %and-vector-reps            ] }
-        { \ (simd-vbitandn)      [ %andn-vector-reps           ] }
-        { \ (simd-vbitor)        [ %or-vector-reps             ] }
-        { \ (simd-vbitxor)       [ %xor-vector-reps            ] }
-        { \ (simd-vbitnot)       [ %xor-vector-reps            ] }
-        { \ (simd-vand)          [ %and-vector-reps            ] }
-        { \ (simd-vandn)         [ %andn-vector-reps           ] }
-        { \ (simd-vor)           [ %or-vector-reps             ] }
-        { \ (simd-vxor)          [ %xor-vector-reps            ] }
-        { \ (simd-vnot)          [ %xor-vector-reps            ] }
-        { \ (simd-vlshift)       [ %shl-vector-reps            ] }
-        { \ (simd-vrshift)       [ %shr-vector-reps            ] }
-        { \ (simd-hlshift)       [ %horizontal-shl-vector-reps ] }
-        { \ (simd-hrshift)       [ %horizontal-shr-vector-reps ] }
-        { \ (simd-vshuffle-elements) [ (%shuffle-imm-reps)         ] }
-        { \ (simd-vshuffle-bytes)    [ %shuffle-vector-reps        ] }
-        { \ (simd-(vmerge-head)) [ %merge-vector-reps          ] }
-        { \ (simd-(vmerge-tail)) [ %merge-vector-reps          ] }
-        { \ (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 ] }
-        { \ (simd-(vunpack-head))   [ (%unpack-reps)             ] }
-        { \ (simd-(vunpack-tail))   [ (%unpack-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-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           ] }
-    } case member? ;
+GENERIC: native/ ( x y -- x/y )
+
+M: integer native/ /i ; inline
+M: float native/ /f ; inline
+
+: (vgetmask) ( a rep -- b )
+    0 [ [ 1 shift ] [ zero? 0 1 ? ] bi* bitor ] bitwise-components-reduce* ; inline
+
+PRIVATE>
+
+: (simd-v+)                ( a b rep -- c ) [ + ] components-2map ;
+: (simd-v-)                ( a b rep -- c ) [ - ] components-2map ;
+: (simd-vneg)              ( a   rep -- c ) [ neg ] components-map ;
+:: (simd-v+-)              ( a b rep -- c )
+    a b rep 2byte>rep-array :> ( a' b' )
+    rep <rep-array> :> c'
+    0  rep rep-length [ 1 -  2 <range> ] [ 2 /i ] bi [| n |
+        n     a' nth-unsafe n     b' nth-unsafe -
+        n     c' set-nth-unsafe
+
+        n 1 + a' nth-unsafe n 1 + b' nth-unsafe +
+        n 1 + c' set-nth-unsafe
+    ] unrolled-each-unsafe
+    c' underlying>> ;
+: (simd-vs+)               ( a b rep -- c )
+    dup rep-component-type '[ + _ c:c-type-clamp ] components-2map ;
+: (simd-vs-)               ( a b rep -- c )
+    dup rep-component-type '[ - _ c:c-type-clamp ] components-2map ;
+: (simd-vs*)               ( a b rep -- c )
+    dup rep-component-type '[ * _ c:c-type-clamp ] components-2map ;
+: (simd-v*)                ( a b rep -- c ) [ * ] components-2map ;
+: (simd-v*high)            ( a b rep -- c )
+    dup rep-component-type c:heap-size -8 * '[ * _ shift ] components-2map ;
+:: (simd-v*hs+)            ( a b rep -- c )
+    rep { char-16-rep uchar-16-rep } member-eq?
+    [ uchar-16-rep char-16-rep ]
+    [ rep rep ] if :> ( a-rep b-rep )
+    b-rep widen-vector-rep signed-rep :> wide-rep
+    wide-rep rep-component-type :> wide-type
+    a a-rep byte>rep-array 2 <groups> :> a'
+    b b-rep byte>rep-array 2 <groups> :> b'
+    a' b' rep rep-length 2 /i [
+        [ [ first  ] bi@ * ]
+        [ [ second ] bi@ * ] 2bi +
+        wide-type c:c-type-clamp
+    ] wide-rep <rep-array> unrolled-2map-as-unsafe underlying>> ;
+: (simd-v/)                ( a b rep -- c ) [ native/ ] components-2map ;
+: (simd-vavg)              ( a b rep -- c )
+    [ + dup integer? [ 1 + -1 shift ] [ 0.5 * ] if ] components-2map ;
+: (simd-vmin)              ( a b rep -- c ) [ min ] components-2map ;
+: (simd-vmax)              ( a b rep -- c ) [ max ] components-2map ;
+! XXX
+: (simd-vdot)              ( a b rep -- n )
+    [ 2byte>rep-array [ [ first ] bi@ * ] 2keep ] keep
+    1 swap rep-length [a..b) [ '[ _ swap nth-unsafe ] bi@ * + ] 2with each ;
+: (simd-vsqrt)             ( a   rep -- c ) [ fsqrt ] components-map ;
+: (simd-vsad)              ( a b rep -- c ) 2byte>rep-array [ - abs ] [ + ] 2map-reduce ;
+: (simd-sum)               ( a   rep -- n ) [ + ] components-reduce ;
+: (simd-vabs)              ( a   rep -- c ) [ abs ] components-map ;
+: (simd-vbitand)           ( a b rep -- c ) [ bitand ] bitwise-components-2map ;
+: (simd-vbitandn)          ( a b rep -- c ) [ [ bitnot ] dip bitand ] bitwise-components-2map ;
+: (simd-vbitor)            ( a b rep -- c ) [ bitor ] bitwise-components-2map ;
+: (simd-vbitxor)           ( a b rep -- c ) [ bitxor ] bitwise-components-2map ;
+: (simd-vbitnot)           ( a   rep -- c ) [ bitnot ] bitwise-components-map ;
+: (simd-vand)              ( a b rep -- c ) [ bitand ] bitwise-components-2map ;
+: (simd-vandn)             ( a b rep -- c ) [ [ bitnot ] dip bitand ] bitwise-components-2map ;
+: (simd-vor)               ( a b rep -- c ) [ bitor ] bitwise-components-2map ;
+: (simd-vxor)              ( a b rep -- c ) [ bitxor ] bitwise-components-2map ;
+: (simd-vnot)              ( a   rep -- c ) [ bitnot ] bitwise-components-map ;
+: (simd-vlshift)           ( a n rep -- c ) swap '[ _ shift ] bitwise-components-map ;
+: (simd-vrshift)           ( a n rep -- c ) swap '[ _ neg shift ] bitwise-components-map ;
+! XXX
+: (simd-hlshift)           ( a n rep -- c )
+    drop head-slice* 16 0 pad-head ;
+! XXX
+: (simd-hrshift)           ( a n rep -- c )
+    drop tail-slice 16 0 pad-tail ;
+: (simd-vshuffle-elements) ( a n rep -- c ) [ rep-length 0 pad-tail ] keep (vshuffle) ;
+: (simd-vshuffle2-elements) ( a b n rep -- c ) [ rep-length 0 pad-tail ] keep (vshuffle2) ;
+: (simd-vshuffle-bytes)    ( a b rep -- c ) drop uchar-16-rep (vshuffle) ;
+:: (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 a' nth-unsafe n 2 *     c' set-nth-unsafe
+        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
+    len [| n |
+        n len + a' nth-unsafe n 2 *     c' set-nth-unsafe
+        n len + b' nth-unsafe n 2 * 1 + c' set-nth-unsafe
+    ] unrolled-each-integer
+    c' underlying>> ;
+: (simd-v<=)               ( a b rep -- c )
+    dup rep-tf-values '[ <= _ _ ? ] components-2map ;
+: (simd-v<)                ( a b rep -- c )
+    dup rep-tf-values '[ <  _ _ ? ] components-2map ;
+: (simd-v=)                ( a b rep -- c )
+    dup rep-tf-values '[ =  _ _ ? ] components-2map ;
+: (simd-v>)                ( a b rep -- c )
+    dup rep-tf-values '[ >  _ _ ? ] components-2map ;
+: (simd-v>=)               ( a b rep -- c )
+    dup rep-tf-values '[ >= _ _ ? ] components-2map ;
+: (simd-vunordered?)       ( a b rep -- c )
+    dup rep-tf-values '[ unordered? _ _ ? ] components-2map ;
+: (simd-vany?)             ( a   rep -- ? ) [ bitor  ] bitwise-components-reduce zero? not ;
+: (simd-vall?)             ( a   rep -- ? ) [ bitand ] bitwise-components-reduce zero? not ;
+: (simd-vnone?)            ( a   rep -- ? ) [ bitor  ] bitwise-components-reduce zero?     ;
+: (simd-vgetmask)          ( a   rep -- n )
+    { float-4-rep double-2-rep } member?
+    [ uint-4-rep (vgetmask) ] [ uchar-16-rep (vgetmask) ] if ;
+: (simd-v>float)           ( a   rep -- c )
+    [ [ byte>rep-array ] [ rep-length ] bi [ >float ] ]
+    [ >float-vector-rep <rep-array> ] bi unrolled-map-as-unsafe underlying>> ;
+: (simd-v>integer)         ( a   rep -- c )
+    [ [ byte>rep-array ] [ rep-length ] bi [ >integer ] ]
+    [ >int-vector-rep <rep-array> ] bi unrolled-map-as-unsafe underlying>> ;
+: (simd-vpack-signed)      ( a b rep -- c )
+    [ [ 2byte>rep-array cord-append ] [ rep-length 2 * ] bi ]
+    [ narrow-vector-rep [ <rep-array> ] [ rep-component-type ] bi ] bi
+    '[ _ c:c-type-clamp ] swap unrolled-map-as-unsafe underlying>> ;
+: (simd-vpack-unsigned)    ( a b rep -- c )
+    [ [ 2byte>rep-array cord-append ] [ rep-length 2 * ] bi ]
+    [ narrow-vector-rep >uint-vector-rep [ <rep-array> ] [ rep-component-type ] bi ] bi
+    '[ _ c:c-type-clamp ] swap unrolled-map-as-unsafe underlying>> ;
+: (simd-vunpack-head)      ( a   rep -- c )
+    [ byte>rep-array ] [ widen-vector-rep [ rep-length ] [ '[ _ >rep-array ] ] bi ] bi
+    [ head-slice ] dip call( a' -- c' ) underlying>> ;
+: (simd-vunpack-tail)      ( a   rep -- c )
+    [ byte>rep-array ] [ widen-vector-rep [ rep-length ] [ '[ _ >rep-array ] ] bi ] bi
+    [ tail-slice ] dip call( a' -- c' ) underlying>> ;
+: (simd-with)              ( n rep -- v )
+    [ rep-length swap '[ _ ] ] [ <rep-array> ] bi replicate-as
+    underlying>> ;
+: (simd-gather-2)          ( m n rep -- v ) <rep-array> [ 2 set-firstn-unsafe ] keep underlying>> ;
+: (simd-gather-4)          ( m n o p rep -- v ) <rep-array> [ 4 set-firstn-unsafe ] keep underlying>> ;
+: (simd-select)            ( a n rep -- x ) swapd byte>rep-array nth-unsafe ;
+
+: alien-vector     ( c-ptr n rep -- value )
+    [ swap <displaced-alien> ] dip rep-size memory>byte-array ;
+: set-alien-vector ( value c-ptr n rep -- )
+    [ swap <displaced-alien> swap ] dip rep-size memcpy ;
+
+"compiler.cfg.intrinsics.simd" require
+"compiler.tree.propagation.simd" require
+"compiler.cfg.value-numbering.simd" require