int ilog2fast(int value) {
  switch (value) {
  case 1:
    return 0;
  case 2:
    return 1;
  case 4:
    return 2;
  case 8:
    return 3;
  case 16:
    return 4;
  case 32:
    return 5;
  case 64:
    return 6;
  case 128:
    return 7;
  case 256:
    return 8;
  case 512:
    return 9;
  case 1024:
    return 10;
  case 2048:
    return 11;
  case 4096:
    return 12;
  case 8192:
    return 13;
  case 16384:
    return 14;
  case 32768:
    return 15;
  case 65536:
    return 16;
  default:
    return -1;
  }
}


x86 assembly (clang, -O0):
<ilog2fast>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
sub    $0x1,%eax
je     123f <ilog2fast+0x13f>
jmp    111b <ilog2fast+0x1b>
mov    -0xc(%rbp),%eax
sub    $0x2,%eax
je     124b <ilog2fast+0x14b>
jmp    112c <ilog2fast+0x2c>
mov    -0xc(%rbp),%eax
sub    $0x4,%eax
je     1257 <ilog2fast+0x157>
jmp    113d <ilog2fast+0x3d>
mov    -0xc(%rbp),%eax
sub    $0x8,%eax
je     1263 <ilog2fast+0x163>
jmp    114e <ilog2fast+0x4e>
mov    -0xc(%rbp),%eax
sub    $0x10,%eax
je     126f <ilog2fast+0x16f>
jmp    115f <ilog2fast+0x5f>
mov    -0xc(%rbp),%eax
sub    $0x20,%eax
je     127b <ilog2fast+0x17b>
jmp    1170 <ilog2fast+0x70>
mov    -0xc(%rbp),%eax
sub    $0x40,%eax
je     1287 <ilog2fast+0x187>
jmp    1181 <ilog2fast+0x81>
mov    -0xc(%rbp),%eax
sub    $0x80,%eax
je     1293 <ilog2fast+0x193>
jmp    1194 <ilog2fast+0x94>
mov    -0xc(%rbp),%eax
sub    $0x100,%eax
je     129f <ilog2fast+0x19f>
jmp    11a7 <ilog2fast+0xa7>
mov    -0xc(%rbp),%eax
sub    $0x200,%eax
je     12ab <ilog2fast+0x1ab>
jmp    11ba <ilog2fast+0xba>
mov    -0xc(%rbp),%eax
sub    $0x400,%eax
je     12b7 <ilog2fast+0x1b7>
jmp    11cd <ilog2fast+0xcd>
mov    -0xc(%rbp),%eax
sub    $0x800,%eax
je     12c3 <ilog2fast+0x1c3>
jmp    11e0 <ilog2fast+0xe0>
mov    -0xc(%rbp),%eax
sub    $0x1000,%eax
je     12cf <ilog2fast+0x1cf>
jmp    11f3 <ilog2fast+0xf3>
mov    -0xc(%rbp),%eax
sub    $0x2000,%eax
je     12db <ilog2fast+0x1db>
jmp    1206 <ilog2fast+0x106>
mov    -0xc(%rbp),%eax
sub    $0x4000,%eax
je     12e7 <ilog2fast+0x1e7>
jmp    1219 <ilog2fast+0x119>
mov    -0xc(%rbp),%eax
sub    $0x8000,%eax
je     12f3 <ilog2fast+0x1f3>
jmp    122c <ilog2fast+0x12c>
mov    -0xc(%rbp),%eax
sub    $0x10000,%eax
je     12ff <ilog2fast+0x1ff>
jmp    130b <ilog2fast+0x20b>
movl   $0x0,-0x4(%rbp)
jmp    1312 <ilog2fast+0x212>
movl   $0x1,-0x4(%rbp)
jmp    1312 <ilog2fast+0x212>
movl   $0x2,-0x4(%rbp)
jmp    1312 <ilog2fast+0x212>
movl   $0x3,-0x4(%rbp)
jmp    1312 <ilog2fast+0x212>
movl   $0x4,-0x4(%rbp)
jmp    1312 <ilog2fast+0x212>
movl   $0x5,-0x4(%rbp)
jmp    1312 <ilog2fast+0x212>
movl   $0x6,-0x4(%rbp)
jmp    1312 <ilog2fast+0x212>
movl   $0x7,-0x4(%rbp)
jmp    1312 <ilog2fast+0x212>
movl   $0x8,-0x4(%rbp)
jmp    1312 <ilog2fast+0x212>
movl   $0x9,-0x4(%rbp)
jmp    1312 <ilog2fast+0x212>
movl   $0xa,-0x4(%rbp)
jmp    1312 <ilog2fast+0x212>
movl   $0xb,-0x4(%rbp)
jmp    1312 <ilog2fast+0x212>
movl   $0xc,-0x4(%rbp)
jmp    1312 <ilog2fast+0x212>
movl   $0xd,-0x4(%rbp)
jmp    1312 <ilog2fast+0x212>
movl   $0xe,-0x4(%rbp)
jmp    1312 <ilog2fast+0x212>
movl   $0xf,-0x4(%rbp)
jmp    1312 <ilog2fast+0x212>
movl   $0x10,-0x4(%rbp)
jmp    1312 <ilog2fast+0x212>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<ilog2fast>:
cmp    $0xff,%edi
jle    1142 <ilog2fast+0x42>
cmp    $0xfff,%edi
jle    1162 <ilog2fast+0x62>
cmp    $0x3fff,%edi
jle    1180 <ilog2fast+0x80>
cmp    $0x4000,%edi
je     11ba <ilog2fast+0xba>
cmp    $0x8000,%edi
je     11c0 <ilog2fast+0xc0>
cmp    $0x10000,%edi
jne    11f6 <ilog2fast+0xf6>
mov    $0x10,%eax
ret
lea    -0x1(%rdi),%ecx
cmp    $0x3f,%ecx
ja     11ac <ilog2fast+0xac>
xor    %eax,%eax
lea    0xead(%rip),%rdx
movslq (%rdx,%rcx,4),%rcx
add    %rdx,%rcx
jmp    *%rcx
mov    $0x1,%eax
ret
cmp    $0x3ff,%edi
jg     1196 <ilog2fast+0x96>
cmp    $0x100,%edi
je     11e4 <ilog2fast+0xe4>
cmp    $0x200,%edi
jne    11f6 <ilog2fast+0xf6>
mov    $0x9,%eax
ret
cmp    $0x1000,%edi
je     11ea <ilog2fast+0xea>
cmp    $0x2000,%edi
jne    11f6 <ilog2fast+0xf6>
mov    $0xd,%eax
ret
cmp    $0x400,%edi
je     11f0 <ilog2fast+0xf0>
cmp    $0x800,%edi
jne    11f6 <ilog2fast+0xf6>
mov    $0xb,%eax
ret
cmp    $0x80,%edi
jne    11f6 <ilog2fast+0xf6>
mov    $0x7,%eax
ret
mov    $0xe,%eax
ret
mov    $0xf,%eax
ret
mov    $0x2,%eax
ret
mov    $0x3,%eax
ret
mov    $0x4,%eax
ret
mov    $0x5,%eax
ret
mov    $0x6,%eax
ret
mov    $0x8,%eax
ret
mov    $0xc,%eax
ret
mov    $0xa,%eax
ret
mov    $0xffffffff,%eax
ret

