Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 1 | /* ===-- floatundidf.c - Implement __floatundidf ---------------------------=== |
| 2 | * |
| 3 | * The LLVM Compiler Infrastructure |
| 4 | * |
Howard Hinnant | 9ad441f | 2010-11-16 22:13:33 +0000 | [diff] [blame] | 5 | * This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | * Source Licenses. See LICENSE.TXT for details. |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 7 | * |
| 8 | * ===----------------------------------------------------------------------=== |
| 9 | * |
| 10 | * This file implements __floatundidf for the compiler_rt library. |
| 11 | * |
| 12 | * ===----------------------------------------------------------------------=== |
| 13 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 14 | |
Anton Korobeynikov | 1c5f89b | 2011-04-19 17:52:09 +0000 | [diff] [blame^] | 15 | #include "abi.h" |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 16 | #include <float.h> |
| 17 | |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 18 | /* Returns: convert a to a double, rounding toward even. */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 19 | |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 20 | /* Assumption: double is a IEEE 64 bit floating point type |
| 21 | * du_int is a 64 bit integral type |
| 22 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 23 | |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 24 | /* 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] | 25 | |
Anton Korobeynikov | 37b97d1 | 2011-04-19 17:51:24 +0000 | [diff] [blame] | 26 | #include "int_lib.h" |
| 27 | |
| 28 | ARM_EABI_FNALIAS(ul2d, floatundidf); |
| 29 | |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 30 | #ifndef __SOFT_FP__ |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 31 | /* Support for systems that have hardware floating-point; we'll set the inexact flag |
| 32 | * as a side-effect of this computation. |
| 33 | */ |
| 34 | |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 35 | #include <stdint.h> |
| 36 | |
Anton Korobeynikov | 1c5f89b | 2011-04-19 17:52:09 +0000 | [diff] [blame^] | 37 | COMPILER_RT_ABI double |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 38 | __floatundidf(du_int a) |
| 39 | { |
| 40 | static const double twop52 = 0x1.0p52; |
| 41 | static const double twop84 = 0x1.0p84; |
| 42 | static const double twop84_plus_twop52 = 0x1.00000001p84; |
| 43 | |
| 44 | union { uint64_t x; double d; } high = { .d = twop84 }; |
| 45 | union { uint64_t x; double d; } low = { .d = twop52 }; |
| 46 | |
| 47 | high.x |= a >> 32; |
| 48 | low.x |= a & UINT64_C(0x00000000ffffffff); |
| 49 | |
| 50 | const double result = (high.d - twop84_plus_twop52) + low.d; |
| 51 | return result; |
| 52 | } |
| 53 | |
| 54 | #else |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 55 | /* Support for systems that don't have hardware floating-point; there are no flags to |
| 56 | * set, and we don't want to code-gen to an unknown soft-float implementation. |
| 57 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 58 | |
Anton Korobeynikov | 1c5f89b | 2011-04-19 17:52:09 +0000 | [diff] [blame^] | 59 | COMPILER_RT_ABI double |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 60 | __floatundidf(du_int a) |
| 61 | { |
| 62 | if (a == 0) |
| 63 | return 0.0; |
| 64 | const unsigned N = sizeof(du_int) * CHAR_BIT; |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 65 | int sd = N - __builtin_clzll(a); /* number of significant digits */ |
| 66 | int e = sd - 1; /* exponent */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 67 | if (sd > DBL_MANT_DIG) |
| 68 | { |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 69 | /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx |
| 70 | * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR |
| 71 | * 12345678901234567890123456 |
| 72 | * 1 = msb 1 bit |
| 73 | * P = bit DBL_MANT_DIG-1 bits to the right of 1 |
| 74 | * Q = bit DBL_MANT_DIG bits to the right of 1 |
| 75 | * R = "or" of all bits to the right of Q |
| 76 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 77 | switch (sd) |
| 78 | { |
| 79 | case DBL_MANT_DIG + 1: |
| 80 | a <<= 1; |
| 81 | break; |
| 82 | case DBL_MANT_DIG + 2: |
| 83 | break; |
| 84 | default: |
| 85 | a = (a >> (sd - (DBL_MANT_DIG+2))) | |
| 86 | ((a & ((du_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0); |
| 87 | }; |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 88 | /* finish: */ |
| 89 | a |= (a & 4) != 0; /* Or P into R */ |
| 90 | ++a; /* round - this step may add a significant bit */ |
| 91 | a >>= 2; /* dump Q and R */ |
| 92 | /* 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] | 93 | if (a & ((du_int)1 << DBL_MANT_DIG)) |
| 94 | { |
| 95 | a >>= 1; |
| 96 | ++e; |
| 97 | } |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 98 | /* a is now rounded to DBL_MANT_DIG bits */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 99 | } |
| 100 | else |
| 101 | { |
| 102 | a <<= (DBL_MANT_DIG - sd); |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 103 | /* a is now rounded to DBL_MANT_DIG bits */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 104 | } |
| 105 | double_bits fb; |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 106 | fb.u.high = ((e + 1023) << 20) | /* exponent */ |
| 107 | ((su_int)(a >> 32) & 0x000FFFFF); /* mantissa-high */ |
| 108 | fb.u.low = (su_int)a; /* mantissa-low */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 109 | return fb.f; |
| 110 | } |
Edward O'Callaghan | 6c307f0 | 2009-08-03 05:59:48 +0000 | [diff] [blame] | 111 | #endif |