1 1.1 joerg /* ===-- floattidf.c - Implement __floattidf -------------------------------=== 2 1.1 joerg * 3 1.1 joerg * The LLVM Compiler Infrastructure 4 1.1 joerg * 5 1.1 joerg * This file is dual licensed under the MIT and the University of Illinois Open 6 1.1 joerg * Source Licenses. See LICENSE.TXT for details. 7 1.1 joerg * 8 1.1 joerg * ===----------------------------------------------------------------------=== 9 1.1 joerg * 10 1.1 joerg * This file implements __floattidf for the compiler_rt library. 11 1.1 joerg * 12 1.1 joerg * ===----------------------------------------------------------------------=== 13 1.1 joerg */ 14 1.1 joerg 15 1.1 joerg #include "int_lib.h" 16 1.1 joerg 17 1.1 joerg #ifdef CRT_HAS_128BIT 18 1.1 joerg 19 1.1 joerg /* Returns: convert a to a double, rounding toward even.*/ 20 1.1 joerg 21 1.1 joerg /* Assumption: double is a IEEE 64 bit floating point type 22 1.1 joerg * ti_int is a 128 bit integral type 23 1.1 joerg */ 24 1.1 joerg 25 1.1 joerg /* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */ 26 1.1 joerg 27 1.1.1.2 joerg COMPILER_RT_ABI double 28 1.1 joerg __floattidf(ti_int a) 29 1.1 joerg { 30 1.1 joerg if (a == 0) 31 1.1 joerg return 0.0; 32 1.1 joerg const unsigned N = sizeof(ti_int) * CHAR_BIT; 33 1.1 joerg const ti_int s = a >> (N-1); 34 1.1 joerg a = (a ^ s) - s; 35 1.1 joerg int sd = N - __clzti2(a); /* number of significant digits */ 36 1.1 joerg int e = sd - 1; /* exponent */ 37 1.1 joerg if (sd > DBL_MANT_DIG) 38 1.1 joerg { 39 1.1 joerg /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx 40 1.1 joerg * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR 41 1.1 joerg * 12345678901234567890123456 42 1.1 joerg * 1 = msb 1 bit 43 1.1 joerg * P = bit DBL_MANT_DIG-1 bits to the right of 1 44 1.1 joerg * Q = bit DBL_MANT_DIG bits to the right of 1 45 1.1 joerg * R = "or" of all bits to the right of Q 46 1.1 joerg */ 47 1.1 joerg switch (sd) 48 1.1 joerg { 49 1.1 joerg case DBL_MANT_DIG + 1: 50 1.1 joerg a <<= 1; 51 1.1 joerg break; 52 1.1 joerg case DBL_MANT_DIG + 2: 53 1.1 joerg break; 54 1.1 joerg default: 55 1.1 joerg a = ((tu_int)a >> (sd - (DBL_MANT_DIG+2))) | 56 1.1 joerg ((a & ((tu_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0); 57 1.1 joerg }; 58 1.1 joerg /* finish: */ 59 1.1 joerg a |= (a & 4) != 0; /* Or P into R */ 60 1.1 joerg ++a; /* round - this step may add a significant bit */ 61 1.1 joerg a >>= 2; /* dump Q and R */ 62 1.1 joerg /* a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits */ 63 1.1 joerg if (a & ((tu_int)1 << DBL_MANT_DIG)) 64 1.1 joerg { 65 1.1 joerg a >>= 1; 66 1.1 joerg ++e; 67 1.1 joerg } 68 1.1 joerg /* a is now rounded to DBL_MANT_DIG bits */ 69 1.1 joerg } 70 1.1 joerg else 71 1.1 joerg { 72 1.1 joerg a <<= (DBL_MANT_DIG - sd); 73 1.1 joerg /* a is now rounded to DBL_MANT_DIG bits */ 74 1.1 joerg } 75 1.1 joerg double_bits fb; 76 1.1 joerg fb.u.s.high = ((su_int)s & 0x80000000) | /* sign */ 77 1.1 joerg ((e + 1023) << 20) | /* exponent */ 78 1.1 joerg ((su_int)(a >> 32) & 0x000FFFFF); /* mantissa-high */ 79 1.1 joerg fb.u.s.low = (su_int)a; /* mantissa-low */ 80 1.1 joerg return fb.f; 81 1.1 joerg } 82 1.1 joerg 83 1.1 joerg #endif /* CRT_HAS_128BIT */ 84