Fabs Ridgeline
Rendering & Graphics
An animated terrain ridgeline on a Super Nintendo driven by the
tent map
iteration using __builtin_fabsf as the hot absolute-value operation.
The kink at x=0.5 creates sharp V-shaped peaks; as the seeds drift each frame the
terrain undulates. This is the first demo to fire the LLVM backend's
TARGET-CUSTOM legalizeFAbs path — a single inline
AND src, 0x7FFFFFFF that clears the float sign bit with no libcall.
Compiler stress-test #80 of the battery.
Self-running — the terrain undulates as the tent-map seeds drift.
Click the screen to focus, then watch. Tab away and it pauses.
What it is
The tent map f(x) = 1 − |2x − 1| is a simple chaotic function on [0,1]. The absolute value creates a sharp V-notch: points below x=0.5 reflect up, points above reflect down. After a few iterations from different seeds, the outputs spread across [0,1] in a pattern that depends sensitively on the starting value.
// One tent-map step — each op a separate statement (no FMA fusion) float two_x = 2.0f * x; // __mulsf3 float shifted = two_x - 1.0f; // __subsf3 float abs_val = __builtin_fabsf(shifted); // G_FABS → legalizeFAbs inline AND float next = 1.0f - abs_val; // __subsf3 // legalizeFAbs (MOSLegalizerInfo.cpp:3410) expands to: // AND src, 0x7FFFFFFF ← clear bit 31 (sign bit); zero libcall
Each of the 16 tile columns gets a height from three tent-map iterations seeded by (column, time). Sky tiles fill above the ridge, a bright cap marks the ridge line, medium and dark tiles fill below. The seeds shift by a small offset each frame so the terrain slowly undulates. The V-notches in the tent map appear as sharp triangular peaks and valleys.
Compiler stress-test
| Item | What it exercises |
|---|---|
| G_FABS | The float absolute-value operation legalizes through a TARGET-CUSTOM path (MOSLegalizerInfo.cpp:369 → legalizeFAbs at :3410) that emits a single AND instruction, clearing only the sign bit (bit 31 of the 32-bit float representation). No libcall — the SDK has no fabsf symbol because inline sign-clear is cheaper and sufficient. |
| tent map | x → 1 − |2x − 1| is a chaotic map on [0,1] with a V-notch kink at x=0.5. The absolute value of (2x−1) creates the kink; peaks in the terrain profile correspond to seeds that iterate close to x=0.5. Three iterations are enough to produce a varied ridgeline. |
| distinct from #57 and #45 | #57 medfilt uses integer G_ABS (a different node, different legalizer at :281) on int16_t values — never a float sign-bit operation. #45 metaball type-puns a float to uint32_t and back through the integer ALU — also never G_FABS. This is the first demo that generates the float sign-clear path. |
| negative-zero edge | fabsf(−0.0f) = +0.0f (IEEE 754 negative zero canonicalizes to positive zero). The legalizeFAbs comment at :3420 explicitly guards this: the AND with 0x7FFFFFFF also clears the sign bit of −0.0f → +0.0f, matching host behaviour. |
| GATE_N = 120 | With 128 columns and a rotating-XOR accumulator of period 16, a gate of exactly 128 steps reduces to 0 (8 full rotations cancel all contributions). Using 120 steps (a non-multiple of 16) breaks the period and gives a non-trivial CRC 0x161A. |
Written in C with the llvm-mos 65816 toolchain and verified against
bsnes-jg and MAME. Hit Verify fidelity to reproduce the build gate's WRAM assert
(gate CRC 0x161A — a fold of 120 tent-map column heights) live in this tab.
No far pointers — host == default == +mos-a16 == +mos-xy16,
-verify clean.