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 / gpu / algebra / pack.om

251 lines · 4744 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note gpu/algebra/pack.om: pure-Omega bitplane & dense trit packing (replaces src/algebra/oma_pack.{h,c}).
3note Bitplane: ceil(n/64) blocks (pos, neg), 16 bytes LE serialized.
4note Dense: 5 trits/byte, sum_{i<5} (t_i + 1) * 3^i, valid bytes 0..242, zero padding required.
5note Every function validates its full input before writing any output.
6note Compose after: si/directives/om2.om gpu/algebra/trit.om
7
8fn oma_bitplane_blocks n ulong to ulong
9q = div n 64
10if ne rem n 64 0
11q = add q 1
12end
13ret q
14end
15
16fn oma_dense_bytes n ulong to ulong
17q = div n 5
18if ne rem n 5 0
19q = add q 1
20end
21ret q
22end
23
24fn oma_pack_one_block t view byte * n ulong base ulong blk edit OmaBlock to int
25lane = new byte 64 0
26m = sub n base
27if gt m 64
28m = ulong 64
29end
30j = ulong 0
31while lt j m bound 64
32put lane j get t add base j
33j = add j 1
34end
35ret oma_block_encode view lane blk
36end
37
38fn oma_pack_bitplane t view byte * n ulong out_planes edit ulong * to int
39if gt n len t
40ret OMA_E_ARG
41end
42nb = oma_bitplane_blocks n
43if lt len out_planes mul nb 2
44ret OMA_E_OVERFLOW
45end
46i = ulong 0
47while lt i n bound 65536
48tv = int get t i
49if or lt tv -1 gt tv 1
50ret OMA_E_INVALID_TRIT
51end
52i = add i 1
53end
54k = ulong 0
55blk = new OmaBlock 0 0
56while lt k nb bound 1024
57rc = oma_pack_one_block t n mul k 64 edit blk
58if ne rc OMA_OK
59ret rc
60end
61put out_planes mul k 2 get blk pos
62put out_planes add mul k 2 1 get blk negp
63k = add k 1
64end
65ret OMA_OK
66end
67
68fn oma_unpack_one_block wp ulong wn ulong n ulong base ulong res edit byte * to int
69lane = new byte 64 0
70blk = new OmaBlock wp wn
71rc = oma_block_decode view blk edit lane
72if ne rc OMA_OK
73ret rc
74end
75m = sub n base
76if gt m 64
77m = ulong 64
78end
79j = ulong 0
80while lt j m bound 64
81put res add base j get lane j
82j = add j 1
83end
84ret OMA_OK
85end
86
87fn oma_unpack_bitplane in_planes view ulong * n ulong res edit byte * to int
88if gt n len res
89ret OMA_E_ARG
90end
91nb = oma_bitplane_blocks n
92if lt len in_planes mul nb 2
93ret OMA_E_ARG
94end
95note pass 1: validate every block and unused lanes
96k = ulong 0
97while lt k nb bound 1024
98wp = get in_planes mul k 2
99wn = get in_planes add mul k 2 1
100rc = oma_check_planes_live wp wn n mul k 64
101if ne rc OMA_OK
102ret rc
103end
104k = add k 1
105end
106note pass 2: decode
107k = ulong 0
108while lt k nb bound 1024
109wp = get in_planes mul k 2
110wn = get in_planes add mul k 2 1
111rc = oma_unpack_one_block wp wn n mul k 64 res
112if ne rc OMA_OK
113ret rc
114end
115k = add k 1
116end
117ret OMA_OK
118end
119
120fn oma_block_serialize b view OmaBlock res edit uchar 16 to int
121rc = oma_block_validate b
122if ne rc OMA_OK
123ret rc
124end
125bp = get b pos
126bn = get b negp
127for i ulong 8
128sh = mul i 8
129put res i uchar and shr bp sh 255
130put res add 8 i uchar and shr bn sh 255
131end
132ret OMA_OK
133end
134
135fn oma_block_deserialize inp view uchar 16 res edit OmaBlock to int
136bp = ulong 0
137bn = ulong 0
138for i ulong 8
139sh = mul i 8
140bp = or bp shl ulong get inp i sh
141bn = or bn shl ulong get inp add 8 i sh
142end
143b = new OmaBlock bp bn
144rc = oma_block_validate view b
145if ne rc OMA_OK
146ret rc
147end
148put res pos bp
149put res negp bn
150ret OMA_OK
151end
152
153fn oma_dense_byte_decode bv int res edit byte 5 to int
154if or lt bv 0 ge bv OMA_DENSE_BYTE_LIMIT
155ret OMA_E_INVALID_BYTE
156end
157v = uint bv
158for j ulong 5
159d = sub int rem v 3 1
160put res j byte d
161v = div v 3
162end
163ret OMA_OK
164end
165
166fn oma_pack_dense_byte t view byte * n ulong base ulong to uchar
167v = uint 0
168p = uint 1
169for j ulong 5
170idx = add base j
171d = int 0
172if lt idx n
173d = int get t idx
174end
175v = add v mul uint add d 1 p
176p = mul p 3
177end
178ret uchar v
179end
180
181fn oma_pack_dense t view byte * n ulong res edit uchar * out_len edit OmaVal to int
182if gt n len t
183put out_len rc OMA_E_ARG
184ret OMA_E_ARG
185end
186nbytes = oma_dense_bytes n
187if lt len res nbytes
188put out_len rc OMA_E_OVERFLOW
189ret OMA_E_OVERFLOW
190end
191i = ulong 0
192while lt i n bound 65536
193tv = int get t i
194if or lt tv -1 gt tv 1
195put out_len rc OMA_E_INVALID_TRIT
196ret OMA_E_INVALID_TRIT
197end
198i = add i 1
199end
200k = ulong 0
201while lt k nbytes bound 16384
202put res k oma_pack_dense_byte t n mul k 5
203k = add k 1
204end
205put out_len val long nbytes
206put out_len rc OMA_OK
207ret OMA_OK
208end
209
210fn oma_unpack_dense inp view uchar * n ulong res edit byte * to int
211if gt n len res
212ret OMA_E_ARG
213end
214nbytes = oma_dense_bytes n
215if lt len inp nbytes
216ret OMA_E_ARG
217end
218d = new byte 5 0
219note pass 1: every byte < 243 and padding trits in last byte are 0
220k = ulong 0
221while lt k nbytes bound 16384
222rc = oma_dense_byte_decode int get inp k edit d
223if ne rc OMA_OK
224ret rc
225end
226for j ulong 5
227if ge add mul k 5 j n
228if ne int get d j 0
229ret OMA_E_INVALID_BYTE
230end
231end
232end
233k = add k 1
234end
235note pass 2: decode
236k = ulong 0
237while lt k nbytes bound 16384
238rc = oma_dense_byte_decode int get inp k edit d
239if ne rc OMA_OK
240ret rc
241end
242for j ulong 5
243idx = add mul k 5 j
244if lt idx n
245put res idx get d j
246end
247end
248k = add k 1
249end
250ret OMA_OK
251end