blob: 5ba952635030eac5746f715af9c92e902d7a7c1e [file] [log] [blame]
Edward O'Callaghan55836322009-08-07 20:30:09 +00001/*===-- floatdidf.c - Implement __floatdidf -------------------------------===
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 __floatdidf for the compiler_rt library.
11 *
12 *===----------------------------------------------------------------------===
13 */
Anton Korobeynikove63da932011-04-19 17:52:09 +000014#include "abi.h"
Daniel Dunbarfd089992009-06-26 16:47:03 +000015
16#include "int_lib.h"
17#include <float.h>
18
Edward O'Callaghan55836322009-08-07 20:30:09 +000019/* Returns: convert a to a double, rounding toward even. */
Daniel Dunbarfd089992009-06-26 16:47:03 +000020
Edward O'Callaghan55836322009-08-07 20:30:09 +000021/* Assumption: double is a IEEE 64 bit floating point type
22 * di_int is a 64 bit integral type
23 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000024
Edward O'Callaghan55836322009-08-07 20:30:09 +000025/* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */
Daniel Dunbarfd089992009-06-26 16:47:03 +000026
Anton Korobeynikov75e3c192011-04-19 17:51:24 +000027ARM_EABI_FNALIAS(l2d, floatdidf);
28
Daniel Dunbarfd089992009-06-26 16:47:03 +000029#ifndef __SOFT_FP__
Edward O'Callaghan55836322009-08-07 20:30:09 +000030/* Support for systems that have hardware floating-point; we'll set the inexact flag
31 * as a side-effect of this computation.
32 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000033#include <stdint.h>
34
Anton Korobeynikove63da932011-04-19 17:52:09 +000035COMPILER_RT_ABI double
Daniel Dunbarfd089992009-06-26 16:47:03 +000036__floatdidf(di_int a)
37{
38 static const double twop52 = 0x1.0p52;
39 static const double twop32 = 0x1.0p32;
40
41 union { int64_t x; double d; } low = { .d = twop52 };
42
43 const double high = (int32_t)(a >> 32) * twop32;
44 low.x |= a & INT64_C(0x00000000ffffffff);
45
46 const double result = (high - twop52) + low.d;
47 return result;
48}
49
50#else
Edward O'Callaghan55836322009-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 Dunbarfd089992009-06-26 16:47:03 +000054
Anton Korobeynikove63da932011-04-19 17:52:09 +000055COMPILER_RT_ABI double
Daniel Dunbarfd089992009-06-26 16:47:03 +000056__floatdidf(di_int a)
57{
58 if (a == 0)
59 return 0.0;
60 const unsigned N = sizeof(di_int) * CHAR_BIT;
61 const di_int s = a >> (N-1);
62 a = (a ^ s) - s;
Edward O'Callaghan55836322009-08-07 20:30:09 +000063 int sd = N - __builtin_clzll(a); /* number of significant digits */
64 int e = sd - 1; /* exponent */
Daniel Dunbarfd089992009-06-26 16:47:03 +000065 if (sd > DBL_MANT_DIG)
66 {
Edward O'Callaghan55836322009-08-07 20:30:09 +000067 /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
68 * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
69 * 12345678901234567890123456
70 * 1 = msb 1 bit
71 * P = bit DBL_MANT_DIG-1 bits to the right of 1
72 * Q = bit DBL_MANT_DIG bits to the right of 1
73 * R = "or" of all bits to the right of Q
74 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000075 switch (sd)
76 {
77 case DBL_MANT_DIG + 1:
78 a <<= 1;
79 break;
80 case DBL_MANT_DIG + 2:
81 break;
82 default:
83 a = ((du_int)a >> (sd - (DBL_MANT_DIG+2))) |
84 ((a & ((du_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0);
85 };
Edward O'Callaghan55836322009-08-07 20:30:09 +000086 /* finish: */
87 a |= (a & 4) != 0; /* Or P into R */
88 ++a; /* round - this step may add a significant bit */
89 a >>= 2; /* dump Q and R */
90 /* a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits */
Daniel Dunbarfd089992009-06-26 16:47:03 +000091 if (a & ((du_int)1 << DBL_MANT_DIG))
92 {
93 a >>= 1;
94 ++e;
95 }
Edward O'Callaghan55836322009-08-07 20:30:09 +000096 /* a is now rounded to DBL_MANT_DIG bits */
Daniel Dunbarfd089992009-06-26 16:47:03 +000097 }
98 else
99 {
100 a <<= (DBL_MANT_DIG - sd);
Edward O'Callaghan55836322009-08-07 20:30:09 +0000101 /* a is now rounded to DBL_MANT_DIG bits */
Daniel Dunbarfd089992009-06-26 16:47:03 +0000102 }
103 double_bits fb;
Edward O'Callaghan55836322009-08-07 20:30:09 +0000104 fb.u.high = ((su_int)s & 0x80000000) | /* sign */
105 ((e + 1023) << 20) | /* exponent */
106 ((su_int)(a >> 32) & 0x000FFFFF); /* mantissa-high */
107 fb.u.low = (su_int)a; /* mantissa-low */
Daniel Dunbarfd089992009-06-26 16:47:03 +0000108 return fb.f;
109}
110#endif