1156cd587Sjoerg /* ===-- floatuntidf.c - Implement __floatuntidf ---------------------------=== 2156cd587Sjoerg * 3156cd587Sjoerg * The LLVM Compiler Infrastructure 4156cd587Sjoerg * 5156cd587Sjoerg * This file is dual licensed under the MIT and the University of Illinois Open 6156cd587Sjoerg * Source Licenses. See LICENSE.TXT for details. 7156cd587Sjoerg * 8156cd587Sjoerg * ===----------------------------------------------------------------------=== 9156cd587Sjoerg * 10156cd587Sjoerg * This file implements __floatuntidf for the compiler_rt library. 11156cd587Sjoerg * 12156cd587Sjoerg * ===----------------------------------------------------------------------=== 13156cd587Sjoerg */ 14156cd587Sjoerg 15156cd587Sjoerg #include "int_lib.h" 16156cd587Sjoerg 17156cd587Sjoerg #ifdef CRT_HAS_128BIT 18156cd587Sjoerg 19156cd587Sjoerg /* Returns: convert a to a double, rounding toward even. */ 20156cd587Sjoerg 21156cd587Sjoerg /* Assumption: double is a IEEE 64 bit floating point type 22156cd587Sjoerg * tu_int is a 128 bit integral type 23156cd587Sjoerg */ 24156cd587Sjoerg 25156cd587Sjoerg /* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */ 26156cd587Sjoerg 27*f7f78b33Sjoerg COMPILER_RT_ABI double __floatuntidf(tu_int a)28156cd587Sjoerg__floatuntidf(tu_int a) 29156cd587Sjoerg { 30156cd587Sjoerg if (a == 0) 31156cd587Sjoerg return 0.0; 32156cd587Sjoerg const unsigned N = sizeof(tu_int) * CHAR_BIT; 33156cd587Sjoerg int sd = N - __clzti2(a); /* number of significant digits */ 34156cd587Sjoerg int e = sd - 1; /* exponent */ 35156cd587Sjoerg if (sd > DBL_MANT_DIG) 36156cd587Sjoerg { 37156cd587Sjoerg /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx 38156cd587Sjoerg * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR 39156cd587Sjoerg * 12345678901234567890123456 40156cd587Sjoerg * 1 = msb 1 bit 41156cd587Sjoerg * P = bit DBL_MANT_DIG-1 bits to the right of 1 42156cd587Sjoerg * Q = bit DBL_MANT_DIG bits to the right of 1 43156cd587Sjoerg * R = "or" of all bits to the right of Q 44156cd587Sjoerg */ 45156cd587Sjoerg switch (sd) 46156cd587Sjoerg { 47156cd587Sjoerg case DBL_MANT_DIG + 1: 48156cd587Sjoerg a <<= 1; 49156cd587Sjoerg break; 50156cd587Sjoerg case DBL_MANT_DIG + 2: 51156cd587Sjoerg break; 52156cd587Sjoerg default: 53156cd587Sjoerg a = (a >> (sd - (DBL_MANT_DIG+2))) | 54156cd587Sjoerg ((a & ((tu_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0); 55156cd587Sjoerg }; 56156cd587Sjoerg /* finish: */ 57156cd587Sjoerg a |= (a & 4) != 0; /* Or P into R */ 58156cd587Sjoerg ++a; /* round - this step may add a significant bit */ 59156cd587Sjoerg a >>= 2; /* dump Q and R */ 60156cd587Sjoerg /* a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits */ 61156cd587Sjoerg if (a & ((tu_int)1 << DBL_MANT_DIG)) 62156cd587Sjoerg { 63156cd587Sjoerg a >>= 1; 64156cd587Sjoerg ++e; 65156cd587Sjoerg } 66156cd587Sjoerg /* a is now rounded to DBL_MANT_DIG bits */ 67156cd587Sjoerg } 68156cd587Sjoerg else 69156cd587Sjoerg { 70156cd587Sjoerg a <<= (DBL_MANT_DIG - sd); 71156cd587Sjoerg /* a is now rounded to DBL_MANT_DIG bits */ 72156cd587Sjoerg } 73156cd587Sjoerg double_bits fb; 74156cd587Sjoerg fb.u.s.high = ((e + 1023) << 20) | /* exponent */ 75156cd587Sjoerg ((su_int)(a >> 32) & 0x000FFFFF); /* mantissa-high */ 76156cd587Sjoerg fb.u.s.low = (su_int)a; /* mantissa-low */ 77156cd587Sjoerg return fb.f; 78156cd587Sjoerg } 79156cd587Sjoerg 80156cd587Sjoerg #endif /* CRT_HAS_128BIT */ 81