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 endian fry grouping kernel math
6 math.bitwise math.ranges sequences sequences.private ;
8 IN: checksums.superfast
10 TUPLE: superfast seed ;
11 C: <superfast> superfast
15 : (main-loop) ( hash n -- hash' )
16 [ 16 bits ] [ -16 shift ] bi
17 [ + ] [ 11 shift dupd bitxor ] bi*
18 [ 16 shift ] [ bitxor ] bi* 32 bits
19 [ -11 shift ] [ + ] bi ; inline
21 : main-loop ( seq hash -- seq hash' )
22 over byte-array? little-endian? and [
23 [ 0 over length 4 - 4 <range> ] dip
24 [ pick <displaced-alien> int deref (main-loop) ] reduce
26 [ dup length 4 mod dupd head-slice* 4 <groups> ] dip
27 [ le> (main-loop) ] reduce
30 : end-case ( seq hash -- hash' )
31 swap dup length 4 mod [ tail-slice* ] keep {
34 first + [ 10 shift ] [ bitxor ] bi 32 bits
38 le> + [ 11 shift ] [ bitxor ] bi 32 bits
39 [ -17 shift ] [ + ] bi
43 [ le> + [ 16 shift ] [ bitxor ] bi ]
44 [ 18 shift bitxor ] bi* 32 bits
45 [ -11 shift ] [ + ] bi
49 : avalanche ( hash -- hash' )
50 [ 3 shift ] [ bitxor ] bi 32 bits [ -5 shift ] [ + ] bi
51 [ 4 shift ] [ bitxor ] bi 32 bits [ -17 shift ] [ + ] bi
52 [ 25 shift ] [ bitxor ] bi 32 bits [ -6 shift ] [ + ] bi ; inline
56 M: superfast checksum-bytes
57 seed>> 32 bits main-loop end-case avalanche ;