io.binary.fast: adding fast version of signed> words.

db4
John Benediktsson 2015-05-31 08:44:55 -07:00
parent e04dc6db29
commit 69a6fa72bc
2 changed files with 31 additions and 0 deletions

View File

@ -11,3 +11,8 @@ IN: io.binary.fast.tests
[ 0x01020304 ] [ B{ 04 03 02 01 } 4le> ] unit-test
[ 0x0102030405060708 ] [ B{ 08 07 06 05 04 03 02 01 } 8le> ] unit-test
{ 0x04030201 } [ B{ 1 2 3 4 } signed-le> ] unit-test
{ 0x01020304 } [ B{ 1 2 3 4 } signed-be> ] unit-test
{ -12 } [ B{ 0xf4 0xff 0xff 0xff } signed-le> ] unit-test
{ -12 } [ B{ 0xff 0xff 0xff 0xf4 } signed-be> ] unit-test

View File

@ -5,6 +5,8 @@ combinators.smart endian fry hints kernel locals macros math
math.ranges sequences sequences.generalizations ;
RENAME: be> io.binary => slow-be>
RENAME: le> io.binary => slow-le>
RENAME: signed-be> io.binary => slow-signed-be>
RENAME: signed-le> io.binary => slow-signed-le>
RENAME: >be io.binary => >slow-be
RENAME: >le io.binary => >slow-le
IN: io.binary.fast
@ -64,6 +66,18 @@ PRIVATE>
[ drop slow-be> ]
} case ;
: signed-be> ( bytes -- x )
compute-native-endianness big-endian = [
dup byte-array? [
dup length {
{ 2 [ int16_t deref ] }
{ 4 [ int32_t deref ] }
{ 8 [ int64_t deref ] }
[ drop slow-signed-be> ]
} case
] [ slow-signed-be> ] if
] [ slow-signed-be> ] if ;
: 2le> ( bytes -- x )
little-endian [ uint16_t deref ] [ 2 n-le> ] if-endian ;
@ -81,6 +95,18 @@ PRIVATE>
[ drop slow-le> ]
} case ;
: signed-le> ( bytes -- x )
compute-native-endianness little-endian = [
dup byte-array? [
dup length {
{ 2 [ int16_t deref ] }
{ 4 [ int32_t deref ] }
{ 8 [ int64_t deref ] }
[ drop slow-signed-le> ]
} case
] [ slow-signed-le> ] if
] [ slow-signed-le> ] if ;
: >le ( x n -- bytes )
compute-native-endianness little-endian = [
{