Home | History | Annotate | Line # | Download | only in builtins
      1      1.1  joerg /* ===-- floatuntidf.c - Implement __floatuntidf ---------------------------===
      2      1.1  joerg  *
      3      1.1  joerg  *                     The LLVM Compiler Infrastructure
      4      1.1  joerg  *
      5      1.1  joerg  * This file is dual licensed under the MIT and the University of Illinois Open
      6      1.1  joerg  * Source Licenses. See LICENSE.TXT for details.
      7      1.1  joerg  *
      8      1.1  joerg  * ===----------------------------------------------------------------------===
      9      1.1  joerg  *
     10      1.1  joerg  * This file implements __floatuntidf for the compiler_rt library.
     11      1.1  joerg  *
     12      1.1  joerg  * ===----------------------------------------------------------------------===
     13      1.1  joerg  */
     14      1.1  joerg 
     15      1.1  joerg #include "int_lib.h"
     16      1.1  joerg 
     17      1.1  joerg #ifdef CRT_HAS_128BIT
     18      1.1  joerg 
     19      1.1  joerg /* Returns: convert a to a double, rounding toward even. */
     20      1.1  joerg 
     21      1.1  joerg /* Assumption: double is a IEEE 64 bit floating point type
     22      1.1  joerg  *             tu_int is a 128 bit integral type
     23      1.1  joerg  */
     24      1.1  joerg 
     25      1.1  joerg /* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */
     26      1.1  joerg 
     27  1.1.1.2  joerg COMPILER_RT_ABI double
     28      1.1  joerg __floatuntidf(tu_int a)
     29      1.1  joerg {
     30      1.1  joerg     if (a == 0)
     31      1.1  joerg         return 0.0;
     32      1.1  joerg     const unsigned N = sizeof(tu_int) * CHAR_BIT;
     33      1.1  joerg     int sd = N - __clzti2(a);  /* number of significant digits */
     34      1.1  joerg     int e = sd - 1;             /* exponent */
     35      1.1  joerg     if (sd > DBL_MANT_DIG)
     36      1.1  joerg     {
     37      1.1  joerg         /*  start:  0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
     38      1.1  joerg          *  finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
     39      1.1  joerg          *                                                12345678901234567890123456
     40      1.1  joerg          *  1 = msb 1 bit
     41      1.1  joerg          *  P = bit DBL_MANT_DIG-1 bits to the right of 1
     42      1.1  joerg          *  Q = bit DBL_MANT_DIG bits to the right of 1
     43      1.1  joerg          *  R = "or" of all bits to the right of Q
     44      1.1  joerg          */
     45      1.1  joerg         switch (sd)
     46      1.1  joerg         {
     47      1.1  joerg         case DBL_MANT_DIG + 1:
     48      1.1  joerg             a <<= 1;
     49      1.1  joerg             break;
     50      1.1  joerg         case DBL_MANT_DIG + 2:
     51      1.1  joerg             break;
     52      1.1  joerg         default:
     53      1.1  joerg             a = (a >> (sd - (DBL_MANT_DIG+2))) |
     54      1.1  joerg                 ((a & ((tu_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0);
     55      1.1  joerg         };
     56      1.1  joerg         /* finish: */
     57      1.1  joerg         a |= (a & 4) != 0;  /* Or P into R */
     58      1.1  joerg         ++a;  /* round - this step may add a significant bit */
     59      1.1  joerg         a >>= 2;  /* dump Q and R */
     60      1.1  joerg         /* a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits */
     61      1.1  joerg         if (a & ((tu_int)1 << DBL_MANT_DIG))
     62      1.1  joerg         {
     63      1.1  joerg             a >>= 1;
     64      1.1  joerg             ++e;
     65      1.1  joerg         }
     66      1.1  joerg         /* a is now rounded to DBL_MANT_DIG bits */
     67      1.1  joerg     }
     68      1.1  joerg     else
     69      1.1  joerg     {
     70      1.1  joerg         a <<= (DBL_MANT_DIG - sd);
     71      1.1  joerg         /* a is now rounded to DBL_MANT_DIG bits */
     72      1.1  joerg     }
     73      1.1  joerg     double_bits fb;
     74      1.1  joerg     fb.u.s.high = ((e + 1023) << 20)      |        /* exponent */
     75      1.1  joerg                 ((su_int)(a >> 32) & 0x000FFFFF); /* mantissa-high */
     76      1.1  joerg     fb.u.s.low = (su_int)a;                         /* mantissa-low */
     77      1.1  joerg     return fb.f;
     78      1.1  joerg }
     79      1.1  joerg 
     80      1.1  joerg #endif /* CRT_HAS_128BIT */
     81