blob: 67aa86e5e5b86b9a69d51c5dcaec37ef5ecc22e9 [file] [log] [blame]
Edward O'Callaghan55836322009-08-07 20:30:09 +00001/* ===-- floatundidf.c - Implement __floatundidf ---------------------------===
2 *
3 * The LLVM Compiler Infrastructure
4 *
Howard Hinnant5b791f62010-11-16 22:13:33 +00005 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
Edward O'Callaghan55836322009-08-07 20:30:09 +00007 *
8 * ===----------------------------------------------------------------------===
9 *
10 * This file implements __floatundidf for the compiler_rt library.
11 *
12 * ===----------------------------------------------------------------------===
13 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000014
Edward O'Callaghan55836322009-08-07 20:30:09 +000015/* Returns: convert a to a double, rounding toward even. */
Daniel Dunbarfd089992009-06-26 16:47:03 +000016
Edward O'Callaghan55836322009-08-07 20:30:09 +000017/* Assumption: double is a IEEE 64 bit floating point type
18 * du_int is a 64 bit integral type
19 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000020
Edward O'Callaghan55836322009-08-07 20:30:09 +000021/* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */
Daniel Dunbarfd089992009-06-26 16:47:03 +000022
Anton Korobeynikov75e3c192011-04-19 17:51:24 +000023#include "int_lib.h"
24
Chandler Carruth321916a2012-06-22 21:09:15 +000025ARM_EABI_FNALIAS(ul2d, floatundidf)
Anton Korobeynikov75e3c192011-04-19 17:51:24 +000026
Daniel Dunbarfd089992009-06-26 16:47:03 +000027#ifndef __SOFT_FP__
Edward O'Callaghan55836322009-08-07 20:30:09 +000028/* Support for systems that have hardware floating-point; we'll set the inexact flag
29 * as a side-effect of this computation.
30 */
31
Anton Korobeynikove63da932011-04-19 17:52:09 +000032COMPILER_RT_ABI double
Daniel Dunbarfd089992009-06-26 16:47:03 +000033__floatundidf(du_int a)
34{
Saleem Abdulrasoolb5bba5c2015-10-15 04:26:19 +000035 static const double twop52 = 4503599627370496.0; // 0x1.0p52
36 static const double twop84 = 19342813113834066795298816.0; // 0x1.0p84
37 static const double twop84_plus_twop52 = 19342813118337666422669312.0; // 0x1.00000001p84
Daniel Dunbarfd089992009-06-26 16:47:03 +000038
39 union { uint64_t x; double d; } high = { .d = twop84 };
40 union { uint64_t x; double d; } low = { .d = twop52 };
41
42 high.x |= a >> 32;
43 low.x |= a & UINT64_C(0x00000000ffffffff);
44
45 const double result = (high.d - twop84_plus_twop52) + low.d;
46 return result;
47}
48
49#else
Edward O'Callaghan55836322009-08-07 20:30:09 +000050/* Support for systems that don't have hardware floating-point; there are no flags to
51 * set, and we don't want to code-gen to an unknown soft-float implementation.
52 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000053
Anton Korobeynikove63da932011-04-19 17:52:09 +000054COMPILER_RT_ABI double
Daniel Dunbarfd089992009-06-26 16:47:03 +000055__floatundidf(du_int a)
56{
57 if (a == 0)
58 return 0.0;
59 const unsigned N = sizeof(du_int) * CHAR_BIT;
Edward O'Callaghan55836322009-08-07 20:30:09 +000060 int sd = N - __builtin_clzll(a); /* number of significant digits */
61 int e = sd - 1; /* exponent */
Daniel Dunbarfd089992009-06-26 16:47:03 +000062 if (sd > DBL_MANT_DIG)
63 {
Edward O'Callaghan55836322009-08-07 20:30:09 +000064 /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
65 * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
66 * 12345678901234567890123456
67 * 1 = msb 1 bit
68 * P = bit DBL_MANT_DIG-1 bits to the right of 1
69 * Q = bit DBL_MANT_DIG bits to the right of 1
70 * R = "or" of all bits to the right of Q
71 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000072 switch (sd)
73 {
74 case DBL_MANT_DIG + 1:
75 a <<= 1;
76 break;
77 case DBL_MANT_DIG + 2:
78 break;
79 default:
80 a = (a >> (sd - (DBL_MANT_DIG+2))) |
81 ((a & ((du_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0);
82 };
Edward O'Callaghan55836322009-08-07 20:30:09 +000083 /* finish: */
84 a |= (a & 4) != 0; /* Or P into R */
85 ++a; /* round - this step may add a significant bit */
86 a >>= 2; /* dump Q and R */
87 /* a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits */
Daniel Dunbarfd089992009-06-26 16:47:03 +000088 if (a & ((du_int)1 << DBL_MANT_DIG))
89 {
90 a >>= 1;
91 ++e;
92 }
Edward O'Callaghan55836322009-08-07 20:30:09 +000093 /* a is now rounded to DBL_MANT_DIG bits */
Daniel Dunbarfd089992009-06-26 16:47:03 +000094 }
95 else
96 {
97 a <<= (DBL_MANT_DIG - sd);
Edward O'Callaghan55836322009-08-07 20:30:09 +000098 /* a is now rounded to DBL_MANT_DIG bits */
Daniel Dunbarfd089992009-06-26 16:47:03 +000099 }
100 double_bits fb;
Edward O'Callaghan55836322009-08-07 20:30:09 +0000101 fb.u.high = ((e + 1023) << 20) | /* exponent */
102 ((su_int)(a >> 32) & 0x000FFFFF); /* mantissa-high */
103 fb.u.low = (su_int)a; /* mantissa-low */
Daniel Dunbarfd089992009-06-26 16:47:03 +0000104 return fb.f;
105}
Edward O'Callaghan7578f802009-08-03 05:59:48 +0000106#endif