blob: 36b856e078d49ede5005a4c96d172947abb5cee2 [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"
Daniel Dunbarfd089992009-06-26 16:47:03 +000016
Edward O'Callaghan55836322009-08-07 20:30:09 +000017/* Returns: convert a to a double, rounding toward even. */
Daniel Dunbarfd089992009-06-26 16:47:03 +000018
Jeroen Ketema0b748f22016-06-13 15:21:04 +000019/* Assumption: double is a IEEE 64 bit floating point type
Edward O'Callaghan55836322009-08-07 20:30:09 +000020 * di_int is a 64 bit integral type
21 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000022
Edward O'Callaghan55836322009-08-07 20:30:09 +000023/* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */
Daniel Dunbarfd089992009-06-26 16:47:03 +000024
25#ifndef __SOFT_FP__
Edward O'Callaghan55836322009-08-07 20:30:09 +000026/* Support for systems that have hardware floating-point; we'll set the inexact flag
27 * as a side-effect of this computation.
28 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000029
Anton Korobeynikove63da932011-04-19 17:52:09 +000030COMPILER_RT_ABI double
Daniel Dunbarfd089992009-06-26 16:47:03 +000031__floatdidf(di_int a)
32{
Jeroen Ketema0b748f22016-06-13 15:21:04 +000033 static const double twop52 = 4503599627370496.0; // 0x1.0p52
34 static const double twop32 = 4294967296.0; // 0x1.0p32
35
36 union { int64_t x; double d; } low = { .d = twop52 };
37
38 const double high = (int32_t)(a >> 32) * twop32;
39 low.x |= a & INT64_C(0x00000000ffffffff);
40
41 const double result = (high - twop52) + low.d;
42 return result;
Daniel Dunbarfd089992009-06-26 16:47:03 +000043}
44
45#else
Edward O'Callaghan55836322009-08-07 20:30:09 +000046/* Support for systems that don't have hardware floating-point; there are no flags to
47 * set, and we don't want to code-gen to an unknown soft-float implementation.
48 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000049
Anton Korobeynikove63da932011-04-19 17:52:09 +000050COMPILER_RT_ABI double
Daniel Dunbarfd089992009-06-26 16:47:03 +000051__floatdidf(di_int a)
52{
53 if (a == 0)
54 return 0.0;
55 const unsigned N = sizeof(di_int) * CHAR_BIT;
56 const di_int s = a >> (N-1);
57 a = (a ^ s) - s;
Edward O'Callaghan55836322009-08-07 20:30:09 +000058 int sd = N - __builtin_clzll(a); /* number of significant digits */
59 int e = sd - 1; /* exponent */
Daniel Dunbarfd089992009-06-26 16:47:03 +000060 if (sd > DBL_MANT_DIG)
61 {
Edward O'Callaghan55836322009-08-07 20:30:09 +000062 /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
63 * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
64 * 12345678901234567890123456
65 * 1 = msb 1 bit
66 * P = bit DBL_MANT_DIG-1 bits to the right of 1
67 * Q = bit DBL_MANT_DIG bits to the right of 1
68 * R = "or" of all bits to the right of Q
69 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000070 switch (sd)
71 {
72 case DBL_MANT_DIG + 1:
73 a <<= 1;
74 break;
75 case DBL_MANT_DIG + 2:
76 break;
77 default:
78 a = ((du_int)a >> (sd - (DBL_MANT_DIG+2))) |
79 ((a & ((du_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0);
80 };
Edward O'Callaghan55836322009-08-07 20:30:09 +000081 /* finish: */
82 a |= (a & 4) != 0; /* Or P into R */
83 ++a; /* round - this step may add a significant bit */
84 a >>= 2; /* dump Q and R */
85 /* a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits */
Daniel Dunbarfd089992009-06-26 16:47:03 +000086 if (a & ((du_int)1 << DBL_MANT_DIG))
87 {
88 a >>= 1;
89 ++e;
90 }
Edward O'Callaghan55836322009-08-07 20:30:09 +000091 /* a is now rounded to DBL_MANT_DIG bits */
Daniel Dunbarfd089992009-06-26 16:47:03 +000092 }
93 else
94 {
95 a <<= (DBL_MANT_DIG - sd);
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 double_bits fb;
Jeroen Ketemaaf0bbd92016-06-13 15:24:16 +000099 fb.u.s.high = ((su_int)s & 0x80000000) | /* sign */
100 ((e + 1023) << 20) | /* exponent */
101 ((su_int)(a >> 32) & 0x000FFFFF); /* mantissa-high */
102 fb.u.s.low = (su_int)a; /* mantissa-low */
Daniel Dunbarfd089992009-06-26 16:47:03 +0000103 return fb.f;
104}
105#endif
Saleem Abdulrasool36ac5dd2017-05-16 16:41:37 +0000106
Saleem Abdulrasool4a458382017-05-16 20:25:07 +0000107#if defined(__ARM_EABI__)
Eli Friedman0d586d02017-10-03 21:25:07 +0000108#if defined(COMPILER_RT_ARMHF_TARGET)
Saleem Abdulrasool36ac5dd2017-05-16 16:41:37 +0000109AEABI_RTABI double __aeabi_l2d(di_int a) {
110 return __floatdidf(a);
111}
Eli Friedman0d586d02017-10-03 21:25:07 +0000112#else
113AEABI_RTABI double __aeabi_l2d(di_int a) COMPILER_RT_ALIAS(__floatdidf);
Saleem Abdulrasool36ac5dd2017-05-16 16:41:37 +0000114#endif
Eli Friedman0d586d02017-10-03 21:25:07 +0000115#endif