home: hub: hare-crc32

ref: fdb251d1136d8b8a92c520fc0bf6ae7adbcfe154
dir: /cmd/tblgen/crctbl.ha/

View raw version
// Copyright (c) 2022 grobe0ba (grobe0ba@tcp80.org)
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

// A method for generating the polynomial (taken from zlib):
// x^26+x^23+x^22+x^16+x^12+X^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1
// static const unsigned char p[] = { 0, 1, 2, 4, 5, 7, 8, 10, 11,
// 						12, 16, 22, 23, 26 };
//
// The CRC16 polynomial used in github.com/google/ahdlc:
// x^16 + x^15 + x^2 + 1
// static const unsigned char p[] = { 0, 2, 15 };
//
// uint32_t poly = 0;
// uint16_t lower = 0;
// uint16_t upper = 0;
//
// for (uint32_t i = 0; i < sizeof(p) / sizeof(unsigned char); ++i) {
//   poly |= 1UL << (15 - p[i]);
// }
//
// lower = poly & ((1U << 16) - 1);
// upper = poly >> 16;
// poly = (lower << 16) | upper;

use fmt;

export fn main() void = {
//	let poly: u32 = 0;
//	let lower: u32 = 0;
//	let upper: u32 = 0;
//
//	const p: []u32 = [ 0, 1, 2, 4, 5, 7, 8, 10, 11, 12, 16, 22, 23, 26 ];
//
//	for (let i = 0z; i < len(p); i += 1) {
//		poly |= (1: u32) << (15 - p[i]);
//	};
//
//	lower = poly & ((1 << 16) - 1);
//	upper = poly >> 16;
//	poly = (lower << 16) | upper;
//
//	assert(poly == 0xedb88320);

	const poly: u32 = 0xedb88320;

	let t: [256]u32 = [0...];

	for (let i = 0z; i < len(t); i += 1) {
		let crc = i;
		for (let j = 0z; j < 8; j += 1) {
			if ((crc & 1) == 1) {
				crc = (crc >> 1) ^ poly;
			} else {
				crc >>= 1;
			};
		};
		t[i] = (crc: u32);
	};

	fmt::printf("// This file is generated using cmd/tblgen. DO NOT EDIT.\n\n")!;
	fmt::printf("const crc32_tab: []u32 = [\n   ")!;

	let col = 0;

	for (let i = 0z; i < len(t); i += 1) {
		if ((col + 12) >= 76) {
			fmt::printf("\n   ")!;
			col = 0;
		};
		fmt::printf(" 0x{:_08x},", t[i])!;
		col += 12;
	};

	fmt::printf("\n];\n")!;
};