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_rans.om

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