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 ) 0 [ 8 * shift + ] reduce-index ;
8 : be> ( seq -- x ) 0 [ [ 8 shift ] dip + ] reduce ;
10 : mask-byte ( x -- y ) 0xff bitand ; inline
12 : nth-byte ( x n -- b ) -8 * shift mask-byte ; inline
16 : map-bytes ( x seq -- byte-array )
17 [ nth-byte ] with B{ } map-as ; inline
21 : >le ( x n -- byte-array ) <iota> map-bytes ;
23 : >be ( x n -- byte-array ) <iota> <reversed> map-bytes ;
25 : d>w/w ( d -- w1 w2 )
26 [ 0xffffffff bitand ] [ -32 shift 0xffffffff bitand ] bi ;
28 : w>h/h ( w -- h1 h2 )
29 [ 0xffff bitand ] [ -16 shift 0xffff bitand ] bi ;
31 : h>b/b ( h -- b1 b2 )
32 [ mask-byte ] [ -8 shift mask-byte ] bi ;
36 : signed> ( x seq -- n )
37 length 8 * 2dup 1 - bit? [ 2^ - ] [ drop ] if ; inline
41 : signed-le> ( bytes -- x ) [ le> ] [ signed> ] bi ;
43 : signed-be> ( bytes -- x ) [ be> ] [ signed> ] bi ;