# sig-guard — sigmoid Thumb rounding guard and hard path **Outcome: the success bar was missed.** No change inside this lever lowers thumbv6m S on the three seeds. The delivered `entry.c` carries one proven-exact guard change that is S-neutral on seeds 1001/2002/3003 (ratio 1.00000), so it contributes nothing towards the 0.939x threshold. The useful result is negative but exhaustive: every sigmoid rounding guard in the base is already the tightest contiguous window, shown by enumeration of all 2^32 inputs. ## Result table (thumbv6m, `--no-sample`, fingerprint 2c8fd1f3…a46322) | seed | base S | new S | ratio | bytes base → new | sigmoid typical | sigmoid wide | sigmoid hard | hard p99.9 | |---|---|---|---|---|---|---|---|---| | 1001 | 40.3574 | 40.3574 | 1.00000 | 20500 → 20500 | 37.7675 → 37.7675 | 28.6190 → 28.6190 | 47.7467 → 47.7467 | 178 → 178 | | 2002 | 40.3698 | 40.3698 | 1.00000 | 20500 → 20500 | 37.8705 → 37.8705 | 29.1390 → 29.1390 | 46.9400 → 46.9400 | 187 → 187 | | 3003 | 40.0637 | 40.0637 | 1.00000 | 20500 → 20500 | 38.0305 → 38.0305 | 29.5780 → 29.5780 | 45.4500 → 45.4500 | 178 → 178 | - rv32im: S 28.4863 / 28.5180 / 28.2202 and 18960 bytes, identical base vs new on all seeds. `sub-rv32im.o` `.text` (5484 B) and `.rodata` (13476 B) are byte-identical (sha256 prefixes 55d8b1bd88d62f23 / bcbbf38d5ec674e7 for both). The `images.rv32im` hash in the measure JSON does differ between base and new; I did not find out why (UNVERIFIED cause; the object sections that hold code and tables are identical). - Target vs goal: typical down 0 (goal ≥ 2), hard down 0% (goal ≥ 10%). - Held-out seeds 4004/5005/6006 were not run (integration only). ## What was delivered `entry.c` (sha256 5b78c72a…48e19c) = base (sha256 88617bb4…4060c3) plus one line in `t_drj`, the sigmoid-only exp-tail helper: ``` - if ((t_R & 127u) >= 13u) + if ((t_R & 120u) != 0u) ``` The fast return is now taken when the low 7 bits are ≥ 8 instead of ≥ 13, and the test compiles to `lsls/lsrs/beq` instead of `movs/ands/…/cmp/blo` (one instruction fewer, same 160-byte function, no new frame, spill, literal or call). Effect on the Thumb object, measured on **all 19,450 inputs of the 2^32 domain that reach this guard**: mean 93.494 → 88.732 instructions, 0 inputs slower, 843 inputs moved from slow to fast path. None of the scored inputs of seeds 1001/2002/3003 reach this guard, hence ratio 1.00000. By the item's own keep rule ("S falls on all three seeds") this change does not qualify; it is delivered only as the "best change" the brief asks for when the bar is missed. The integrator can take it or leave it; it is harmless either way. **Bound for the new guard (exhaustive).** With the guard forced to "fast" for every input, the fast result differs from the correct one only when `t_R & 127` ∈ {0,1,2,3,4,5} (663 inputs out of 19,450). So any threshold ≥ 6 is exact; 8 is the smallest power of two and is the one whose test is cheapest in Thumb-1. See `logs/guard-scan-all-2pow32.txt`, line "site 17". ## Guard analysis: all 17 sigmoid guards over all 2^32 inputs Method (`tools/guard_scan_instrument.py`, `tools/guard_scan_harness.c`): the Thumb path (`EIK_SEL` forced to 0) is compiled natively; each guard is wrapped so the run can force the fast branch. For every input the forced-fast result is compared with the unforced result, and the guard's low-bit value is recorded. A low-bit value "needs" the slow path iff at least one input with that value gives a wrong forced-fast result. The unforced path was itself compared with the reference `er_sigmoid` on a 1/1024 sample inside this run (0 mismatches) and on all 2^32 inputs separately (0 mismatches). | site | where | guard in base (slow window) | values that truly need slow | verdict | |---|---|---|---|---| | 1 | x ≥ 2 (`t_ev14`) | `(uint8)y9 == 0` | 0 | tight | | 2 | x ≤ −2, upper binade | `(uint8)m6 ≥ 249` (odd only) | 249, 251, 253, 255 | tight | | 3 | x ≤ −2, lower binade | `(uint8)m6 ≤ 19` (multiples of 4) | 0, 4, 8, 12, 16 | tight | | 4 | 0.25 ≤ x < 2 (hot) | `y9 & 0x1FC == 0` (0..3 of 512) | 0, 1, 2, 3 | tight | | 5 | −2 < x ≤ −0.25 | `(uint8)z7 < 7` (odd only) | 1, 3, 5 | tight | | 6 | −2 < x ≤ −0.25, lower | `m4 & 63 < 4` | 0, 1, 2, 3 | tight | | 7 | −2 < x ≤ −0.25, Y<0 | `(uint8)Y < 3` | 0, 1, 2 | tight | | 8 | 1/32 ≤ x < 1/4 | `yy & 0x7F8 == 0` (0..7 of 2048) | 0..7 | tight | | 9 | −1/4 < x ≤ −1/32 | `yy & 0x3F8 == 0` (0..7 of 1024) | 0..7 | tight | | 10 | poly, x>0 | `U & 0x1FF0 == 0` (0..15 of 8192) | 0..13 | 2 spare values of 8192; a 14-wide test costs more than the mask test | | 11 | poly, x<0 | `(U<<20)>>24 == 0` (0..15 of 4096) | 0..13 | same as 10 | | 12 | cbank, x ≤ −3.4 | `(uint8)V < 10` | 0..9 | tight | | 13 | `t_drb` | `V & 127 < 16` | 0..11 | spare 12..15; tried, see below | | 14 | exp tail | `(uint8)R < 10` | 0..9 | tight | | 15 | exp tail | `(uint8)R ≤ 4` | 0..4 | tight | | 16 | exp tail | `low8 < 9` | 0..8 | tight | | 17 | `t_drj` | `R & 127 < 13` | 0..5 | **loosened to < 8 (delivered)** | Inside each window the need is not total (for example site 4, value 3: 11 of 49,055 inputs; site 3, value 16: 204 of 46,287), but those inputs cannot be separated without extra hot-path work, so a contiguous window cannot shrink. This is why the guard failure rate in the typical class (about 1.5% of inputs, each costing about +78 instructions, about 1.2 of the 37.8 typical mean) cannot be lowered by retuning constants. ## Instruction-count notes (base, thumbv6m object, seed 1001) Paths the typical inputs take (share of typical inputs, instructions per call including the 2-instruction prologue and the return): | region | share | fast path | guard cost | slow path | |---|---|---|---|---| | 0.25 ≤ x < 2 | 24% | 32 | 3 (`lsls, lsrs, bne`) | 111 | | −2 < x ≤ −0.25 | 26% | 32 / 35 / 39 | 3 (`uxtb, cmp, b`) | 109–114 | | x ≥ 2 | 21% | 36 | 2 (`lsls, bne`) + literal load | 114 | | −4 < x ≤ −2 | 13% | 38 / 41 / 45 | 3 | 115–133 | | −8 < x ≤ −4 | 7% | 45, or 86 on the exp tail | 3 | 133 | | 1/32 ≤ \|x\| < 1/4 | 8% | 42 / 43 | 3 | 121 | | \|x\| < 1/32 | 1% | 36 / 37 | 3 | 75–79 | - Refinement `t_mt` (shared with tanh): 66 instructions straight-line, 144 bytes, 3 literals, one push and one pop. The only instruction that does no work is the frame-pointer set-up `add r7, sp, #0xc`, which comes from the kit's fixed build flags. Call overhead around it is about 11 instructions before and 4 after. - Hard class, seed 1001 (mean 47.75): 197 of 300 inputs are in the linear region |x| < 2^-11 at 23 instructions (14.8 of the mean); 31 are polynomial-region refinements at 74–79 (7.9); about 70 are `t_mt` refinements at 109–178 (about 23). A 10% cut of the hard mean (4.8 instructions) would need about 20 instructions off every `t_mt` refinement, which is a redesign of the 66-instruction kernel, not a micro-change. ## Changes tried and rejected (measure JSON kept as `measure/rejected-*.json`) | id | change | thumbv6m ratio 1001 / 2002 / 3003 | bytes | why it failed | |---|---|---|---|---| | v1 | site 4 guard as `tst` against a mask loaded with the existing `ldm` (3 → 2 instructions on the hottest path) | 1.03463 / 1.03521 / 1.03450 | 20560 | a seventh live value forced spills; sigmoid typical 37.77 → 42.62 | | v3 | site 13 threshold 16 → 12 | 1.00002 / 1.00002 / 0.99980 | 20500 | a non-power-of-two threshold needs a compare, +1 instruction on the `t_drb` path; mixed | | v4 | site 17 threshold 13 → 6 | 1.00000 ×3 | 20500 | no scored input affected; superseded by the delivered power-of-two form | | v2 | v3 + v4 | 1.00002 / 1.00002 / 0.99980 | 20500 | as v3 | | v5 | site 4 slow path: reuse the fast result as base of the refinement (−1 instruction per slow call) | 1.00015 / 1.00007 / 1.00009 | 20528 | broke tail merging of the three `t_sr` call sites: +28 bytes outweigh typical −0.004 to −0.009 | ## Checks on the delivered `entry.c` | check | result | |---|---| | `python -m eik check` | PASS, bytes rv32im 18960 / thumbv6m 20500 / thumbv7em 21254, stack bound thumbv6m 312 (`logs/check.log`) | | `fullcheck --function sigmoid` (all 2^32, ASan+UBSan) | passed, 4,294,967,296 words, 0 mismatches, 49.8 s (`logs/fullcheck-sigmoid.log`). On this x86-64 host `EIK_SEL` selects the RV path, so this run does not exercise the changed Thumb code. | | `fullcheck --quick` tanh, exp | passed, 134,217,728 words each, 0 mismatches | | Thumb path compiled natively (`EIK_SEL=0`), all 2^32 inputs vs `er_sigmoid` and vs base | 0 mismatches, 0 differences (`logs/native-thumb-path-all-2pow32.log`) | | Thumb object in the kit's Cortex-M0 emulator: all 19,450 inputs that reach the changed guard | 0 mismatches vs `er_sigmoid` (`logs/thumb-object-site17.log`) | | Thumb object, kit stratified sample (measure with sample, seed 1001) | 98,304 words, 0 mismatches; scored inputs `wrong: []` on all seeds | | `python -m eik targetcheck --function sigmoid --target thumbv6m` | **UNVERIFIED — not run.** It needs `qemu-arm`, which is not installed on this machine, and there was no time left to obtain it. The three rows above are the substitute evidence; they are not the required check. | ## Setup notes `python -m eik setup` worked after installing the four pinned wheels from `requirements.txt` with `--require-hashes` into a pip-less venv (the machine has no pip; it was bootstrapped with get-pip). The kit's sandbox ran in mode `none` (bubblewrap cannot nest here), recorded in every report's fingerprint; the measurement fingerprint matches the one in the brief. `zig objdump` in this wheel has no disassembly mode, so the object was disassembled with the kit's own capstone (`tools/thumb_disasm.py`). ## Next steps 1. Do not spend further items on sigmoid guard constants: the table above closes that lever. 2. The remaining cost in this lever is the 1.5% of typical inputs that pay about +78 for a full `t_mt` refinement. The only way to cut it is a mid-precision second stage that resolves most guard failures without the full kernel; it must cost under about 40 instructions and under about 100 bytes to pay (best case about −0.6 typical instructions, about 0.3% of S). Not attempted here. 3. For the hard class the larger lever is outside this item: 66% of the hard pool and 53% of the wide class sit in the linear region at 23 instructions, 11 of which are the prologue and branch chain before the arithmetic starts. One instruction there is worth about 0.66 on hard and 0.34 on wide. 4. The hot-path literal loads (x ≥ 2: `0x3f400001`; x ≤ −2: `0x3E000001`) cost one instruction each, but v1 shows that any extra live register on these paths triggers spills; a fix has to free a register first. ## Files in this item `entry.c`, `patch.diff`, `measure/base-{seed}.json`, `measure/final-{seed}.json`, `measure/rejected-v{1..5}-{seed}.json`, `logs/` (check, fullcheck, guard scan, native full-domain check, Thumb-object run, sampled measure), `tools/` (the scripts and harnesses used), `result.md`, `sources.json`.