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