Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 1 | /*===-- floatdidf.c - Implement __floatdidf -------------------------------=== |
| 2 | * |
Chandler Carruth | 7a739a0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 3 | * 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'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 6 | * |
| 7 | *===----------------------------------------------------------------------=== |
| 8 | * |
| 9 | * This file implements __floatdidf for the compiler_rt library. |
| 10 | * |
| 11 | *===----------------------------------------------------------------------=== |
| 12 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 13 | |
| 14 | #include "int_lib.h" |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 15 | |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 16 | /* Returns: convert a to a double, rounding toward even. */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 17 | |
Jeroen Ketema | c84c407 | 2016-06-13 15:21:04 +0000 | [diff] [blame] | 18 | /* Assumption: double is a IEEE 64 bit floating point type |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 19 | * di_int is a 64 bit integral type |
| 20 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 21 | |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 22 | /* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 23 | |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 24 | #ifndef __SOFT_FP__ |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 25 | /* Support for systems that have hardware floating-point; we'll set the inexact flag |
| 26 | * as a side-effect of this computation. |
| 27 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 28 | |
Anton Korobeynikov | 1c5f89b | 2011-04-19 17:52:09 +0000 | [diff] [blame] | 29 | COMPILER_RT_ABI double |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 30 | __floatdidf(di_int a) |
| 31 | { |
Jeroen Ketema | c84c407 | 2016-06-13 15:21:04 +0000 | [diff] [blame] | 32 | 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 Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | #else |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 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 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 48 | |
Anton Korobeynikov | 1c5f89b | 2011-04-19 17:52:09 +0000 | [diff] [blame] | 49 | COMPILER_RT_ABI double |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 50 | __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'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 57 | int sd = N - __builtin_clzll(a); /* number of significant digits */ |
| 58 | int e = sd - 1; /* exponent */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 59 | if (sd > DBL_MANT_DIG) |
| 60 | { |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 61 | /* 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 Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 69 | 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'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 80 | /* 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 Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 85 | if (a & ((du_int)1 << DBL_MANT_DIG)) |
| 86 | { |
| 87 | a >>= 1; |
| 88 | ++e; |
| 89 | } |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 90 | /* a is now rounded to DBL_MANT_DIG bits */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 91 | } |
| 92 | else |
| 93 | { |
| 94 | a <<= (DBL_MANT_DIG - sd); |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 95 | /* a is now rounded to DBL_MANT_DIG bits */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 96 | } |
| 97 | double_bits fb; |
Jeroen Ketema | 51be894 | 2016-06-13 15:24:16 +0000 | [diff] [blame] | 98 | 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 Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 102 | return fb.f; |
| 103 | } |
| 104 | #endif |
Saleem Abdulrasool | 99e2e66 | 2017-05-16 16:41:37 +0000 | [diff] [blame] | 105 | |
Saleem Abdulrasool | 016b0e3 | 2017-05-16 20:25:07 +0000 | [diff] [blame] | 106 | #if defined(__ARM_EABI__) |
Eli Friedman | 83774b4 | 2017-10-03 21:25:07 +0000 | [diff] [blame] | 107 | #if defined(COMPILER_RT_ARMHF_TARGET) |
Saleem Abdulrasool | 99e2e66 | 2017-05-16 16:41:37 +0000 | [diff] [blame] | 108 | AEABI_RTABI double __aeabi_l2d(di_int a) { |
| 109 | return __floatdidf(a); |
| 110 | } |
Eli Friedman | 83774b4 | 2017-10-03 21:25:07 +0000 | [diff] [blame] | 111 | #else |
| 112 | AEABI_RTABI double __aeabi_l2d(di_int a) COMPILER_RT_ALIAS(__floatdidf); |
Saleem Abdulrasool | 99e2e66 | 2017-05-16 16:41:37 +0000 | [diff] [blame] | 113 | #endif |
Eli Friedman | 83774b4 | 2017-10-03 21:25:07 +0000 | [diff] [blame] | 114 | #endif |