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

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


Vocabulary
checksums

Inputs and outputs
bytesa sequence of bytes
checksuma checksum specifier
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:: 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:: 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 ;