1 ! Copyright (C) 2013 John Benediktsson.
2 ! See http://factorcode.org/license.txt for BSD license.
4 USING: accessors alien alien.c-types alien.data byte-arrays
5 checksums combinators fry grouping io.binary kernel math
6 math.bitwise math.ranges sequences sequences.private ;
8 IN: checksums.superfast
10 TUPLE: superfast seed ;
11 C: <superfast> superfast
15 : 32-bit ( n -- n' ) 32 on-bits mask ; inline
17 : (main-loop) ( hash m n -- hash' )
18 [ + ] [ 11 shift dupd bitxor ] bi*
19 [ 16 shift ] [ bitxor ] bi* 32-bit
20 [ -11 shift ] [ + ] bi ; inline
22 : main-loop ( seq hash -- seq hash' )
23 over byte-array? little-endian? and [
24 [ 0 over length 4 - 4 <range> ] dip [
25 pick <displaced-alien> int deref
26 [ 16 on-bits mask ] [ -16 shift ] bi
30 [ dup length 4 mod dupd head-slice* 4 <groups> ] dip [
31 2 cut-slice [ le> ] bi@ (main-loop)
35 : end-case ( seq hash -- hash' )
36 swap dup length 4 mod [ tail-slice* ] keep {
39 first + [ 10 shift ] [ bitxor ] bi 32-bit
43 le> + [ 11 shift ] [ bitxor ] bi 32-bit
44 [ -17 shift ] [ + ] bi
48 [ le> + [ 16 shift ] [ bitxor ] bi ]
49 [ 18 shift bitxor ] bi* 32-bit
50 [ -11 shift ] [ + ] bi
54 : avalanche ( hash -- hash' )
55 [ 3 shift ] [ bitxor ] bi 32-bit
57 [ 4 shift ] [ bitxor ] bi 32-bit
58 [ -17 shift ] [ + ] bi
59 [ 25 shift ] [ bitxor ] bi 32-bit
60 [ -6 shift ] [ + ] bi ; inline
64 M: superfast checksum-bytes
65 seed>> 32-bit main-loop end-case avalanche ;