= Solution
Let the new byte table store $L[0]=8$ and, for $1\le b\le255$, $L[b]=7-\lfloor\log_2 b\rfloor$. Thus it stores the number of leading zeros in an eight-bit argument: $L[1]=7$, $L[2]=L[3]=6$, and $L[128],\ldots,L[255]=0$. All entries fit in one byte.
``
countlz:
adr r3, lzbyte
movs r1, #0
lsrs r2, r0, #24
bne lz_found
movs r1, #8
lsrs r2, r0, #16
bne lz_found
movs r1, #16
lsrs r2, r0, #8
bne lz_found
movs r1, #24
movs r2, r0
lz_found:
ldrb r2, [r3, r2]
adds r0, r1, r2
bx lr
``
The high byte is tried first. A failed test proves that byte was zero, so the next right-shifted value is again a byte-sized index without masking: all higher bytes have already been proved zero. The offset in `r1` counts those skipped zero bytes in bits. Adding the table result for the first nonzero byte gives the <leading-zero count>. If every byte is zero, the last lookup contributes eight to the offset 24, giving \b[32]. The example with high byte one gives \b[7]. At most three conditional branches and one table read are needed; no per-bit loop is used.
Back to article page