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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Watermark: ip zymatica.space | astronautshe.com
|
| 2 |
+
// Copyright (c) 2026 Zymatica. All rights reserved.
|
| 3 |
+
|
| 4 |
+
const std = @import("std");
|
| 5 |
+
|
| 6 |
+
const SparseTransition = struct {
|
| 7 |
+
key: u32,
|
| 8 |
+
sym: u8,
|
| 9 |
+
count: u32,
|
| 10 |
+
};
|
| 11 |
+
|
| 12 |
+
const RadicalPredictor = struct {
|
| 13 |
+
alpha: u32,
|
| 14 |
+
weight: u32,
|
| 15 |
+
trans_rc: [256]SparseTransition,
|
| 16 |
+
trans_rc_len: usize,
|
| 17 |
+
trans_rf: [256]SparseTransition,
|
| 18 |
+
trans_rf_len: usize,
|
| 19 |
+
trans_ra: [256]SparseTransition,
|
| 20 |
+
trans_ra_len: usize,
|
| 21 |
+
prev_rc: u8,
|
| 22 |
+
prev_rf: u8,
|
| 23 |
+
prev_ra: u8,
|
| 24 |
+
|
| 25 |
+
pub fn init(alpha: u32, weight: u32) RadicalPredictor {
|
| 26 |
+
return .{
|
| 27 |
+
.alpha = alpha,
|
| 28 |
+
.weight = weight,
|
| 29 |
+
.trans_rc = undefined,
|
| 30 |
+
.trans_rc_len = 0,
|
| 31 |
+
.trans_rf = undefined,
|
| 32 |
+
.trans_rf_len = 0,
|
| 33 |
+
.trans_ra = undefined,
|
| 34 |
+
.trans_ra_len = 0,
|
| 35 |
+
.prev_rc = 0,
|
| 36 |
+
.prev_rf = 0,
|
| 37 |
+
.prev_ra = 0,
|
| 38 |
+
};
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
pub fn observe(self: *RadicalPredictor, rc: u8, rf: u8, ra: u8) void {
|
| 42 |
+
const w = self.weight;
|
| 43 |
+
|
| 44 |
+
// RC Transitions
|
| 45 |
+
const key_rc = @as(u32, self.prev_rc);
|
| 46 |
+
var found_rc = false;
|
| 47 |
+
for (self.trans_rc[0..self.trans_rc_len]) |*entry| {
|
| 48 |
+
if (entry.key == key_rc and entry.sym == rc) {
|
| 49 |
+
entry.count += w;
|
| 50 |
+
found_rc = true;
|
| 51 |
+
break;
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
if (!found_rc and self.trans_rc_len < 256) {
|
| 55 |
+
self.trans_rc[self.trans_rc_len] = .{ .key = key_rc, .sym = rc, .count = w };
|
| 56 |
+
self.trans_rc_len += 1;
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
// RF Transitions
|
| 60 |
+
const key_rf = (@as(u32, rc) << 8) | @as(u32, self.prev_rf);
|
| 61 |
+
var found_rf = false;
|
| 62 |
+
for (self.trans_rf[0..self.trans_rf_len]) |*entry| {
|
| 63 |
+
if (entry.key == key_rf and entry.sym == rf) {
|
| 64 |
+
entry.count += w;
|
| 65 |
+
found_rf = true;
|
| 66 |
+
break;
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
if (!found_rf and self.trans_rf_len < 256) {
|
| 70 |
+
self.trans_rf[self.trans_rf_len] = .{ .key = key_rf, .sym = rf, .count = w };
|
| 71 |
+
self.trans_rf_len += 1;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
// RA Transitions
|
| 75 |
+
const key_ra = (@as(u32, rc) << 16) | (@as(u32, rf) << 8) | @as(u32, self.prev_ra);
|
| 76 |
+
var found_ra = false;
|
| 77 |
+
for (self.trans_ra[0..self.trans_ra_len]) |*entry| {
|
| 78 |
+
if (entry.key == key_ra and entry.sym == ra) {
|
| 79 |
+
entry.count += w;
|
| 80 |
+
found_ra = true;
|
| 81 |
+
break;
|
| 82 |
+
}
|
| 83 |
+
}
|
| 84 |
+
if (!found_ra and self.trans_ra_len < 256) {
|
| 85 |
+
self.trans_ra[self.trans_ra_len] = .{ .key = key_ra, .sym = ra, .count = w };
|
| 86 |
+
self.trans_ra_len += 1;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
self.prev_rc = rc;
|
| 90 |
+
self.prev_rf = rf;
|
| 91 |
+
self.prev_ra = ra;
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
pub fn getCumFreqsRC(self: *const RadicalPredictor, prev_rc: u8, cum_freqs: *[257]u32) void {
|
| 95 |
+
var freqs = [_]u32{self.alpha} ** 256;
|
| 96 |
+
for (self.trans_rc[0..self.trans_rc_len]) |entry| {
|
| 97 |
+
if (entry.key == @as(u32, prev_rc)) {
|
| 98 |
+
freqs[entry.sym] += entry.count;
|
| 99 |
+
}
|
| 100 |
+
}
|
| 101 |
+
cum_freqs[0] = 0;
|
| 102 |
+
var i: usize = 0;
|
| 103 |
+
while (i < 256) : (i += 1) {
|
| 104 |
+
cum_freqs[i + 1] = cum_freqs[i] + freqs[i];
|
| 105 |
+
}
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
pub fn getCumFreqsRF(self: *const RadicalPredictor, curr_rc: u8, prev_rf: u8, cum_freqs: *[257]u32) void {
|
| 109 |
+
var freqs = [_]u32{self.alpha} ** 256;
|
| 110 |
+
const key = (@as(u32, curr_rc) << 8) | @as(u32, prev_rf);
|
| 111 |
+
for (self.trans_rf[0..self.trans_rf_len]) |entry| {
|
| 112 |
+
if (entry.key == key) {
|
| 113 |
+
freqs[entry.sym] += entry.count;
|
| 114 |
+
}
|
| 115 |
+
}
|
| 116 |
+
cum_freqs[0] = 0;
|
| 117 |
+
var i: usize = 0;
|
| 118 |
+
while (i < 256) : (i += 1) {
|
| 119 |
+
cum_freqs[i + 1] = cum_freqs[i] + freqs[i];
|
| 120 |
+
}
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
pub fn getCumFreqsRA(self: *const RadicalPredictor, curr_rc: u8, curr_rf: u8, prev_ra: u8, cum_freqs: *[257]u32) void {
|
| 124 |
+
var freqs = [_]u32{self.alpha} ** 256;
|
| 125 |
+
const key = (@as(u32, curr_rc) << 16) | (@as(u32, curr_rf) << 8) | @as(u32, prev_ra);
|
| 126 |
+
for (self.trans_ra[0..self.trans_ra_len]) |entry| {
|
| 127 |
+
if (entry.key == key) {
|
| 128 |
+
freqs[entry.sym] += entry.count;
|
| 129 |
+
}
|
| 130 |
+
}
|
| 131 |
+
cum_freqs[0] = 0;
|
| 132 |
+
var i: usize = 0;
|
| 133 |
+
while (i < 256) : (i += 1) {
|
| 134 |
+
cum_freqs[i + 1] = cum_freqs[i] + freqs[i];
|
| 135 |
+
}
|
| 136 |
+
}
|
| 137 |
+
};
|
| 138 |
+
|
| 139 |
+
const BitWriter = struct {
|
| 140 |
+
buffer: [1024]u8,
|
| 141 |
+
bit_index: usize,
|
| 142 |
+
|
| 143 |
+
pub fn init() BitWriter {
|
| 144 |
+
return .{
|
| 145 |
+
.buffer = [_]u8{0} ** 1024,
|
| 146 |
+
.bit_index = 0,
|
| 147 |
+
};
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
pub fn writeBit(self: *BitWriter, bit: u8) void {
|
| 151 |
+
const byte_pos = self.bit_index / 8;
|
| 152 |
+
const bit_pos = @as(u3, @intCast(7 - (self.bit_index % 8)));
|
| 153 |
+
if (byte_pos < 1024) {
|
| 154 |
+
if (bit != 0) {
|
| 155 |
+
self.buffer[byte_pos] |= (@as(u8, 1) << bit_pos);
|
| 156 |
+
} else {
|
| 157 |
+
self.buffer[byte_pos] &= ~(@as(u8, 1) << bit_pos);
|
| 158 |
+
}
|
| 159 |
+
self.bit_index += 1;
|
| 160 |
+
}
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
pub fn writeBitHelper(self: *BitWriter, underflow_bits: *u32, bit: u8) void {
|
| 164 |
+
self.writeBit(bit);
|
| 165 |
+
while (underflow_bits.* > 0) {
|
| 166 |
+
self.writeBit(1 - bit);
|
| 167 |
+
underflow_bits.* -= 1;
|
| 168 |
+
}
|
| 169 |
+
}
|
| 170 |
+
};
|
| 171 |
+
|
| 172 |
+
const BitReader = struct {
|
| 173 |
+
buffer: []const u8,
|
| 174 |
+
bit_index: usize,
|
| 175 |
+
total_bits: usize,
|
| 176 |
+
|
| 177 |
+
pub fn init(buffer: []const u8) BitReader {
|
| 178 |
+
return .{
|
| 179 |
+
.buffer = buffer,
|
| 180 |
+
.bit_index = 0,
|
| 181 |
+
.total_bits = buffer.len * 8,
|
| 182 |
+
};
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
pub fn readBit(self: *BitReader) u8 {
|
| 186 |
+
if (self.bit_index >= self.total_bits) {
|
| 187 |
+
return 0;
|
| 188 |
+
}
|
| 189 |
+
const byte_pos = self.bit_index / 8;
|
| 190 |
+
const bit_pos = @as(u3, @intCast(7 - (self.bit_index % 8)));
|
| 191 |
+
const bit = (self.buffer[byte_pos] >> bit_pos) & 1;
|
| 192 |
+
self.bit_index += 1;
|
| 193 |
+
return bit;
|
| 194 |
+
}
|
| 195 |
+
};
|
| 196 |
+
|
| 197 |
+
const Concept6D = struct {
|
| 198 |
+
domain: u8,
|
| 199 |
+
subdomain: u8,
|
| 200 |
+
operation: u8,
|
| 201 |
+
modality: u8,
|
| 202 |
+
depth: u8,
|
| 203 |
+
polarity: u8,
|
| 204 |
+
};
|
| 205 |
+
|
| 206 |
+
fn encode(concepts: []const Concept6D, writer: *BitWriter) void {
|
| 207 |
+
var pred = RadicalPredictor.init(1, 128);
|
| 208 |
+
var low: u32 = 0;
|
| 209 |
+
var high: u32 = 0xFFFFFFFF;
|
| 210 |
+
var underflow_bits: u32 = 0;
|
| 211 |
+
var cum_freqs: [257]u32 = undefined;
|
| 212 |
+
|
| 213 |
+
for (concepts) |c| {
|
| 214 |
+
const rc = (c.domain << 4) | c.subdomain;
|
| 215 |
+
const rf = (c.operation << 4) | c.modality;
|
| 216 |
+
const ra = (c.depth << 4) | c.polarity;
|
| 217 |
+
const symbols = [_]u8{ rc, rf, ra };
|
| 218 |
+
|
| 219 |
+
const prev_rc = pred.prev_rc;
|
| 220 |
+
const prev_rf = pred.prev_rf;
|
| 221 |
+
const prev_ra = pred.prev_ra;
|
| 222 |
+
|
| 223 |
+
var step: usize = 0;
|
| 224 |
+
while (step < 3) : (step += 1) {
|
| 225 |
+
switch (step) {
|
| 226 |
+
0 => pred.getCumFreqsRC(prev_rc, &cum_freqs),
|
| 227 |
+
1 => pred.getCumFreqsRF(symbols[0], prev_rf, &cum_freqs),
|
| 228 |
+
else => pred.getCumFreqsRA(symbols[0], symbols[1], prev_ra, &cum_freqs),
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
const sym = @as(usize, symbols[step]);
|
| 232 |
+
const total = cum_freqs[256];
|
| 233 |
+
const cum_low = cum_freqs[sym];
|
| 234 |
+
const cum_high = cum_freqs[sym + 1];
|
| 235 |
+
|
| 236 |
+
const range_width = @as(u64, high) - @as(u64, low) + 1;
|
| 237 |
+
high = low +% @as(u32, @truncate(@divTrunc(range_width * cum_high, total))) -% 1;
|
| 238 |
+
low = low +% @as(u32, @truncate(@divTrunc(range_width * cum_low, total)));
|
| 239 |
+
|
| 240 |
+
while (true) {
|
| 241 |
+
if (high < 0x80000000) {
|
| 242 |
+
writer.writeBitHelper(&underflow_bits, 0);
|
| 243 |
+
low <<= 1;
|
| 244 |
+
high = (high << 1) | 1;
|
| 245 |
+
} else if (low >= 0x80000000) {
|
| 246 |
+
writer.writeBitHelper(&underflow_bits, 1);
|
| 247 |
+
low = (low - 0x80000000) << 1;
|
| 248 |
+
high = ((high - 0x80000000) << 1) | 1;
|
| 249 |
+
} else if (low >= 0x40000000 and high < 0xC0000000) {
|
| 250 |
+
underflow_bits += 1;
|
| 251 |
+
low = (low - 0x40000000) << 1;
|
| 252 |
+
high = ((high - 0x40000000) << 1) | 1;
|
| 253 |
+
} else {
|
| 254 |
+
break;
|
| 255 |
+
}
|
| 256 |
+
}
|
| 257 |
+
}
|
| 258 |
+
pred.observe(rc, rf, ra);
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
underflow_bits += 1;
|
| 262 |
+
if (low < 0x40000000) {
|
| 263 |
+
writer.writeBitHelper(&underflow_bits, 0);
|
| 264 |
+
} else {
|
| 265 |
+
writer.writeBitHelper(&underflow_bits, 1);
|
| 266 |
+
}
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
fn decode(encoded_bytes: []const u8, num_concepts: usize, decoded: []Concept6D) void {
|
| 270 |
+
var pred = RadicalPredictor.init(1, 128);
|
| 271 |
+
var r = BitReader.init(encoded_bytes);
|
| 272 |
+
|
| 273 |
+
var value: u32 = 0;
|
| 274 |
+
var i: usize = 0;
|
| 275 |
+
while (i < 32) : (i += 1) {
|
| 276 |
+
value = (value << 1) | @as(u32, r.readBit());
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
var low: u32 = 0;
|
| 280 |
+
var high: u32 = 0xFFFFFFFF;
|
| 281 |
+
var cum_freqs: [257]u32 = undefined;
|
| 282 |
+
|
| 283 |
+
var c_idx: usize = 0;
|
| 284 |
+
while (c_idx < num_concepts) : (c_idx += 1) {
|
| 285 |
+
const prev_rc = pred.prev_rc;
|
| 286 |
+
const prev_rf = pred.prev_rf;
|
| 287 |
+
const prev_ra = pred.prev_ra;
|
| 288 |
+
var symbols = [_]u8{ 0, 0, 0 };
|
| 289 |
+
|
| 290 |
+
var step: usize = 0;
|
| 291 |
+
while (step < 3) : (step += 1) {
|
| 292 |
+
switch (step) {
|
| 293 |
+
0 => pred.getCumFreqsRC(prev_rc, &cum_freqs),
|
| 294 |
+
1 => pred.getCumFreqsRF(symbols[0], prev_rf, &cum_freqs),
|
| 295 |
+
else => pred.getCumFreqsRA(symbols[0], symbols[1], prev_ra, &cum_freqs),
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
const total = @as(u64, cum_freqs[256]);
|
| 299 |
+
const range_width = @as(u64, high) - @as(u64, low) + 1;
|
| 300 |
+
const scaled_val = @divTrunc(((@as(u64, value) - @as(u64, low)) + 1) * total - 1, range_width);
|
| 301 |
+
|
| 302 |
+
var sym: u8 = 0;
|
| 303 |
+
var l: i32 = 0;
|
| 304 |
+
var rr: i32 = 255;
|
| 305 |
+
while (l <= rr) {
|
| 306 |
+
const mid = @divTrunc(l + rr, 2);
|
| 307 |
+
if (@as(u64, cum_freqs[@intCast(mid)]) <= scaled_val and scaled_val < @as(u64, cum_freqs[@intCast(mid + 1)])) {
|
| 308 |
+
sym = @intCast(mid);
|
| 309 |
+
break;
|
| 310 |
+
} else if (scaled_val >= @as(u64, cum_freqs[@intCast(mid + 1)])) {
|
| 311 |
+
l = mid + 1;
|
| 312 |
+
} else {
|
| 313 |
+
rr = mid - 1;
|
| 314 |
+
}
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
+
symbols[step] = sym;
|
| 318 |
+
const sym_idx = @as(usize, sym);
|
| 319 |
+
const cum_low = cum_freqs[sym_idx];
|
| 320 |
+
const cum_high = cum_freqs[sym_idx + 1];
|
| 321 |
+
|
| 322 |
+
high = low +% @as(u32, @truncate(@divTrunc(range_width * cum_high, total))) -% 1;
|
| 323 |
+
low = low +% @as(u32, @truncate(@divTrunc(range_width * cum_low, total)));
|
| 324 |
+
|
| 325 |
+
while (true) {
|
| 326 |
+
if (high < 0x80000000) {
|
| 327 |
+
low <<= 1;
|
| 328 |
+
high = (high << 1) | 1;
|
| 329 |
+
value = (value << 1) | @as(u32, r.readBit());
|
| 330 |
+
} else if (low >= 0x80000000) {
|
| 331 |
+
low = (low - 0x80000000) << 1;
|
| 332 |
+
high = ((high - 0x80000000) << 1) | 1;
|
| 333 |
+
value = ((value - 0x80000000) << 1) | @as(u32, r.readBit());
|
| 334 |
+
} else if (low >= 0x40000000 and high < 0xC0000000) {
|
| 335 |
+
low = (low - 0x40000000) << 1;
|
| 336 |
+
high = ((high - 0x40000000) << 1) | 1;
|
| 337 |
+
value = ((value - 0x40000000) << 1) | @as(u32, r.readBit());
|
| 338 |
+
} else {
|
| 339 |
+
break;
|
| 340 |
+
}
|
| 341 |
+
}
|
| 342 |
+
}
|
| 343 |
+
|
| 344 |
+
decoded[c_idx] = .{
|
| 345 |
+
.domain = symbols[0] >> 4,
|
| 346 |
+
.subdomain = symbols[0] & 0x0F,
|
| 347 |
+
.operation = symbols[1] >> 4,
|
| 348 |
+
.modality = symbols[1] & 0x0F,
|
| 349 |
+
.depth = symbols[2] >> 4,
|
| 350 |
+
.polarity = symbols[2] & 0x0F,
|
| 351 |
+
};
|
| 352 |
+
pred.observe(symbols[0], symbols[1], symbols[2]);
|
| 353 |
+
}
|
| 354 |
+
}
|
| 355 |
+
|
| 356 |
+
export fn run_verification() i32 {
|
| 357 |
+
const inputs = [_]Concept6D{
|
| 358 |
+
.{ .domain = 1, .subdomain = 2, .operation = 3, .modality = 4, .depth = 5, .polarity = 6 },
|
| 359 |
+
.{ .domain = 8, .subdomain = 0, .operation = 15, .modality = 1, .depth = 0, .polarity = 15 },
|
| 360 |
+
.{ .domain = 0, .subdomain = 0, .operation = 0, .modality = 0, .depth = 0, .polarity = 0 },
|
| 361 |
+
.{ .domain = 15, .subdomain = 15, .operation = 15, .modality = 15, .depth = 15, .polarity = 15 },
|
| 362 |
+
.{ .domain = 4, .subdomain = 5, .operation = 6, .modality = 7, .depth = 8, .polarity = 9 },
|
| 363 |
+
};
|
| 364 |
+
|
| 365 |
+
var writer = BitWriter.init();
|
| 366 |
+
encode(&inputs, &writer);
|
| 367 |
+
|
| 368 |
+
const written_bytes = (writer.bit_index + 7) / 8;
|
| 369 |
+
var decoded_buf: [5]Concept6D = undefined;
|
| 370 |
+
decode(writer.buffer[0..written_bytes], 5, &decoded_buf);
|
| 371 |
+
|
| 372 |
+
var i: usize = 0;
|
| 373 |
+
while (i < 5) : (i += 1) {
|
| 374 |
+
const orig = inputs[i];
|
| 375 |
+
const dec = decoded_buf[i];
|
| 376 |
+
if (orig.domain != dec.domain or
|
| 377 |
+
orig.subdomain != dec.subdomain or
|
| 378 |
+
orig.operation != dec.operation or
|
| 379 |
+
orig.modality != dec.modality or
|
| 380 |
+
orig.depth != dec.depth or
|
| 381 |
+
orig.polarity != dec.polarity) {
|
| 382 |
+
return 0;
|
| 383 |
+
}
|
| 384 |
+
}
|
| 385 |
+
return 1;
|
| 386 |
+
}
|
| 387 |
+
|
| 388 |
+
pub fn main() void {}
|