Path: blob/master/libs/compiler-rt/lib/builtins/floatdidf.c
4395 views
/*===-- floatdidf.c - Implement __floatdidf -------------------------------===1*2* The LLVM Compiler Infrastructure3*4* This file is dual licensed under the MIT and the University of Illinois Open5* Source Licenses. See LICENSE.TXT for details.6*7*===----------------------------------------------------------------------===8*9* This file implements __floatdidf for the compiler_rt library.10*11*===----------------------------------------------------------------------===12*/1314#include "int_lib.h"1516/* Returns: convert a to a double, rounding toward even. */1718/* Assumption: double is a IEEE 64 bit floating point type19* di_int is a 64 bit integral type20*/2122/* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */2324#ifndef __SOFT_FP__25/* Support for systems that have hardware floating-point; we'll set the inexact flag26* as a side-effect of this computation.27*/2829COMPILER_RT_ABI double30__floatdidf(di_int a)31{32static const double twop52 = 4503599627370496.0; // 0x1.0p5233static const double twop32 = 4294967296.0; // 0x1.0p323435union { int64_t x; double d; } low = { .d = twop52 };3637const double high = (int32_t)(a >> 32) * twop32;38low.x |= a & INT64_C(0x00000000ffffffff);3940const double result = (high - twop52) + low.d;41return result;42}4344#else45/* Support for systems that don't have hardware floating-point; there are no flags to46* set, and we don't want to code-gen to an unknown soft-float implementation.47*/4849COMPILER_RT_ABI double50__floatdidf(di_int a)51{52if (a == 0)53return 0.0;54const unsigned N = sizeof(di_int) * CHAR_BIT;55const di_int s = a >> (N-1);56a = (a ^ s) - s;57int sd = N - __builtin_clzll(a); /* number of significant digits */58int e = sd - 1; /* exponent */59if (sd > DBL_MANT_DIG)60{61/* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx62* finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR63* 1234567890123456789012345664* 1 = msb 1 bit65* P = bit DBL_MANT_DIG-1 bits to the right of 166* Q = bit DBL_MANT_DIG bits to the right of 167* R = "or" of all bits to the right of Q68*/69switch (sd)70{71case DBL_MANT_DIG + 1:72a <<= 1;73break;74case DBL_MANT_DIG + 2:75break;76default:77a = ((du_int)a >> (sd - (DBL_MANT_DIG+2))) |78((a & ((du_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0);79};80/* finish: */81a |= (a & 4) != 0; /* Or P into R */82++a; /* round - this step may add a significant bit */83a >>= 2; /* dump Q and R */84/* a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits */85if (a & ((du_int)1 << DBL_MANT_DIG))86{87a >>= 1;88++e;89}90/* a is now rounded to DBL_MANT_DIG bits */91}92else93{94a <<= (DBL_MANT_DIG - sd);95/* a is now rounded to DBL_MANT_DIG bits */96}97double_bits fb;98fb.u.s.high = ((su_int)s & 0x80000000) | /* sign */99((e + 1023) << 20) | /* exponent */100((su_int)(a >> 32) & 0x000FFFFF); /* mantissa-high */101fb.u.s.low = (su_int)a; /* mantissa-low */102return fb.f;103}104#endif105106#if defined(__ARM_EABI__)107#if defined(COMPILER_RT_ARMHF_TARGET)108AEABI_RTABI double __aeabi_l2d(di_int a) {109return __floatdidf(a);110}111#else112AEABI_RTABI double __aeabi_l2d(di_int a) COMPILER_RT_ALIAS(__floatdidf);113#endif114#endif115116117