]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/checksums/xxhash/xxhash.factor
factor: trim using lists
[factor.git] / basis / checksums / xxhash / xxhash.factor
index 1d690d949a5497e90b7d931d300b69ca74b2ecc3..23f32738fe0b545534d4f0b9025593dae607eae0 100644 (file)
@@ -1,9 +1,9 @@
 ! Copyright (C) 2014 John Benediktsson.
 ! See http://factorcode.org/license.txt for BSD license.
 
-USING: accessors alien alien.c-types alien.data byte-arrays
-checksums combinators generalizations grouping io.binary kernel
-locals math math.bitwise math.ranges sequences ;
+USING: accessors alien.c-types alien.data byte-arrays
+checksums combinators endian generalizations grouping kernel
+math math.bitwise sequences ;
 
 IN: checksums.xxhash
 
@@ -17,10 +17,23 @@ TUPLE: xxhash seed ;
 
 C: <xxhash> xxhash
 
+<PRIVATE
+
+:: native-mapper ( from to bytes c-type -- seq )
+    from to bytes <slice>
+    bytes byte-array? alien.data:little-endian? and
+    [ c-type cast-array ]
+    [ c-type heap-size <groups> [ le> ] map ] if ; inline
+
+PRIVATE>
+
 M:: xxhash checksum-bytes ( bytes checksum -- value )
     checksum seed>> :> seed
     bytes length :> len
 
+    len dup 16 mod - :> len/16
+    len dup 4 mod - :> len/4
+
     len 16 >= [
 
         seed prime1 w+ prime2 w+
@@ -28,12 +41,7 @@ M:: xxhash checksum-bytes ( bytes checksum -- value )
         seed
         seed prime1 w-
 
-        bytes byte-array? little-endian? and [
-            0 len dup 16 mod - 4 - 4 <range>
-            [ bytes <displaced-alien> uint deref ] map
-        ] [
-            bytes len 16 mod head-slice* 4 <groups> [ le> ] map
-        ] if
+        0 len/16 bytes uint native-mapper
 
         4 <groups> [
             first4
@@ -53,16 +61,11 @@ M:: xxhash checksum-bytes ( bytes checksum -- value )
 
     len w+
 
-    bytes len 16 mod tail-slice*
-    dup length dup 4 mod - cut-slice [
-        4 <groups> [
-            le> prime3 w* w+ 17 bitroll-32 prime4 w*
-        ] each
-    ] [
-        [
-            prime5 w* w+ 11 bitroll-32 prime1 w*
-        ] each
-    ] bi*
+    len/16 len/4 bytes uint native-mapper
+    [ prime3 w* w+ 17 bitroll-32 prime4 w* ] each
+
+    bytes len/4 tail-slice
+    [ prime5 w* w+ 11 bitroll-32 prime1 w* ] each
 
     [ -15 shift ] [ bitxor ] bi prime2 w*
     [ -13 shift ] [ bitxor ] bi prime3 w*