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