double round(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 < -1)
    return u.i &= 0x8000000000000000, u.f;
  if (exp == -1)
    return u.i1 & 0x80000000 ? -1.0 : 1.0;
  if (u.i & (0x0008000000000000 >> exp))
    u.i += 0x0010000000000000 >> exp;
  return u.i &= ~(0x000FFFFFFFFFFFFF >> exp), u.f;
}


x86 assembly (clang, -O0):
<round>:
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 <round+0x3f>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    121e <round+0x11e>
cmpl   $0xffffffff,-0x1c(%rbp)
jge    116a <round+0x6a>
movabs $0x8000000000000000,%rax
and    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    121e <round+0x11e>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11b8 <round+0xb8>
mov    -0x14(%rbp),%eax
and    $0x80000000,%eax
movsd  0xe7c(%rip),%xmm0
movsd  0xe7c(%rip),%xmm1
movsd  %xmm1,-0x30(%rbp)
cmp    $0x0,%eax
movsd  %xmm0,-0x28(%rbp)
jne    11a9 <round+0xa9>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    121e <round+0x11e>
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movabs $0x8000000000000,%rdx
sar    %cl,%rdx
mov    %rdx,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
je     11f6 <round+0xf6>
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
movabs $0x10000000000000,%rax
sar    %cl,%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
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):
<round>:
movq   %xmm0,%rax
mov    %rax,%rcx
shr    $0x34,%rcx
and    $0x7ff,%ecx
cmp    $0x433,%ecx
ja     1176 <round+0x76>
cmp    $0x3fd,%ecx
ja     1135 <round+0x35>
movabs $0x8000000000000000,%rcx
and    %rcx,%rax
movq   %rax,%xmm0
ret
add    $0xfffffc01,%ecx
cmp    $0xffffffff,%ecx
je     1177 <round+0x77>
mov    %rax,%rdx
shl    %cl,%rdx
shl    $0xc,%rdx
sar    $0x3f,%rdx
movabs $0x10000000000000,%rsi
shr    %cl,%rsi
movabs $0xfff0000000000000,%rdi
sar    %cl,%rdi
and    %rdx,%rsi
add    %rax,%rsi
and    %rdi,%rsi
movq   %rsi,%xmm0
ret
pand   0xe81(%rip),%xmm0
por    0xe89(%rip),%xmm0
ret

LLVM IR (-O0):
%union.anon = type { double }
%struct.anon = type { i32, i32 }
define double @round(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 %63
18: ; preds = %1
%19 = load i32, i32* %5
%20 = icmp slt i32 %19, -1
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 %63
27: ; preds = %18
%28 = load i32, i32* %5
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %38
30: ; preds = %27
%31 = bitcast %union.anon* %4 to %struct.anon*
%32 = getelementptr inbounds %struct.anon, %struct.anon* %31, i32 0, i32 1
%33 = load i32, i32* %32
%34 = and i32 %33, -2147483648
%35 = icmp ne i32 %34, 0
%36 = zext i1 %35 to i64
%37 = select i1 %35, double -1.000000e+00, double 1.000000e+00
store double %37, double* %2
br label %63
38: ; preds = %27
%39 = bitcast %union.anon* %4 to i64*
%40 = load i64, i64* %39
%41 = load i32, i32* %5
%42 = zext i32 %41 to i64
%43 = ashr i64 2251799813685248, %42
%44 = and i64 %40, %43
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %53
46: ; preds = %38
%47 = load i32, i32* %5
%48 = zext i32 %47 to i64
%49 = ashr i64 4503599627370496, %48
%50 = bitcast %union.anon* %4 to i64*
%51 = load i64, i64* %50
%52 = add nsw i64 %51, %49
store i64 %52, i64* %50
br label %53
53: ; preds = %46, %38
%54 = load i32, i32* %5
%55 = zext i32 %54 to i64
%56 = ashr i64 4503599627370495, %55
%57 = xor i64 %56, -1
%58 = bitcast %union.anon* %4 to i64*
%59 = load i64, i64* %58
%60 = and i64 %59, %57
store i64 %60, i64* %58
%61 = bitcast %union.anon* %4 to double*
%62 = load double, double* %61
store double %62, double* %2
br label %63
63: ; preds = %53, %30, %21, %16
%64 = load double, double* %2
ret double %64
}

LLVM IR (-O2):
define double @round(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 %28, label %8
8: ; preds = %1
%9 = icmp ult i32 %5, 1022
br i1 %9, label %10, label %13
10: ; preds = %8
%11 = and i64 %2, -9223372036854775808
%12 = bitcast i64 %11 to double
br label %28
13: ; preds = %8
%14 = icmp eq i32 %6, -1
br i1 %14, label %15, label %17
15: ; preds = %13
%16 = tail call double @llvm.copysign.f64(double 1.000000e+00, double %0)
br label %28
17: ; preds = %13
%18 = zext i32 %6 to i64
%19 = lshr i64 2251799813685248, %18
%20 = and i64 %19, %2
%21 = icmp eq i64 %20, 0
%22 = lshr i64 4503599627370496, %18
%23 = ashr i64 -4503599627370496, %18
%24 = select i1 %21, i64 0, i64 %22
%25 = add nsw i64 %24, %2
%26 = and i64 %25, %23
%27 = bitcast i64 %26 to double
br label %28
28: ; preds = %1, %17, %15, %10
%29 = phi double [ %12, %10 ], [ %16, %15 ], [ %27, %17 ], [ %0, %1 ]
ret double %29
}