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