Hacker News new | ask | show | jobs
by hairtuq 248 days ago
The mapping after we have the leading 1's count can be done in 3 instructions (in 32-bit math) on either x86 or ARM:

    t = 0x2020c6 >> x
    return (t << (t & 31)) >> 27
2 comments

Clever, but I prefer the parent's proposal:

  return (0x43201 >> x*4) & 7
It's more understandable and the number of instructions is the same.
That is extremely clever (how did you come up with it?), but definitely needs more than 3 instructions (you need at least 2 just to shift the constant):

    mov eax, 0x2020c6
    shr eax, cl
    mov cl, al
    shl eax, cl
    shr eax, 27
Yet those 14 bytes definitely beats my first attempt... although I've since found an even shorter solution:

    mov ax, 0x4681
    lea ecx, [ecx+ecx*2]
    shr eax, cl
    and eax, 7
12 bytes.
I see what you're doing. 13 bytes, 3 cycles. Why does newline not work?

lzcnt ecx,ecx

mov eax, 00100 0011 0010 0000 0001b shl ecx,2 shrx eax,eax,ecx and eax,15