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 / src / turing / tc_rans.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | note |
| 4 | note src/turing/tc_rans.om: Reference coder B (32-bit rANS, byte renormalization, TCA1) |
| 5 | note in pure seeded OM-2. Composed after tc_pstream.om. |
| 6 | note Consumes only TPS1/TSY1 arrays; never references any model code. |
| 7 | |
| 8 | const TC_RANS_ID ulong 2 |
| 9 | const TC_RANS_L ulong 8388608 |
| 10 | const TC_RANS_MAX ulong 2147483648 |
| 11 | |
| 12 | text TC_W_RA_TRUNC4 "rANS payload has fewer than 4 bytes (truncated bitstream)" |
| 13 | text TC_W_RA_INIT "rANS initial state outside [2^23, 2^31)" |
| 14 | text TC_W_RA_TRUNC "rANS payload ended early (truncated bitstream)" |
| 15 | text TC_W_RA_TERM "rANS termination check failed (final state != L)" |
| 16 | text TC_W_RA_TRAIL "rANS payload has trailing bytes" |
| 17 | text TC_W_RA_ENC "rANS encode failed" |
| 18 | |
| 19 | note rANS state: 0=cum, 1=x, 2=pos, 3=in_off, 4=plen |
| 20 | global tc_ra_st ulong 5 |
| 21 | |
| 22 | fn tc_ra_row_cum qoff ulong k ulong s ulong to int |
| 23 | tot = ulong 0 |
| 24 | c = ulong 0 |
| 25 | for x ulong k bound 16 |
| 26 | v = ulong get tc_ps_q add qoff x |
| 27 | if eq v 0 |
| 28 | ret TC_E_ZERO |
| 29 | end |
| 30 | if eq x s |
| 31 | c = tot |
| 32 | end |
| 33 | tot = add tot v |
| 34 | end |
| 35 | if ne tot TC_QONE |
| 36 | ret TC_E_NORM |
| 37 | end |
| 38 | put tc_ra_st 0 c |
| 39 | ret TC_OK |
| 40 | end |
| 41 | |
| 42 | fn tc_ra_enc_sym t ulong k ulong to int |
| 43 | qoff = mul t k |
| 44 | s = ulong get tc_sy_sym t |
| 45 | if ge s k |
| 46 | ret TC_E_SYMBOL |
| 47 | end |
| 48 | rc = tc_ra_row_cum qoff k s |
| 49 | if ne rc TC_OK |
| 50 | ret rc |
| 51 | end |
| 52 | c = get tc_ra_st 0 |
| 53 | f = ulong get tc_ps_q add qoff s |
| 54 | xmax = mul 32768 f |
| 55 | x = get tc_ra_st 1 |
| 56 | p = get tc_ra_st 2 |
| 57 | while ge x xmax bound 2 |
| 58 | p = sub p 1 |
| 59 | put tc_cb p uchar and x 255 |
| 60 | x = shr x 8 |
| 61 | end |
| 62 | x = add add shl div x f 16 rem x f c |
| 63 | put tc_ra_st 1 x |
| 64 | put tc_ra_st 2 p |
| 65 | ret TC_OK |
| 66 | end |
| 67 | |
| 68 | fn tc_ra_finish out_off ulong to void |
| 69 | x = get tc_ra_st 1 |
| 70 | p = sub get tc_ra_st 2 4 |
| 71 | put tc_cb p uchar and x 255 |
| 72 | put tc_cb add p 1 uchar and shr x 8 255 |
| 73 | put tc_cb add p 2 uchar and shr x 16 255 |
| 74 | put tc_cb add p 3 uchar and shr x 24 255 |
| 75 | nb = sub TC_CB_CAP p |
| 76 | for j ulong nb bound 4194304 |
| 77 | put tc_cb add out_off j get tc_cb add p j |
| 78 | end |
| 79 | put tc_cb_plen 0 nb |
| 80 | end |
| 81 | |
| 82 | fn tc_rans_encode_raw lo ulong n ulong k ulong out_off ulong to int |
| 83 | if lt k TC_KMIN |
| 84 | ret TC_E_ARG |
| 85 | end |
| 86 | if gt k TC_KMAX |
| 87 | ret TC_E_ARG |
| 88 | end |
| 89 | if gt add add mul n 2 4 out_off TC_CB_CAP |
| 90 | ret TC_E_IO |
| 91 | end |
| 92 | put tc_ra_st 1 TC_RANS_L |
| 93 | put tc_ra_st 2 TC_CB_CAP |
| 94 | i = n |
| 95 | while gt i 0 bound 500000 |
| 96 | i = sub i 1 |
| 97 | rc = tc_ra_enc_sym add lo i k |
| 98 | if ne rc TC_OK |
| 99 | ret rc |
| 100 | end |
| 101 | end |
| 102 | tc_ra_finish out_off |
| 103 | ret TC_OK |
| 104 | end |
| 105 | |
| 106 | fn tc_ra_dec_renorm to int |
| 107 | x = get tc_ra_st 1 |
| 108 | pos = get tc_ra_st 2 |
| 109 | in_off = get tc_ra_st 3 |
| 110 | plen = get tc_ra_st 4 |
| 111 | while lt x TC_RANS_L bound 2 |
| 112 | if ge pos plen |
| 113 | tc_why_set TC_W_RA_TRUNC |
| 114 | ret TC_E_TRUNC |
| 115 | end |
| 116 | x = or shl x 8 ulong get tc_cb add in_off pos |
| 117 | pos = add pos 1 |
| 118 | end |
| 119 | put tc_ra_st 1 x |
| 120 | put tc_ra_st 2 pos |
| 121 | ret TC_OK |
| 122 | end |
| 123 | |
| 124 | fn tc_ra_dec_sym t ulong i ulong k ulong to int |
| 125 | qoff = mul t k |
| 126 | x = get tc_ra_st 1 |
| 127 | m = and x 65535 |
| 128 | cum = ulong 0 |
| 129 | tot = ulong 0 |
| 130 | s = k |
| 131 | for y ulong k bound 16 |
| 132 | v = ulong get tc_ps_q add qoff y |
| 133 | if eq v 0 |
| 134 | tc_why_set TC_W_ZERO |
| 135 | ret TC_E_ZERO |
| 136 | end |
| 137 | if eq s k |
| 138 | if lt m add tot v |
| 139 | s = y |
| 140 | cum = tot |
| 141 | end |
| 142 | end |
| 143 | tot = add tot v |
| 144 | end |
| 145 | if or ne tot TC_QONE eq s k |
| 146 | tc_why_set TC_W_NORM |
| 147 | ret TC_E_NORM |
| 148 | end |
| 149 | put tc_dsy_sym i uchar s |
| 150 | f = ulong get tc_ps_q add qoff s |
| 151 | x = sub add mul f shr x 16 m cum |
| 152 | put tc_ra_st 1 x |
| 153 | ret tc_ra_dec_renorm |
| 154 | end |
| 155 | |
| 156 | fn tc_rans_decode_raw lo ulong n ulong k ulong in_off ulong plen ulong to int |
| 157 | if lt k TC_KMIN |
| 158 | ret TC_E_ARG |
| 159 | end |
| 160 | if gt k TC_KMAX |
| 161 | ret TC_E_ARG |
| 162 | end |
| 163 | if lt plen 4 |
| 164 | tc_why_set TC_W_RA_TRUNC4 |
| 165 | ret TC_E_TRUNC |
| 166 | end |
| 167 | b0 = ulong get tc_cb in_off |
| 168 | b1 = ulong get tc_cb add in_off 1 |
| 169 | b2 = ulong get tc_cb add in_off 2 |
| 170 | b3 = ulong get tc_cb add in_off 3 |
| 171 | x = or or b0 shl b1 8 or shl b2 16 shl b3 24 |
| 172 | if or lt x TC_RANS_L ge x TC_RANS_MAX |
| 173 | tc_why_set TC_W_RA_INIT |
| 174 | ret TC_E_CORRUPT |
| 175 | end |
| 176 | put tc_ra_st 1 x |
| 177 | put tc_ra_st 2 4 |
| 178 | put tc_ra_st 3 in_off |
| 179 | put tc_ra_st 4 plen |
| 180 | for i ulong n bound 500000 |
| 181 | rc = tc_ra_dec_sym add lo i i k |
| 182 | if ne rc TC_OK |
| 183 | ret rc |
| 184 | end |
| 185 | end |
| 186 | if ne get tc_ra_st 1 TC_RANS_L |
| 187 | tc_why_set TC_W_RA_TERM |
| 188 | ret TC_E_CORRUPT |
| 189 | end |
| 190 | if ne get tc_ra_st 2 plen |
| 191 | tc_why_set TC_W_RA_TRAIL |
| 192 | ret TC_E_TRAIL |
| 193 | end |
| 194 | ret TC_OK |
| 195 | end |
| 196 | |
| 197 | fn tc_rans_encode to int |
| 198 | rc = tc_pair_check |
| 199 | if ne rc TC_OK |
| 200 | ret rc |
| 201 | end |
| 202 | rc = tc_ps_check_rows |
| 203 | if ne rc TC_OK |
| 204 | ret rc |
| 205 | end |
| 206 | rc = tc_rans_encode_raw 0 get tc_ps_n 0 get tc_ps_k 0 TC_CODED_HEADER |
| 207 | if ne rc TC_OK |
| 208 | tc_why_set TC_W_RA_ENC |
| 209 | ret rc |
| 210 | end |
| 211 | pn = get tc_cb_plen 0 |
| 212 | tc_coded_hdr_write TC_RANS_ID pn |
| 213 | put tc_cb_len 0 add TC_CODED_HEADER pn |
| 214 | ret TC_OK |
| 215 | end |
| 216 | |
| 217 | fn tc_rans_decode clen ulong to int |
| 218 | rc = tc_coded_hdr_check TC_RANS_ID clen |
| 219 | if ne rc TC_OK |
| 220 | ret rc |
| 221 | end |
| 222 | k = get tc_ps_k 0 |
| 223 | n = get tc_ps_n 0 |
| 224 | put tc_dsy_k 0 k |
| 225 | put tc_dsy_n 0 n |
| 226 | for i ulong 32 |
| 227 | put tc_dsy_dat i get tc_ps_dat i |
| 228 | end |
| 229 | ret tc_rans_decode_raw 0 n k TC_CODED_HEADER get tc_cb_plen 0 |
| 230 | end |