float fast_log2(float val) {
  union {
    float f;
    int i;
  } t;
  t.f = val;
  int *const exp_ptr = &t.i;
  int x = *exp_ptr;
  const int log_2 = ((x >> 23) & 255) - 128;
  x &= ~(255 << 23);
  x += 127 << 23;
  *exp_ptr = x;
  val = ((-1.0f / 3) * t.f + 2) * t.f - 2.0f / 3;
  return (val + log_2);
}


x86 assembly (clang, -O0):
<fast_log2>:
push   %rbp
mov    %rsp,%rbp
movss  %xmm0,-0x4(%rbp)
movss  -0x4(%rbp),%xmm0
movss  %xmm0,-0x8(%rbp)
lea    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
sar    $0x17,%eax
and    $0xff,%eax
sub    $0x80,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
and    $0x807fffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x3f800000,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movss  -0x8(%rbp),%xmm0
movss  0xea1(%rip),%xmm1
mulss  %xmm1,%xmm0
movss  0xe99(%rip),%xmm1
addss  %xmm1,%xmm0
movss  -0x8(%rbp),%xmm1
mulss  %xmm1,%xmm0
movss  0xe7c(%rip),%xmm1
addss  %xmm1,%xmm0
movss  %xmm0,-0x4(%rbp)
movss  -0x4(%rbp),%xmm0
cvtsi2ssl -0x18(%rbp),%xmm1
addss  %xmm1,%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<fast_log2>:
movd   %xmm0,%eax
mov    %eax,%ecx
shr    $0x17,%ecx
movzbl %cl,%ecx
and    $0x807fffff,%eax
or     $0x3f800000,%eax
movd   %eax,%xmm0
movss  0xede(%rip),%xmm1
mulss  %xmm0,%xmm1
addss  0xed6(%rip),%xmm1
add    $0xffffff80,%ecx
mulss  %xmm0,%xmm1
addss  0xecb(%rip),%xmm1
xorps  %xmm0,%xmm0
cvtsi2ss %ecx,%xmm0
addss  %xmm1,%xmm0
ret

LLVM IR (-O0):
%union.anon = type { float }
define float @fast_log2(float %0) {
%2 = alloca float
%3 = alloca %union.anon
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store float %0, float* %2
%7 = load float, float* %2
%8 = bitcast %union.anon* %3 to float*
store float %7, float* %8
%9 = bitcast %union.anon* %3 to i32*
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = load i32, i32* %10
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = ashr i32 %12, 23
%14 = and i32 %13, 255
%15 = sub nsw i32 %14, 128
store i32 %15, i32* %6
%16 = load i32, i32* %5
%17 = and i32 %16, -2139095041
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = add nsw i32 %18, 1065353216
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = load i32*, i32** %4
store i32 %20, i32* %21
%22 = bitcast %union.anon* %3 to float*
%23 = load float, float* %22
%24 = call float @llvm.fmuladd.f32(float 0xBFD5555560000000, float %23, float 2.000000e+00)
%25 = bitcast %union.anon* %3 to float*
%26 = load float, float* %25
%27 = call float @llvm.fmuladd.f32(float %24, float %26, float 0xBFE5555560000000)
store float %27, float* %2
%28 = load float, float* %2
%29 = load i32, i32* %6
%30 = sitofp i32 %29 to float
%31 = fadd float %28, %30
ret float %31
}

LLVM IR (-O2):
define float @fast_log2(float %0) {
%2 = bitcast float %0 to i32
%3 = lshr i32 %2, 23
%4 = and i32 %3, 255
%5 = add nsw i32 %4, -128
%6 = and i32 %2, -2139095041
%7 = or i32 %6, 1065353216
%8 = bitcast i32 %7 to float
%9 = tail call float @llvm.fmuladd.f32(float %8, float 0xBFD5555560000000, float 2.000000e+00)
%10 = tail call float @llvm.fmuladd.f32(float %9, float %8, float 0xBFE5555560000000)
%11 = sitofp i32 %5 to float
%12 = fadd float %10, %11
ret float %12
}