aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / om / omega-reference / vendor / skills / omega-encoding / examples / fnv1a32.om
| 1 | note fnv1a32: read all of stdin (at most 65535 bytes), write its FNV-1a 32-bit hash as 4 little-endian bytes |
| 2 | note exit 0 ok, 2 read error or input of 65536 bytes or more, 3 short or failed write |
| 3 | global inb uchar 65536 |
| 4 | global outb uchar 4 |
| 5 | fn rdall to long |
| 6 | t = ulong 0 |
| 7 | while lt t len inb bound 65536 |
| 8 | k = read 0 inb t sub len inb t |
| 9 | if lt k 0 |
| 10 | ret k |
| 11 | end |
| 12 | if eq k 0 |
| 13 | ret long t |
| 14 | end |
| 15 | t = add t ulong k |
| 16 | end |
| 17 | ret neg long 27 |
| 18 | end |
| 19 | fn fnv n ulong to uint |
| 20 | h = uint 2166136261 |
| 21 | for i ulong n |
| 22 | h = mul xor h uint get inb i 16777619 |
| 23 | end |
| 24 | ret h |
| 25 | end |
| 26 | fn put4 h uint to void |
| 27 | put outb 0 uchar and h 255 |
| 28 | put outb 1 uchar and shr h 8 255 |
| 29 | put outb 2 uchar and shr h 16 255 |
| 30 | put outb 3 uchar and shr h 24 255 |
| 31 | end |
| 32 | fn wrall n ulong to long |
| 33 | d = ulong 0 |
| 34 | while lt d n bound 4 |
| 35 | w = write 1 outb d sub n d |
| 36 | if le w 0 |
| 37 | ret w |
| 38 | end |
| 39 | d = add d ulong w |
| 40 | end |
| 41 | ret long d |
| 42 | end |
| 43 | fn main to int |
| 44 | r = rdall |
| 45 | if lt r 0 |
| 46 | ret 2 |
| 47 | end |
| 48 | h = fnv ulong r |
| 49 | put4 h |
| 50 | w = wrall 4 |
| 51 | if ne w 4 |
| 52 | ret 3 |
| 53 | end |
| 54 | ret 0 |
| 55 | end |