]> gitweb.factorcode.org Git - factor.git/commitdiff
io.binary.fast: adding fast alien version of >le and >be.
authorJohn Benediktsson <mrjbq7@gmail.com>
Mon, 2 Jun 2014 23:30:12 +0000 (16:30 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Mon, 2 Jun 2014 23:30:12 +0000 (16:30 -0700)
basis/endian/endian.factor
extra/io/binary/fast/fast.factor

index 4dd5ecf4075b7af9f33bc4ffc67b3875617f3c6c..4956d341d35bd5bb47e7349a4f531ec452d58f1f 100644 (file)
@@ -7,7 +7,7 @@ IN: endian
 SINGLETONS: big-endian little-endian ;
 
 : compute-native-endianness ( -- class )
-    1 int <ref> char deref 0 = big-endian little-endian ? ;
+    1 int <ref> char deref 0 = big-endian little-endian ? ; foldable
 
 SYMBOL: native-endianness
 native-endianness [ compute-native-endianness ] initialize
index 4773434608a305c2290365996fa20929ac493dcc..445f3a9bb1af39a098602cf060cc5ba0c0cc7c62 100644 (file)
@@ -1,10 +1,13 @@
 ! Copyright (C) 2011 Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: combinators combinators.smart fry kernel macros math
-math.ranges sequences sequences.generalizations io.binary
-locals ;
+USING: alien.data combinators combinators.smart endian fry
+io.binary kernel locals macros math math.ranges sequences
+sequences.generalizations ;
+QUALIFIED-WITH: alien.c-types c
 RENAME: be> io.binary => slow-be>
 RENAME: le> io.binary => slow-le>
+RENAME: >be io.binary => >slow-be
+RENAME: >le io.binary => >slow-le
 IN: io.binary.fast
 
 ERROR: bad-length bytes n ;
@@ -57,3 +60,23 @@ MACRO: reassemble-le ( n -- quot ) le-range reassemble-bytes ;
         { 8 [ 8le> ] }
         [ drop slow-le> ]
     } case ;
+
+: >le ( x n -- bytes )
+    compute-native-endianness little-endian = [
+        {
+            { 2 [ c:short <ref> ] }
+            { 4 [ c:int <ref> ] }
+            { 8 [ c:longlong <ref> ] }
+            [ >slow-le ]
+        } case
+    ] [ >slow-le ] if ;
+
+: >be ( x n -- bytes )
+    compute-native-endianness big-endian = [
+        {
+            { 2 [ c:short <ref> ] }
+            { 4 [ c:int <ref> ] }
+            { 8 [ c:longlong <ref> ] }
+            [ >slow-be ]
+        } case
+    ] [ >slow-be ] if ;