Solution (source code)

= Solution

Extract successive bytes into indices of the <byte-table population count> table. The last shift leaves only the original high byte, so that index needs no final mask.

``
popcount_table:
    push {r4}
    adr r2, popbyte
    movs r3, #255
    movs r1, r0
    ands r1, r1, r3
    ldrb r4, [r2, r1]
    lsrs r0, r0, #8
    movs r1, r0
    ands r1, r1, r3
    ldrb r1, [r2, r1]
    adds r4, r4, r1
    lsrs r0, r0, #8
    movs r1, r0
    ands r1, r1, r3
    ldrb r1, [r2, r1]
    adds r4, r4, r1
    lsrs r0, r0, #8
    ldrb r1, [r2, r0]
    adds r0, r4, r1
    pop {r4}
    bx lr
``

Place the byte table at an aligned nearby label in the instruction-address range of `adr`. That instruction forms the table's address relative to the <program counter>; it does not load a table entry. Each `ldrb` uses <register-offset addressing>: the base is `r2`, the byte offset is the index register, and the loaded byte is zero-extended. Because entries occupy one byte, the index needs no scaling. `ldrb r1, [r2, r1]` uses the old `r1` to form the address before overwriting it with the result.

Every bit belongs to exactly one extracted byte, so adding the four table counts gives \b[the whole word's <population count>], independently of <endianness>. The calculation reads four bytes from the table and contains no loop.