long double truncl(long double x) {
  union {
    long 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):
<truncl>:
push   %rbp
mov    %rsp,%rbp
fldt   0x10(%rbp)
fstpt  -0x20(%rbp)
fldt   -0x20(%rbp)
fstpt  -0x30(%rbp)
mov    -0x2c(%rbp),%eax
sar    $0x14,%eax
and    $0x7ff,%eax
sub    $0x3ff,%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x35,-0x34(%rbp)
jl     1138 <truncl+0x38>
fldt   -0x20(%rbp)
fstpt  -0x10(%rbp)
jmp    1189 <truncl+0x89>
cmpl   $0x0,-0x34(%rbp)
jge    1162 <truncl+0x62>
mov    -0x30(%rbp),%rax
movabs $0x8000000000000000,%rcx
and    %rcx,%rax
mov    %rax,-0x30(%rbp)
fldt   -0x30(%rbp)
fstpt  -0x10(%rbp)
jmp    1189 <truncl+0x89>
mov    -0x34(%rbp),%cl
movabs $0xfffffffffffff,%rax
shr    %cl,%rax
mov    %rax,%rcx
not    %rcx
mov    -0x30(%rbp),%rax
and    %rcx,%rax
mov    %rax,-0x30(%rbp)
fldt   -0x30(%rbp)
fstpt  -0x10(%rbp)
fldt   -0x10(%rbp)
pop    %rbp
ret

x86 assembly (clang, -O2):
<truncl>:
fldt   0x8(%rsp)
fld    %st(0)
fstpt  -0x18(%rsp)
mov    -0x18(%rsp),%rax
mov    %rax,%rcx
shr    $0x34,%rcx
and    $0x7ff,%ecx
cmp    $0x433,%ecx
ja     1172 <truncl+0x72>
fstp   %st(0)
mov    -0x10(%rsp),%edx
cmp    $0x3fe,%ecx
ja     114e <truncl+0x4e>
movabs $0x8000000000000000,%rcx
and    %rcx,%rax
mov    %rax,-0x38(%rsp)
mov    %dx,-0x30(%rsp)
fldt   -0x38(%rsp)
ret
add    $0xfffffc01,%ecx
movabs $0xfff0000000000000,%rsi
sar    %cl,%rsi
and    %rax,%rsi
mov    %dx,-0x20(%rsp)
mov    %rsi,-0x28(%rsp)
fldt   -0x28(%rsp)
ret

LLVM IR (-O0):
%union.anon = type { x86_fp80 }
%struct.anon = type { i32, i32 }
define x86_fp80 @truncl(x86_fp80 %0) {
%2 = alloca x86_fp80
%3 = alloca x86_fp80
%4 = alloca %union.anon
%5 = alloca i32
store x86_fp80 %0, x86_fp80* %3
%6 = bitcast %union.anon* %4 to x86_fp80*
%7 = load x86_fp80, x86_fp80* %3
store x86_fp80 %7, x86_fp80* %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 x86_fp80, x86_fp80* %3
store x86_fp80 %17, x86_fp80* %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 x86_fp80*
%26 = load x86_fp80, x86_fp80* %25
store x86_fp80 %26, x86_fp80* %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 x86_fp80*
%36 = load x86_fp80, x86_fp80* %35
store x86_fp80 %36, x86_fp80* %2
br label %37
37: ; preds = %27, %21, %16
%38 = load x86_fp80, x86_fp80* %2
ret x86_fp80 %38
}

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