Files
bux-lang/examples/ctfe_crc.bux
T
dimgigov a785747c37
ci / build (ubuntu) (push) Has been cancelled
ci / macos smoke (push) Has been cancelled
ci / windows smoke (push) Has been cancelled
selfhost-loop / bootstrap determinism (push) Has been cancelled
ci / unit + fmt (push) Has been cancelled
ci / examples (push) Has been cancelled
ci / goldens + tools (push) Has been cancelled
ci / apps (push) Has been cancelled
ci / selfhost smoke (push) Has been cancelled
ci / CI gate (push) Has been cancelled
feat: Linux/cloud platform stack (TLS, registry, static/cross, selfhost PM)
Ship the QUALITY_PLAN platform focus: thin/minimal runtime, --static/--target,
Nexus HTTPS/mTLS with graceful stop, lock checksums + install --locked,
selfhost registry (search/add/HTTP), containers, and CI smokes for cloud path.
2026-07-23 23:00:55 +03:00

83 lines
2.0 KiB
Plaintext

// Session 75 — CTFE tables for embedded / firmware-style use.
// Precomputes CRC-8 (poly 0x07) cells at compile time; runtime only indexes them.
import Std::Io::{PrintLine, PrintInt};
const POLY: int = 0x07;
// One shift step of CRC-8 (MSB-first).
const func CrcStep(crc: int) -> int {
let c: int = crc & 0xFF;
if (c & 0x80) != 0 {
return ((c << 1) ^ POLY) & 0xFF;
}
return (c << 1) & 0xFF;
}
// Fold remaining shift steps (recursive — CTFE-friendly).
const func CrcFold(crc: int, bits: int) -> int {
if bits <= 0 {
return crc & 0xFF;
}
return CrcFold(CrcStep(crc), bits - 1);
}
const func Crc8Byte(byte: int) -> int {
return CrcFold(byte & 0xFF, 8);
}
// Known table cells (full 256-entry array const init is future work).
const CRC_0: int = Crc8Byte(0);
const CRC_1: int = Crc8Byte(1);
const CRC_2: int = Crc8Byte(2);
const CRC_65: int = Crc8Byte(65); // 'A'
const CRC_255: int = Crc8Byte(255);
// Table size as CTFE power-of-two (classic embedded pattern).
const func Pow2(n: int) -> int {
if n <= 0 {
return 1;
}
return 2 * Pow2(n - 1);
}
const TABLE_SIZE: int = Pow2(8); // 256
func Crc8Known(b: int) -> int {
if b == 0 { return CRC_0; }
if b == 1 { return CRC_1; }
if b == 2 { return CRC_2; }
if b == 65 { return CRC_65; }
if b == 255 { return CRC_255; }
return -1;
}
func Main() -> int {
PrintInt(TABLE_SIZE);
PrintLine("");
PrintInt(CRC_0);
PrintLine("");
PrintInt(CRC_1);
PrintLine("");
PrintInt(CRC_65);
PrintLine("");
if TABLE_SIZE != 256 {
PrintLine("FAIL ctfe_crc TABLE_SIZE");
return 1;
}
if CRC_0 != 0 {
PrintLine("FAIL ctfe_crc CRC_0");
return 1;
}
// Reference: poly 0x07, byte 0x01 → 0x07 after 8 steps
if CRC_1 != 7 {
PrintLine("FAIL ctfe_crc CRC_1 expected 7");
return 1;
}
let a: int = Crc8Known(1);
if a != 7 {
PrintLine("FAIL ctfe_crc runtime path");
return 1;
}
PrintLine("PASS ctfe_crc");
return 0;
}