2022 version worked but had loose ends. Unicode support was incomplete—couldn’t mix ASCII with Katakana; Phosphor decay was stored in a separate array when it should’ve been packed with RGB; Code was harder to read than it needed to be.
Moved the phosphor decay factor into the 4th byte of the RGB union—should’ve done this in 2022; What was I thinking.
Kept the RGB union. Portability is not a concern. All my systems are little-endian and the code is cleaner this way.
Fixed Unicode issues by introducing a charset array and a macro that packs Unicode ranges into 64-bit unsigned integers: low four bytes for start, high four bytes for end. insert_code() unpacks a random block and picks a character from it:
#define UNICODE(min, max) (((uint64_t)max << 32) | min)
static uint64_t glyphs[] = {
UNICODE(0x0021, 0x007E), /* ASCII */
UNICODE(0xFF65, 0xFF9F), /* Half-width Katakana */
};
static inline void insert_code(matrix *mat,
size_t row, size_t col)
{
uint64_t blk;
uint32_t min, max;
blk = glyphs[(xor() % glyphlen)];
min = (uint32_t)blk;
max = (uint32_t)(blk >> 32);
mat->code[index(mat, row, col)] = xor() % (max - min) + min;
}
Full-width Katakana breaks column alignment. Stick to half-width (U+FF61-U+FF9F) range. Added -DNOKANA flag to disable Katakana altogether.
blend() function is still good. Left it alone:
static inline void blend(matrix *mat,
size_t row, size_t col)
{
unsigned char *color;
color = mat->rgb[index(mat, row, col)].color;
color[R] = color[R] - (color[R] - RGB_BG_RED) / DECAY_MPLIER;
color[G] = color[G] - (color[G] - RGB_BG_GRN) / DECAY_MPLIER;
color[B] = color[B] - (color[B] - RGB_BG_BLU) / DECAY_MPLIER;
}
Replaced rand() with xorshift:
static inline uint32_t xor(void)
{
/* Xorshift RNGs, George Marsaglia, Florida State University. */
static uint32_t y = 2463534242;
y ^= (y << 13);
y = (y >> 17);
return (y ^= (y << 5));
}
Came across faster non-linear variants (xorshiftr+). Xorshift was good enough.
Tossed the license and the automake cruft. Just cc -O3 main.c -o matrix now.
Don’t need the ceremony.
Runs at 2-3% CPU on OpenBSD (T490). No regressions. Fans are quiet.
Commit: f71b0de.