double ldbl_nearbyint(double a) {
  double two52 = 0x1p52;
  if (__glibc_likely((__builtin_fabs(a) < two52))) {
    if (__glibc_likely((a > 0.0))) {
      a += two52;
      a -= two52;
    } else if (__glibc_likely((a < 0.0))) {
      a = two52 - a;
      a = -(a - two52);
    }
  }
  return a;
}


x86 assembly (clang, -O0):
<ldbl_nearbyint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  0xeeb(%rip),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x8(%rbp),%xmm1
movaps 0xeca(%rip),%xmm0
pand   %xmm0,%xmm1
movsd  -0x10(%rbp),%xmm0
ucomisd %xmm1,%xmm0
seta   %al
and    $0x1,%al
movzbl %al,%edi
mov    $0x0,%al
call   1030 <__glibc_likely@plt>
cmp    $0x0,%eax
je     1205 <ldbl_nearbyint+0xf5>
movsd  -0x8(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
seta   %al
and    $0x1,%al
movzbl %al,%edi
mov    $0x0,%al
call   1030 <__glibc_likely@plt>
cmp    $0x0,%eax
je     11a6 <ldbl_nearbyint+0x96>
movsd  -0x10(%rbp),%xmm0
addsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x10(%rbp),%xmm1
movsd  -0x8(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1200 <ldbl_nearbyint+0xf0>
xorps  %xmm0,%xmm0
ucomisd -0x8(%rbp),%xmm0
seta   %al
and    $0x1,%al
movzbl %al,%edi
mov    $0x0,%al
call   1030 <__glibc_likely@plt>
cmp    $0x0,%eax
je     11fb <ldbl_nearbyint+0xeb>
movsd  -0x10(%rbp),%xmm0
subsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1200 <ldbl_nearbyint+0xf0>
jmp    1205 <ldbl_nearbyint+0xf5>
movsd  -0x8(%rbp),%xmm0
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ldbl_nearbyint>:
sub    $0x18,%rsp
movapd 0xee4(%rip),%xmm1
movapd %xmm0,(%rsp)
andpd  %xmm0,%xmm1
movsd  0xef3(%rip),%xmm0
xor    %edi,%edi
ucomisd %xmm1,%xmm0
seta   %dil
xor    %eax,%eax
call   1030 <__glibc_likely@plt>
test   %eax,%eax
je     11b6 <ldbl_nearbyint+0xa6>
xorpd  %xmm1,%xmm1
xor    %edi,%edi
movapd (%rsp),%xmm0
ucomisd %xmm1,%xmm0
seta   %dil
xor    %eax,%eax
call   1030 <__glibc_likely@plt>
test   %eax,%eax
je     117a <ldbl_nearbyint+0x6a>
movapd (%rsp),%xmm0
addsd  0xeb3(%rip),%xmm0
addsd  0xeb3(%rip),%xmm0
add    $0x18,%rsp
ret
xor    %edi,%edi
xorpd  %xmm0,%xmm0
ucomisd (%rsp),%xmm0
seta   %dil
xor    %eax,%eax
call   1030 <__glibc_likely@plt>
test   %eax,%eax
je     11b6 <ldbl_nearbyint+0xa6>
movsd  0xe84(%rip),%xmm0
subsd  (%rsp),%xmm0
addsd  0xe7f(%rip),%xmm0
xorpd  0xe5f(%rip),%xmm0
add    $0x18,%rsp
ret
movaps (%rsp),%xmm0
add    $0x18,%rsp
ret

LLVM IR (-O0):
define double @ldbl_nearbyint(double %0) {
%2 = alloca double
%3 = alloca double
store double %0, double* %2
store double 0x4330000000000000, double* %3
%4 = load double, double* %2
%5 = call double @llvm.fabs.f64(double %4)
%6 = load double, double* %3
%7 = fcmp olt double %5, %6
%8 = zext i1 %7 to i32
%9 = call i32 (i32, ...) bitcast (i32 (...)* @__glibc_likely to i32 (i32, ...)*)(i32 %8)
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %40
11: ; preds = %1
%12 = load double, double* %2
%13 = fcmp ogt double %12, 0.000000e+00
%14 = zext i1 %13 to i32
%15 = call i32 (i32, ...) bitcast (i32 (...)* @__glibc_likely to i32 (i32, ...)*)(i32 %14)
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %11
%18 = load double, double* %3
%19 = load double, double* %2
%20 = fadd double %19, %18
store double %20, double* %2
%21 = load double, double* %3
%22 = load double, double* %2
%23 = fsub double %22, %21
store double %23, double* %2
br label %39
24: ; preds = %11
%25 = load double, double* %2
%26 = fcmp olt double %25, 0.000000e+00
%27 = zext i1 %26 to i32
%28 = call i32 (i32, ...) bitcast (i32 (...)* @__glibc_likely to i32 (i32, ...)*)(i32 %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %38
30: ; preds = %24
%31 = load double, double* %3
%32 = load double, double* %2
%33 = fsub double %31, %32
store double %33, double* %2
%34 = load double, double* %2
%35 = load double, double* %3
%36 = fsub double %34, %35
%37 = fneg double %36
store double %37, double* %2
br label %38
38: ; preds = %30, %24
br label %39
39: ; preds = %38, %17
br label %40
40: ; preds = %39, %1
%41 = load double, double* %2
ret double %41
}

LLVM IR (-O2):
define double @ldbl_nearbyint(double %0) {
%2 = tail call double @llvm.fabs.f64(double %0)
%3 = fcmp olt double %2, 0x4330000000000000
%4 = zext i1 %3 to i32
%5 = tail call i32 (i32, ...) bitcast (i32 (...)* @__glibc_likely to i32 (i32, ...)*)(i32 %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %24, label %7
7: ; preds = %1
%8 = fcmp ogt double %0, 0.000000e+00
%9 = zext i1 %8 to i32
%10 = tail call i32 (i32, ...) bitcast (i32 (...)* @__glibc_likely to i32 (i32, ...)*)(i32 %9)
%11 = icmp eq i32 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %7
%13 = fadd double %0, 0x4330000000000000
%14 = fadd double %13, 0xC330000000000000
br label %24
15: ; preds = %7
%16 = fcmp olt double %0, 0.000000e+00
%17 = zext i1 %16 to i32
%18 = tail call i32 (i32, ...) bitcast (i32 (...)* @__glibc_likely to i32 (i32, ...)*)(i32 %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %15
%21 = fsub double 0x4330000000000000, %0
%22 = fadd double %21, 0xC330000000000000
%23 = fneg double %22
br label %24
24: ; preds = %12, %20, %15, %1
%25 = phi double [ %14, %12 ], [ %23, %20 ], [ %0, %15 ], [ %0, %1 ]
ret double %25
}