2009-10-06 15:00:16 -04:00
|
|
|
! (c)Joe Groff bsd license
|
2009-10-06 18:42:36 -04:00
|
|
|
USING: accessors alien.c-types arrays assocs classes combinators
|
2009-10-06 15:00:16 -04:00
|
|
|
combinators.short-circuit cords fry kernel locals math
|
2009-10-06 22:28:33 -04:00
|
|
|
math.vectors math.vectors.conversion.backend sequences ;
|
2009-10-06 15:00:16 -04:00
|
|
|
FROM: alien.c-types => char uchar short ushort int uint longlong ulonglong float double ;
|
|
|
|
IN: math.vectors.conversion
|
|
|
|
|
|
|
|
ERROR: bad-vconvert from-type to-type ;
|
2009-10-06 18:42:36 -04:00
|
|
|
ERROR: bad-vconvert-input value expected-type ;
|
2009-10-06 15:00:16 -04:00
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
: float-type? ( c-type -- ? )
|
|
|
|
{ float double } memq? ;
|
|
|
|
: unsigned-type? ( c-type -- ? )
|
|
|
|
{ uchar ushort uint ulonglong } memq? ;
|
|
|
|
|
2009-10-06 18:42:36 -04:00
|
|
|
: check-vconvert-type ( value expected-type -- value )
|
|
|
|
2dup instance? [ drop ] [ bad-vconvert-input ] if ; inline
|
|
|
|
|
2009-10-06 15:00:16 -04:00
|
|
|
:: [vconvert] ( from-element to-element from-size to-size from-type to-type -- quot )
|
|
|
|
{
|
|
|
|
{
|
|
|
|
[ from-element to-element eq? ]
|
|
|
|
[ [ ] ]
|
|
|
|
}
|
|
|
|
{
|
|
|
|
[ from-element to-element [ float-type? not ] both? ]
|
|
|
|
[ [ underlying>> to-type boa ] ]
|
|
|
|
}
|
|
|
|
{
|
|
|
|
[ from-element float-type? ]
|
|
|
|
[ [ to-type (v>integer) ] ]
|
|
|
|
}
|
|
|
|
{
|
|
|
|
[ to-element float-type? ]
|
|
|
|
[ [ to-type (v>float) ] ]
|
|
|
|
}
|
2009-10-06 18:42:36 -04:00
|
|
|
} cond
|
|
|
|
[ from-type check-vconvert-type ] prepose ;
|
2009-10-06 15:00:16 -04:00
|
|
|
|
|
|
|
:: [vpack] ( from-element to-element from-size to-size from-type to-type -- quot )
|
|
|
|
from-size to-size /i log2 :> steps
|
|
|
|
|
|
|
|
{
|
|
|
|
[ steps 1 = not ]
|
|
|
|
[ from-element to-element [ float-type? ] bi@ xor ]
|
|
|
|
[ from-element unsigned-type? to-element unsigned-type? not and ]
|
|
|
|
} 0|| [ from-type to-type bad-vconvert ] when
|
|
|
|
|
2009-10-06 18:42:36 -04:00
|
|
|
to-element unsigned-type? [ to-type (vpack-unsigned) ] [ to-type (vpack-signed) ] ?
|
|
|
|
[ [ from-type check-vconvert-type ] bi@ ] prepose ;
|
2009-10-06 15:00:16 -04:00
|
|
|
|
|
|
|
:: [vunpack] ( from-element to-element from-size to-size from-type to-type -- quot )
|
|
|
|
to-size from-size /i log2 :> steps
|
|
|
|
|
|
|
|
{
|
|
|
|
[ steps 1 = not ]
|
|
|
|
[ from-element to-element [ float-type? ] bi@ xor ]
|
|
|
|
[ from-element unsigned-type? not to-element unsigned-type? and ]
|
|
|
|
} 0|| [ from-type to-type bad-vconvert ] when
|
|
|
|
|
2009-10-06 18:42:36 -04:00
|
|
|
[
|
|
|
|
from-type check-vconvert-type
|
|
|
|
[ to-type (vunpack-head) ] [ to-type (vunpack-tail) ] bi
|
|
|
|
] ;
|
2009-10-06 15:00:16 -04:00
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
MACRO:: vconvert ( from-type to-type -- )
|
|
|
|
from-type new [ element-type ] [ byte-length ] bi :> from-length :> from-element
|
|
|
|
to-type new [ element-type ] [ byte-length ] bi :> to-length :> to-element
|
|
|
|
from-element heap-size :> from-size
|
|
|
|
to-element heap-size :> to-size
|
|
|
|
|
|
|
|
from-length to-length = [ from-type to-type bad-vconvert ] unless
|
|
|
|
|
|
|
|
from-element to-element from-size to-size from-type to-type {
|
|
|
|
{ [ from-size to-size < ] [ [vunpack] ] }
|
|
|
|
{ [ from-size to-size = ] [ [vconvert] ] }
|
|
|
|
{ [ from-size to-size > ] [ [vpack] ] }
|
|
|
|
} cond ;
|
2009-10-06 18:42:36 -04:00
|
|
|
|