# Item tanh-hard-wide: eik_tanh Thumb refinement, tiny and saturation paths **Result: target missed.** The best change kept (B1, "merge the tanh refinement call sites") is a real gain on all three seeds, but a negligible one: thumbv6m S ratio **0.99991–0.99993x former 50** and −24 bytes. tanh hard stays at about 72 (the target was ≤ 65), and wide is unchanged. On its own this lever is far from the takeover threshold (≤ 0.939x). Everything else I tried was worse on every seed or failed exactness (table below). I never registered, signed, submitted or spent TAO. ## Setup (verified) - Kit `conjectures-io/exact-integer-kernels-miner` @ `5a32428ad4aa23bce737d50a738ace24ddeeed2e`. The pinned wheels (capstone 5.0.9, pyelftools 0.33, unicorn 2.1.4, ziglang 0.17.1) were fetched from PyPI, their sha256 checked against `requirements.txt`, and unpacked into a `venv --without-pip`, because the host has no pip or ensurepip. `python -m eik setup` → "incumbent registered as champion of rv32im, thumbv6m, overall". Measurement fingerprint in every report: `2c8fd1f3a6711f0639685319fb25262b02bfecf4c6589c52cb58928270a46322` (matches the brief). - Base `eik-work/baselines/former-50/entry.c` from the prior repo @ `c5e3f912…`: sha256 `88617bb49e36a06cabd7d84ce6fe504ee337f8f41ad7c55bc72f696db94060c3` (matches). - qemu-arm for `targetcheck`: Debian `qemu-user-static_7.2+dfsg-7+deb12u18+b3_amd64.deb` (URL from the prior toolkit's dependencies.json). The package sha256 `c3e3ba2b…b6e402` matches that manifest. Version: qemu-arm 7.2.22. ## Results (thumbv6m; `measure --targets thumbv6m,rv32im --no-sample`) tanh per-class means are typical / uniform / wide / hard. | seed | base S | new S | ratio | bytes base→new | tanh base (t/u/w/h) | tanh new (t/u/w/h) | hard p999 | |---|---|---|---|---|---|---|---| | 1001 | 40.3574 | 40.3540 | 0.99992 | 20500→20476 | 37.072 / 14.695 / 20.642 / 76.370 | 37.075 / 14.695 / 20.642 / 76.453 | 117→117 | | 2002 | 40.3698 | 40.3671 | 0.99993 | 20500→20476 | 36.884 / 14.281 / 20.803 / 71.950 | 36.887 / 14.281 / 20.804 / 72.067 | 117→117 | | 3003 | 40.0637 | 40.0600 | 0.99991 | 20500→20476 | 36.958 / 14.722 / 19.867 / 67.507 | 36.961 / 14.722 / 19.867 / 67.573 | 117→117 | - sigmoid and exp thumbv6m per-class counts are identical to base on all seeds (checked programmatically). Peak stack is 60 → 60. - rv32im: S identical (28.4863 / 28.5180 / 28.2202) and bytes identical (18960). The rv32im object's .text/.rodata/.data/.bss sections are byte-identical to base (compared with pyelftools). Note: the report's `images.rv32im` hash changes with *any* source edit, even an appended blank line, while the object sections stay identical. Compare object sections, not that hash. - Base S for 1001 is 40.357 (the brief's 40.43 is a different draw or average). Base tanh means over the 3 seeds: typical ≈ 36.97, wide ≈ 20.44, hard ≈ 71.94. - The held-out seeds (4004/5005/6006) were not run (integration only). UNVERIFIED on them. ## Correctness of the final file (entry.c = base + patch below, sha256 `a2663192fa455b535b5935d2a7a81f0d949b53ff4d05684240bb579f83cfe4f6`) - `python -m eik check` → `PASS {"functions": 42, "bytes": {"rv32im": 18960, "thumbv6m": 20476, "thumbv7em": 21190}, "stack_bound": {"rv32im": 176, "thumbv6m": 312, "thumbv7em": 280}}` - `python -m eik fullcheck --function tanh` (all 2^32, ASan+UBSan): complete, range_complete, **0 mismatches**. **Caveat:** the host build runs on x86-64, where wchar_t is signed, so `EIK_SEL` is true and this exercises the `_r` (RV) code, not the Thumb `t_` code I changed. A Thumb-only change can pass it trivially (see D1 below). The check that matters here is targetcheck. - `python -m eik targetcheck --function tanh --target thumbv6m` (all 2^32 inputs on the scored Thumb object under qemu-arm): complete, **0 mismatches**. - `fullcheck --quick` sigmoid: 0 mismatches; exp: 0 mismatches. Those functions' code is unchanged. - No new rounding guard or table was added. B1 only moves where the existing `t_hi + t_tr(t_w, t_V)` is evaluated (same operands, same arithmetic), so no new integer bound is needed. Exactness was also confirmed exhaustively by targetcheck. ## patch.diff (against base, the only change) ```diff --- base.c +++ entry.c @@ -3634,8 +3634,9 @@ { return t_w - new_var22; } - new_var = (t_w - ((((t_w << 8) - t_Y) - 8u) >> 8)) + t_tr(t_w, (t_Y >> 4) | 0x18000008u); - return new_var; + t_hi = (t_w - ((((t_w << 8) - t_Y) - 8u) >> 8)); + t_V = (t_Y >> 4) | 0x18000008u; + goto t_rj; } new_var3 = t_Y; new_var12 = (int32_t) (new_var3 << new_var11); @@ -3666,7 +3667,9 @@ new_var12 = t_w - ((new_var16 - t_z) >> 5); return new_var12; } - return ((((new_var7 - 6u) << new_var6) + (t_z >> 5)) - (-1u)) + t_tr(t_w, (t_z >> 4) + 0x6000001u); + t_hi = ((((new_var7 - 6u) << new_var6) + (t_z >> 5)) - (-1u)); + t_V = (t_z >> 4) + 0x6000001u; + goto t_rj; } uint32_t t_dd = (t_w << 6) - t_z; if ((t_z & 63u) >= 4u) @@ -3674,8 +3677,9 @@ return t_w - (t_dd >> 6); } { - uint32_t t_hi = t_w - ((t_dd - 1u) >> 6); - return t_hi + t_tr(t_w, (t_z >> 4) + 0xA000002u); + t_hi = t_w - ((t_dd - 1u) >> 6); + t_V = (t_z >> 4) + 0xA000002u; + goto t_rj; } } } ``` Effect: four `bl t_mt` call sites in `eik_tanh` become one, for −24 bytes. On the e=128/129 refinement paths it costs one extra instruction (113→114), so hard rises by about 0.08–0.12. The size gain outweighs that on every seed. ## Instruction-count notes (thumbv6m, base, from capstone disassembly + per-input emulator counts) Per-exponent counts over seeds 1001+2002+3003, exponent field e. Every function has a frame-pointer prologue: `push {…, r7, lr}; add r7, sp, #N` … `pop`. Neither clang path (frame pointer or shrink-wrapping) ever moves the push below an early return. - **Tiny, e < 115 (49% of wide): 10 instr.** `push; add r7; lsls r1,r0,#1; bmi; asrs r2,r1,#24; cmp #0x7c; bhs; cmp #0x73; bhs; pop`. Given the check order (|x|≥2, then e≥124, then e<115) this is minimal. Only the push/add/pop overhead could shrink, and clang does not shrink-wrap here (trial A). - **Saturation, |x| ≳ 9 (e ≥ 131, about 11% of wide): 16 instr.** `push; add; lsls; bmi; ldr =t_bk0; ldm {p,a}; adds t_s; bmi; lsrs/lsls (sign offset); lsrs #23; subs #1; cmp #0xf8; bhi; ldr ±1; pop`. e=130: 16, 27 or 33. - e=115: 19. e=116–118: 27 (31 occasionally). e=119: 32. **e=120–123: 41** = 9 classify + 10 row setup (`ldr; ldm{3}; ands; lsrs; ands; adds; lsrs #14; bne`) + 16 polynomial + 6 round/return. - Typical main paths: e=124: 39; 125: 37; 126: 34/37; 127: 37; 128: 35; 129: 33/35. - **Hard (near-midpoint) refinement:** e=121–122: 106; 123: 114; 124: 117; 125: 115; 126: 112–115; 127: 115; 128–129: 113 (114 after B1); e=119: 75 and e=120: 83 (via t_pd2). Refinement adds about 65–78 instr to the fast path. `t_mt` alone is 66 instructions with its prologue. It is shared with sigmoid through `t_sr`/`t_nr`, has two early exits (`tail==0`, `B==0`), and holds no literal-pool loads except 2 constants and the table base. Hard mean 72 → 65 would need about 14 instructions off every refinement. That is not reachable with micro-changes; see next steps. - Weight check (kit config: typical 0.55, wide 0.30, hard 0.10): hard 72→65 alone is only about −0.34% of S. One instruction off the tiny path (49% of wide) is about −0.24% of S. ## Trials (seeds 1001/2002/3003 thumbv6m S ratio vs base; rv32im sections identical in all) | id | change | ratio | verdict | |---|---|---|---| | A | tiny pre-check `if ((w<<1) < 0x73000000u) return w;` in `eik_tanh` wrapper | 1.0255 / 1.0259 / 1.0252 | rejected: no shrink-wrap, push still first. Tiny path 7 instead of 10, but typical +4 (37.07→41.07) | | **B1** | merge 3 `t_tr` call sites into `t_rj` | 0.99992 / 0.99993 / 0.99991 | **kept** | | B2 | B1 + tanh-only inlined copy of `t_mt` at the single site | 1.0345 / 1.0349 / 1.0354 | rejected: register pressure in eik_tanh, typical +3, uniform +2.5, +180 B | | C1 | B1 + saturation range check as `(t_s - lo) < rg`, with lo/rg added to `t_bk0` (exactly equivalent) | 1.0299 / 1.0299 / 1.0302 | rejected: new `push r6` + `sub sp,#4` frame, extra loads; typical +2.8 | | C2 | C1 + `±1.0` computed as `((w>>31)<<31)+0x3F800000` | 1.0086 / 1.0082 / 1.0084 | rejected | | D1 | B1 without the `t_f>>14==0 → t_tw_low2` branch in e=120–123 | n/a | **incorrect**: host fullcheck 0 mismatches (it tests the `_r` path), but thumbv6m targetcheck found 170 mismatches, e.g. in 3c000000 got 3bffff55 want 3bfffeab. The branch is needed | Lesson repeated from the prior job: in `eik_tanh` the register allocator is at its limit. Any extra live value (struct field, inlined callee) adds a stack frame or spills and costs 2–4 instructions on the typical path. ## Next steps (not done, UNVERIFIED) 1. Hard: a cheaper exact decision for near-midpoint tanh. One option is a second-stage table holding the residual sign for the few rows/segments the refinement actually visits. Another is to feed the refinement a longer `Y` already computed on the fast path (skip the `R0/h/ka/l` recomputation in `t_mt`). Either needs a proper error bound, plus targetcheck as the only valid exhaustive check for `t_` code. 2. Wide e=120–123 (41 instr): its `ldm` struct layout is already minimal. Gains need a different segment polynomial (fewer multiplies), not micro-rewrites. 3. The tiny and saturation paths are at their minimum for the current check order. They can only get cheaper if another item reorders the top-level classification in a way that also pays for itself on typical. ## Evidence not delivered as files The brief says to write only result.md and sources.json, so these stayed in the worker's scratch directory and are summarised above rather than shipped: the full entry.c (reproducible as base + the patch above, sha256 given), the six measure JSONs, and the check logs.