double trunc(double x) {
  union {
    double f;
    long i;
    struct {
      int i0, i1;
    };
  } u = {x};
  int exp = ((u.i1 >> 20) & 0x7FF) - 1023;
  if (exp >= 53)
    return x;
  if (exp < 0)
    return u.i &= 0x8000000000000000, u.f;
  return u.i &= ~(0x000FFFFFFFFFFFFF >> exp), u.f;
}


x86 assembly (clang, -O0):
<trunc>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x14(%rbp),%eax
sar    $0x14,%eax
and    $0x7ff,%eax
sub    $0x3ff,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x35,-0x1c(%rbp)
jl     113f <trunc+0x3f>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1192 <trunc+0x92>
cmpl   $0x0,-0x1c(%rbp)
jge    116a <trunc+0x6a>
movabs $0x8000000000000000,%rax
and    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1192 <trunc+0x92>
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
movabs $0xfffffffffffff,%rax
sar    %cl,%rax
xor    $0xffffffffffffffff,%rax
and    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<trunc>:
movq   %xmm0,%rax
mov    %rax,%rcx
shr    $0x34,%rcx
and    $0x7ff,%ecx
cmp    $0x433,%ecx
ja     1150 <trunc+0x50>
cmp    $0x3fe,%ecx
ja     1135 <trunc+0x35>
movabs $0x8000000000000000,%rcx
and    %rcx,%rax
movq   %rax,%xmm0
ret
add    $0xfffffc01,%ecx
movabs $0xfff0000000000000,%rdx
sar    %cl,%rdx
and    %rax,%rdx
movq   %rdx,%xmm0
ret

LLVM IR (-O0):
%union.anon = type { double }
%struct.anon = type { i32, i32 }
define double @trunc(double %0) {
%2 = alloca double
%3 = alloca double
%4 = alloca %union.anon
%5 = alloca i32
store double %0, double* %3
%6 = bitcast %union.anon* %4 to double*
%7 = load double, double* %3
store double %7, double* %6
%8 = bitcast %union.anon* %4 to %struct.anon*
%9 = getelementptr inbounds %struct.anon, %struct.anon* %8, i32 0, i32 1
%10 = load i32, i32* %9
%11 = ashr i32 %10, 20
%12 = and i32 %11, 2047
%13 = sub nsw i32 %12, 1023
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = icmp sge i32 %14, 53
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = load double, double* %3
store double %17, double* %2
br label %37
18: ; preds = %1
%19 = load i32, i32* %5
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %18
%22 = bitcast %union.anon* %4 to i64*
%23 = load i64, i64* %22
%24 = and i64 %23, -9223372036854775808
store i64 %24, i64* %22
%25 = bitcast %union.anon* %4 to double*
%26 = load double, double* %25
store double %26, double* %2
br label %37
27: ; preds = %18
%28 = load i32, i32* %5
%29 = zext i32 %28 to i64
%30 = ashr i64 4503599627370495, %29
%31 = xor i64 %30, -1
%32 = bitcast %union.anon* %4 to i64*
%33 = load i64, i64* %32
%34 = and i64 %33, %31
store i64 %34, i64* %32
%35 = bitcast %union.anon* %4 to double*
%36 = load double, double* %35
store double %36, double* %2
br label %37
37: ; preds = %27, %21, %16
%38 = load double, double* %2
ret double %38
}

LLVM IR (-O2):
define double @trunc(double %0) {
%2 = bitcast double %0 to i64
%3 = lshr i64 %2, 52
%4 = trunc i64 %3 to i32
%5 = and i32 %4, 2047
%6 = add nsw i32 %5, -1023
%7 = icmp ugt i32 %5, 1075
br i1 %7, label %18, label %8
8: ; preds = %1
%9 = icmp ult i32 %5, 1023
br i1 %9, label %10, label %13
10: ; preds = %8
%11 = and i64 %2, -9223372036854775808
%12 = bitcast i64 %11 to double
br label %18
13: ; preds = %8
%14 = zext i32 %6 to i64
%15 = ashr i64 -4503599627370496, %14
%16 = and i64 %15, %2
%17 = bitcast i64 %16 to double
br label %18
18: ; preds = %1, %13, %10
%19 = phi double [ %12, %10 ], [ %17, %13 ], [ %0, %1 ]
ret double %19
}