# Item sig-via-shared: sharing sigmoid with tanh/exp cells (thumbv6m) **Verdict:** the lever does not reach the 0.985x target. The image already shares sigmoid with exp: sigmoid's cells sit inside the exp pool `t_eps`. The only table used by sigmoid alone, `t_crows` (1,572 B), costs more to remove than it saves. The best change I kept inside sigmoid's Thumb code is a one-line branch-layout hint. It lowers thumbv6m S on all three shared seeds, but only by about 0.004% (ratio 0.99995–0.99996). rv32im code and data stay byte-identical. It passes `eik check` and the host fullcheck of all 2^32 inputs for sigmoid, tanh and exp. The Thumb-object check is partial (see Correctness). Base: former-50 `entry.c` from the prior goal repo at c5e3f91, sha256 `88617bb49e36a06cabd7d84ce6fe504ee337f8f41ad7c55bc72f696db94060c3` (confirmed after download). Kit: conjectures-io/exact-integer-kernels-miner at 5a32428. `python -m eik setup` succeeded in a venv (pip bootstrapped, requirements installed with `--require-hashes`). Every report has the measurement fingerprint `2c8fd1f3a6711f0639685319fb25262b02bfecf4c6589c52cb58928270a46322`, as the objective requires. I scored no other former champions. Final file: base plus the patch below, sha256 `d928a932e4ae0b76daa1178eeb158b1905d6a2c955c08778ff4530d3602f8c3d`. ## Patch (against base) ```diff --- a/entry.c (former-50 base) +++ b/entry.c @@ -4333,7 +4333,7 @@ { uint32_t t_m6 = (t_Y << 1) - 135u; new_var19 = 1; - if (((int32_t) t_m6) >= 0) + if (__builtin_expect(((int32_t) t_m6) >= 0, 1)) { if (((uint8_t) t_m6) < 249u) { ``` RULES.md allows `__builtin_expect` with a literal hint, used as an `if` condition on a 32-bit value, which is how it is used here. The line is in `t_eik_sigmoid`, on the x in [-3.42, -2] branch, which uses the `t_skn` cells in `t_eps`. The rv32im build never compiles it (EIK_SEL selects the `_r` path there). ## Results: `python -m eik measure F --seed N --targets thumbv6m,rv32im --no-sample` | seed | base S (thumbv6m) | new S (thumbv6m) | ratio | bytes thumbv6m base/new | rv32im S / bytes | sigmoid typical base→new | sigmoid wide base→new | sigmoid hard base→new (p999) | |---|---|---|---|---|---|---|---|---| | 1001 | 40.357409 | 40.355880 | 0.999962 | 20500 / 20500 | 28.486294 / 18960, identical | 37.7675 → 37.7675 | 28.619 → 28.612 | 47.7467 → 47.7300 (178 → 178) | | 2002 | 40.369801 | 40.368137 | 0.999959 | 20500 / 20500 | identical / 18960 | 37.8705 → 37.8705 | 29.139 → 29.132 | 46.9400 → 46.9233 (187 → 187) | | 3003 | 40.063729 | 40.061775 | 0.999951 | 20500 / 20500 | identical / 18960 | 38.0305 → 38.0305 | 29.578 → 29.567 | 45.4500 → 45.4367 (178 → 178) | - tanh and exp per-class means are identical to base on every seed. For seed 1001: tanh 37.0715 / 20.642 / 76.37, exp 42.8235 / 34.677 / 57.3667. - thumbv7em bytes (reported, not scored): 21254 → 21250. - rv32im check: I compared the allocated-section contents (`.text` plus `.rodata`, 18,960 B) of `sub-rv32im.o` with pyelftools. They hash the same (`c3baa079c6e6a5c2`) for base, final and every experimental variant. The `.o` and image files differ only in non-allocated sections (symbol and metadata bytes). - Success threshold 0.985x: **missed**. Best ratio 0.99995, about 1.5% short. Against the takeover threshold (0.939x former 50) the item contributes essentially nothing. ## Where sigmoid's typical inputs go (base, thumbv6m, seed 1001, 2,000 typical draws) I counted instructions per input with the kit's own `Core` (Unicorn plus Capstone, the same counting as `measure`) and traced paths with a `UC_HOOK_CODE` hook. Mean count by (sign, biased exponent): | inputs | share of draws | mean instructions | path | |---|---|---|---| | +[0.25, 2) | 24% | 32–33 | `t_skl` cells (`t_eps[0..383]` q/v/p words), quadratic, 1 rounding test | | +[2, 8) | 20% | 36 | `t_skp` cells (`t_eps[384..727]`), `t_epn2` | | −[0.25, 2) | 26% | 32–36 | `t_skl`, negative slot (`t_eps` via `(t_s>>12)&0x1FF0`) | | −[2, 3.42) | about 9% | 38–41 | `t_skn` cells (`t_eps` ≤ 475), `t_epn2` | | −[3.42, 6.94) | about 9% | 45 | `t_crows` (131 × 12 B, **sigmoid-only**) | | −[6.94, 8) | <1% | 86 | exp-based tail (`t_dka`, `t_eps[].t_aux`) | | \|x\| < 0.25 | about 6% | 36–43 | polynomial without a table (`t_sgr`) | Hot path for x = 1.5 (32 instructions): `push{r4,r5,r7,lr}; add r7,sp,#8` (2) + sign/exponent dispatch `lsls/bmi/asrs/cmp/blt` (5) + `ldr` literal + `ldm` of the 5-word `t_skl` key (2) + index `subs/bmi/lsrs/lsls/subs` (5) + `ldm` cell (1) + 10 mul/shift/add + rounding test `lsls/lsrs/bne` (3) + `asrs/adds` + `pop` (3). The hot path has no `__aeabi_*` calls, spills or outlined calls. The x = −3.0 path (41 instructions) runs one extra unconditional `b`, because Thumb-1 `bcc` reaches only −256..+254 bytes. Hinting that branch (above) changes rarer sub-paths only: traces at x = −2.125, −2.5, −3.0 and −3.25 stay at 38, 38, 41 and 41 instructions, and only the wide and hard means move. ## The sharing lever: what was tried and measured 1. **Already shared:** `struct t_ep {t_q,t_v,t_p,t_aux}`. Sigmoid reads q/v/p, and exp and tanh read the `t_aux` words of the same 16-byte rows (`t_xkm`, `t_dkn` → `&t_eps[0].t_aux`, `t_m1t` → `t_eps[512..554].t_aux`, `t_bk0` → `t_eps+728`). `t_srow` is used by both tanh (`t_bk1`) and sigmoid (`t_skm`, `t_srow[32+n].t_aux`). Sigmoid's [0.25, 2), [2, 13.5) and [−3.42, −2] cells therefore have no duplicate table to drop. 2. **Drop `t_crows` and send [−6.94, −3.42] to the exp-table tail (`t_l724981`, `t_dka`/`t_eps` aux):** measured, not kept. - thumbv6m bytes 20500 → 18896 (−1,604 B, about −0.91% S). - Sigmoid typical mean 37.77 → 41.24, 40.78, 41.54 on seeds 1001, 2002, 3003 (+3.5 instructions, about +1.7% S by the objective's 0.183/mean). Net about +0.8% worse. - It was also **wrong**: 181, 172 and 195 typical mismatches per seed. That tail is only exact below −6.94, and making it exact higher up would need a new rounding guard on top of the cost. 3. **sigmoid = (1 + tanh(x/2))/2 for [0.25, 2):** not implemented. The tanh path costs about 37 instructions per call against sigmoid's 32–33 here. +4 instructions on about 24% of typical draws is about +1 instruction on the mean (about +0.5% S). The q/v/p words it would free cannot be dropped, because exp and tanh still use the `t_aux` word of the same rows, so it saves no bytes. It would also need a new rounding window for the result's binade [0.5, 1). It loses before any rounding work, so I wrote no code for it. 4. **1/(1+e^−x) tails:** M0+ has no divider, so any reciprocal means an `__aeabi_uidiv` call or a Newton step. Both are slower than the existing one-cell evaluations, so not attempted. Other hint variants, measured the same way: | variant | thumb bytes | sigmoid typical, seeds 1001 / 2002 / 3003 | kept | |---|---|---|---| | same `t_m6` branch hinted unlikely (`, 0`) | 20500 | 37.7900 / 37.8830 / 38.0500 (worse) | no | | plus `t_w >= t_k` hinted unlikely | 20476 | 37.944 / 38.036 / 38.224 (worse) | no | | plus `t_V < 131` hinted likely | 20496 | 39.646 / 39.777 / 39.952 (worse) | no | | plus `(uint8_t)t_m6 < 249` hinted likely | 20500 | identical to the kept change | no gain, dropped | ## Correctness of the final file | check | result | |---|---| | `python -m eik check entry.c` | PASS: 42 functions; bytes rv32im 18960, thumbv6m 20500, thumbv7em 21250; stack_bound 176 / 312 / 280 | | `python -m eik fullcheck --function sigmoid` (all 2^32, host build with ASan+UBSan) | passed, complete, 4,294,967,296 words, 0 mismatches | | `python -m eik fullcheck --function tanh` (full, not `--quick`) | passed, complete, 0 mismatches | | `python -m eik fullcheck --function exp` (full, not `--quick`) | passed, complete, 0 mismatches | | `python -m eik targetcheck --function sigmoid --target thumbv6m` | **UNVERIFIED: not run.** qemu-user (`qemu-arm`) is not installed on this machine, and installing it needs root. | | Substitute: kit `Core.batch` (Unicorn) on the thumbv6m image, sigmoid words 0xBF800000..0xC0E00000 (x in [−7, −1], which contains the only changed branch) | 23,068,672 words, 0 mismatches against `reference.outputs` | | `eik measure --seed 1001 --targets thumbv6m` with the stratified target sample | 98,304 words, 0 mismatches, no wrong outputs, no reasons | | Scored inputs in all six measure runs (final) | no `wrong` entries | ## Next steps (outside this lever's reach) - The sigmoid budget is mostly fixed overhead. Of the 32 instructions at x = 1.5, the 2-instruction prologue, `pop`, and 5-instruction dispatch are about 25%. The cell evaluation is already about 10 ALU instructions. A measurable win needs a cheaper dispatch, for example an exponent-indexed key load that merges `cmp/blt` with `subs/bmi`, or a 2-instruction rounding test. That belongs to a dispatch item, not to sharing. - The `t_crows` region (−[3.42, 6.94), 45 instructions, about 9% of typical draws) is the most expensive common sigmoid path. Re-fitting it as `t_epn2` cells stored in spare `t_eps` q/v/p rows would cut about 4 instructions there (about 0.2% S) and save up to 1.5 KB (about 0.9% S). Only rows 728+ might be free, and I did not establish whether they are. UNVERIFIED. - The full deliverable set for this item is `result.md` and `sources.json`. The final `entry.c` is the base plus the one-line patch above (sha256 given). Per-seed measure numbers are copied into the table above from the JSON reports `eik measure` produced in the scratch area.