From 69a6fa72bc9359c20e9caac8c33fe65129204126 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Sun, 31 May 2015 08:44:55 -0700 Subject: [PATCH] io.binary.fast: adding fast version of signed> words. --- extra/io/binary/fast/fast-tests.factor | 5 +++++ extra/io/binary/fast/fast.factor | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/extra/io/binary/fast/fast-tests.factor b/extra/io/binary/fast/fast-tests.factor index 575c13012a..d1ebb9a32e 100644 --- a/extra/io/binary/fast/fast-tests.factor +++ b/extra/io/binary/fast/fast-tests.factor @@ -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 diff --git a/extra/io/binary/fast/fast.factor b/extra/io/binary/fast/fast.factor index 60ca17c7e4..64206a7cd5 100644 --- a/extra/io/binary/fast/fast.factor +++ b/extra/io/binary/fast/fast.factor @@ -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 = [ {