1214152Sed/* ===-- fixxfti.c - Implement __fixxfti -----------------------------------===
2214152Sed *
3214152Sed *      	       The LLVM Compiler Infrastructure
4214152Sed *
5222656Sed * This file is dual licensed under the MIT and the University of Illinois Open
6222656Sed * Source Licenses. See LICENSE.TXT for details.
7214152Sed *
8214152Sed * ===----------------------------------------------------------------------===
9214152Sed *
10214152Sed * This file implements __fixxfti for the compiler_rt library.
11214152Sed *
12214152Sed * ===----------------------------------------------------------------------===
13214152Sed */
14214152Sed
15239138Sandrew#include "int_lib.h"
16239138Sandrew
17263763Sdim#ifdef CRT_HAS_128BIT
18214152Sed
19214152Sed/* Returns: convert a to a signed long long, rounding toward zero. */
20214152Sed
21214152Sed/* Assumption: long double is an intel 80 bit floating point type padded with 6 bytes
22214152Sed *             su_int is a 32 bit integral type
23214152Sed *             value in long double is representable in ti_int (no range checking performed)
24214152Sed */
25214152Sed
26214152Sed/* gggg gggg gggg gggg gggg gggg gggg gggg | gggg gggg gggg gggg seee eeee eeee eeee |
27214152Sed * 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm
28214152Sed */
29214152Sed
30214152Sedti_int
31214152Sed__fixxfti(long double a)
32214152Sed{
33214152Sed    long_double_bits fb;
34214152Sed    fb.f = a;
35214152Sed    int e = (fb.u.high.s.low & 0x00007FFF) - 16383;
36214152Sed    if (e < 0)
37214152Sed        return 0;
38214152Sed    ti_int s = -(si_int)((fb.u.high.s.low & 0x00008000) >> 15);
39214152Sed    ti_int r = fb.u.low.all;
40214152Sed    if (e > 63)
41214152Sed        r <<= (e - 63);
42214152Sed    else
43214152Sed        r >>= (63 - e);
44214152Sed    return (r ^ s) - s;
45214152Sed}
46214152Sed
47263763Sdim#endif /* CRT_HAS_128BIT */
48