checksum-bytes ( bytes checksum -- value )
Factor handbook » Input and output » Checksums

Prev:checksum
Next:checksum-stream ( stream checksum -- value )


Vocabulary
checksums

Inputs
bytesa sequence of bytes
checksuma checksum specifier


Outputs
valuea byte-array


Generic word contract
Computes the checksum of all data in a sequence.

Examples
USING: checksums checksums.crc32 prettyprint ; B{ 1 10 100 } crc32 checksum-bytes .
B{ 78 179 254 238 }


Definition

GENERIC: checksum-bytes ( bytes checksum -- value )


Methods



































































M:: khash64 checksum-bytes ( bytes checksum -- value )
checksum seed>> :> seed bytes length :> len len 8 mod
0 assert= seed 0 len bytes uint64_t native-mapper
[ khash64-step ] each ;



M:: metrohash-128 checksum-bytes ( bytes checksum -- value )
3359281633 :> k0 2252921819 :> k1 2078195771 :> k2 794325157
:> k3 checksum seed>> :> seed bytes length :> len len dup 32
mod - :> len/32 len dup 16 mod - :> len/16 len dup 8 mod -
:> len/8 len dup 4 mod - :> len/4 len dup 2 mod -
:> len/2 seed k0 W- k3 W* :> v0! seed k1 W+ k2 W*
:> v1! seed k0 W+ k2 W* :> v2! seed k1 W- k3 W*
:> v3! len 32 >= [
0 len/32 bytes uint64_t native-mapper 4 <groups> [
first4 {
[ k0 W* v0 W+ -29 bitroll-64 v2 W+ v0! ]
[ k1 W* v1 W+ -29 bitroll-64 v3 W+ v1! ]
[ k2 W* v2 W+ -29 bitroll-64 v0 W+ v2! ]
[ k3 W* v3 W+ -29 bitroll-64 v1 W+ v3! ]
} spread
] each v0 v3 W+ k0 W* v1 W+ -21 bitroll-64 k1 W*
v2 bitxor v2! v1 v2 W+ k1 W* v0 W+ -21 bitroll-64 k0 W*
v3 bitxor v3! v0 v2 W+ k0 W* v3 W+ -21 bitroll-64 k1 W*
v0 bitxor v0! v1 v3 W+ k1 W* v2 W+ -21 bitroll-64 k0 W*
v1 bitxor v1!
] when len/32 len/16 bytes uint64_t native-mapper [
first2
[ k2 W* v0 W+ -33 bitroll-64 k3 W* v0! ]
[ k2 W* v1 W+ -33 bitroll-64 k3 W* v1! ] bi* v0 k2 W*
v1 W+ -45 bitroll-64 k1 W* v0 bitxor v0! v1 k3 W* v0 W+
-45 bitroll-64 k0 W* v1 bitxor v1!
] unless-empty len/16 len/8 bytes uint64_t native-mapper [
first k2 W* v0 W+ -33 bitroll-64 k3 W* v0! v0 k2 W*
v1 W+ -27 bitroll-64 k1 W* v0 bitxor v0!
] unless-empty len/8 len/4 bytes uint32_t native-mapper [
first k2 W* v1 W+ -33 bitroll-64 k3 W* v1! v1 k3 W*
v0 W+ -46 bitroll-64 k0 W* v1 bitxor v1!
] unless-empty len/4 len/2 bytes uint16_t native-mapper
~52 more~ ;



