Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +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 | |
| 14 | #if __x86_64 |
| 15 | |
| 16 | #include "int_lib.h" |
| 17 | #include <float.h> |
| 18 | |
| 19 | // Returns: convert a to a long double, rounding toward even. |
| 20 | |
| 21 | // Assumption: long double is a IEEE 80 bit floating point type padded to 128 bits |
| 22 | // ti_int is a 128 bit integral type |
| 23 | |
| 24 | // gggg gggg gggg gggg gggg gggg gggg gggg | gggg gggg gggg gggg seee eeee eeee eeee | |
| 25 | // 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm |
| 26 | |
| 27 | si_int __clzti2(ti_int a); |
| 28 | |
| 29 | long double |
| 30 | __floattixf(ti_int a) |
| 31 | { |
| 32 | if (a == 0) |
| 33 | return 0.0; |
| 34 | const unsigned N = sizeof(ti_int) * CHAR_BIT; |
| 35 | const ti_int s = a >> (N-1); |
| 36 | a = (a ^ s) - s; |
| 37 | int sd = N - __clzti2(a); // number of significant digits |
| 38 | int e = sd - 1; // exponent |
| 39 | if (sd > LDBL_MANT_DIG) |
| 40 | { |
| 41 | // start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx |
| 42 | // finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR |
| 43 | // 12345678901234567890123456 |
| 44 | // 1 = msb 1 bit |
| 45 | // P = bit LDBL_MANT_DIG-1 bits to the right of 1 |
| 46 | // Q = bit LDBL_MANT_DIG bits to the right of 1 |
| 47 | // R = "or" of all bits to the right of Q |
| 48 | switch (sd) |
| 49 | { |
| 50 | case LDBL_MANT_DIG + 1: |
| 51 | a <<= 1; |
| 52 | break; |
| 53 | case LDBL_MANT_DIG + 2: |
| 54 | break; |
| 55 | default: |
| 56 | a = ((tu_int)a >> (sd - (LDBL_MANT_DIG+2))) | |
| 57 | ((a & ((tu_int)(-1) >> ((N + LDBL_MANT_DIG+2) - sd))) != 0); |
| 58 | }; |
| 59 | // finish: |
| 60 | a |= (a & 4) != 0; // Or P into R |
| 61 | ++a; // round - this step may add a significant bit |
| 62 | a >>= 2; // dump Q and R |
| 63 | // a is now rounded to LDBL_MANT_DIG or LDBL_MANT_DIG+1 bits |
| 64 | if (a & ((tu_int)1 << LDBL_MANT_DIG)) |
| 65 | { |
| 66 | a >>= 1; |
| 67 | ++e; |
| 68 | } |
| 69 | // a is now rounded to LDBL_MANT_DIG bits |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | a <<= (LDBL_MANT_DIG - sd); |
| 74 | // a is now rounded to LDBL_MANT_DIG bits |
| 75 | } |
| 76 | long_double_bits fb; |
| 77 | fb.u.high.low = ((su_int)s & 0x8000) | // sign |
| 78 | (e + 16383); // exponent |
| 79 | fb.u.low.all = (du_int)a; // mantissa |
| 80 | return fb.f; |
| 81 | } |
| 82 | |
| 83 | #endif |