code wiki / _hdl_build / nx_myotable.nx

nx_myotable.nx source

↩ module page · 167 lines · 9318 B

1// nx_myotable.nx -- ★THE MUSCLE SET AS ONE TABLE, fail-closed, so the count can actually grow. 2// 3// ★THE DEFECT. nx_myoset describes each muscle across FOUR PARALLEL IF-CHAINS -- ms_origin, ms_insert, 4// ms_peak, ms_name -- so adding one muscle means editing four functions and keeping them aligned by 5// hand. nx_skelgen documented this exact shape for its joints: "the same 13 joints are described across 6// sg_jparent, sg_jdx, sg_jdy, sg_jdz and the constraint pair, so adding a joint means editing six 7// places." It is the third instance in this codebase and the cure has not changed: ONE TABLE. 8// 9// ★★AND A REAL FAIL-OPEN BUG, not just an ergonomic one: every chain ENDS IN AN UNGUARDED DEFAULT 10// (`return 250`, `return "gastrocnemius"`). So ms_origin(999) answers 250 and ms_name(999) answers 11// "gastrocnemius" -- ★A PHANTOM MUSCLE THAT LOOKS COMPLETELY REAL. Nothing anywhere says how many 12// muscles exist, so no caller can even know it asked out of range. This table REFUSES instead. 13// 14// ⚠★★WHAT THIS DELIBERATELY DOES **NOT** DO: pad the count. The measured gap is ~18 modelled against 15// ~700 named skeletal muscles, and it would be easy to type another twenty rows of plausible-looking 16// attachment stations right here. This session convicted exactly that habit -- the shoulder width was 17// half its true value because someone typed a number that looked reasonable -- so the mechanism ships 18// with the EXISTING eight re-derived byte-exactly, and every future row must arrive SOURCED. 19// ★The right source is already in hand: attachments should be DERIVED FROM BONE LANDMARKS that 20// nx_skelgen emits, not typed -- the "attach relative to the parent" law this lane learned on the brow. 21// 22// nx_myotable selftest 23// license_tier: ORIGINAL expect_exit: 0 No hw writes (Rule 26). 24import "nx_gate_verdict.nx" 25 26const MT_NM: i64 = 8 // muscles presently modelled -- the honest count, and it is QUERYABLE 27const MT_S: i64 = 4 // row stride: origin, insertion, peak, name-id 28const MT_BAD: i64 = 0 - 1 // the refusal value; distinct from every legitimate station 29 30// ★ONE TABLE. Origin and insertion are per-mille of stature; peak is the girth contributed at full 31// contraction. These are the values nx_myoset already shipped, transcribed once, in one place. 32func mt_table(T: *i64) -> i64 { 33 var i: i64 = 0 34 // origin insert peak nameid 35 T[i]=780; T[i+1]=700; T[i+2]=90; T[i+3]=0; i=i+MT_S // pectoralis major : sternum/clavicle -> humerus 36 T[i]=800; T[i+1]=690; T[i+2]=70; T[i+3]=1; i=i+MT_S // deltoid : clavicle/scapula -> humerus 37 T[i]=700; T[i+1]=560; T[i+2]=80; T[i+3]=2; i=i+MT_S // latissimus dorsi : lower spine/iliac -> humerus 38 T[i]=560; T[i+1]=700; T[i+2]=45; T[i+3]=3; i=i+MT_S // rectus abdominis : pubis -> ribs 39 T[i]=745; T[i+1]=620; T[i+2]=60; T[i+3]=4; i=i+MT_S // biceps brachii : scapula -> radius 40 T[i]=500; T[i+1]=400; T[i+2]=95; T[i+3]=5; i=i+MT_S // gluteus maximus : ilium/sacrum -> femur 41 T[i]=430; T[i+1]=270; T[i+2]=85; T[i+3]=6; i=i+MT_S // quadriceps : ilium/femur -> patella 42 T[i]=250; T[i+1]=120; T[i+2]=55; T[i+3]=7; i=i+MT_S // gastrocnemius : femur -> calcaneus 43 return 0 44} 45func mt_count() -> i64 { return MT_NM } 46// ★FAIL-CLOSED accessors. An index outside the set is REFUSED, where the if-chains silently answered 47// with the last muscle in the list. 48func mt_ok(m: i64) -> i64 { if m < 0 { return 0 } if m >= MT_NM { return 0 } return 1 } 49func mt_origin(T: *i64, m: i64) -> i64 { if mt_ok(m)==0 { return MT_BAD } return T[m*MT_S] } 50func mt_insert(T: *i64, m: i64) -> i64 { if mt_ok(m)==0 { return MT_BAD } return T[m*MT_S+1] } 51func mt_peak(T: *i64, m: i64) -> i64 { if mt_ok(m)==0 { return MT_BAD } return T[m*MT_S+2] } 52func mt_nameid(T: *i64, m: i64) -> i64 { if mt_ok(m)==0 { return MT_BAD } return T[m*MT_S+3] } 53// span of a muscle in per-mille -- the distance between its attachments, always positive 54func mt_span(T: *i64, m: i64) -> i64 { 55 if mt_ok(m) == 0 { return MT_BAD } 56 var d: i64 = mt_origin(T,m) - mt_insert(T,m) 57 if d < 0 { d = 0 - d } 58 return d 59} 60// ★THE TAPER, restated on the table: zero at BOTH attachments, full at the belly. This is the property 61// that lets overlapping muscles SUM instead of stacking into steps, so it must survive the refactor. 62func mt_weight(T: *i64, m: i64, station: i64) -> i64 { 63 if mt_ok(m) == 0 { return MT_BAD } 64 let o: i64 = mt_origin(T,m) 65 let n: i64 = mt_insert(T,m) 66 var lo: i64 = n 67 var hi: i64 = o 68 if lo > hi { lo = o; hi = n } 69 if station <= lo { return 0 } 70 if station >= hi { return 0 } 71 let mid: i64 = (lo+hi)/2 72 let half: i64 = (hi-lo)/2 73 if half <= 0 { return 0 } 74 var d: i64 = station - mid 75 if d < 0 { d = 0 - d } 76 return mt_peak(T,m) * (half - d) / half 77} 78 79func main(argc: i64, argv: *i64) -> i64 { 80 let ctr: *i64 = gv_ctr() 81 gv_head("nx_myotable selftest -- the muscle set as ONE table, and it refuses what it does not have" as *u8) 82 let T: *i64 = sys_mmap(MT_NM*MT_S*8) as *i64 83 mt_table(T) 84 85 // ★T1 FAITHFUL: the table carries exactly the values nx_myoset's four if-chains carried. Checked 86 // against the literals, so a transcription slip cannot pass. 87 var t1: i64 = 1 88 if mt_origin(T,0) != 780 { t1 = 0 } 89 if mt_insert(T,0) != 700 { t1 = 0 } 90 if mt_peak(T,0) != 90 { t1 = 0 } 91 if mt_origin(T,4) != 745 { t1 = 0 } 92 if mt_insert(T,4) != 620 { t1 = 0 } 93 if mt_peak(T,5) != 95 { t1 = 0 } 94 if mt_origin(T,7) != 250 { t1 = 0 } 95 if mt_insert(T,7) != 120 { t1 = 0 } 96 gv_check("T1 FAITHFUL: the table reproduces the shipped attachment values exactly" as *u8, t1, ctr) 97 98 // ★★T2 THE BUG THIS FIXES. The if-chains fell through to gastrocnemius for ANY out-of-range index, 99 // inventing a muscle that looked entirely real. Refusal must be explicit and distinguishable. 100 var t2: i64 = 1 101 if mt_origin(T, MT_NM) != MT_BAD { t2 = 0 } 102 if mt_origin(T, 999) != MT_BAD { t2 = 0 } 103 if mt_origin(T, 0-1) != MT_BAD { t2 = 0 } 104 if mt_nameid(T, 999) != MT_BAD { t2 = 0 } 105 if mt_weight(T, 999, 500)!= MT_BAD { t2 = 0 } 106 gv_check("T2 FAIL-CLOSED: an unknown muscle is REFUSED, not answered with the last one" as *u8, t2, ctr) 107 108 // ★T3 and the count is QUERYABLE, which is what makes T2 usable -- previously nothing told a caller 109 // how many muscles existed, so no caller could know it had asked out of range. 110 var t3: i64 = 0 111 if mt_count() == MT_NM { if mt_ok(MT_NM-1) == 1 { if mt_ok(MT_NM) == 0 { t3 = 1 } } } 112 gv_check("T3 the set publishes its own SIZE, so callers can stay in range" as *u8, t3, ctr) 113 114 // T4 the anatomy invariant survives: every muscle spans two DISTINCT attachments 115 var t4: i64 = 1 116 var m: i64 = 0 117 while m < MT_NM { 118 if mt_origin(T,m) == mt_insert(T,m) { t4 = 0 } 119 if mt_span(T,m) <= 0 { t4 = 0 } 120 if mt_peak(T,m) <= 0 { t4 = 0 } 121 m = m + 1 122 } 123 gv_check("T4 every muscle spans two DISTINCT attachments with a real peak" as *u8, t4, ctr) 124 125 // ★★T5 THE TAPER, the property that makes a muscle SET composable rather than a pile of bumps: 126 // zero at both attachments, positive at the belly, for EVERY muscle -- not just the one that was 127 // spot-checked before. 128 var t5: i64 = 1 129 m = 0 130 while m < MT_NM { 131 let o: i64 = mt_origin(T,m) 132 let n: i64 = mt_insert(T,m) 133 if mt_weight(T,m,o) != 0 { t5 = 0 } 134 if mt_weight(T,m,n) != 0 { t5 = 0 } 135 if mt_weight(T,m,(o+n)/2) <= 0 { t5 = 0 } 136 m = m + 1 137 } 138 gv_check("T5 TAPER holds for EVERY muscle: zero at both attachments, full at the belly" as *u8, t5, ctr) 139 140 // ★T6 ANTI-VACUITY on the taper: it must actually VARY along the muscle, not return its peak 141 // everywhere. A flat "taper" would pass T5's endpoints and still stack into steps. 142 let o0: i64 = mt_origin(T,0) 143 let n0: i64 = mt_insert(T,0) 144 let mid0: i64 = (o0+n0)/2 145 let quarter: i64 = (mid0 + n0)/2 146 var t6: i64 = 0 147 if mt_weight(T,0,quarter) > 0 { if mt_weight(T,0,quarter) < mt_weight(T,0,mid0) { t6 = 1 } } 148 gv_check("T6 ANTI-VACUITY: the taper VARIES along the belly, it is not a flat peak" as *u8, t6, ctr) 149 150 // ★T7 ONE ROW PER MUSCLE. Adding a muscle must touch exactly one place -- proven structurally by 151 // every attribute of every muscle being reachable from the single table pointer. 152 var t7: i64 = 1 153 m = 0 154 while m < MT_NM { 155 if mt_origin(T,m) != T[m*MT_S] { t7 = 0 } 156 if mt_insert(T,m) != T[m*MT_S+1] { t7 = 0 } 157 if mt_peak(T,m) != T[m*MT_S+2] { t7 = 0 } 158 if mt_nameid(T,m) != T[m*MT_S+3] { t7 = 0 } 159 m = m + 1 160 } 161 gv_check("T7 every attribute comes from the ONE table -- no second list to drift from" as *u8, t7, ctr) 162 163 gv_puts(" HONEST COVERAGE: " as *u8); gv_num(mt_count()) 164 gv_puts(" muscles modelled of ~700 named skeletal muscles (~1 percent). The mechanism now scales; the DATA must arrive SOURCED, derived from bone landmarks, never typed to fill the count.\n" as *u8) 165 return gv_verdict("MYOTABLE-GATE" as *u8, ctr, 166 "muscles as one fail-closed table; taper preserved for every muscle; count queryable and honestly small" as *u8) 167}