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