interplane.Get Omega
Read in your language

cortex

aien-dev/cortex

Brought by drakestapleton

branch main Branches Tags History

cortex / om / crc32c.om

101 lines · 2815 bytes Raw Blame History
1note crc32c.om - CRC-32C (Castagnoli), table driven, owner SqliteImport.
2note Reflected polynomial 0x82F63B78 = 2197175160, init and xorout 0xFFFFFFFF = 4294967295.
3note Check value of the 9 bytes "123456789" is 0xE3069283 = 3808858755.
4note (decimal forms from WolframLanguageEvaluator FromDigits[...,16]; the table is built at run time
5note from the polynomial by crc_init, 8 shift steps per entry, so no generated data table is committed.)
6note
7note Shared buffers (declared here so the server unit and cortex-import use the same storage):
8note klw uchar 8388608 : log record staging buffer (writer side). A record is staged as
9note u32 payload_len, u32 crc32c(payload), payload (little endian).
10note klr uchar 8388608 : log replay read buffer (reader side).
11note API:
12note crc_klw o n -> uint : CRC-32C of klw[o .. o+n). Trap 3 when the range is outside klw.
13note crc_klr o n -> uint : CRC-32C of klr[o .. o+n). Trap 3 when the range is outside klr.
14note streaming: crc_reset; crc_feed o n (klw range) / crc_feedr o n (klr range) / crc_byte b;
15note crc_value -> uint. State lives in kcrcst[0] (the running register, pre-xorout).
16note Globals read: kcrctab (256 uint table), kcrcok (table built flag), kcrcst, klw, klr.
17global klw uchar 8388608
18global klr uchar 8388608
19global kcrctab uint 256
20global kcrcok uchar 1
21global kcrcst uint 1
22fn crc_init to void
23for i uint 256
24c = i
25for k uint 8
26if eq and c 1 1
27c = xor shr c 1 2197175160
28else
29c = shr c 1
30end
31end
32put kcrctab ulong i c
33end
34put kcrcok 0 1
35end
36fn crc_reset to void
37if eq get kcrcok 0 0
38crc_init
39end
40put kcrcst 0 4294967295
41end
42fn crc_byte b uchar to void
43s = get kcrcst 0
44put kcrcst 0 xor get kcrctab ulong and xor s uint b 255 shr s 8
45end
46fn crc_value to uint
47ret xor get kcrcst 0 4294967295
48end
49note crc_feed: klw[o .. o+n). The range is checked first (trap 3), then fed in 65536-byte chunks:
50note n <= 8388608 gives at most 128 outer passes (bound 200), each inner pass consumes one byte.
51fn crc_feed o ulong n ulong to void
52if or gt n len klw gt o sub len klw n
53trap 3
54end
55s = get kcrcst 0
56p = o
57e = add o n
58while lt p e bound 200
59q = add p 65536
60if gt q e
61q = e
62end
63while lt p q bound 65536
64x = ulong and xor s uint get klw p 255
65s = xor get kcrctab x shr s 8
66p = add p 1
67end
68end
69put kcrcst 0 s
70end
71note crc_feedr: the same over klr.
72fn crc_feedr o ulong n ulong to void
73if or gt n len klr gt o sub len klr n
74trap 3
75end
76s = get kcrcst 0
77p = o
78e = add o n
79while lt p e bound 200
80q = add p 65536
81if gt q e
82q = e
83end
84while lt p q bound 65536
85x = ulong and xor s uint get klr p 255
86s = xor get kcrctab x shr s 8
87p = add p 1
88end
89end
90put kcrcst 0 s
91end
92fn crc_klw o ulong n ulong to uint
93crc_reset
94crc_feed o n
95ret crc_value
96end
97fn crc_klr o ulong n ulong to uint
98crc_reset
99crc_feedr o n
100ret crc_value
101end