interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / turing / tc_range_lib.om

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