factor/basis/math/floats/half/half.factor

47 lines
1.0 KiB
Factor
Raw Normal View History

2009-06-10 14:06:06 -04:00
! (c)2009 Joe Groff bsd license
USING: accessors alien.accessors alien.c-types alien.data
alien.syntax kernel math math.order ;
FROM: math => float ;
IN: math.floats.half
2009-06-10 14:06:06 -04:00
: half>bits ( float -- bits )
float>bits
[ -31 shift 15 shift ] [
2011-11-23 21:49:33 -05:00
0x7fffffff bitand
2009-06-10 14:06:06 -04:00
dup zero? [
2011-11-23 21:49:33 -05:00
dup 0x7f800000 >= [ -13 shift 0x7fff bitand ] [
2009-06-10 14:06:06 -04:00
-13 shift
112 10 shift -
2011-11-23 21:49:33 -05:00
0 0x7c00 clamp
2009-06-10 14:06:06 -04:00
] if
] unless
] bi bitor ;
: bits>half ( bits -- float )
[ -15 shift 31 shift ] [
2011-11-23 21:49:33 -05:00
0x7fff bitand
2009-06-10 14:06:06 -04:00
dup zero? [
2011-11-23 21:49:33 -05:00
dup 0x7c00 >= [ 13 shift 0x7f800000 bitor ] [
2009-06-10 14:06:06 -04:00
13 shift
112 23 shift +
2009-06-10 14:06:06 -04:00
] if
] unless
] bi bitor bits>float ;
SYMBOL: half
2009-06-10 14:06:06 -04:00
<<
<c-type>
float >>class
float >>boxed-class
[ alien-unsigned-2 bits>half ] >>getter
[ [ >float half>bits ] 2dip set-alien-unsigned-2 ] >>setter
2 >>size
2 >>align
2009-11-10 20:34:14 -05:00
2 >>align-first
[ >float ] >>unboxer-quot
\ half typedef
2009-06-10 14:06:06 -04:00
>>