1*0a6a1f1dSLionel Sambuc /*===-- floatdidf.c - Implement __floatdidf -------------------------------=== 2*0a6a1f1dSLionel Sambuc * 3*0a6a1f1dSLionel Sambuc * The LLVM Compiler Infrastructure 4*0a6a1f1dSLionel Sambuc * 5*0a6a1f1dSLionel Sambuc * This file is dual licensed under the MIT and the University of Illinois Open 6*0a6a1f1dSLionel Sambuc * Source Licenses. See LICENSE.TXT for details. 7*0a6a1f1dSLionel Sambuc * 8*0a6a1f1dSLionel Sambuc *===----------------------------------------------------------------------=== 9*0a6a1f1dSLionel Sambuc * 10*0a6a1f1dSLionel Sambuc * This file implements __floatdidf for the compiler_rt library. 11*0a6a1f1dSLionel Sambuc * 12*0a6a1f1dSLionel Sambuc *===----------------------------------------------------------------------=== 13*0a6a1f1dSLionel Sambuc */ 14*0a6a1f1dSLionel Sambuc 15*0a6a1f1dSLionel Sambuc #include "int_lib.h" 16*0a6a1f1dSLionel Sambuc 17*0a6a1f1dSLionel Sambuc /* Returns: convert a to a double, rounding toward even. */ 18*0a6a1f1dSLionel Sambuc 19*0a6a1f1dSLionel Sambuc /* Assumption: double is a IEEE 64 bit floating point type 20*0a6a1f1dSLionel Sambuc * di_int is a 64 bit integral type 21*0a6a1f1dSLionel Sambuc */ 22*0a6a1f1dSLionel Sambuc 23*0a6a1f1dSLionel Sambuc /* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */ 24*0a6a1f1dSLionel Sambuc ARM_EABI_FNALIAS(l2d,floatdidf)25*0a6a1f1dSLionel SambucARM_EABI_FNALIAS(l2d, floatdidf) 26*0a6a1f1dSLionel Sambuc 27*0a6a1f1dSLionel Sambuc #ifndef __SOFT_FP__ 28*0a6a1f1dSLionel Sambuc /* Support for systems that have hardware floating-point; we'll set the inexact flag 29*0a6a1f1dSLionel Sambuc * as a side-effect of this computation. 30*0a6a1f1dSLionel Sambuc */ 31*0a6a1f1dSLionel Sambuc 32*0a6a1f1dSLionel Sambuc COMPILER_RT_ABI double 33*0a6a1f1dSLionel Sambuc __floatdidf(di_int a) 34*0a6a1f1dSLionel Sambuc { 35*0a6a1f1dSLionel Sambuc static const double twop52 = 0x1.0p52; 36*0a6a1f1dSLionel Sambuc static const double twop32 = 0x1.0p32; 37*0a6a1f1dSLionel Sambuc 38*0a6a1f1dSLionel Sambuc union { int64_t x; double d; } low = { .d = twop52 }; 39*0a6a1f1dSLionel Sambuc 40*0a6a1f1dSLionel Sambuc const double high = (int32_t)(a >> 32) * twop32; 41*0a6a1f1dSLionel Sambuc low.x |= a & INT64_C(0x00000000ffffffff); 42*0a6a1f1dSLionel Sambuc 43*0a6a1f1dSLionel Sambuc const double result = (high - twop52) + low.d; 44*0a6a1f1dSLionel Sambuc return result; 45*0a6a1f1dSLionel Sambuc } 46*0a6a1f1dSLionel Sambuc 47*0a6a1f1dSLionel Sambuc #else 48*0a6a1f1dSLionel Sambuc /* Support for systems that don't have hardware floating-point; there are no flags to 49*0a6a1f1dSLionel Sambuc * set, and we don't want to code-gen to an unknown soft-float implementation. 50*0a6a1f1dSLionel Sambuc */ 51*0a6a1f1dSLionel Sambuc 52*0a6a1f1dSLionel Sambuc COMPILER_RT_ABI double 53*0a6a1f1dSLionel Sambuc __floatdidf(di_int a) 54*0a6a1f1dSLionel Sambuc { 55*0a6a1f1dSLionel Sambuc if (a == 0) 56*0a6a1f1dSLionel Sambuc return 0.0; 57*0a6a1f1dSLionel Sambuc const unsigned N = sizeof(di_int) * CHAR_BIT; 58*0a6a1f1dSLionel Sambuc const di_int s = a >> (N-1); 59*0a6a1f1dSLionel Sambuc a = (a ^ s) - s; 60*0a6a1f1dSLionel Sambuc int sd = N - __builtin_clzll(a); /* number of significant digits */ 61*0a6a1f1dSLionel Sambuc int e = sd - 1; /* exponent */ 62*0a6a1f1dSLionel Sambuc if (sd > DBL_MANT_DIG) 63*0a6a1f1dSLionel Sambuc { 64*0a6a1f1dSLionel Sambuc /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx 65*0a6a1f1dSLionel Sambuc * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR 66*0a6a1f1dSLionel Sambuc * 12345678901234567890123456 67*0a6a1f1dSLionel Sambuc * 1 = msb 1 bit 68*0a6a1f1dSLionel Sambuc * P = bit DBL_MANT_DIG-1 bits to the right of 1 69*0a6a1f1dSLionel Sambuc * Q = bit DBL_MANT_DIG bits to the right of 1 70*0a6a1f1dSLionel Sambuc * R = "or" of all bits to the right of Q 71*0a6a1f1dSLionel Sambuc */ 72*0a6a1f1dSLionel Sambuc switch (sd) 73*0a6a1f1dSLionel Sambuc { 74*0a6a1f1dSLionel Sambuc case DBL_MANT_DIG + 1: 75*0a6a1f1dSLionel Sambuc a <<= 1; 76*0a6a1f1dSLionel Sambuc break; 77*0a6a1f1dSLionel Sambuc case DBL_MANT_DIG + 2: 78*0a6a1f1dSLionel Sambuc break; 79*0a6a1f1dSLionel Sambuc default: 80*0a6a1f1dSLionel Sambuc a = ((du_int)a >> (sd - (DBL_MANT_DIG+2))) | 81*0a6a1f1dSLionel Sambuc ((a & ((du_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0); 82*0a6a1f1dSLionel Sambuc }; 83*0a6a1f1dSLionel Sambuc /* finish: */ 84*0a6a1f1dSLionel Sambuc a |= (a & 4) != 0; /* Or P into R */ 85*0a6a1f1dSLionel Sambuc ++a; /* round - this step may add a significant bit */ 86*0a6a1f1dSLionel Sambuc a >>= 2; /* dump Q and R */ 87*0a6a1f1dSLionel Sambuc /* a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits */ 88*0a6a1f1dSLionel Sambuc if (a & ((du_int)1 << DBL_MANT_DIG)) 89*0a6a1f1dSLionel Sambuc { 90*0a6a1f1dSLionel Sambuc a >>= 1; 91*0a6a1f1dSLionel Sambuc ++e; 92*0a6a1f1dSLionel Sambuc } 93*0a6a1f1dSLionel Sambuc /* a is now rounded to DBL_MANT_DIG bits */ 94*0a6a1f1dSLionel Sambuc } 95*0a6a1f1dSLionel Sambuc else 96*0a6a1f1dSLionel Sambuc { 97*0a6a1f1dSLionel Sambuc a <<= (DBL_MANT_DIG - sd); 98*0a6a1f1dSLionel Sambuc /* a is now rounded to DBL_MANT_DIG bits */ 99*0a6a1f1dSLionel Sambuc } 100*0a6a1f1dSLionel Sambuc double_bits fb; 101*0a6a1f1dSLionel Sambuc fb.u.high = ((su_int)s & 0x80000000) | /* sign */ 102*0a6a1f1dSLionel Sambuc ((e + 1023) << 20) | /* exponent */ 103*0a6a1f1dSLionel Sambuc ((su_int)(a >> 32) & 0x000FFFFF); /* mantissa-high */ 104*0a6a1f1dSLionel Sambuc fb.u.low = (su_int)a; /* mantissa-low */ 105*0a6a1f1dSLionel Sambuc return fb.f; 106*0a6a1f1dSLionel Sambuc } 107*0a6a1f1dSLionel Sambuc #endif 108