blob: 9580770327253bd78031dae5fd5c2f5822413ea5 [file] [log] [blame]
Edward O'Callaghan37a6a452009-08-07 20:30:09 +00001/*===-- floatdidf.c - Implement __floatdidf -------------------------------===
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 __floatdidf for the compiler_rt library.
11 *
12 *===----------------------------------------------------------------------===
13 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000014
15#include "int_lib.h"
16#include <float.h>
17
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000018/* Returns: convert a to a double, rounding toward even. */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000019
Edward O'Callaghan37a6a452009-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 Dunbarb3a69012009-06-26 16:47:03 +000023
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000024/* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000025
Anton Korobeynikov37b97d12011-04-19 17:51:24 +000026ARM_EABI_FNALIAS(l2d, floatdidf);
27
Daniel Dunbarb3a69012009-06-26 16:47:03 +000028#ifndef __SOFT_FP__
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000029/* Support for systems that have hardware floating-point; we'll set the inexact flag
30 * as a side-effect of this computation.
31 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000032#include <stdint.h>
33
Anton Korobeynikov1c5f89b2011-04-19 17:52:09 +000034COMPILER_RT_ABI double
Daniel Dunbarb3a69012009-06-26 16:47:03 +000035__floatdidf(di_int a)
36{
37 static const double twop52 = 0x1.0p52;
38 static const double twop32 = 0x1.0p32;
39
40 union { int64_t x; double d; } low = { .d = twop52 };
41
42 const double high = (int32_t)(a >> 32) * twop32;
43 low.x |= a & INT64_C(0x00000000ffffffff);
44
45 const double result = (high - twop52) + low.d;
46 return result;
47}
48
49#else
Edward O'Callaghan37a6a452009-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 Dunbarb3a69012009-06-26 16:47:03 +000053
Anton Korobeynikov1c5f89b2011-04-19 17:52:09 +000054COMPILER_RT_ABI double
Daniel Dunbarb3a69012009-06-26 16:47:03 +000055__floatdidf(di_int a)
56{
57 if (a == 0)
58 return 0.0;
59 const unsigned N = sizeof(di_int) * CHAR_BIT;
60 const di_int s = a >> (N-1);
61 a = (a ^ s) - s;
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000062 int sd = N - __builtin_clzll(a); /* number of significant digits */
63 int e = sd - 1; /* exponent */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000064 if (sd > DBL_MANT_DIG)
65 {
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000066 /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
67 * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
68 * 12345678901234567890123456
69 * 1 = msb 1 bit
70 * P = bit DBL_MANT_DIG-1 bits to the right of 1
71 * Q = bit DBL_MANT_DIG bits to the right of 1
72 * R = "or" of all bits to the right of Q
73 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000074 switch (sd)
75 {
76 case DBL_MANT_DIG + 1:
77 a <<= 1;
78 break;
79 case DBL_MANT_DIG + 2:
80 break;
81 default:
82 a = ((du_int)a >> (sd - (DBL_MANT_DIG+2))) |
83 ((a & ((du_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0);
84 };
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000085 /* finish: */
86 a |= (a & 4) != 0; /* Or P into R */
87 ++a; /* round - this step may add a significant bit */
88 a >>= 2; /* dump Q and R */
89 /* a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000090 if (a & ((du_int)1 << DBL_MANT_DIG))
91 {
92 a >>= 1;
93 ++e;
94 }
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000095 /* a is now rounded to DBL_MANT_DIG bits */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000096 }
97 else
98 {
99 a <<= (DBL_MANT_DIG - sd);
Edward O'Callaghan37a6a452009-08-07 20:30:09 +0000100 /* a is now rounded to DBL_MANT_DIG bits */
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000101 }
102 double_bits fb;
Edward O'Callaghan37a6a452009-08-07 20:30:09 +0000103 fb.u.high = ((su_int)s & 0x80000000) | /* sign */
104 ((e + 1023) << 20) | /* exponent */
105 ((su_int)(a >> 32) & 0x000FFFFF); /* mantissa-high */
106 fb.u.low = (su_int)a; /* mantissa-low */
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000107 return fb.f;
108}
109#endif