Home | History | Annotate | Download | only in builtins
      1 /* ===-- fixxfti.c - Implement __fixxfti -----------------------------------===
      2  *
      3  *      	       The LLVM Compiler Infrastructure
      4  *
      5  * This file is dual licensed under the MIT and the University of Illinois Open
      6  * Source Licenses. See LICENSE.TXT for details.
      7  *
      8  * ===----------------------------------------------------------------------===
      9  *
     10  * This file implements __fixxfti for the compiler_rt library.
     11  *
     12  * ===----------------------------------------------------------------------===
     13  */
     14 
     15 #include "int_lib.h"
     16 
     17 #ifdef CRT_HAS_128BIT
     18 
     19 /* Returns: convert a to a signed long long, rounding toward zero. */
     20 
     21 /* Assumption: long double is an intel 80 bit floating point type padded with 6 bytes
     22  *             ti_int is a 128 bit integral type
     23  *             value in long double is representable in ti_int
     24  */
     25 
     26 /* gggg gggg gggg gggg gggg gggg gggg gggg | gggg gggg gggg gggg seee eeee eeee eeee |
     27  * 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm
     28  */
     29 
     30 COMPILER_RT_ABI ti_int
     31 __fixxfti(long double a)
     32 {
     33     const ti_int ti_max = (ti_int)((~(tu_int)0) / 2);
     34     const ti_int ti_min = -ti_max - 1;
     35     long_double_bits fb;
     36     fb.f = a;
     37     int e = (fb.u.high.s.low & 0x00007FFF) - 16383;
     38     if (e < 0)
     39         return 0;
     40     ti_int s = -(si_int)((fb.u.high.s.low & 0x00008000) >> 15);
     41     ti_int r = fb.u.low.all;
     42     if ((unsigned)e >= sizeof(ti_int) * CHAR_BIT)
     43         return a > 0 ? ti_max : ti_min;
     44     if (e > 63)
     45         r <<= (e - 63);
     46     else
     47         r >>= (63 - e);
     48     return (r ^ s) - s;
     49 }
     50 
     51 #endif /* CRT_HAS_128BIT */
     52