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_range_lib.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_range_lib.om: Reference coder A (byte-oriented range coder, TCR1) |
| 5 | note library 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_RANGE_ID ulong 1 |
| 9 | const TC_RC_TOP ulong 16777216 |
| 10 | |
| 11 | text TC_W_RC_TRUNC4 "range payload has fewer than 4 bytes (truncated bitstream)" |
| 12 | text TC_W_RC_CORR_ST "range decoder state invalid (code >= range)" |
| 13 | text TC_W_RC_TRUNC "range payload ended early (truncated bitstream)" |
| 14 | text TC_W_RC_TERM "range termination check failed (final code != 0)" |
| 15 | text TC_W_RC_TRAIL "range payload has trailing bytes" |
| 16 | text TC_W_RC_ENC "range encode failed" |
| 17 | |
| 18 | note Encoder state: 0=low, 1=range, 2=cache, 3=have_cache, 4=pending, 5=pos, 6=err, 7=carries, 8=pending_max |
| 19 | global tc_rc_st ulong 9 |
| 20 | |
| 21 | fn tc_rc_emit b ulong to void |
| 22 | pos = get tc_rc_st 5 |
| 23 | if ge pos TC_CB_CAP |
| 24 | put tc_rc_st 6 1 |
| 25 | ret |
| 26 | end |
| 27 | put tc_cb pos uchar and b 255 |
| 28 | put tc_rc_st 5 add pos 1 |
| 29 | end |
| 30 | |
| 31 | fn tc_rc_shift_low to void |
| 32 | lo = get tc_rc_st 0 |
| 33 | c = shr lo 32 |
| 34 | lo32 = and lo 4294967295 |
| 35 | if or lt lo32 4278190080 ne c 0 |
| 36 | if ne c 0 |
| 37 | put tc_rc_st 7 add get tc_rc_st 7 1 |
| 38 | if eq get tc_rc_st 3 0 |
| 39 | put tc_rc_st 6 2 |
| 40 | end |
| 41 | end |
| 42 | if ne get tc_rc_st 3 0 |
| 43 | tc_rc_emit add get tc_rc_st 2 c |
| 44 | end |
| 45 | pend = get tc_rc_st 4 |
| 46 | while gt pend 0 bound 500000 |
| 47 | tc_rc_emit add 255 c |
| 48 | pend = sub pend 1 |
| 49 | end |
| 50 | put tc_rc_st 4 0 |
| 51 | put tc_rc_st 2 and shr lo 24 255 |
| 52 | put tc_rc_st 3 1 |
| 53 | else |
| 54 | pend = add get tc_rc_st 4 1 |
| 55 | put tc_rc_st 4 pend |
| 56 | if gt pend get tc_rc_st 8 |
| 57 | put tc_rc_st 8 pend |
| 58 | end |
| 59 | end |
| 60 | put tc_rc_st 0 shl and lo 16777215 8 |
| 61 | end |
| 62 | |
| 63 | fn tc_rc_row_ok qoff ulong k ulong to bool |
| 64 | s = ulong 0 |
| 65 | for x ulong k bound 16 |
| 66 | v = ulong get tc_ps_q add qoff x |
| 67 | if eq v 0 |
| 68 | ret false |
| 69 | end |
| 70 | s = add s v |
| 71 | end |
| 72 | ret eq s TC_QONE |
| 73 | end |
| 74 | |
| 75 | fn tc_rc_enc_sym t ulong k ulong to int |
| 76 | qoff = mul t k |
| 77 | s = ulong get tc_sy_sym t |
| 78 | if ge s k |
| 79 | ret TC_E_SYMBOL |
| 80 | end |
| 81 | if not tc_rc_row_ok qoff k |
| 82 | ret TC_E_NORM |
| 83 | end |
| 84 | cum = ulong 0 |
| 85 | for x ulong s bound 16 |
| 86 | cum = add cum ulong get tc_ps_q add qoff x |
| 87 | end |
| 88 | rg = get tc_rc_st 1 |
| 89 | r = shr rg 16 |
| 90 | put tc_rc_st 0 add get tc_rc_st 0 mul r cum |
| 91 | if eq s sub k 1 |
| 92 | rg = sub rg mul r cum |
| 93 | else |
| 94 | rg = mul r ulong get tc_ps_q add qoff s |
| 95 | end |
| 96 | while lt rg TC_RC_TOP bound 2 |
| 97 | rg = shl rg 8 |
| 98 | tc_rc_shift_low |
| 99 | end |
| 100 | put tc_rc_st 1 rg |
| 101 | ret TC_OK |
| 102 | end |
| 103 | |
| 104 | fn tc_range_encode_raw lo ulong n ulong k ulong out_off ulong to int |
| 105 | if lt k TC_KMIN |
| 106 | ret TC_E_ARG |
| 107 | end |
| 108 | if gt k TC_KMAX |
| 109 | ret TC_E_ARG |
| 110 | end |
| 111 | put tc_rc_st 0 0 |
| 112 | put tc_rc_st 1 4294967295 |
| 113 | put tc_rc_st 2 0 |
| 114 | put tc_rc_st 3 0 |
| 115 | put tc_rc_st 4 0 |
| 116 | put tc_rc_st 5 out_off |
| 117 | put tc_rc_st 6 0 |
| 118 | put tc_rc_st 7 0 |
| 119 | put tc_rc_st 8 0 |
| 120 | for i ulong n bound 500000 |
| 121 | rc = tc_rc_enc_sym add lo i k |
| 122 | if ne rc TC_OK |
| 123 | ret rc |
| 124 | end |
| 125 | end |
| 126 | for f ulong 5 |
| 127 | tc_rc_shift_low |
| 128 | end |
| 129 | err = get tc_rc_st 6 |
| 130 | if ne err 0 |
| 131 | if eq err 2 |
| 132 | ret TC_E_CORRUPT |
| 133 | end |
| 134 | ret TC_E_IO |
| 135 | end |
| 136 | put tc_cb_plen 0 sub get tc_rc_st 5 out_off |
| 137 | ret TC_OK |
| 138 | end |
| 139 | |
| 140 | note Decoder state in tc_rc_st: 0=code, 1=range, 2=pos, 3=in_off, 4=plen |
| 141 | |
| 142 | fn tc_rc_dec_renorm to int |
| 143 | code = get tc_rc_st 0 |
| 144 | rg = get tc_rc_st 1 |
| 145 | pos = get tc_rc_st 2 |
| 146 | in_off = get tc_rc_st 3 |
| 147 | plen = get tc_rc_st 4 |
| 148 | while lt rg TC_RC_TOP bound 2 |
| 149 | if ge pos plen |
| 150 | tc_why_set TC_W_RC_TRUNC |
| 151 | ret TC_E_TRUNC |
| 152 | end |
| 153 | b = ulong get tc_cb add in_off pos |
| 154 | code = and or shl code 8 b 4294967295 |
| 155 | pos = add pos 1 |
| 156 | rg = shl rg 8 |
| 157 | end |
| 158 | put tc_rc_st 0 code |
| 159 | put tc_rc_st 1 rg |
| 160 | put tc_rc_st 2 pos |
| 161 | ret TC_OK |
| 162 | end |
| 163 | |
| 164 | fn tc_rc_dec_sym t ulong i ulong k ulong to int |
| 165 | code = get tc_rc_st 0 |
| 166 | rg = get tc_rc_st 1 |
| 167 | if ge code rg |
| 168 | tc_why_set TC_W_RC_CORR_ST |
| 169 | ret TC_E_CORRUPT |
| 170 | end |
| 171 | qoff = mul t k |
| 172 | if not tc_rc_row_ok qoff k |
| 173 | tc_why_set TC_W_NORM |
| 174 | ret TC_E_NORM |
| 175 | end |
| 176 | r = shr rg 16 |
| 177 | cum = ulong 0 |
| 178 | s = ulong 0 |
| 179 | nk = sub k 1 |
| 180 | while lt s nk bound 16 |
| 181 | nx = add cum ulong get tc_ps_q add qoff s |
| 182 | if lt code mul r nx |
| 183 | break |
| 184 | end |
| 185 | cum = nx |
| 186 | s = add s 1 |
| 187 | end |
| 188 | put tc_dsy_sym i uchar s |
| 189 | code = sub code mul r cum |
| 190 | if eq s nk |
| 191 | rg = sub rg mul r cum |
| 192 | else |
| 193 | rg = mul r ulong get tc_ps_q add qoff s |
| 194 | end |
| 195 | put tc_rc_st 0 code |
| 196 | put tc_rc_st 1 rg |
| 197 | ret tc_rc_dec_renorm |
| 198 | end |
| 199 | |
| 200 | fn tc_range_decode_raw lo ulong n ulong k ulong in_off ulong plen ulong to int |
| 201 | if lt k TC_KMIN |
| 202 | ret TC_E_ARG |
| 203 | end |
| 204 | if gt k TC_KMAX |
| 205 | ret TC_E_ARG |
| 206 | end |
| 207 | if lt plen 4 |
| 208 | tc_why_set TC_W_RC_TRUNC4 |
| 209 | ret TC_E_TRUNC |
| 210 | end |
| 211 | code = ulong 0 |
| 212 | for j ulong 4 |
| 213 | code = or shl code 8 ulong get tc_cb add in_off j |
| 214 | end |
| 215 | put tc_rc_st 0 code |
| 216 | put tc_rc_st 1 4294967295 |
| 217 | put tc_rc_st 2 4 |
| 218 | put tc_rc_st 3 in_off |
| 219 | put tc_rc_st 4 plen |
| 220 | for i ulong n bound 500000 |
| 221 | rc = tc_rc_dec_sym add lo i i k |
| 222 | if ne rc TC_OK |
| 223 | ret rc |
| 224 | end |
| 225 | end |
| 226 | if ne get tc_rc_st 0 0 |
| 227 | tc_why_set TC_W_RC_TERM |
| 228 | ret TC_E_CORRUPT |
| 229 | end |
| 230 | if ne get tc_rc_st 2 plen |
| 231 | tc_why_set TC_W_RC_TRAIL |
| 232 | ret TC_E_TRAIL |
| 233 | end |
| 234 | ret TC_OK |
| 235 | end |
| 236 | |
| 237 | fn tc_range_encode to int |
| 238 | rc = tc_pair_check |
| 239 | if ne rc TC_OK |
| 240 | ret rc |
| 241 | end |
| 242 | rc = tc_ps_check_rows |
| 243 | if ne rc TC_OK |
| 244 | ret rc |
| 245 | end |
| 246 | rc = tc_range_encode_raw 0 get tc_ps_n 0 get tc_ps_k 0 TC_CODED_HEADER |
| 247 | if ne rc TC_OK |
| 248 | tc_why_set TC_W_RC_ENC |
| 249 | ret rc |
| 250 | end |
| 251 | pn = get tc_cb_plen 0 |
| 252 | tc_coded_hdr_write TC_RANGE_ID pn |
| 253 | put tc_cb_len 0 add TC_CODED_HEADER pn |
| 254 | ret TC_OK |
| 255 | end |
| 256 | |
| 257 | fn tc_range_decode clen ulong to int |
| 258 | rc = tc_coded_hdr_check TC_RANGE_ID clen |
| 259 | if ne rc TC_OK |
| 260 | ret rc |
| 261 | end |
| 262 | k = get tc_ps_k 0 |
| 263 | n = get tc_ps_n 0 |
| 264 | put tc_dsy_k 0 k |
| 265 | put tc_dsy_n 0 n |
| 266 | for i ulong 32 |
| 267 | put tc_dsy_dat i get tc_ps_dat i |
| 268 | end |
| 269 | ret tc_range_decode_raw 0 n k TC_CODED_HEADER get tc_cb_plen 0 |
| 270 | end |