]> gitweb.factorcode.org Git - factor.git/commitdiff
io.binary.fast: adding fast version of signed> words.
authorJohn Benediktsson <mrjbq7@gmail.com>
Sun, 31 May 2015 15:44:55 +0000 (08:44 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sun, 31 May 2015 15:44:55 +0000 (08:44 -0700)
extra/io/binary/fast/fast-tests.factor
extra/io/binary/fast/fast.factor

index 575c13012a03a861b388008455948a2a168577f2..d1ebb9a32ee03519cc832b35b0b6eb365b96b08f 100644 (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
index 60ca17c7e469841d74ee6a4205ee630e94bcc40c..64206a7cd52fb77bc13b03dff2195b2e59757567 100644 (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 = [
         {