Skip to content

examples/static_net: WaveOf binds its source to a local for right shifts (wave4 44 B → 42 B) - #10

Merged
neu-rah merged 1 commit into
mainfrom
claude/project-thread-45k66a-wavecell
Sep 30, 2026
Merged

neu-rah merged 1 commit into
mainfrom
claude/project-thread-45k66a-wavecell

Conversation

@neu-rah

@neu-rah neu-rah commented Sep 30, 2026

Copy link
Copy Markdown
Member

Requested by Roy · project thread

Before: on avr-gcc 7.3 the Banknote wave4 cell was one instruction longer than the same cell written in plain C (28 vs 27 instructions, #9). The extra instruction was a register copy. static_net reported it as 44 B and 25 cycles.

After: the HAPI cell is byte-identical to the plain C one. static_net's wave4 is 42 B, 24 cycles, and every other program of check/build.sh keeps its size.

gcc 7.3 at -Os does a right shift followed by a mask in 8 bits only when the shifted byte is a variable and nothing narrows it before the mask. WaveOf read the byte as Src::get(in) inside the expression and cast the shift to u8, so the shift stayed in int. AVR gcc 16.1 folds the old spelling as well, so this is a 7.3 workaround in the example's cell, not a change to HAPI.

How:

  • examples/static_net/include/waveCell.h: WaveOf::proc reads the source into a local first.
    • Right shifts do shift, add and mask with no narrowing in between.
    • Left shifts keep the early u8: without it gcc 7.3 does them in 16 bits, and mixed_avr_size grows by 30 B.
    • The result is the same as before for every n, every s in −7..7, and every p, m and input byte (3.0e9 cases, host).
  • check/build.sh passes 65 ok. The only expectation that changed is banknote_avr_check: 2344 B, was 2346.
  • compare_emlearn/run.sh: wave4 is 42 B, 24 cycles, 274/274 correct, was 44 B, 25 cycles. results.md is regenerated and only that row changed.
  • measure/'s bn_wave is unchanged at 23 cycles.
  • tests/single_header/run.sh passes 36 ok.
  • These files quote the figures and are updated:
    • the root README
    • examples/static_net/README.md, including its ratios against emlearn
    • .RnD/openDerivation/godbolt/README.md
  • measure/silicon_results.md now notes that its ELFs predate this change. The new wave4 ELF has not been run on a chip.

Not changed: .RnD/openDerivation/round2 has failed on main since #6 (5 ok, 32 FAIL): its : sources still use snet::Slot. Its copy of waveCell.h and the FINDINGS.md figures are left as that round's record.

🤖 Generated with Claude Code

https://claude.ai/code/session_01PttWicEZBc92PTtygV18zb


Generated by Claude Code

…fts (wave4 44 B -> 42 B)

avr-gcc 7.3 -Os does a right shift followed by a mask in 8 bits only
when the shifted byte is a variable and nothing narrows it before the
mask. WaveOf read the byte as Src::get(in) inside the expression and
cast the shift to u8, so the shift stayed in int and register
allocation added a copy: the Banknote cell was 28 instructions where
the same cell in plain C is 27.

WaveOf::proc now reads the source into a local and, for right shifts,
shifts, adds and masks without narrowing in between. Left shifts keep
the early u8 cast: without it gcc 7.3 does them in 16 bits
(mixed_avr_size grows by 30 B). The result is the same for every n,
s in -7..7, p, m and input byte (3.0e9 cases, host).

check/build.sh: 65 ok. banknote_avr_check is 2344 B (was 2346); every
other program keeps its size. compare_emlearn: wave4 42 B, 24 cycles
(was 44 B, 25). measure/'s bn_wave is unchanged at 23 cycles.
tests/single_header/run.sh: 36 ok. The READMEs quoting the figures
are updated; silicon_results.md notes its ELFs predate the change.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PttWicEZBc92PTtygV18zb
@neu-rah neu-rah self-assigned this Sep 30, 2026
@neu-rah
neu-rah merged commit a0c9db7 into main Sep 30, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants