blob: f76d1b899eab87e4329437bde0c8650ed8906875 [file] [log] [blame]
Daniel Dunbarb3a69012009-06-26 16:47:03 +00001//===-- floatdidf.c - Implement __floatdidf -------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements __floatdidf for the compiler_rt library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "int_lib.h"
15#include <float.h>
16
17// Returns: convert a to a double, rounding toward even.
18
19// Assumption: double is a IEEE 64 bit floating point type
20// di_int is a 64 bit integral type
21
22// seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm
23
24#ifndef __SOFT_FP__
25// Support for systems that have hardware floating-point; we'll set the inexact flag
26// as a side-effect of this computation.
27#include <stdint.h>
28
29double
30__floatdidf(di_int a)
31{
32 static const double twop52 = 0x1.0p52;
33 static const double twop32 = 0x1.0p32;
34
35 union { int64_t x; double d; } low = { .d = twop52 };
36
37 const double high = (int32_t)(a >> 32) * twop32;
38 low.x |= a & INT64_C(0x00000000ffffffff);
39
40 const double result = (high - twop52) + low.d;
41 return result;
42}
43
44#else
45// Support for systems that don't have hardware floating-point; there are no flags to
46// set, and we don't want to code-gen to an unknown soft-float implementation.
47
48double
49__floatdidf(di_int a)
50{
51 if (a == 0)
52 return 0.0;
53 const unsigned N = sizeof(di_int) * CHAR_BIT;
54 const di_int s = a >> (N-1);
55 a = (a ^ s) - s;
56 int sd = N - __builtin_clzll(a); // number of significant digits
57 int e = sd - 1; // exponent
58 if (sd > DBL_MANT_DIG)
59 {
60 // start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
61 // finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
62 // 12345678901234567890123456
63 // 1 = msb 1 bit
64 // P = bit DBL_MANT_DIG-1 bits to the right of 1
65 // Q = bit DBL_MANT_DIG bits to the right of 1
66 // R = "or" of all bits to the right of Q
67 switch (sd)
68 {
69 case DBL_MANT_DIG + 1:
70 a <<= 1;
71 break;
72 case DBL_MANT_DIG + 2:
73 break;
74 default:
75 a = ((du_int)a >> (sd - (DBL_MANT_DIG+2))) |
76 ((a & ((du_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0);
77 };
78 // finish:
79 a |= (a & 4) != 0; // Or P into R
80 ++a; // round - this step may add a significant bit
81 a >>= 2; // dump Q and R
82 // a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits
83 if (a & ((du_int)1 << DBL_MANT_DIG))
84 {
85 a >>= 1;
86 ++e;
87 }
88 // a is now rounded to DBL_MANT_DIG bits
89 }
90 else
91 {
92 a <<= (DBL_MANT_DIG - sd);
93 // a is now rounded to DBL_MANT_DIG bits
94 }
95 double_bits fb;
96 fb.u.high = ((su_int)s & 0x80000000) | // sign
97 ((e + 1023) << 20) | // exponent
98 ((su_int)(a >> 32) & 0x000FFFFF); // mantissa-high
99 fb.u.low = (su_int)a; // mantissa-low
100 return fb.f;
101}
102#endif