LLVM IR (-O0):
define i32 @ilog2fast(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
switch i32 %4, label %22 [
i32 1, label %5
i32 2, label %6
i32 4, label %7
i32 8, label %8
i32 16, label %9
i32 32, label %10
i32 64, label %11
i32 128, label %12
i32 256, label %13
i32 512, label %14
i32 1024, label %15
i32 2048, label %16
i32 4096, label %17
i32 8192, label %18
i32 16384, label %19
i32 32768, label %20
i32 65536, label %21
]
5: ; preds = %1
store i32 0, i32* %2
br label %23
6: ; preds = %1
store i32 1, i32* %2
br label %23
7: ; preds = %1
store i32 2, i32* %2
br label %23
8: ; preds = %1
store i32 3, i32* %2
br label %23
9: ; preds = %1
store i32 4, i32* %2
br label %23
10: ; preds = %1
store i32 5, i32* %2
br label %23
11: ; preds = %1
store i32 6, i32* %2
br label %23
12: ; preds = %1
store i32 7, i32* %2
br label %23
13: ; preds = %1
store i32 8, i32* %2
br label %23
14: ; preds = %1
store i32 9, i32* %2
br label %23
15: ; preds = %1
store i32 10, i32* %2
br label %23
16: ; preds = %1
store i32 11, i32* %2
br label %23
17: ; preds = %1
store i32 12, i32* %2
br label %23
18: ; preds = %1
store i32 13, i32* %2
br label %23
19: ; preds = %1
store i32 14, i32* %2
br label %23
20: ; preds = %1
store i32 15, i32* %2
br label %23
21: ; preds = %1
store i32 16, i32* %2
br label %23
22: ; preds = %1
store i32 -1, i32* %2
br label %23
23: ; preds = %22, %21, %20, %19, %18, %17, %16, %15, %14, %13, %12, %11, %10, %9, %8, %7, %6, %5
%24 = load i32, i32* %2
ret i32 %24
}

LLVM IR (-O2):
define i32 @ilog2fast(i32 %0) {
switch i32 %0, label %18 [
i32 1, label %19
i32 2, label %2
i32 4, label %3
i32 8, label %4
i32 16, label %5
i32 32, label %6
i32 64, label %7
i32 128, label %8
i32 256, label %9
i32 512, label %10
i32 1024, label %11
i32 2048, label %12
i32 4096, label %13
i32 8192, label %14
i32 16384, label %15
i32 32768, label %16
i32 65536, label %17
]
2: ; preds = %1
br label %19
3: ; preds = %1
br label %19
4: ; preds = %1
br label %19
5: ; preds = %1
br label %19
6: ; preds = %1
br label %19
7: ; preds = %1
br label %19
8: ; preds = %1
br label %19
9: ; preds = %1
br label %19
10: ; preds = %1
br label %19
11: ; preds = %1
br label %19
12: ; preds = %1
br label %19
13: ; preds = %1
br label %19
14: ; preds = %1
br label %19
15: ; preds = %1
br label %19
16: ; preds = %1
br label %19
17: ; preds = %1
br label %19
18: ; preds = %1
br label %19
19: ; preds = %1, %18, %17, %16, %15, %14, %13, %12, %11, %10, %9, %8, %7, %6, %5, %4, %3, %2
%20 = phi i32 [ -1, %18 ], [ 16, %17 ], [ 15, %16 ], [ 14, %15 ], [ 13, %14 ], [ 12, %13 ], [ 11, %12 ], [ 10, %11 ], [ 9, %10 ], [ 8, %9 ], [ 7, %8 ], [ 6, %7 ], [ 5, %6 ], [ 4, %5 ], [ 3, %4 ], [ 2, %3 ], [ 1, %2 ], [ 0, %1 ]
ret i32 %20
}