Home | History | Annotate | Line # | Download | only in builtins
      1      1.1  joerg /* ===-- floatuntixf.c - Implement __floatuntixf ---------------------------===
      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 __floatuntixf 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 long double, rounding toward even. */
     20      1.1  joerg 
     21      1.1  joerg /* Assumption: long double is a IEEE 80 bit floating point type padded to 128 bits
     22      1.1  joerg  *             tu_int is a 128 bit integral type
     23      1.1  joerg  */
     24      1.1  joerg 
     25      1.1  joerg /* gggg gggg gggg gggg gggg gggg gggg gggg | gggg gggg gggg gggg seee eeee eeee eeee |
     26      1.1  joerg  * 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm
     27      1.1  joerg  */
     28      1.1  joerg 
     29  1.1.1.2  joerg COMPILER_RT_ABI long double
     30      1.1  joerg __floatuntixf(tu_int a)
     31      1.1  joerg {
     32      1.1  joerg     if (a == 0)
     33      1.1  joerg         return 0.0;
     34      1.1  joerg     const unsigned N = sizeof(tu_int) * CHAR_BIT;
     35      1.1  joerg     int sd = N - __clzti2(a);  /* number of significant digits */
     36      1.1  joerg     int e = sd - 1;             /* exponent */
     37      1.1  joerg     if (sd > LDBL_MANT_DIG)
     38      1.1  joerg     {
     39      1.1  joerg         /*  start:  0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
     40      1.1  joerg          *  finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
     41      1.1  joerg          *                                                12345678901234567890123456
     42      1.1  joerg          *  1 = msb 1 bit
     43      1.1  joerg          *  P = bit LDBL_MANT_DIG-1 bits to the right of 1
     44      1.1  joerg          *  Q = bit LDBL_MANT_DIG bits to the right of 1
     45      1.1  joerg          *  R = "or" of all bits to the right of Q
     46      1.1  joerg 	 */
     47      1.1  joerg         switch (sd)
     48      1.1  joerg         {
     49      1.1  joerg         case LDBL_MANT_DIG + 1:
     50      1.1  joerg             a <<= 1;
     51      1.1  joerg             break;
     52      1.1  joerg         case LDBL_MANT_DIG + 2:
     53      1.1  joerg             break;
     54      1.1  joerg         default:
     55      1.1  joerg             a = (a >> (sd - (LDBL_MANT_DIG+2))) |
     56      1.1  joerg                 ((a & ((tu_int)(-1) >> ((N + LDBL_MANT_DIG+2) - sd))) != 0);
     57      1.1  joerg         };
     58      1.1  joerg         /* finish: */
     59      1.1  joerg         a |= (a & 4) != 0;  /* Or P into R */
     60      1.1  joerg         ++a;  /* round - this step may add a significant bit */
     61      1.1  joerg         a >>= 2;  /* dump Q and R */
     62      1.1  joerg         /* a is now rounded to LDBL_MANT_DIG or LDBL_MANT_DIG+1 bits */
     63      1.1  joerg         if (a & ((tu_int)1 << LDBL_MANT_DIG))
     64      1.1  joerg         {
     65      1.1  joerg             a >>= 1;
     66      1.1  joerg             ++e;
     67      1.1  joerg         }
     68      1.1  joerg         /* a is now rounded to LDBL_MANT_DIG bits */
     69      1.1  joerg     }
     70      1.1  joerg     else
     71      1.1  joerg     {
     72      1.1  joerg         a <<= (LDBL_MANT_DIG - sd);
     73      1.1  joerg         /* a is now rounded to LDBL_MANT_DIG bits */
     74      1.1  joerg     }
     75      1.1  joerg     long_double_bits fb;
     76      1.1  joerg     fb.u.high.s.low = (e + 16383);                  /* exponent */
     77      1.1  joerg     fb.u.low.all = (du_int)a;                     /* mantissa */
     78      1.1  joerg     return fb.f;
     79      1.1  joerg }
     80      1.1  joerg 
     81      1.1  joerg #endif
     82