1 /*===-- floatdidf.c - Implement __floatdidf -------------------------------===
3 * The LLVM Compiler Infrastructure
5 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
8 *===----------------------------------------------------------------------===
10 * This file implements __floatdidf for the compiler_rt library.
12 *===----------------------------------------------------------------------===
17 /* Returns: convert a to a double, rounding toward even. */
19 /* Assumption: double is a IEEE 64 bit floating point type
20 * di_int is a 64 bit integral type
23 /* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */
26 /* Support for systems that have hardware floating-point; we'll set the inexact flag
27 * as a side-effect of this computation.
30 COMPILER_RT_ABI double
33 static const double twop52 = 4503599627370496.0; // 0x1.0p52
34 static const double twop32 = 4294967296.0; // 0x1.0p32
36 union { int64_t x; double d; } low = { .d = twop52 };
38 const double high = (int32_t)(a >> 32) * twop32;
39 low.x |= a & INT64_C(0x00000000ffffffff);
41 const double result = (high - twop52) + low.d;
46 /* Support for systems that don't have hardware floating-point; there are no flags to
47 * set, and we don't want to code-gen to an unknown soft-float implementation.
50 COMPILER_RT_ABI double
55 const unsigned N = sizeof(di_int) * CHAR_BIT;
56 const di_int s = a >> (N-1);
58 int sd = N - __builtin_clzll(a); /* number of significant digits */
59 int e = sd - 1; /* exponent */
60 if (sd > DBL_MANT_DIG)
62 /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
63 * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
64 * 12345678901234567890123456
66 * P = bit DBL_MANT_DIG-1 bits to the right of 1
67 * Q = bit DBL_MANT_DIG bits to the right of 1
68 * R = "or" of all bits to the right of Q
72 case DBL_MANT_DIG + 1:
75 case DBL_MANT_DIG + 2:
78 a = ((du_int)a >> (sd - (DBL_MANT_DIG+2))) |
79 ((a & ((du_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0);
82 a |= (a & 4) != 0; /* Or P into R */
83 ++a; /* round - this step may add a significant bit */
84 a >>= 2; /* dump Q and R */
85 /* a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits */
86 if (a & ((du_int)1 << DBL_MANT_DIG))
91 /* a is now rounded to DBL_MANT_DIG bits */
95 a <<= (DBL_MANT_DIG - sd);
96 /* a is now rounded to DBL_MANT_DIG bits */
99 fb.u.s.high = ((su_int)s & 0x80000000) | /* sign */
100 ((e + 1023) << 20) | /* exponent */
101 ((su_int)(a >> 32) & 0x000FFFFF); /* mantissa-high */
102 fb.u.s.low = (su_int)a; /* mantissa-low */
107 #if defined(__ARM_EABI__)
108 AEABI_RTABI double __aeabi_l2d(di_int a) {
109 return __floatdidf(a);