Skip to content

RnD openDerivation/godbolt: compare the HAPI wave4 cell with the same cell in plain C - #9

Merged
neu-rah merged 6 commits into
mainfrom
claude/project-thread-45k66a
Sep 30, 2026
Merged

neu-rah merged 6 commits into
mainfrom
claude/project-thread-45k66a

Conversation

@neu-rah

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

Copy link
Copy Markdown
Member

Requested by Roy · project thread

Before: the Compiler Explorer demo in .RnD/openDerivation/godbolt compared the hand-written hapi::APIOf cell with the Open Derivation translation of it. Those are the same program, so the empty diff (https://godbolt.org/z/TvM334frd) only showed that the translator is faithful.

After: the demo compares the HAPI cell with the same trained cell written by hand in plain C, bool wave4(const uint8_t* x) in wave4_c.c. The README shows both listings side by side. On AVR gcc 7.3.0 (-std=c++17 -Os -mmcu=atmega328p) HAPI is 28 instructions / 56 B and plain C 27 / 54 B. The only difference is one register copy (ldd r25,Z+3; mov r24,r25).

The README explains where that mov comes from: gcc 7.3 narrows (x >> k) & m to an 8-bit shift only when it sees the byte as a variable. In the HAPI cell the byte arrives as a call, Src::get(in). Plain C++ that reads through an accessor hits the same mov. It is not HAPI's composition and not the : translation. Moving the input into static data on the terminal does not help: that comes to 30 instructions. AVR gcc 16.1 on Compiler Explorer compiles both to the same 24 instructions.

The README also records a WaveOf::proc spelling (bind the source to a local, no casts between shift and mask) that makes the HAPI cell byte-identical to wave4_c.c on 7.3. It is not applied here, because it changes examples/static_net/include/waveCell.h and static_net's measured figures. That change goes in a separate PR.

How:

  • wave4_c.c (new): the plain C cell. It compiles as C and as C++ to the same code.
  • verify.sh:
    • builds wave4_c.c as C++ and as C
    • prints size and instruction count for each build
    • lists the instructions that differ from the HAPI cell
    • checks the HAPI cell and wave4_c.c give the same class on all 2^32 inputs (host g++)
    • result: 9/9 ok (verify.log)
  • README.md (godbolt) and the godbolt/ row in .RnD/openDerivation/README.md are updated.

The Compiler Explorer link in the README is still a long /clientstate/ link that has not been opened: godbolt.org is unreachable from the session that wrote it. Draft until it is replaced by short links for the 7.3.0 and 16.1 comparisons.

🤖 Generated with Claude Code

https://claude.ai/code/session_01PttWicEZBc92PTtygV18zb


Generated by Claude Code

… cell in plain C

The Compiler Explorer demo compared the hand-written APIOf cell with the
Open Derivation translation of it, the same program, so its empty diff
showed only that the translator is faithful. wave4_c.c writes the cell
in plain C as the comparator. verify.sh now compiles it as C++ and C,
lists the instructions that differ from the HAPI cell (one register
copy: 28 vs 27 instructions, 56 vs 54 B) and checks both give the same
answer on all 2^32 inputs. The README shows the two listings side by
side, explains the difference and links both sources on Compiler
Explorer.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PttWicEZBc92PTtygV18zb
…ove the extra mov

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PttWicEZBc92PTtygV18zb
… with no HAPI

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PttWicEZBc92PTtygV18zb
…e extra mov

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PttWicEZBc92PTtygV18zb
…he input through Src::get

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PttWicEZBc92PTtygV18zb
…he mov on gcc 7.3, and gcc 16.1's result

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 marked this pull request as ready for review September 30, 2026 06:26
@neu-rah
neu-rah merged commit 4ec6a44 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