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 t = 0x2020c6 >> x
return (t << (t & 31)) >> 27 return (0x43201 >> x*4) & 7
It's more understandable and the number of instructions is the same. 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.lzcnt ecx,ecx
mov eax, 00100 0011 0010 0000 0001b shl ecx,2 shrx eax,eax,ecx and eax,15