tinygrad-notes

LOP3 truth tables and current tinygrad

All tutorials · Updated September 21, 2026 · tinygrad 8ad8f73

NVIDIA PTX’s lop3.b32 applies an arbitrary Boolean function to three inputs, independently at each bit position. Its eight-bit immediate encodes the truth table. The PTX specification defines the table using input patterns 0xF0, 0xCC, and 0xAA. PTX ISA reference.

Build and verify a table

def choose(a, b, c):
  return (a & b) | ((~a) & c)

lut = choose(0xF0, 0xCC, 0xAA) & 0xFF
assert lut == 0xCA
for a in range(2):
  for b in range(2):
    for c in range(2):
      index = (a << 2) | (b << 1) | c
      assert ((lut >> index) & 1) == (choose(a, b, c) & 1)
print(hex(lut))

This checks all eight input combinations for a bitwise selection function. Python’s unbounded complement is masked to the intended width when producing the table.

Where this fits in tinygrad

The pinned Ops enumeration has AND, OR, and XOR, but no dedicated LOP3 opcode. The PTX renderer maps these operations to ordinary bitwise instructions. A later target compiler may combine expressions, but a high-level Boolean expression is not evidence that LOP3 appeared in the final binary.

To investigate a particular kernel, preserve its Tensor expression, generated PTX, target architecture, compiler version, and disassembly. Compare the Boolean function first, then inspect instruction selection and measure the full kernel.

PTX and machine assembly are different representations. Do not infer final register usage or instruction count solely from a source-level identity. This tutorial checks the truth-table calculation on the host; it does not claim NVIDIA hardware execution.

Original chapter by Di Zhu: historical version.