Home | History | Annotate | Download | only in lib
      1 /* ===-- floattixf.c - Implement __floattixf -------------------------------===
      2  *
      3  *      	       The LLVM Compiler Infrastructure
      4  *
      5  * This file is distributed under the University of Illinois Open Source
      6  * License. See LICENSE.TXT for details.
      7  *
      8  * ===----------------------------------------------------------------------===
      9  *
     10  * This file implements __floattixf for the compiler_rt library.
     11  *
     12  * ===----------------------------------------------------------------------===
     13  */
     14 
     15 #if __x86_64
     16 
     17 #include "int_lib.h"
     18 #include <float.h>
     19 
     20 /* Returns: convert a to a long double, rounding toward even. */
     21 
     22 /* Assumption: long double is a IEEE 80 bit floating point type padded to 128 bits
     23  *             ti_int is a 128 bit integral type
     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 si_int __clzti2(ti_int a);
     31 
     32 long double
     33 __floattixf(ti_int a)
     34 {
     35     if (a == 0)
     36         return 0.0;
     37     const unsigned N = sizeof(ti_int) * CHAR_BIT;
     38     const ti_int s = a >> (N-1);
     39     a = (a ^ s) - s;
     40     int sd = N - __clzti2(a);  /* number of significant digits */
     41     int e = sd - 1;             /* exponent */
     42     if (sd > LDBL_MANT_DIG)
     43     {
     44         /*  start:  0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
     45          *  finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
     46          *                                                12345678901234567890123456
     47          *  1 = msb 1 bit
     48          *  P = bit LDBL_MANT_DIG-1 bits to the right of 1
     49          *  Q = bit LDBL_MANT_DIG bits to the right of 1
     50          *  R = "or" of all bits to the right of Q
     51 	      */
     52         switch (sd)
     53         {
     54         case LDBL_MANT_DIG + 1:
     55             a <<= 1;
     56             break;
     57         case LDBL_MANT_DIG + 2:
     58             break;
     59         default:
     60             a = ((tu_int)a >> (sd - (LDBL_MANT_DIG+2))) |
     61                 ((a & ((tu_int)(-1) >> ((N + LDBL_MANT_DIG+2) - sd))) != 0);
     62         };
     63         /* finish: */
     64         a |= (a & 4) != 0;  /* Or P into R */
     65         ++a;  /* round - this step may add a significant bit */
     66         a >>= 2;  /* dump Q and R */
     67         /* a is now rounded to LDBL_MANT_DIG or LDBL_MANT_DIG+1 bits */
     68         if (a & ((tu_int)1 << LDBL_MANT_DIG))
     69         {
     70             a >>= 1;
     71             ++e;
     72         }
     73         /* a is now rounded to LDBL_MANT_DIG bits */
     74     }
     75     else
     76     {
     77         a <<= (LDBL_MANT_DIG - sd);
     78         /* a is now rounded to LDBL_MANT_DIG bits */
     79     }
     80     long_double_bits fb;
     81     fb.u.high.s.low = ((su_int)s & 0x8000) |        /* sign */
     82                     (e + 16383);                  /* exponent */
     83     fb.u.low.all = (du_int)a;                     /* mantissa */
     84     return fb.f;
     85 }
     86 
     87 #endif
     88