blob: 11f80848ef902d4640384fd2c15d6419df5bd194 [file] [log] [blame]
Edward O'Callaghan37a6a452009-08-07 20:30:09 +00001/*===-- floatdidf.c - Implement __floatdidf -------------------------------===
2 *
Chandler Carruth7a739a02019-01-19 10:56:40 +00003 * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 * See https://llvm.org/LICENSE.txt for license information.
5 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Edward O'Callaghan37a6a452009-08-07 20:30:09 +00006 *
7 *===----------------------------------------------------------------------===
8 *
9 * This file implements __floatdidf for the compiler_rt library.
10 *
11 *===----------------------------------------------------------------------===
12 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000013
14#include "int_lib.h"
Daniel Dunbarb3a69012009-06-26 16:47:03 +000015
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000016/* Returns: convert a to a double, rounding toward even. */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000017
Jeroen Ketemac84c4072016-06-13 15:21:04 +000018/* Assumption: double is a IEEE 64 bit floating point type
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000019 * di_int is a 64 bit integral type
20 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000021
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000022/* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000023
Daniel Dunbarb3a69012009-06-26 16:47:03 +000024#ifndef __SOFT_FP__
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000025/* Support for systems that have hardware floating-point; we'll set the inexact flag
26 * as a side-effect of this computation.
27 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000028
Anton Korobeynikov1c5f89b2011-04-19 17:52:09 +000029COMPILER_RT_ABI double
Daniel Dunbarb3a69012009-06-26 16:47:03 +000030__floatdidf(di_int a)
31{
Jeroen Ketemac84c4072016-06-13 15:21:04 +000032 static const double twop52 = 4503599627370496.0; // 0x1.0p52
33 static const double twop32 = 4294967296.0; // 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;
Daniel Dunbarb3a69012009-06-26 16:47:03 +000042}
43
44#else
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000045/* 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 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000048
Anton Korobeynikov1c5f89b2011-04-19 17:52:09 +000049COMPILER_RT_ABI double
Daniel Dunbarb3a69012009-06-26 16:47:03 +000050__floatdidf(di_int a)
51{
52 if (a == 0)
53 return 0.0;
54 const unsigned N = sizeof(di_int) * CHAR_BIT;
55 const di_int s = a >> (N-1);
56 a = (a ^ s) - s;
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000057 int sd = N - __builtin_clzll(a); /* number of significant digits */
58 int e = sd - 1; /* exponent */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000059 if (sd > DBL_MANT_DIG)
60 {
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000061 /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
62 * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
63 * 12345678901234567890123456
64 * 1 = msb 1 bit
65 * P = bit DBL_MANT_DIG-1 bits to the right of 1
66 * Q = bit DBL_MANT_DIG bits to the right of 1
67 * R = "or" of all bits to the right of Q
68 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000069 switch (sd)
70 {
71 case DBL_MANT_DIG + 1:
72 a <<= 1;
73 break;
74 case DBL_MANT_DIG + 2:
75 break;
76 default:
77 a = ((du_int)a >> (sd - (DBL_MANT_DIG+2))) |
78 ((a & ((du_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0);
79 };
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000080 /* finish: */
81 a |= (a & 4) != 0; /* Or P into R */
82 ++a; /* round - this step may add a significant bit */
83 a >>= 2; /* dump Q and R */
84 /* a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000085 if (a & ((du_int)1 << DBL_MANT_DIG))
86 {
87 a >>= 1;
88 ++e;
89 }
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000090 /* a is now rounded to DBL_MANT_DIG bits */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000091 }
92 else
93 {
94 a <<= (DBL_MANT_DIG - sd);
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 double_bits fb;
Jeroen Ketema51be8942016-06-13 15:24:16 +000098 fb.u.s.high = ((su_int)s & 0x80000000) | /* sign */
99 ((e + 1023) << 20) | /* exponent */
100 ((su_int)(a >> 32) & 0x000FFFFF); /* mantissa-high */
101 fb.u.s.low = (su_int)a; /* mantissa-low */
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000102 return fb.f;
103}
104#endif
Saleem Abdulrasool99e2e662017-05-16 16:41:37 +0000105
Saleem Abdulrasool016b0e32017-05-16 20:25:07 +0000106#if defined(__ARM_EABI__)
Eli Friedman83774b42017-10-03 21:25:07 +0000107#if defined(COMPILER_RT_ARMHF_TARGET)
Saleem Abdulrasool99e2e662017-05-16 16:41:37 +0000108AEABI_RTABI double __aeabi_l2d(di_int a) {
109 return __floatdidf(a);
110}
Eli Friedman83774b42017-10-03 21:25:07 +0000111#else
112AEABI_RTABI double __aeabi_l2d(di_int a) COMPILER_RT_ALIAS(__floatdidf);
Saleem Abdulrasool99e2e662017-05-16 16:41:37 +0000113#endif
Eli Friedman83774b42017-10-03 21:25:07 +0000114#endif