Edward O'Callaghan | 2bf6272 | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 1 | /* ===-- floattixf.c - Implement __floattixf -------------------------------=== |
| 2 | * |
| 3 | * The LLVM Compiler Infrastructure |
| 4 | * |
| 5 | * This file is distributed under the University of Illinois Open Source |
| 6 | * License. See LICENSE.TXT for details. |
| 7 | * |
| 8 | * ===----------------------------------------------------------------------=== |
| 9 | * |
| 10 | * This file implements __floattixf for the compiler_rt library. |
| 11 | * |
| 12 | * ===----------------------------------------------------------------------=== |
| 13 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 14 | |
| 15 | #if __x86_64 |
| 16 | |
| 17 | #include "int_lib.h" |
| 18 | #include <float.h> |
| 19 | |
Edward O'Callaghan | 2bf6272 | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 20 | /* Returns: convert a to a long double, rounding toward even. */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 21 | |
Edward O'Callaghan | 2bf6272 | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 22 | /* Assumption: long double is a IEEE 80 bit floating point type padded to 128 bits |
| 23 | * ti_int is a 128 bit integral type |
| 24 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 25 | |
Edward O'Callaghan | 2bf6272 | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 26 | /* gggg gggg gggg gggg gggg gggg gggg gggg | gggg gggg gggg gggg seee eeee eeee eeee | |
| 27 | * 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm |
| 28 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 29 | |
| 30 | si_int __clzti2(ti_int a); |
| 31 | |
| 32 | long double |
| 33 | __floattixf(ti_int a) |
| 34 | { |
| 35 | if (a == 0) |
| 36 | return 0.0; |
| 37 | const unsigned N = sizeof(ti_int) * CHAR_BIT; |
| 38 | const ti_int s = a >> (N-1); |
| 39 | a = (a ^ s) - s; |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 40 | int sd = N - __clzti2(a); /* number of significant digits */ |
| 41 | int e = sd - 1; /* exponent */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 42 | if (sd > LDBL_MANT_DIG) |
| 43 | { |
Edward O'Callaghan | 2bf6272 | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 44 | /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx |
| 45 | * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR |
| 46 | * 12345678901234567890123456 |
| 47 | * 1 = msb 1 bit |
| 48 | * P = bit LDBL_MANT_DIG-1 bits to the right of 1 |
| 49 | * Q = bit LDBL_MANT_DIG bits to the right of 1 |
| 50 | * R = "or" of all bits to the right of Q |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 51 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 52 | switch (sd) |
| 53 | { |
| 54 | case LDBL_MANT_DIG + 1: |
| 55 | a <<= 1; |
| 56 | break; |
| 57 | case LDBL_MANT_DIG + 2: |
| 58 | break; |
| 59 | default: |
| 60 | a = ((tu_int)a >> (sd - (LDBL_MANT_DIG+2))) | |
| 61 | ((a & ((tu_int)(-1) >> ((N + LDBL_MANT_DIG+2) - sd))) != 0); |
| 62 | }; |
Edward O'Callaghan | 2bf6272 | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 63 | /* finish: */ |
| 64 | a |= (a & 4) != 0; /* Or P into R */ |
| 65 | ++a; /* round - this step may add a significant bit */ |
| 66 | a >>= 2; /* dump Q and R */ |
| 67 | /* a is now rounded to LDBL_MANT_DIG or LDBL_MANT_DIG+1 bits */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 68 | if (a & ((tu_int)1 << LDBL_MANT_DIG)) |
| 69 | { |
| 70 | a >>= 1; |
| 71 | ++e; |
| 72 | } |
Edward O'Callaghan | 2bf6272 | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 73 | /* a is now rounded to LDBL_MANT_DIG bits */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 74 | } |
| 75 | else |
| 76 | { |
| 77 | a <<= (LDBL_MANT_DIG - sd); |
Edward O'Callaghan | 2bf6272 | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 78 | /* a is now rounded to LDBL_MANT_DIG bits */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 79 | } |
| 80 | long_double_bits fb; |
Edward O'Callaghan | aabd961 | 2009-09-03 09:12:20 +0000 | [diff] [blame^] | 81 | fb.u.high.s.low = ((su_int)s & 0x8000) | /* sign */ |
Edward O'Callaghan | 2bf6272 | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 82 | (e + 16383); /* exponent */ |
| 83 | fb.u.low.all = (du_int)a; /* mantissa */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 84 | return fb.f; |
| 85 | } |
| 86 | |
| 87 | #endif |