floattidf.c revision 239138
18876Srgrimes/* ===-- floattidf.c - Implement __floattidf -------------------------------=== 24Srgrimes * 34Srgrimes * The LLVM Compiler Infrastructure 44Srgrimes * 58876Srgrimes * This file is dual licensed under the MIT and the University of Illinois Open 64Srgrimes * Source Licenses. See LICENSE.TXT for details. 74Srgrimes * 84Srgrimes * ===----------------------------------------------------------------------=== 94Srgrimes * 104Srgrimes * This file implements __floattidf for the compiler_rt library. 118876Srgrimes * 128876Srgrimes * ===----------------------------------------------------------------------=== 134Srgrimes */ 144Srgrimes 158876Srgrimes#include "int_lib.h" 164Srgrimes 178876Srgrimes#if __x86_64 184Srgrimes 194Srgrimes/* Returns: convert a to a double, rounding toward even.*/ 204Srgrimes 214Srgrimes/* Assumption: double is a IEEE 64 bit floating point type 228876Srgrimes * ti_int is a 128 bit integral type 234Srgrimes */ 244Srgrimes 254Srgrimes/* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */ 2650477Speter 274Srgrimessi_int __clzti2(ti_int a); 28623Srgrimes 29716Swollmandouble 3012472Sbde__floattidf(ti_int a) 31716Swollman{ 324Srgrimes if (a == 0) 334Srgrimes return 0.0; 344Srgrimes const unsigned N = sizeof(ti_int) * CHAR_BIT; 354Srgrimes const ti_int s = a >> (N-1); 364Srgrimes a = (a ^ s) - s; 374Srgrimes int sd = N - __clzti2(a); /* number of significant digits */ 384Srgrimes int e = sd - 1; /* exponent */ 394Srgrimes if (sd > DBL_MANT_DIG) 404Srgrimes { 4112472Sbde /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx 4212472Sbde * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR 4312472Sbde * 12345678901234567890123456 4412472Sbde * 1 = msb 1 bit 4512472Sbde * P = bit DBL_MANT_DIG-1 bits to the right of 1 46 * Q = bit DBL_MANT_DIG bits to the right of 1 47 * R = "or" of all bits to the right of Q 48 */ 49 switch (sd) 50 { 51 case DBL_MANT_DIG + 1: 52 a <<= 1; 53 break; 54 case DBL_MANT_DIG + 2: 55 break; 56 default: 57 a = ((tu_int)a >> (sd - (DBL_MANT_DIG+2))) | 58 ((a & ((tu_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0); 59 }; 60 /* finish: */ 61 a |= (a & 4) != 0; /* Or P into R */ 62 ++a; /* round - this step may add a significant bit */ 63 a >>= 2; /* dump Q and R */ 64 /* a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits */ 65 if (a & ((tu_int)1 << DBL_MANT_DIG)) 66 { 67 a >>= 1; 68 ++e; 69 } 70 /* a is now rounded to DBL_MANT_DIG bits */ 71 } 72 else 73 { 74 a <<= (DBL_MANT_DIG - sd); 75 /* a is now rounded to DBL_MANT_DIG bits */ 76 } 77 double_bits fb; 78 fb.u.s.high = ((su_int)s & 0x80000000) | /* sign */ 79 ((e + 1023) << 20) | /* exponent */ 80 ((su_int)(a >> 32) & 0x000FFFFF); /* mantissa-high */ 81 fb.u.s.low = (su_int)a; /* mantissa-low */ 82 return fb.f; 83} 84 85#endif 86