omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / si / rosetta / tasks / crc-32 / crc-32.om
| 1 | note Rosetta Code task "CRC-32": https://rosettacode.org/wiki/CRC-32 |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text msg "The quick brown fox jumps over the lazy dog" |
| 6 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 7 | fn emit a view uchar * to void |
| 8 | o = ulong 0 |
| 9 | while lt o len a bound 4096 |
| 10 | k = write 1 a o sub len a o |
| 11 | if le k 0 |
| 12 | exit 1 |
| 13 | end |
| 14 | o = add o ulong k |
| 15 | end |
| 16 | end |
| 17 | note emitx writes n as 8 lowercase hex digits |
| 18 | fn emitx n uint to void |
| 19 | d = new uchar 8 0 |
| 20 | v = n |
| 21 | for i ulong 8 |
| 22 | q = uchar and v 15 |
| 23 | if lt q 10 |
| 24 | put d sub 7 i add q 48 |
| 25 | else |
| 26 | put d sub 7 i add q 87 |
| 27 | end |
| 28 | v = shr v 4 |
| 29 | end |
| 30 | emit d |
| 31 | end |
| 32 | note bitwise reflected CRC-32 (polynomial 0xedb88320), no table |
| 33 | fn crc32 a view uchar * to uint |
| 34 | c = uint 0xffffffff |
| 35 | for i ulong len a |
| 36 | c = xor c uint get a i |
| 37 | for k ulong 8 |
| 38 | if eq and c 1 1 |
| 39 | c = xor shr c 1 0xedb88320 |
| 40 | else |
| 41 | c = shr c 1 |
| 42 | end |
| 43 | end |
| 44 | end |
| 45 | ret not c |
| 46 | end |
| 47 | fn main to int |
| 48 | emitx crc32 msg |
| 49 | emit nl |
| 50 | ret 0 |
| 51 | end |