// Same shape as lib/hash/crc32: per-byte inline polynomial shift, no // precomputed table. Slower than Hare's table-driven path by ~8x per // byte but produces identical answers for the documented polynomials. // // Polynomials are given in reversed form, matching Hare. package crc64; def ECMA: u64 = 0xC96C5795D7870F42u64; // ECMA-182, xz-utils def ISO: u64 = 0xD800000000000000u64; // ISO 3309 HDLC // sum64 — fold `buf` under `poly` (reversed form). Initial cval is ~0u64. export fn sum64(buf: []u8, poly: u64) u64 = { let c: u64 = 0xFFFFFFFFFFFFFFFFu64; let i: i32 = 0; for (i < buf.len) { let t: u64 = (c & 0xFFu64) ^ (buf[i]: u64); let z: i32 = 0; for (z < 8) { if ((t & 1u64) == 1u64) { t = (t >> 1u64) ^ poly; } else { t = t >> 1u64; }; z += 1; }; c = t ^ (c >> 8u64); i += 1; }; return ~c; }; export fn sum64ecma(buf: []u8) u64 = { return sum64(buf, ECMA); }; export fn sum64iso(buf: []u8) u64 = { return sum64(buf, ISO); };