Chris Low-level systems developer
← Back to all posts

The Borrow That Betrays the Zero

ISA

No instruction-set claim is made. The guarantee is C unsigned arithmetic: subtraction wraps modulo 2^64 and each byte borrows exactly as the language defines, so the identity holds on any target with two's-complement integers. No instruction, CSR, or privilege behavior is asserted or measured.

MICROARCH

No claim depends on how any core implements anything. The verdicts are differential equalities between the mask and a per-byte reference loop that hold regardless of pipeline or cache behavior; no timing, throughput, or implementation detail is asserted.

FIRMWARE

No boot code, machine-mode service, or privilege handoff takes part. The test compiles the identity as an ordinary C function and runs it as a userspace program; no firmware call enters the computation, so nothing firmware does can move the result.

KERNEL

No kernel is involved at any step. The program makes no system call and no kernel data structure appears in the argument; the scanner use case is ordinary string handling, and kernel behavior cannot move any verdict.

ALGORITHM

The claim is an arithmetic identity proved against an independent oracle: the mask is nonzero exactly when a zero byte is present, checked exhaustively over all 16,777,216 24-bit inputs plus 65,536 16-bit and 2 million random 64-bit words against a per-byte reference loop, with zero mismatches. The phantom-borrow boundary is measured the same way: 1001 counting overcounts in 10,065,536 differential inputs.

A strlen loop checks one byte at a time: is this the terminator, is this the terminator. Eight checks for eight bytes. There is a way to ask all eight bytes at once, and the whole trick is a subtraction.

Take the word 0x4200. Its lowest byte is 0x00, the byte you are hunting. Subtract 0x0101, which takes 0x01 off each of the two low bytes. The low byte computes 0x00 minus 0x01. That borrows, wraps to 0xFF, and the borrow sets the byte's top bit. The byte above computes 0x42 minus 0x01 minus the incoming borrow, landing at 0x40 with its top bit quiet. The zero byte just announced itself through its top bit.

A nonzero byte cannot fake this. Subtract 0x01 from 0x05 and you get 0x04: no borrow, top bit unchanged. Subtract 0x01 from 0xFF and the top bit stays on, but that is what the second half of the expression is for. AND the result with ~x, the bitwise inverse of the original word: a byte that already had its top bit set has a zero in ~x at that position, so the AND clears it. Only a top bit that the subtraction itself set survives, and the subtraction sets it by borrowing out of a zero byte. [1]

The whole test, for all eight bytes at once:

uint64_t has_zero_byte(uint64_t x) {
    return (x - 0x0101010101010101ULL) & ~x & 0x8080808080808080ULL;
}
Blueprint-style diagram of the zero-byte borrow mechanism: a 0x00 byte shown as eight zero bits becomes eight one bits after subtracting 0x01, with the top bit lit amber, while a 0x42 byte becomes 0x40 with its top bit quiet; below, the full expression (x - 0x0101010101010101) & ~x & 0x8080808080808080 with the verdict that a nonzero mask means a zero byte lives in the word
The borrow is the signal: 0x00 minus 0x01 wraps to 0xFF and flips the top bit on, while a nonzero byte subtracts cleanly. One 64-bit subtraction asks all eight bytes at once.
Diagram: hand-built by Chris Dillinger for this post

I ran this over every 24-bit input, all 16,777,216 of them, against a byte-by-byte reference loop: the mask fired exactly when a zero byte was present, with zero misses, and a zero mask always meant no zero byte. That is the direction a scanner needs. A word-at-a-time strlen walks the string in 8-byte steps; when the mask is zero the whole word is clean and it moves on, and only when the mask fires does it drop to the byte loop to find where. [1]

One measured boundary, not an assumption: the mask names the word, not the byte. A borrow out of a zero byte can spill into the byte above it. With x = 0x0100, byte 0 is 0x00 and byte 1 is 0x01, and the borrow turns byte 1 into 0xFF, setting its top bit too: a phantom. The lab behind this post measured 1001 such overcounts in 10,065,536 differential inputs when the identity was used for counting. So never count the mask's bits as zero positions. Use it as the tripwire, then let the byte loop do the locating. [1]

The bench habit: when you scan for a sentinel byte, test the word first and read the bytes only on a hit.

One sentence to carry away: one subtraction asks eight bytes whether any of them is zero, and a zero mask is proof that none are.

Sources

  1. 01
    lab/93-swar-zero-count: module source and firsthand PROOF.mdChris Dillinger

    The verification behind the post: the zero-byte detection identity, the measured 1001 counting overcounts in 10,065,536 differential inputs that bound the naive identity to existence testing, and the guarded 16-bit lane construction that counts exactly. The post's own firsthand run (exhaustive 24-bit sweep, 16,777,216 inputs, zero mismatches between the mask and a per-byte reference) re-verified the existence direction on the build VM.

Related work: C audit from $350, fixed price. .

Choose how to get in touch

The project brief is filled in for you.

Compose in Gmail Use my email app