Skip to content

Instantly share code, notes, and snippets.

@josharian
Created April 18, 2023 20:20
Show Gist options
  • Save josharian/dc6e17bee03410a79c031db8c74d8f0b to your computer and use it in GitHub Desktop.
Save josharian/dc6e17bee03410a79c031db8c74d8f0b to your computer and use it in GitHub Desktop.
func checksumNoFoldTwoAccum(b []byte, initial uint64) uint64 {
ac := initial
var bc, bcarr uint64
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)
bc, bcarr = bits.Add64(bc, binary.BigEndian.Uint64(b[8:16]), bcarr)
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[16:24]), carry)
bc, bcarr = bits.Add64(bc, binary.BigEndian.Uint64(b[24:32]), bcarr)
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[32:40]), carry)
bc, bcarr = bits.Add64(bc, binary.BigEndian.Uint64(b[40:48]), bcarr)
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[48:56]), carry)
bc, bcarr = bits.Add64(bc, binary.BigEndian.Uint64(b[56:64]), bcarr)
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[64:72]), carry)
bc, bcarr = bits.Add64(bc, binary.BigEndian.Uint64(b[72:80]), bcarr)
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[80:88]), carry)
bc, bcarr = bits.Add64(bc, binary.BigEndian.Uint64(b[88:96]), bcarr)
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[96:104]), carry)
bc, bcarr = bits.Add64(bc, binary.BigEndian.Uint64(b[104:112]), bcarr)
ac, carry = bits.Add64(ac, binary.BigEndian.Uint64(b[112:120]), carry)
bc, bcarr = bits.Add64(bc, binary.BigEndian.Uint64(b[120:128]), bcarr)
b = b[128:]
}
ac, carry = bits.Add64(ac, bc, carry)
ac, carry = bits.Add64(ac, bcarr, carry)
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