omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / crc-32.wiki
| 1 | =={{header|Omegese}}== |
| 2 | {{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} |
| 3 | Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program. |
| 4 | <syntaxhighlight lang="text"> |
| 5 | om2 |
| 6 | text nl "\n" |
| 7 | text msg "The quick brown fox jumps over the lazy dog" |
| 8 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 9 | fn emit a view uchar * to void |
| 10 | o = ulong 0 |
| 11 | while lt o len a bound 4096 |
| 12 | k = write 1 a o sub len a o |
| 13 | if le k 0 |
| 14 | exit 1 |
| 15 | end |
| 16 | o = add o ulong k |
| 17 | end |
| 18 | end |
| 19 | note emitx writes n as 8 lowercase hex digits |
| 20 | fn emitx n uint to void |
| 21 | d = new uchar 8 0 |
| 22 | v = n |
| 23 | for i ulong 8 |
| 24 | q = uchar and v 15 |
| 25 | if lt q 10 |
| 26 | put d sub 7 i add q 48 |
| 27 | else |
| 28 | put d sub 7 i add q 87 |
| 29 | end |
| 30 | v = shr v 4 |
| 31 | end |
| 32 | emit d |
| 33 | end |
| 34 | note bitwise reflected CRC-32 (polynomial 0xedb88320), no table |
| 35 | fn crc32 a view uchar * to uint |
| 36 | c = uint 0xffffffff |
| 37 | for i ulong len a |
| 38 | c = xor c uint get a i |
| 39 | for k ulong 8 |
| 40 | if eq and c 1 1 |
| 41 | c = xor shr c 1 0xedb88320 |
| 42 | else |
| 43 | c = shr c 1 |
| 44 | end |
| 45 | end |
| 46 | end |
| 47 | ret not c |
| 48 | end |
| 49 | fn main to int |
| 50 | emitx crc32 msg |
| 51 | emit nl |
| 52 | ret 0 |
| 53 | end |
| 54 | </syntaxhighlight> |
| 55 | {{out}} |
| 56 | <pre> |
| 57 | 414fa339 |
| 58 | </pre> |