1 ! Copyright (C) 2003, 2007 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math sequences ;
6 : le> ( seq -- x ) dup length iota 0 [ 8 * shift + ] 2reduce ;
7 : be> ( seq -- x ) 0 [ [ 8 shift ] dip + ] reduce ;
9 : mask-byte ( x -- y ) 0xff bitand ; inline
11 : nth-byte ( x n -- b ) -8 * shift mask-byte ; inline
13 : >le ( x n -- byte-array ) iota [ nth-byte ] with B{ } map-as ;
14 : >be ( x n -- byte-array ) >le reverse! ;
16 : d>w/w ( d -- w1 w2 )
18 [ -32 shift 0xffffffff bitand ] bi ;
20 : w>h/h ( w -- h1 h2 )
22 [ -16 shift 0xffff bitand ] bi ;
24 : h>b/b ( h -- b1 b2 )
26 [ -8 shift mask-byte ] bi ;
28 : signed-le> ( bytes -- x )
29 [ le> ] [ length 8 * 1 - 2^ 1 - ] bi
30 2dup > [ bitnot bitor ] [ drop ] if ;
32 : signed-be> ( bytes -- x )
33 <reversed> signed-le> ;