M:: metrohash-64 checksum-bytes ( bytes checksum -- value )
3603962101 :> k0 2729050939 :> k1 1654206401 :> k2 817650473
:> k3 checksum seed>> :> seed bytes length :> len len dup 32
mod - :> len/32 len dup 16 mod - :> len/16 len dup 8 mod -
:> len/8 len dup 4 mod - :> len/4 len dup 2 mod -
:> len/2 seed k2 W+ k0 W*
:> h h h h h :> ( v0! v1! v2! v3! ) len 32 >= [
0 len/32 bytes uint64_t native-mapper 4 <groups> [
first4 {
[ k0 W* v0 W+ -29 bitroll-64 v2 W+ v0! ]
[ k1 W* v1 W+ -29 bitroll-64 v3 W+ v1! ]
[ k2 W* v2 W+ -29 bitroll-64 v0 W+ v2! ]
[ k3 W* v3 W+ -29 bitroll-64 v1 W+ v3! ]
} spread
] each v0 v3 W+ k0 W* v1 W+ -37 bitroll-64 k1 W*
v2 bitxor v2! v1 v2 W+ k1 W* v0 W+ -37 bitroll-64 k0 W*
v3 bitxor v3! v0 v2 W+ k0 W* v3 W+ -37 bitroll-64 k1 W*
v0 bitxor v0! v1 v3 W+ k1 W* v2 W+ -37 bitroll-64 k0 W*
v1 bitxor v1! v0 v1 bitxor h W+ v0!
] when len/32 len/16 bytes uint64_t native-mapper [
first2
[ k2 W* v0 W+ -29 bitroll-64 k3 W* v1! ]
[ k2 W* v0 W+ -29 bitroll-64 k3 W* v2! ] bi* v1 k0 W*
-21 bitroll-64 v2 W+ v1 bitxor v1! v2 k3 W*
-21 bitroll-64 v1 W+ v2 bitxor v2! v2 v0 W+ v0!
] unless-empty len/16 len/8 bytes uint64_t native-mapper [
first k3 W* v0 W+ v0! v0 -55 bitroll-64 k1 W* v0 bitxor
v0!
] unless-empty len/8 len/4 bytes uint32_t native-mapper [
first k3 W* v0 W+ v0! v0 -26 bitroll-64 k1 W* v0 bitxor
v0!
] unless-empty len/4 len/2 bytes uint16_t native-mapper [
first k3 W* v0 W+ v0! v0 -48 bitroll-64 k1 W* v0 bitxor
v0!
] unless-empty bytes len/2 tail-slice [
first k3 W* v0 W+ v0! v0 -37 bitroll-64 k1 W* v0 bitxor
v0!
] unless-empty v0 -28 bitroll-64 v0 bitxor v0! ~11 more~ ;












M:: wyhash checksum-bytes ( bytes checksum -- value )
checksum seed>> P0 bitxor
:> seed! bytes length :> len len 16 <= [
len 8 <= [
len 4 >= [
bytes [ 4 head-slice ] [ 4 tail-slice* ] bi
[ le> ] bi@
] [
len 0 > [
0 bytes nth 16 shift len 2/ bytes nth
8 shift bitor len 1 - bytes nth bitor 0
] [ 0 0 ] if
] if
] [
bytes [ 8 head-slice ] [ 8 tail-slice* ] bi
[ le> ] bi@
] if
] [
len 1 - dup 48 mod - :> len/48 len 1 - dup 16 mod -
:> len/16 0 len/48 bytes uint64_t native-mapper [
seed :> see1! seed :> see2! 6 <groups> [
6 firstn-unsafe :> ( n0 n1 n2 n3 n4 n5 ) n0 P1
bitxor n1 seed bitxor wymix seed! n2 P2 bitxor
n3 see1 bitxor wymix see1! n4 P3 bitxor
n5 see2 bitxor wymix see2!
] each see1 see2 bitxor seed bitxor seed!
] unless-empty
len/48 len/16 bytes uint64_t native-mapper [
2 <groups> [
first2-unsafe :> ( n0 n1 ) n0 P1 bitxor
n1 seed bitxor wymix seed!
] each
] unless-empty len 16 - len bytes uint64_t native-mapper
first2
] if :> ( a b ) len P1 bitxor a P1 bitxor b seed bitxor
wymix wymix ;



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+ seed prime2 w+
seed seed prime1 w- 0 len/16 bytes uint native-mapper
4 <groups> [
first4
[ prime2 w* w+ 13 bitroll-32 prime1 w* ] 4 napply
] each {
[ 1 bitroll-32 ]
[ 7 bitroll-32 ]
[ 12 bitroll-32 ]
[ 18 bitroll-32 ]
} spread w+ w+ w+
] [ seed prime5 w+ ] if len w+
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*
[ -16 shift ] [ bitxor ] bi ;