Created
April 18, 2023 20:19
-
-
Save josharian/0b97e2ff9c9f50bb52187aa68f91058b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func checksumNoFoldBy8s(b []byte, initial uint64) uint64 { | |
ac := initial | |
var carry uint64 | |
for len(b) >= 128 { | |
// add in chunks of eight up to 128 | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[:8]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[8:16]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[16:24]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[24:32]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[32:40]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[40:48]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[48:56]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[56:64]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[64:72]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[72:80]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[80:88]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[88:96]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[96:104]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[104:112]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[112:120]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[120:128]), carry) | |
b = b[128:] | |
} | |
if len(b) >= 64 { | |
// add in chunks of eight up to 64 | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[:8]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[8:16]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[16:24]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[24:32]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[32:40]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[40:48]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[48:56]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[56:64]), carry) | |
b = b[64:] | |
} | |
if len(b) >= 32 { | |
// add in chunks of eight up to 32 | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[:8]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[8:16]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[16:24]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[24:32]), carry) | |
b = b[32:] | |
} | |
if len(b) >= 16 { | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[:8]), carry) | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[8:16]), carry) | |
b = b[16:] | |
} | |
if len(b) >= 8 { | |
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[:8]), carry) | |
b = b[8:] | |
} | |
if len(b) >= 4 { | |
ac, carry = bits.Add64(ac, uint64(binary.BigEndian.Uint32(b)), carry) | |
b = b[4:] | |
} | |
if len(b) >= 2 { | |
ac, carry = bits.Add64(ac, uint64(binary.BigEndian.Uint16(b)), carry) | |
b = b[2:] | |
} | |
if len(b) == 1 { | |
ac, carry = bits.Add64(ac, uint64(b[0])<<8, carry) | |
} | |
return ac + carry | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment