void i24_to_double(unsigned char *i24, double *p) {
  int val = (i24[0]) | (i24[1] << 8) | (i24[2] << 16);
  if (val & 0x800000) {
    val |= 0xFF000000;
    *p = ((((double)val)) * (1.0 / (8388608.0)));
  } else {
    val &= 0xFFFFFF;
    *p = ((((double)val)) * (1.0 / (8388608.0)));
  }
}


x86 assembly (clang, -O0):
<i24_to_double>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movzbl (%rax),%eax
mov    -0x8(%rbp),%rcx
movzbl 0x1(%rcx),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
mov    -0x8(%rbp),%rcx
movzbl 0x2(%rcx),%ecx
shl    $0x10,%ecx
or     %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
and    $0x800000,%eax
cmp    $0x0,%eax
je     116a <i24_to_double+0x6a>
mov    -0x14(%rbp),%eax
or     $0xff000000,%eax
mov    %eax,-0x14(%rbp)
cvtsi2sdl -0x14(%rbp),%xmm0
movsd  0xea7(%rip),%xmm1
mulsd  %xmm1,%xmm0
mov    -0x10(%rbp),%rax
movsd  %xmm0,(%rax)
jmp    118e <i24_to_double+0x8e>
mov    -0x14(%rbp),%eax
and    $0xffffff,%eax
mov    %eax,-0x14(%rbp)
cvtsi2sdl -0x14(%rbp),%xmm0
movsd  0xe7e(%rip),%xmm1
mulsd  %xmm1,%xmm0
mov    -0x10(%rbp),%rax
movsd  %xmm0,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<i24_to_double>:
movzwl (%rdi),%eax
movzbl 0x2(%rdi),%ecx
mov    %ecx,%edx
shl    $0x10,%edx
lea    (%rax,%rdx,1),%edi
test   $0x80,%cl
lea    -0x1000000(%rax,%rdx,1),%eax
cmove  %edi,%eax
cvtsi2sd %eax,%xmm0
mulsd  0xed8(%rip),%xmm0
movsd  %xmm0,(%rsi)
ret

LLVM IR (-O0):
define void @i24_to_double(i8* %0, double* %1) {
%3 = alloca i8*
%4 = alloca double*
%5 = alloca i32
store i8* %0, i8** %3
store double* %1, double** %4
%6 = load i8*, i8** %3
%7 = getelementptr inbounds i8, i8* %6, i64 0
%8 = load i8, i8* %7
%9 = zext i8 %8 to i32
%10 = load i8*, i8** %3
%11 = getelementptr inbounds i8, i8* %10, i64 1
%12 = load i8, i8* %11
%13 = zext i8 %12 to i32
%14 = shl i32 %13, 8
%15 = or i32 %9, %14
%16 = load i8*, i8** %3
%17 = getelementptr inbounds i8, i8* %16, i64 2
%18 = load i8, i8* %17
%19 = zext i8 %18 to i32
%20 = shl i32 %19, 16
%21 = or i32 %15, %20
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = and i32 %22, 8388608
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %2
%26 = load i32, i32* %5
%27 = or i32 %26, -16777216
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = sitofp i32 %28 to double
%30 = fmul double %29, 0x3E80000000000000
%31 = load double*, double** %4
store double %30, double* %31
br label %39
32: ; preds = %2
%33 = load i32, i32* %5
%34 = and i32 %33, 16777215
store i32 %34, i32* %5
%35 = load i32, i32* %5
%36 = sitofp i32 %35 to double
%37 = fmul double %36, 0x3E80000000000000
%38 = load double*, double** %4
store double %37, double* %38
br label %39
39: ; preds = %32, %25
ret void
}

LLVM IR (-O2):
define void @i24_to_double(i8* nocapture %0, double* nocapture writeonly %1) {
%3 = load i8, i8* %0
%4 = zext i8 %3 to i32
%5 = getelementptr inbounds i8, i8* %0, i64 1
%6 = load i8, i8* %5
%7 = zext i8 %6 to i32
%8 = shl nuw nsw i32 %7, 8
%9 = or i32 %8, %4
%10 = getelementptr inbounds i8, i8* %0, i64 2
%11 = load i8, i8* %10
%12 = zext i8 %11 to i32
%13 = shl nuw nsw i32 %12, 16
%14 = or i32 %9, %13
%15 = and i32 %12, 128
%16 = icmp eq i32 %15, 0
%17 = or i32 %14, -16777216
%18 = select i1 %16, i32 %14, i32 %17
%19 = sitofp i32 %18 to double
%20 = fmul double %19, 0x3E80000000000000
store double %20, double* %1
ret void
}