blob: 1633f73840831941b43f6ae3a703c3645487234b [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 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000014
15#include "int_lib.h"
16#include <float.h>
17
Edward O'Callaghan55836322009-08-07 20:30:09 +000018/* Returns: convert a to a double, rounding toward even. */
Daniel Dunbarfd089992009-06-26 16:47:03 +000019
Edward O'Callaghan55836322009-08-07 20:30:09 +000020/* Assumption: double is a IEEE 64 bit floating point type
21 * di_int is a 64 bit integral type
22 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000023
Edward O'Callaghan55836322009-08-07 20:30:09 +000024/* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */
Daniel Dunbarfd089992009-06-26 16:47:03 +000025
Anton Korobeynikov75e3c192011-04-19 17:51:24 +000026#include "int_lib.h"
27
28ARM_EABI_FNALIAS(l2d, floatdidf);
29
Daniel Dunbarfd089992009-06-26 16:47:03 +000030#ifndef __SOFT_FP__
Edward O'Callaghan55836322009-08-07 20:30:09 +000031/* Support for systems that have hardware floating-point; we'll set the inexact flag
32 * as a side-effect of this computation.
33 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000034#include <stdint.h>
35
36double
37__floatdidf(di_int a)
38{
39 static const double twop52 = 0x1.0p52;
40 static const double twop32 = 0x1.0p32;
41
42 union { int64_t x; double d; } low = { .d = twop52 };
43
44 const double high = (int32_t)(a >> 32) * twop32;
45 low.x |= a & INT64_C(0x00000000ffffffff);
46
47 const double result = (high - twop52) + low.d;
48 return result;
49}
50
51#else
Edward O'Callaghan55836322009-08-07 20:30:09 +000052/* Support for systems that don't have hardware floating-point; there are no flags to
53 * set, and we don't want to code-gen to an unknown soft-float implementation.
54 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000055
56double
57__floatdidf(di_int a)
58{
59 if (a == 0)
60 return 0.0;
61 const unsigned N = sizeof(di_int) * CHAR_BIT;
62 const di_int s = a >> (N-1);
63 a = (a ^ s) - s;
Edward O'Callaghan55836322009-08-07 20:30:09 +000064 int sd = N - __builtin_clzll(a); /* number of significant digits */
65 int e = sd - 1; /* exponent */
Daniel Dunbarfd089992009-06-26 16:47:03 +000066 if (sd > DBL_MANT_DIG)
67 {
Edward O'Callaghan55836322009-08-07 20:30:09 +000068 /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
69 * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
70 * 12345678901234567890123456
71 * 1 = msb 1 bit
72 * P = bit DBL_MANT_DIG-1 bits to the right of 1
73 * Q = bit DBL_MANT_DIG bits to the right of 1
74 * R = "or" of all bits to the right of Q
75 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000076 switch (sd)
77 {
78 case DBL_MANT_DIG + 1:
79 a <<= 1;
80 break;
81 case DBL_MANT_DIG + 2:
82 break;
83 default:
84 a = ((du_int)a >> (sd - (DBL_MANT_DIG+2))) |
85 ((a & ((du_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0);
86 };
Edward O'Callaghan55836322009-08-07 20:30:09 +000087 /* finish: */
88 a |= (a & 4) != 0; /* Or P into R */
89 ++a; /* round - this step may add a significant bit */
90 a >>= 2; /* dump Q and R */
91 /* a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits */
Daniel Dunbarfd089992009-06-26 16:47:03 +000092 if (a & ((du_int)1 << DBL_MANT_DIG))
93 {
94 a >>= 1;
95 ++e;
96 }
Edward O'Callaghan55836322009-08-07 20:30:09 +000097 /* a is now rounded to DBL_MANT_DIG bits */
Daniel Dunbarfd089992009-06-26 16:47:03 +000098 }
99 else
100 {
101 a <<= (DBL_MANT_DIG - sd);
Edward O'Callaghan55836322009-08-07 20:30:09 +0000102 /* a is now rounded to DBL_MANT_DIG bits */
Daniel Dunbarfd089992009-06-26 16:47:03 +0000103 }
104 double_bits fb;
Edward O'Callaghan55836322009-08-07 20:30:09 +0000105 fb.u.high = ((su_int)s & 0x80000000) | /* sign */
106 ((e + 1023) << 20) | /* exponent */
107 ((su_int)(a >> 32) & 0x000FFFFF); /* mantissa-high */
108 fb.u.low = (su_int)a; /* mantissa-low */
Daniel Dunbarfd089992009-06-26 16:47:03 +0000109 return fb.f;
110}
111#endif