code wiki / _hdl_build / _gcd_replace_gate.nx
_gcd_replace_gate.nx source
↩ module page · 65 lines · 2545 B
1// _gcd_replace_gate.nx -- DIFFERENTIAL-REPLACEMENT test at the DERIVE bar (eve R4):
2// is the SEARCH-derived gcd (nx_evo_gcd's champion, body [1 3 0 2]) behaviorally
3// IDENTICAL to the Claude-authored nx_gcd_euclidean? The operator's exact test:
4// "turn off Claude's gcd, emit ours, see if they function the same." If GREEN across
5// every (0..80)x(0..80) pair, the team's derived STRUCTURE is a verified drop-in for
6// a REAL, USED production organ -- the first real used Bar-C self-authoring.
7// PROVENANCE: gcd_evolved's body is VERBATIM from the search-emitted
8// runtime/_hdl_build/_evo_gcd_champ.nx (the structure the SEARCH composed, not hand-
9// designed -- I expected [swap,mod], the search found this). license_tier: ORIGINAL
10import "nx_syscalls.nx"
11import "nx_classical_unpatented.nx"
12
13// THE SEARCH-DERIVED gcd (copied verbatim from _evo_gcd_champ.nx body [1 3 0 2]).
14func gcd_evolved(a: i64, b: i64) -> i64 {
15 var r0: i64 = a
16 var r1: i64 = b
17 var swp: i64 = 0
18 var guard: i64 = 0
19 while r1 != 0 {
20 if guard >= 256 { r1 = 0 } else {
21 if r0 != 0 { r1 = r1 % r0 }
22 r0 = r0 - r1
23 if r1 != 0 { r0 = r0 % r1 }
24 swp = r0
25 r0 = r1
26 r1 = swp
27 guard = guard + 1
28 }
29 }
30 return r0
31}
32
33func grg_w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
34func grg_n(v: i64) -> i64 {
35 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 }
36 var m: i64 = v; let t: *u8 = sys_mmap(28); var k: i64 = 0
37 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
38 while k > 0 { k = k - 1; sys_write(1, (((t as i64)+k) as *u8), 1) }
39 return 0
40}
41
42func main() -> i64 {
43 var bad: i64 = 0
44 var mismatch_a: i64 = 0 - 1
45 var mismatch_b: i64 = 0 - 1
46 var a: i64 = 0
47 while a <= 80 {
48 var b: i64 = 0
49 while b <= 80 {
50 let mine: i64 = gcd_evolved(a, b)
51 let claude: i64 = nx_gcd_euclidean(a, b)
52 if mine != claude { bad = bad + 1; if mismatch_a < 0 { mismatch_a = a; mismatch_b = b } }
53 b = b + 1
54 }
55 a = a + 1
56 }
57 if bad == 0 {
58 grg_w("GCD-REPLACE GREEN: search-derived gcd == Claude nx_gcd_euclidean on ALL (0..80)x(0..80) = 6561 pairs\n" as *u8)
59 } else {
60 grg_w("GCD-REPLACE RED mismatches=" as *u8); grg_n(bad)
61 grg_w(" first@a=" as *u8); grg_n(mismatch_a); grg_w(" b=" as *u8); grg_n(mismatch_b); grg_w("\n" as *u8)
62 }
63 sys_exit(bad)
64 return bad
65}