nx_nxa_hierarchy_correction_gate_t332.nx source
↩ module page · 73 lines · 5909 B
1// nx_nxa_hierarchy_correction_gate_t332.nx -- Validates affine hierarchy corrections with error checks and refusal conditions.
2import "nx_nxa_hierarchy_correction_candidate_t332.nx"
3import "nx_nxa_hierarchy_fixture_t332.nx"
4import "nx_gate_verdict.nx"
5func hg_identity(p:*i64)->i64{var i:i64=0;while i<16{p[i]=0;i=i+1};p[0]=FQ_ONE;p[5]=FQ_ONE;p[10]=FQ_ONE;p[15]=FQ_ONE;return 0}
6func hg_fill(p:*i64,n:i64,v:i64)->i64{var i:i64=0;while i<n{p[i]=v;i=i+1};return 0}
7func hg_same(p:*i64,n:i64,v:i64)->i64{var i:i64=0;while i<n{if p[i]!=v{return 0};i=i+1};return 1}
8func main()->i64{
9 let ctr:*i64=sys_mmap(24) as *i64
10 let p:*i64=sys_mmap(5*128) as *i64;let par:*i64=sys_mmap(40) as *i64;let c:*i64=sys_mmap(128) as *i64;let out:*i64=sys_mmap(5*128) as *i64;let scratch:*i64=sys_mmap(5*128) as *i64
11 var i:i64=0;while i<5{let matrix:*i64=hc_matrix(p,i);hg_identity(matrix);matrix[12]=i*FQ_ONE;i=i+1}
12 par[0]=3;par[1]=0-1;par[2]=0;par[3]=0-1;par[4]=1
13 hg_identity(c);c[0]=0;c[1]=FQ_ONE;c[4]=0-FQ_ONE;c[5]=0;c[12]=5*FQ_ONE
14 var rc:i64=hc_apply(par,5,p,1,0,c,HC_Q30_BUDGET,out,80,scratch,80)
15 gv_check("permuted parents and multiple roots admitted",rc==0,ctr)
16 gv_check("anchor corrected in world space",out[12]==5*FQ_ONE&&out[1]==FQ_ONE,ctr)
17 gv_check("true descendant inherits correction",out[44]==5*FQ_ONE&&out[45]==2*FQ_ONE&&out[33]==FQ_ONE,ctr)
18 gv_check("other root and its child unchanged",out[16+12]==FQ_ONE&&out[64+12]==4*FQ_ONE&&out[16]==FQ_ONE,ctr)
19 gv_check("parent outside selected subtree unchanged",out[48+12]==3*FQ_ONE&&out[48]==FQ_ONE,ctr)
20 hg_fill(out,80,717);par[0]=2;par[2]=0
21 rc=hc_apply(par,5,p,1,0,c,HC_Q30_BUDGET,out,80,scratch,80)
22 gv_check("cycle refuses and preserves all output",rc==HC_BAD_GRAPH&&hg_same(out,80,717)==1,ctr)
23 par[0]=3;par[2]=0;par[4]=5
24 gv_check("out of range parent refuses",hc_apply(par,5,p,1,0,c,HC_Q30_BUDGET,out,80,scratch,80)==HC_BAD_GRAPH,ctr)
25 par[4]=0-2
26 gv_check("invalid negative parent refuses",hc_apply(par,5,p,1,0,c,HC_Q30_BUDGET,out,80,scratch,80)==HC_BAD_GRAPH,ctr)
27 par[4]=1;hg_identity(c);c[0]=2*FQ_ONE
28 gv_check("scaled correction refuses",hc_apply(par,5,p,1,0,c,HC_Q30_BUDGET,out,80,scratch,80)==HC_NONRIGID,ctr)
29 hg_identity(c);c[4]=FQ_ONE/2
30 gv_check("sheared correction refuses",hc_apply(par,5,p,1,0,c,HC_Q30_BUDGET,out,80,scratch,80)==HC_NONRIGID,ctr)
31 hg_identity(c);c[0]=0-FQ_ONE
32 gv_check("reflection refuses",hc_apply(par,5,p,1,0,c,HC_Q30_BUDGET,out,80,scratch,80)==HC_NONRIGID,ctr)
33 hg_identity(c);p[0]=2*FQ_ONE
34 gv_check("nonrigid source palette refuses",hc_apply(par,5,p,1,0,c,HC_Q30_BUDGET,out,80,scratch,80)==HC_NONRIGID,ctr)
35 p[0]=FQ_ONE
36 gv_check("declared affine mode is not silently converted",hc_apply(par,5,p,2,0,c,HC_Q30_BUDGET,out,80,scratch,80)==HC_NONRIGID,ctr)
37 gv_check("aliased scratch refuses",hc_apply(par,5,p,1,0,c,HC_Q30_BUDGET,out,80,out,80)==HC_STORAGE,ctr)
38 gv_check("short output refuses",hc_apply(par,5,p,1,0,c,HC_Q30_BUDGET,out,79,scratch,80)==HC_BAD_DOMAIN,ctr)
39 gv_check("negative anchor refuses",hc_apply(par,5,p,1,0-1,c,HC_Q30_BUDGET,out,80,scratch,80)==HC_BAD_DOMAIN,ctr)
40 gv_check("negative residual budget refuses",hc_apply(par,5,p,1,0,c,0-1,out,80,scratch,80)==HC_BAD_DOMAIN,ctr)
41 gv_check("all refusal outputs remain unchanged",hg_same(out,80,717)==1,ctr)
42
43 hg_identity(c);c[12]=LA_AFFINE_MAX-1
44 rc=hc_apply(par,5,p,1,0,c,HC_F32_BUDGET,out,80,scratch,80)
45 gv_check("near-domain translated composition refuses without wrapping",rc==LA_AFFINE_RANGE&&hg_same(out,80,717)==1,ctr)
46 hg_identity(c);c[0]=LA_AFFINE_MAX-1
47 gv_check("domain-largest linear input refuses before determinant overflow",hc_apply(par,5,p,1,0,c,HC_F32_BUDGET,out,80,scratch,80)==HC_NONRIGID,ctr)
48 hg_identity(c)
49 gv_check("loose tolerance cannot admit unsupported scale",hc_apply(par,5,p,1,0,c,FQ_ONE-1,out,80,scratch,80)==HC_BAD_DOMAIN,ctr)
50 c[0]=FQ_ONE+HC_F32_BUDGET
51 gv_check("scale beyond quantization residual refuses at maximum budget",hc_apply(par,5,p,1,0,c,HC_F32_BUDGET,out,80,scratch,80)==HC_NONRIGID,ctr)
52 hg_identity(c);c[12]=0-LA_AFFINE_MAX
53 gv_check("negative domain endpoint refuses",hc_apply(par,5,p,1,0,c,HC_F32_BUDGET,out,80,scratch,80)==HC_BAD_DOMAIN,ctr)
54 hg_identity(c)
55
56 c[0]=1000690112;c[1]=389282496;c[4]=0-389282496;c[5]=1000690112
57 gv_check("actual binary32 rotation admitted by its encoding budget",hc_apply(par,5,p,1,0,c,HC_F32_BUDGET,out,80,scratch,80)==0,ctr)
58 gv_check("binary32 matrix is not mislabeled nearestQ30",hc_rigid(c,HC_Q30_BUDGET)==HC_NONRIGID,ctr)
59 hg_fill(out,80,717);hg_identity(c)
60 gv_check("beyond binary32 rounding budget refuses",hc_apply(par,5,p,1,0,c,HC_F32_BUDGET+1,out,80,scratch,80)==HC_BAD_DOMAIN&&hg_same(out,80,717)==1,ctr)
61 let realpar:*i64=sys_mmap(104*8) as *i64;let realp:*i64=sys_mmap(104*128) as *i64;let cc:*i64=sys_mmap(256) as *i64;let expected:*i64=sys_mmap(104*128) as *i64;let first:*i64=sys_mmap(104*128) as *i64;let second:*i64=sys_mmap(104*128) as *i64;let work:*i64=sys_mmap(104*128) as *i64
62 hc_fixture(realpar,realp,cc,expected)
63 rc=hc_apply(realpar,104,realp,1,8,cc,HC_Q30_BUDGET,first,1664,work,1664)
64 gv_check("captured104joint source first subtree admitted",rc==0,ctr)
65 var next:i64=0-1;if rc==0{next=hc_apply(realpar,104,first,1,24,hc_matrix(cc,1),HC_Q30_BUDGET,second,1664,work,1664)}
66 gv_check("captured104joint source second subtree admitted",next==0,ctr)
67 var maximum:i64=0;i=0;while i<1664{let delta:i64=fq_abs(second[i]-expected[i]);if delta>maximum{maximum=delta};i=i+1}
68 // Four product truncations versus one independent exact integer-dot truncation differ by <=4Q30 units.
69 gv_check("actual capture agrees with independent exact dot oracle",next==0&&maximum<=4,ctr)
70 gv_puts("captured_matrix_max_delta_q30=" as *u8);gv_num(maximum);gv_puts(" source=31778134 actor=0 tick=19037 correction_policy=unqualified
71" as *u8)
72 return gv_verdict("NX-HIERARCHY-CORRECTION" as *u8,ctr,"Native rigid composition only; no contact or visual acceptance" as *u8)
73}