blob: e52fa0a3359bc2f324c66dc30e905caa9abfb9f8 [file] [log] [blame]
Edward O'Callaghan37a6a452009-08-07 20:30:09 +00001/* ===-- floatundidf.c - Implement __floatundidf ---------------------------===
2 *
3 * The LLVM Compiler Infrastructure
4 *
Howard Hinnant9ad441f2010-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'Callaghan37a6a452009-08-07 20:30:09 +00007 *
8 * ===----------------------------------------------------------------------===
9 *
10 * This file implements __floatundidf for the compiler_rt library.
11 *
12 * ===----------------------------------------------------------------------===
13 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000014
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000015/* Returns: convert a to a double, rounding toward even. */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000016
Edward O'Callaghan37a6a452009-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 Dunbarb3a69012009-06-26 16:47:03 +000020
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000021/* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000022
Anton Korobeynikov37b97d12011-04-19 17:51:24 +000023#include "int_lib.h"
24
Chandler Carruth0193b742012-06-22 21:09:15 +000025ARM_EABI_FNALIAS(ul2d, floatundidf)
Anton Korobeynikov37b97d12011-04-19 17:51:24 +000026
Daniel Dunbarb3a69012009-06-26 16:47:03 +000027#ifndef __SOFT_FP__
Edward O'Callaghan37a6a452009-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
Daniel Dunbarb3a69012009-06-26 16:47:03 +000032
Anton Korobeynikov1c5f89b2011-04-19 17:52:09 +000033COMPILER_RT_ABI double
Daniel Dunbarb3a69012009-06-26 16:47:03 +000034__floatundidf(du_int a)
35{
36 static const double twop52 = 0x1.0p52;
37 static const double twop84 = 0x1.0p84;
38 static const double twop84_plus_twop52 = 0x1.00000001p84;
39
40 union { uint64_t x; double d; } high = { .d = twop84 };
41 union { uint64_t x; double d; } low = { .d = twop52 };
42
43 high.x |= a >> 32;
44 low.x |= a & UINT64_C(0x00000000ffffffff);
45
46 const double result = (high.d - twop84_plus_twop52) + low.d;
47 return result;
48}
49
50#else
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000051/* Support for systems that don't have hardware floating-point; there are no flags to
52 * set, and we don't want to code-gen to an unknown soft-float implementation.
53 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000054
Anton Korobeynikov1c5f89b2011-04-19 17:52:09 +000055COMPILER_RT_ABI double
Daniel Dunbarb3a69012009-06-26 16:47:03 +000056__floatundidf(du_int a)
57{
58 if (a == 0)
59 return 0.0;
60 const unsigned N = sizeof(du_int) * CHAR_BIT;
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000061 int sd = N - __builtin_clzll(a); /* number of significant digits */
62 int e = sd - 1; /* exponent */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000063 if (sd > DBL_MANT_DIG)
64 {
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000065 /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
66 * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
67 * 12345678901234567890123456
68 * 1 = msb 1 bit
69 * P = bit DBL_MANT_DIG-1 bits to the right of 1
70 * Q = bit DBL_MANT_DIG bits to the right of 1
71 * R = "or" of all bits to the right of Q
72 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000073 switch (sd)
74 {
75 case DBL_MANT_DIG + 1:
76 a <<= 1;
77 break;
78 case DBL_MANT_DIG + 2:
79 break;
80 default:
81 a = (a >> (sd - (DBL_MANT_DIG+2))) |
82 ((a & ((du_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0);
83 };
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000084 /* finish: */
85 a |= (a & 4) != 0; /* Or P into R */
86 ++a; /* round - this step may add a significant bit */
87 a >>= 2; /* dump Q and R */
88 /* a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000089 if (a & ((du_int)1 << DBL_MANT_DIG))
90 {
91 a >>= 1;
92 ++e;
93 }
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000094 /* a is now rounded to DBL_MANT_DIG bits */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000095 }
96 else
97 {
98 a <<= (DBL_MANT_DIG - sd);
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000099 /* a is now rounded to DBL_MANT_DIG bits */
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000100 }
101 double_bits fb;
Edward O'Callaghan37a6a452009-08-07 20:30:09 +0000102 fb.u.high = ((e + 1023) << 20) | /* exponent */
103 ((su_int)(a >> 32) & 0x000FFFFF); /* mantissa-high */
104 fb.u.low = (su_int)a; /* mantissa-low */
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000105 return fb.f;
106}
Edward O'Callaghan6c307f02009-08-03 05:59:48 +0000107#endif