Skip to content

Instantly share code, notes, and snippets.

@imaami
Created April 22, 2026 21:28
Show Gist options
  • Select an option

  • Save imaami/dc772a0836fa3e9097e382c5a6df6110 to your computer and use it in GitHub Desktop.

Select an option

Save imaami/dc772a0836fa3e9097e382c5a6df6110 to your computer and use it in GitHub Desktop.
Moser-De Bruijn-ish nibble expansion lookup
#include <stdint.h>
static constexpr uint8_t expand2[] = {
[0b0000] = 0b00000000, [0b0001] = 0b00000001,
[0b0010] = 0b00000100, [0b0011] = 0b00000101,
[0b0100] = 0b00010000, [0b0101] = 0b00010001,
[0b0110] = 0b00010100, [0b0111] = 0b00010101,
[0b1000] = 0b01000000, [0b1001] = 0b01000001,
[0b1010] = 0b01000100, [0b1011] = 0b01000101,
[0b1100] = 0b01010000, [0b1101] = 0b01010001,
[0b1110] = 0b01010100, [0b1111] = 0b01010101
};
static constexpr uint16_t expand4[] = {
[0b0000] = 0x0000, [0b0001] = 0x0003,
[0b0010] = 0x0030, [0b0011] = 0x0033,
[0b0100] = 0x0300, [0b0101] = 0x0303,
[0b0110] = 0x0330, [0b0111] = 0x0333,
[0b1000] = 0x3000, [0b1001] = 0x3003,
[0b1010] = 0x3030, [0b1011] = 0x3033,
[0b1100] = 0x3300, [0b1101] = 0x3303,
[0b1110] = 0x3330, [0b1111] = 0x3333
};
static constexpr uint32_t expand8[] = {
[0b0000] = 0x00000000, [0b0001] = 0x0000000f,
[0b0010] = 0x00000f00, [0b0011] = 0x00000f0f,
[0b0100] = 0x000f0000, [0b0101] = 0x000f000f,
[0b0110] = 0x000f0f00, [0b0111] = 0x000f0f0f,
[0b1000] = 0x0f000000, [0b1001] = 0x0f00000f,
[0b1010] = 0x0f000f00, [0b1011] = 0x0f000f0f,
[0b1100] = 0x0f0f0000, [0b1101] = 0x0f0f000f,
[0b1110] = 0x0f0f0f00, [0b1111] = 0x0f0f0f0f
};
static constexpr uint64_t expand16[] = {
[0b0000] = 0x0000000000000000, [0b0001] = 0x00000000000000ff,
[0b0010] = 0x0000000000ff0000, [0b0011] = 0x0000000000ff00ff,
[0b0100] = 0x000000ff00000000, [0b0101] = 0x000000ff000000ff,
[0b0110] = 0x000000ff00ff0000, [0b0111] = 0x000000ff00ff00ff,
[0b1000] = 0x00ff000000000000, [0b1001] = 0x00ff0000000000ff,
[0b1010] = 0x00ff000000ff0000, [0b1011] = 0x00ff000000ff00ff,
[0b1100] = 0x00ff00ff00000000, [0b1101] = 0x00ff00ff000000ff,
[0b1110] = 0x00ff00ff00ff0000, [0b1111] = 0x00ff00ff00ff00ff
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment