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 / z3.om

190 lines · 3571 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note gpu/algebra/z3.om: pure-Omega Z3 (integers mod 3) algebra (replaces src/algebra/oma_z3.{h,c}).
3note Ring isomorphism to balanced trits: 0 <-> 0, 1 <-> +1, 2 <-> -1.
4note Compose after: si/directives/om2.om gpu/algebra/trit.om
5
6fn oma_z3_make v int res edit OmaVal to int
7if or lt v 0 gt v 2
8put res rc OMA_E_INVALID_Z3
9ret OMA_E_INVALID_Z3
10end
11put res val long v
12put res rc OMA_OK
13ret OMA_OK
14end
15
16fn oma_z3_add a int b int res edit OmaVal to int
17if or or lt a 0 gt a 2 or lt b 0 gt b 2
18put res rc OMA_E_INVALID_Z3
19ret OMA_E_INVALID_Z3
20end
21put res val long rem add a b 3
22put res rc OMA_OK
23ret OMA_OK
24end
25
26fn oma_z3_mul a int b int res edit OmaVal to int
27if or or lt a 0 gt a 2 or lt b 0 gt b 2
28put res rc OMA_E_INVALID_Z3
29ret OMA_E_INVALID_Z3
30end
31put res val long rem mul a b 3
32put res rc OMA_OK
33ret OMA_OK
34end
35
36fn oma_z3_neg a int res edit OmaVal to int
37if or lt a 0 gt a 2
38put res rc OMA_E_INVALID_Z3
39ret OMA_E_INVALID_Z3
40end
41put res val long rem sub 3 a 3
42put res rc OMA_OK
43ret OMA_OK
44end
45
46fn oma_z3_to_trit a int res edit OmaVal to int
47if or lt a 0 gt a 2
48put res rc OMA_E_INVALID_Z3
49ret OMA_E_INVALID_Z3
50end
51if eq a 2
52put res val long -1
53else
54put res val long a
55end
56put res rc OMA_OK
57ret OMA_OK
58end
59
60fn oma_trit_to_z3 t int res edit OmaVal to int
61if or lt t -1 gt t 1
62put res rc OMA_E_INVALID_TRIT
63ret OMA_E_INVALID_TRIT
64end
65if lt t 0
66put res val long 2
67else
68put res val long t
69end
70put res rc OMA_OK
71ret OMA_OK
72end
73
74fn oma_z3_block_validate b view OmaZ3Block to int
75if ne and get b one get b two 0
76ret OMA_E_INVALID_PLANES
77end
78ret OMA_OK
79end
80
81fn oma_z3_block_encode inp view uchar 64 res edit OmaZ3Block to int
82o = ulong 0
83t = ulong 0
84for i ulong 64
85v = uint get inp i
86if eq v 1
87o = or o shl ulong 1 i
88else
89if eq v 2
90t = or t shl ulong 1 i
91else
92if ne v 0
93ret OMA_E_INVALID_Z3
94end
95end
96end
97end
98put res one o
99put res two t
100ret OMA_OK
101end
102
103fn oma_z3_block_decode b view OmaZ3Block res edit uchar 64 to int
104rc = oma_z3_block_validate b
105if ne rc OMA_OK
106ret rc
107end
108bo = get b one
109bt = get b two
110for i ulong 64
111v = or and shr bo i 1 shl and shr bt i 1 1
112put res i uchar v
113end
114ret OMA_OK
115end
116
117fn oma_z3_block_from_trits t view OmaBlock res edit OmaZ3Block to int
118rc = oma_block_validate t
119if ne rc OMA_OK
120ret rc
121end
122put res one get t pos
123put res two get t negp
124ret OMA_OK
125end
126
127fn oma_z3_block_to_trits z view OmaZ3Block res edit OmaBlock to int
128rc = oma_z3_block_validate z
129if ne rc OMA_OK
130ret rc
131end
132put res pos get z one
133put res negp get z two
134ret OMA_OK
135end
136
137fn oma_z3_block_add a view OmaZ3Block b view OmaZ3Block res edit OmaZ3Block to int
138rc = oma_z3_block_validate a
139if ne rc OMA_OK
140ret rc
141end
142rc = oma_z3_block_validate b
143if ne rc OMA_OK
144ret rc
145end
146ta = new OmaBlock get a one get a two
147tb = new OmaBlock get b one get b two
148s = new OmaBlock 0 0
149c = new OmaBlock 0 0
150rc = oma_block_add view ta view tb edit s edit c
151if ne rc OMA_OK
152ret rc
153end
154put res one get s pos
155put res two get s negp
156ret OMA_OK
157end
158
159fn oma_z3_block_mul a view OmaZ3Block b view OmaZ3Block res edit OmaZ3Block to int
160rc = oma_z3_block_validate a
161if ne rc OMA_OK
162ret rc
163end
164rc = oma_z3_block_validate b
165if ne rc OMA_OK
166ret rc
167end
168ta = new OmaBlock get a one get a two
169tb = new OmaBlock get b one get b two
170p = new OmaBlock 0 0
171rc = oma_block_mul view ta view tb edit p
172if ne rc OMA_OK
173ret rc
174end
175put res one get p pos
176put res two get p negp
177ret OMA_OK
178end
179
180fn oma_z3_block_neg a view OmaZ3Block res edit OmaZ3Block to int
181rc = oma_z3_block_validate a
182if ne rc OMA_OK
183ret rc
184end
185ao = get a one
186a2 = get a two
187put res one a2
188put res two ao
189ret OMA_OK
190end