[ 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
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
[ 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 ;
[ 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 = [
{