Bit-Census Field
Ciphers & Bit Tricks
A living texture on a Super Nintendo where every cell's colour is a count of bits. Feed
each cell's (x, y, time) into one of the four
bit-population intrinsics —
popcount, clz, ctz, parity — and the count
becomes a colour. The demo cycles through all four: the Sierpiński-like XOR fractal, concentric
magnitude bands, a trailing-zero ruler sequence, and a parity checker. Runs on its own; no joypad
needed. Written in C and compiled with the
llvm-mos-based 65816 toolchain
(+mos-a16, 16-bit accumulator mode).
Self-running demo — the field scrolls and the intrinsic cycles on its own.
Click the screen to focus, then watch. Tab away and it pauses.
What it is
Counting the set bits in a number — its Hamming weight — and its cousins (how many zeros lead or trail, whether the count is odd) are such common operations that C exposes them as compiler intrinsics. This demo turns those counts into a picture:
v = build64(x, y, time); // widen coords to 64 bits
switch (fn) { // cycles every ~150 frames
case POPCOUNT: c = __builtin_popcountll(v); break; // XOR bit-fractal
case CLZ: c = __builtin_clzll(v); break; // magnitude bands
case CTZ: c = __builtin_ctzll(v); break; // ruler bands
case PARITY: c = __builtin_parityll(v); break; // checker
}
colour = c & 3; // count → palette index
Each cell's coordinates and the frame counter are packed into a 64-bit word, and the chosen
intrinsic's result picks the colour. popcount of (x^y) is the famous XOR fractal;
leading- and trailing-zero counts draw bands; parity makes a checker. The picture stays bit-exact
across every codegen mode.
Compiler stress-test
| Item | What it exercises |
|---|---|
| bit-population family | popcount / count-leading-zeros / count-trailing-zeros / parity — the __builtin_*ll intrinsics. No other demo in the 52-strong battery ever emits them; the earlier "bit" demos do their shifts and masks by hand. |
| inline lowering | On the 65816 these don't call a helper — the backend expands each into an inline bit-count tree (population count via the classic SWAR masks 0x5555…, 0x3333…, 0x0F0F…). The 0x55 / 0x33 constants are visible in the disassembly. |
| 64-bit on both sides | The long-long variants are 64-bit on the host and the 65816 alike, so the census is identical bit-for-bit — a divergence would be a real codegen bug. (The plain int/long builtins differ in width host-vs-target and would be a false alarm.) |
| four textures | popcount of (x^y) draws the Sierpiński-like XOR fractal; leading-zeros track magnitude into concentric bands; trailing-zeros give a ruler sequence; parity makes a fine checker. The field scrolls and the function cycles. |
Written in C with the llvm-mos 65816 toolchain and verified against bsnes-jg
(and MAME where the SPC700 IPL is present). Hit Verify fidelity to reproduce the build
gate's WRAM assert (gate CRC 0x9516 — a fold of popcount, clz, ctz and parity over
200 sampled coordinates) live in this tab. No far pointers — the same source passes every way:
host == default == +mos-a16 == +mos-xy16 == bsnes-jg,
-verify clean.