Home | History | Annotate | Line # | Download | only in builtins
      1  1.1   joerg /*===-- floatdidf.c - Implement __floatdidf -------------------------------===
      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 __floatdidf 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 /* Returns: convert a to a double, rounding toward even. */
     18  1.1   joerg 
     19  1.1   joerg /* Assumption: double is a IEEE 64 bit floating point type
     20  1.1   joerg  *             di_int is a 64 bit integral type
     21  1.1   joerg  */
     22  1.1   joerg 
     23  1.1   joerg /* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */
     24  1.1   joerg 
     25  1.1   joerg #ifndef __SOFT_FP__
     26  1.1   joerg /* Support for systems that have hardware floating-point; we'll set the inexact flag
     27  1.1   joerg  * as a side-effect of this computation.
     28  1.1   joerg  */
     29  1.1   joerg 
     30  1.1   joerg COMPILER_RT_ABI double
     31  1.1   joerg __floatdidf(di_int a)
     32  1.1   joerg {
     33  1.2  martin 	static const double twop52 = 4503599627370496.0; // 0x1.0p52
     34  1.2  martin 	static const double twop32 = 4294967296.0; // 0x1.0p32
     35  1.1   joerg 
     36  1.1   joerg 	union { int64_t x; double d; } low = { .d = twop52 };
     37  1.1   joerg 
     38  1.1   joerg 	const double high = (int32_t)(a >> 32) * twop32;
     39  1.1   joerg 	low.x |= a & INT64_C(0x00000000ffffffff);
     40  1.1   joerg 
     41  1.1   joerg 	const double result = (high - twop52) + low.d;
     42  1.1   joerg 	return result;
     43  1.1   joerg }
     44  1.1   joerg 
     45  1.1   joerg #else
     46  1.1   joerg /* Support for systems that don't have hardware floating-point; there are no flags to
     47  1.1   joerg  * set, and we don't want to code-gen to an unknown soft-float implementation.
     48  1.1   joerg  */
     49  1.1   joerg 
     50  1.1   joerg COMPILER_RT_ABI double
     51  1.1   joerg __floatdidf(di_int a)
     52  1.1   joerg {
     53  1.1   joerg     if (a == 0)
     54  1.1   joerg         return 0.0;
     55  1.1   joerg     const unsigned N = sizeof(di_int) * CHAR_BIT;
     56  1.1   joerg     const di_int s = a >> (N-1);
     57  1.1   joerg     a = (a ^ s) - s;
     58  1.1   joerg     int sd = N - __builtin_clzll(a);  /* number of significant digits */
     59  1.1   joerg     int e = sd - 1;             /* exponent */
     60  1.1   joerg     if (sd > DBL_MANT_DIG)
     61  1.1   joerg     {
     62  1.1   joerg         /*  start:  0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
     63  1.1   joerg          *  finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
     64  1.1   joerg          *                                                12345678901234567890123456
     65  1.1   joerg          *  1 = msb 1 bit
     66  1.1   joerg          *  P = bit DBL_MANT_DIG-1 bits to the right of 1
     67  1.1   joerg          * Q = bit DBL_MANT_DIG bits to the right of 1
     68  1.1   joerg          *  R = "or" of all bits to the right of Q
     69  1.1   joerg         */
     70  1.1   joerg         switch (sd)
     71  1.1   joerg         {
     72  1.1   joerg         case DBL_MANT_DIG + 1:
     73  1.1   joerg             a <<= 1;
     74  1.1   joerg             break;
     75  1.1   joerg         case DBL_MANT_DIG + 2:
     76  1.1   joerg             break;
     77  1.1   joerg         default:
     78  1.1   joerg             a = ((du_int)a >> (sd - (DBL_MANT_DIG+2))) |
     79  1.1   joerg                 ((a & ((du_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0);
     80  1.1   joerg         };
     81  1.1   joerg         /* finish: */
     82  1.1   joerg         a |= (a & 4) != 0;  /* Or P into R */
     83  1.1   joerg         ++a;  /* round - this step may add a significant bit */
     84  1.1   joerg         a >>= 2;  /* dump Q and R */
     85  1.1   joerg         /* a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits */
     86  1.1   joerg         if (a & ((du_int)1 << DBL_MANT_DIG))
     87  1.1   joerg         {
     88  1.1   joerg             a >>= 1;
     89  1.1   joerg             ++e;
     90  1.1   joerg         }
     91  1.1   joerg         /* a is now rounded to DBL_MANT_DIG bits */
     92  1.1   joerg     }
     93  1.1   joerg     else
     94  1.1   joerg     {
     95  1.1   joerg         a <<= (DBL_MANT_DIG - sd);
     96  1.1   joerg         /* a is now rounded to DBL_MANT_DIG bits */
     97  1.1   joerg     }
     98  1.1   joerg     double_bits fb;
     99  1.2  martin     fb.u.s.high = ((su_int)s & 0x80000000) |        /* sign */
    100  1.1   joerg                 ((e + 1023) << 20)      |        /* exponent */
    101  1.1   joerg                 ((su_int)(a >> 32) & 0x000FFFFF); /* mantissa-high */
    102  1.2  martin     fb.u.s.low = (su_int)a;                         /* mantissa-low */
    103  1.1   joerg     return fb.f;
    104  1.1   joerg }
    105  1.1   joerg #endif
    106  1.3     rin 
    107  1.4     rin #if defined(__ARM_EABI__)
    108  1.5     rin #if defined(COMPILER_RT_ARMHF_TARGET)
    109  1.3     rin AEABI_RTABI double __aeabi_l2d(di_int a) {
    110  1.3     rin   return __floatdidf(a);
    111  1.3     rin }
    112  1.5     rin #else
    113  1.5     rin AEABI_RTABI double __aeabi_l2d(di_int a) COMPILER_RT_ALIAS(__floatdidf);
    114  1.5     rin #endif
    115  1.3     rin #endif
    116