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