code wiki / _hdl_build / nx_intlog_gate.nx

nx_intlog_gate.nx source

↩ module page · 92 lines · 4043 B

1// nx_intlog_gate.nx -- KAT gate for the integer log2 / IDF / tf-saturation primitives (the BM25 rung's 2// math floor). Exact on powers of two, tight (+-2/1024) between them, monotonic, overflow-safe at the 3// top of the domain, and the derived idf/tfsat behaviors the ranking depends on. license_tier: ORIGINAL 4import "nx_intlog.nx" 5import "nx_g_puts_lib.nx" 6 7func g_num(v: i64) -> i64 { 8 let bb: *u8 = sys_mmap(28); var m: i64 = v 9 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 10 let t: *u8 = sys_mmap(28); var k: i64 = 0 11 if m == 0 { t[0] = 48 as u8; k = 1 } 12 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 13 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } 14 sys_write(1, bb, k); return 0 15} 16func g_check(name: *u8, cond: i64, pass: *i64, total: *i64) -> i64 { 17 total[0] = total[0] + 1 18 g_puts(name) 19 if cond == 1 { pass[0] = pass[0] + 1; g_puts(" PASS\n" as *u8) } else { g_puts(" FAIL\n" as *u8) } 20 return 0 21} 22 23func main() -> i64 { 24 g_puts("=== nx_intlog gate (integer log2 Q10 + idf + tf-saturation KATs) ===\n" as *u8) 25 let pass: *i64 = sys_mmap(16) as *i64; pass[0] = 0 26 let total: *i64 = sys_mmap(16) as *i64; total[0] = 0 27 28 // T1 exact powers of two 29 var t1: i64 = 1 30 if ilog2_1024(1) != 0 { t1 = 0 } 31 if ilog2_1024(2) != 1024 { t1 = 0 } 32 if ilog2_1024(1024) != 10240 { t1 = 0 } 33 if ilog2_1024(1048576) != 20480 { t1 = 0 } 34 g_check("T1 exact on powers of two (1,2,2^10,2^20)" as *u8, t1, pass, total) 35 36 // T2 tight between powers: log2(3)=1.58496 -> 1623.0; accept +-2 37 let l3: i64 = ilog2_1024(3) 38 var t2: i64 = 0 39 if l3 >= 1621 { if l3 <= 1625 { t2 = 1 } } 40 g_check("T2 log2(3) within +-2/1024 of 1623" as *u8, t2, pass, total) 41 42 // T3 monotonic over a sweep 43 var t3: i64 = 1 44 var prev: i64 = 0 - 1 45 var x: i64 = 1 46 while x < 5000 { 47 let v: i64 = ilog2_1024(x) 48 if v < prev { t3 = 0 } 49 prev = v 50 x = x + 7 51 } 52 g_check("T3 monotonic (1..5000 step 7)" as *u8, t3, pass, total) 53 54 // T4 large-domain safety: 2^46 exact, no overflow 55 var t4: i64 = 0 56 if ilog2_1024(70368744177664) == 47104 { t4 = 1 } 57 g_check("T4 2^46 -> 46*1024 (top-of-domain, overflow-safe)" as *u8, t4, pass, total) 58 59 // T5 idf behavior: rare >> common; everywhere-term ~0 and never negative 60 let rare: i64 = idf_q10(1000, 1) 61 let common: i64 = idf_q10(1000, 500) 62 let all: i64 = idf_q10(1000, 1000) 63 var t5: i64 = 0 64 if rare > common { if common > all { if all >= 0 { if all <= 4 { t5 = 1 } } } } 65 g_check("T5 idf: rare>common>everywhere~=0, floored at 0" as *u8, t5, pass, total) 66 67 // T6 tf saturation: increasing, capped near (k1+1)=2252, tf1 = 2252*1024/(1024+1228)=1024ish 68 let s1: i64 = tfsat_q10(1) 69 let s5: i64 = tfsat_q10(5) 70 let s50: i64 = tfsat_q10(50) 71 var t6: i64 = 0 72 if s1 >= 1020 { if s1 <= 1028 { if s5 > s1 { if s50 > s5 { if s50 <= 2252 { if tfsat_q10(0) == 0 { t6 = 1 } } } } } } 73 g_check("T6 tf-saturation: tf1~=1024, increasing, bounded by 2252, tf0=0" as *u8, t6, pass, total) 74 75 // T7 length-norm: at average length it reduces EXACTLY to tfsat (b=0 stays a pinned special case) 76 var t7: i64 = 1 77 if tfnorm_q10(1, 1024) != tfsat_q10(1) { t7 = 0 } 78 if tfnorm_q10(7, 1024) != tfsat_q10(7) { t7 = 0 } 79 g_check("T7 tfnorm at avg length == tfsat (b=0 special case pinned)" as *u8, t7, pass, total) 80 81 // T8 verbosity correction: longer doc scores lower, shorter higher; bounds hold 82 var t8: i64 = 0 83 let sshort: i64 = tfnorm_q10(2, 512) 84 let savg: i64 = tfnorm_q10(2, 1024) 85 let slong: i64 = tfnorm_q10(2, 4096) 86 if sshort > savg { if savg > slong { if slong > 0 { if sshort <= 2252 { t8 = 1 } } } } 87 g_check("T8 tfnorm: shorter>avg>longer, bounded" as *u8, t8, pass, total) 88 89 g_puts("=== intlog gate: " as *u8); g_num(pass[0]); g_puts("/" as *u8); g_num(total[0]); g_puts(" " as *u8) 90 if pass[0] == total[0] { g_puts("GREEN\n" as *u8); return 0 } 91 g_puts("RED\n" as *u8); return 1 92}