code wiki / _hdl_build / nx_derm.nx
nx_derm.nx source
↩ module page · 146 lines · 8430 B
1// nx_derm.nx -- PIPELINE STAGE 4: SKIN AS AN ENVELOPE, computed from the layers beneath it.
2//
3// The industry's fourth stage is "the final visible mesh acts as an envelope, calculating wrinkles and
4// stress maps based on the movement of the inner layers". An audit found ours was the opposite: an
5// INDEPENDENTLY EMITTED SHELL that merely happened to render outermost. Nothing about it was derived from
6// the muscle underneath, so nothing could wrinkle, stretch or slacken when the body moved -- the layer
7// stack had the right topology and the skin was not actually on top of anything.
8//
9// ★THE INVERSION THIS ORGAN MAKES: skin position is no longer authored, it is DERIVED. For each station
10// the skin sits at (muscle radius + fat thickness), so when nx_myo bulges a muscle the skin above it rises
11// BY CONSTRUCTION rather than by a second edit that has to be kept in sync. The envelope invariant --
12// skin can never sink beneath the muscle it covers -- is enforced arithmetically, not checked afterwards.
13//
14// ★AND THE PART THAT MAKES IT A SKIN RATHER THAN A BALLOON: STRESS. When the underlying surface shortens,
15// the skin over it has more area than it needs and must go somewhere: it slackens and WRINKLES. When the
16// surface lengthens, the skin is stretched taut and smooths out. So wrinkle amplitude is a function of
17// local COMPRESSION, which means wrinkles appear where a body actually creases and nowhere else.
18//
19// nx_derm <muscle_r> <rest_muscle_r> <fat> -> the skin state over that muscle
20// nx_derm selftest
21// license_tier: ORIGINAL expect_exit: 0 No hw writes (Rule 26).
22import "nx_gate_verdict.nx"
23import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
24
25const DM_Q: i64 = 1000
26const DM_WRINK: i64 = 800
27const DM_MAXW: i64 = 400
28
29func dm_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
30// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer
31// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the
32// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls).
33// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign.
34func dm_pn(v: i64) -> i64 { nxi_out(v); return 0 }
35func dm_streq(a: *u8, b: *u8) -> i64 {
36 var i: i64=0; var go: i64=1; var eq: i64=1
37 while go==1 { if a[i]!=b[i] { eq=0; go=0 } else { if a[i]==(0 as u8) { go=0 } else { i=i+1 } } }
38 return eq
39}
40func dm_atoi(s: *u8) -> i64 {
41 var i: i64=0; var n: i64=0; var sg: i64=1
42 if s[0]==(45 as u8) { sg=0-1; i=1 }
43 while s[i]!=(0 as u8) { let c: i64=s[i] as i64; if c>=48 { if c<=57 { n=n*10+(c-48) } } i=i+1 }
44 return n*sg
45}
46// ★THE ENVELOPE: skin rides at muscle + fat. Derived, never authored -- bulge the muscle and the skin
47// rises with it, with no second edit to keep in sync and no way for the two to disagree.
48func dm_skin(muscle_r: i64, fat: i64) -> i64 {
49 var m: i64 = muscle_r
50 if m < 0 { m = 0 }
51 var f: i64 = fat
52 if f < 0 { f = 0 }
53 return m + f
54}
55// ★STRESS: per-mille change in the underlying surface. NEGATIVE = the surface shortened, so the skin over
56// it now has slack; POSITIVE = the surface lengthened and the skin is pulled taut.
57func dm_stress(rest_r: i64, muscle_r: i64) -> i64 {
58 if rest_r <= 0 { return 0 }
59 return (muscle_r - rest_r) * DM_Q / rest_r
60}
61// ★WRINKLE: only COMPRESSION creases skin. Slack has to go somewhere and it goes into folds; taut skin
62// smooths. A model that wrinkled under tension too would look busy everywhere and be wrong everywhere.
63func dm_wrinkle(stress: i64) -> i64 {
64 if stress >= 0 { return 0 } // taut or neutral skin does not crease
65 var w: i64 = (0 - stress) * DM_WRINK / DM_Q
66 if w > DM_MAXW { w = DM_MAXW } // bounded: skin folds, it does not explode
67 return w
68}
69func dm_gate() -> i64 {
70 let ctr: *i64 = gv_ctr()
71 gv_head("nx_derm selftest -- skin DERIVED from the layers beneath, creasing only where compressed" as *u8)
72 let rest: i64 = 100
73 let fat: i64 = 20
74 // ★DERIVED, NOT AUTHORED: bulge the muscle and the skin must rise on its own.
75 let s_rest: i64 = dm_skin(rest, fat)
76 let s_flex: i64 = dm_skin(195, fat) // the 95pct bulge nx_myo measures at full flex
77 var t1: i64 = 0
78 if s_flex > s_rest { t1 = 1 }
79 gv_check("T1 DERIVED: bulge the muscle and the skin rises with it" as *u8, t1, ctr)
80 // ★THE ENVELOPE INVARIANT: skin can never sink beneath the muscle it covers. By construction, since
81 // it is muscle PLUS a non-negative fat thickness -- there is no configuration that violates it.
82 var env: i64 = 1
83 var m: i64 = 0
84 while m <= 300 {
85 if dm_skin(m, fat) < m { env = 0 }
86 m = m + 25
87 }
88 gv_check("T2 ENVELOPE INVARIANT: skin never sinks beneath its muscle" as *u8, env, ctr)
89 // ★COMPRESSION CREASES, TENSION SMOOTHS -- the pair that makes it skin rather than a balloon.
90 let comp: i64 = dm_stress(rest, 60) // muscle shrank: skin has slack
91 let tens: i64 = dm_stress(rest, 160) // muscle grew: skin pulled taut
92 var t3: i64 = 0
93 if dm_wrinkle(comp) > 0 { if dm_wrinkle(tens) == 0 { t3 = 1 } }
94 gv_check("T3 wrinkles under COMPRESSION only, never under tension" as *u8, t3, ctr)
95 // ★ANTI-VACUITY: an unstressed skin must be SMOOTH. A model that always wrinkled would pass T3's
96 // first half and be creasing a body that is standing still.
97 var t4: i64 = 0
98 if dm_wrinkle(dm_stress(rest, rest)) == 0 { t4 = 1 }
99 gv_check("T4 anti-vacuity: an unmoved body has SMOOTH skin" as *u8, t4, ctr)
100 // more compression means deeper folds, monotonically
101 var mono: i64 = 1
102 var prev: i64 = 0
103 var r: i64 = 95
104 while r >= 40 {
105 let w: i64 = dm_wrinkle(dm_stress(rest, r))
106 if w < prev { mono = 0 }
107 prev = w
108 r = r - 5
109 }
110 gv_check("T5 monotonic: more compression, deeper folds" as *u8, mono, ctr)
111 // bounded: skin folds, it does not diverge
112 var t6: i64 = 0
113 if dm_wrinkle(dm_stress(rest, 1)) <= DM_MAXW { t6 = 1 }
114 gv_check("T6 BOUNDED: extreme compression folds, it does not explode" as *u8, t6, ctr)
115 // ★STRESS SIGN IS MEANINGFUL, not just magnitude -- the whole model rests on knowing which way it went
116 var t7: i64 = 0
117 if comp < 0 { if tens > 0 { t7 = 1 } }
118 gv_check("T7 stress is SIGNED: shortening and lengthening are distinguishable" as *u8, t7, ctr)
119 var t8: i64 = 0
120 if dm_stress(0, 100) == 0 { if dm_skin(0-5, 0-5) == 0 { t8 = 1 } }
121 gv_check("T8 degenerate input refused, never divided by" as *u8, t8, ctr)
122 return gv_verdict("DERM-GATE" as *u8, ctr, "skin derived from beneath; creases only where compressed" as *u8)
123}
124func main(argc: i64, argv: *i64) -> i64 {
125 if argc >= 2 {
126 if dm_streq(argv[1] as *u8, "selftest" as *u8) == 1 { return dm_gate() }
127 }
128 var mr: i64 = 195
129 var rest: i64 = 100
130 var fat: i64 = 20
131 if argc > 1 { mr = dm_atoi(argv[1] as *u8) }
132 if argc > 2 { rest = dm_atoi(argv[2] as *u8) }
133 if argc > 3 { fat = dm_atoi(argv[3] as *u8) }
134 let st: i64 = dm_stress(rest, mr)
135 dm_puts("{\x22organ\x22:\x22nx_derm\x22,\x22v\x22:1,\x22stage\x22:\x22pipeline stage 4 -- skin as an envelope computed from the inner layers\x22" as *u8)
136 dm_puts(",\x22muscle_radius\x22:" as *u8); dm_pn(mr)
137 dm_puts(",\x22rest_radius\x22:" as *u8); dm_pn(rest)
138 dm_puts(",\x22fat\x22:" as *u8); dm_pn(fat)
139 dm_puts(",\x22skin_radius\x22:" as *u8); dm_pn(dm_skin(mr, fat))
140 dm_puts(",\x22stress_permil\x22:" as *u8); dm_pn(st)
141 dm_puts(",\x22wrinkle\x22:" as *u8); dm_pn(dm_wrinkle(st))
142 dm_puts(",\x22envelope\x22:\x22skin rides at muscle + fat, DERIVED not authored -- bulge the muscle and the skin rises by construction, with no second edit to keep in sync and no way for the two to disagree\x22" as *u8)
143 dm_puts(",\x22stress_model\x22:\x22stress is the SIGNED per-mille change in the surface beneath. Compression means the skin above has slack and it goes into FOLDS; tension pulls it taut and it SMOOTHS. So creases appear where a body actually creases and nowhere else.\x22" as *u8)
144 dm_puts(",\x22composes\x22:\x22nx_myo supplies the muscle radius from the joint angle; nx_fascia supplies the soft-tissue lag; this organ turns both into the visible surface\x22}\n" as *u8)
145 return 0
146}