Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions specs/port/trinity/src/phi-engine/tvc/tvc_phi_integration.t27
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
module port::trinity::phi_engine::tvc_phi_integration;

// Port of gHashTag/trinity src/phi-engine/tvc/tvc_phi_integration.zig
// (commit 5aabfbe8346f, issue #9119): the TVCGoldenWrap integration layer.
//
// Trit in the original is i8 carrying {-1, 0, +1}; the port keeps i8 and the
// arithmetic verbatim. Add, sub and neg wrap through the golden interval:
// a value past +1 comes back from -1, a value past -1 from +1. Max, min and
// abs are the plain i8 operations.

fn TVCGoldenWrap_tvcTritAdd(a: i8, b: i8) -> i8 {
const sum = a + b;
if (sum > 1) {
return sum - 3;
}
if (sum < -1) {
return sum + 3;
}
return sum;
}

fn TVCGoldenWrap_tvcTritSub(a: i8, b: i8) -> i8 {
const diff = a - b;
if (diff > 1) {
return diff - 3;
}
if (diff < -1) {
return diff + 3;
}
return diff;
}

fn TVCGoldenWrap_tvcTritNeg(a: i8) -> i8 {
const neg = -a;
if (neg > 1) {
return neg - 3;
}
if (neg < -1) {
return neg + 3;
}
return neg;
}

fn TVCGoldenWrap_tvcTritMax(a: i8, b: i8) -> i8 {
if (a > b) {
return a;
}
return b;
}

fn TVCGoldenWrap_tvcTritMin(a: i8, b: i8) -> i8 {
if (a < b) {
return a;
}
return b;
}

fn TVCGoldenWrap_tvcTritAbs(a: i8) -> i8 {
if (a < 0) {
return -a;
}
return a;
}

test tvc_trit_add_wraps_both_ends {
// 1 + 1 = -1 (wrap past +1), the original's own case.
assert(TVCGoldenWrap_tvcTritAdd(1, 1) == -1);
// -1 + -1 = +1 (wrap past -1).
assert(TVCGoldenWrap_tvcTritAdd(-1, -1) == 1);
}

test tvc_trit_add_stays_inside {
assert(TVCGoldenWrap_tvcTritAdd(1, 0) == 1);
assert(TVCGoldenWrap_tvcTritAdd(0, 0) == 0);
assert(TVCGoldenWrap_tvcTritAdd(-1, 0) == -1);
assert(TVCGoldenWrap_tvcTritAdd(1, -1) == 0);
}

test tvc_trit_sub_wraps_and_stays {
// The original's own case: 1 - 1 = 0.
assert(TVCGoldenWrap_tvcTritSub(1, 1) == 0);
// -1 - 1 = -2 wraps to +1.
assert(TVCGoldenWrap_tvcTritSub(-1, 1) == 1);
assert(TVCGoldenWrap_tvcTritSub(0, 1) == -1);
assert(TVCGoldenWrap_tvcTritSub(-1, -1) == 0);
}

test tvc_trit_neg_flips_inside_the_interval {
// The original's own case: neg(1) = -1.
assert(TVCGoldenWrap_tvcTritNeg(1) == -1);
assert(TVCGoldenWrap_tvcTritNeg(-1) == 1);
assert(TVCGoldenWrap_tvcTritNeg(0) == 0);
}

test tvc_trit_max_and_min {
// The original's own cases: max(1, 0) = 1, min(1, 0) = 0.
assert(TVCGoldenWrap_tvcTritMax(1, 0) == 1);
assert(TVCGoldenWrap_tvcTritMax(-1, 1) == 1);
assert(TVCGoldenWrap_tvcTritMax(0, 0) == 0);
assert(TVCGoldenWrap_tvcTritMin(1, 0) == 0);
assert(TVCGoldenWrap_tvcTritMin(-1, 1) == -1);
assert(TVCGoldenWrap_tvcTritMin(0, 0) == 0);
}

test tvc_trit_abs_is_always_positive_or_zero {
// The original's own case: abs(-1) = 1.
assert(TVCGoldenWrap_tvcTritAbs(-1) == 1);
assert(TVCGoldenWrap_tvcTritAbs(1) == 1);
assert(TVCGoldenWrap_tvcTritAbs(0) == 0);
}

test tvc_wrap_is_closed_over_all_trit_pairs {
// Every pair of trits lands back on a trit: the wrap table is total.
var a: i8 = -1;
while (a <= 1) {
var b: i8 = -1;
while (b <= 1) {
const s = TVCGoldenWrap_tvcTritAdd(a, b);
assert(s >= -1 and s <= 1);
const d = TVCGoldenWrap_tvcTritSub(a, b);
assert(d >= -1 and d <= 1);
b += 1;
}
a += 1;
}
}
Loading