floatundidf.c revision b3a6901e66f55b35aa9e01bcb24134e6a65ea004
1//===-- floatundidf.c - Implement __floatundidf ---------------------------===// 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 __floatundidf for the compiler_rt library. 11// 12//===----------------------------------------------------------------------===// 13 14#include "int_lib.h" 15#include <float.h> 16 17// Returns: convert a to a double, rounding toward even. 18 19// Assumption: double is a IEEE 64 bit floating point type 20// du_int is a 64 bit integral type 21 22// seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm 23 24#ifndef __SOFT_FP__ 25// Support for systems that have hardware floating-point; we'll set the inexact flag 26// as a side-effect of this computation. 27#include <stdint.h> 28 29double 30__floatundidf(du_int a) 31{ 32 static const double twop52 = 0x1.0p52; 33 static const double twop84 = 0x1.0p84; 34 static const double twop84_plus_twop52 = 0x1.00000001p84; 35 36 union { uint64_t x; double d; } high = { .d = twop84 }; 37 union { uint64_t x; double d; } low = { .d = twop52 }; 38 39 high.x |= a >> 32; 40 low.x |= a & UINT64_C(0x00000000ffffffff); 41 42 const double result = (high.d - twop84_plus_twop52) + low.d; 43 return result; 44} 45 46#else 47// Support for systems that don't have hardware floating-point; there are no flags to 48// set, and we don't want to code-gen to an unknown soft-float implementation. 49 50double 51__floatundidf(du_int a) 52{ 53 if (a == 0) 54 return 0.0; 55 const unsigned N = sizeof(du_int) * CHAR_BIT; 56 int sd = N - __builtin_clzll(a); // number of significant digits 57 int e = sd - 1; // exponent 58 if (sd > DBL_MANT_DIG) 59 { 60 // start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx 61 // finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR 62 // 12345678901234567890123456 63 // 1 = msb 1 bit 64 // P = bit DBL_MANT_DIG-1 bits to the right of 1 65 // Q = bit DBL_MANT_DIG bits to the right of 1 66 // R = "or" of all bits to the right of Q 67 switch (sd) 68 { 69 case DBL_MANT_DIG + 1: 70 a <<= 1; 71 break; 72 case DBL_MANT_DIG + 2: 73 break; 74 default: 75 a = (a >> (sd - (DBL_MANT_DIG+2))) | 76 ((a & ((du_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0); 77 }; 78 // finish: 79 a |= (a & 4) != 0; // Or P into R 80 ++a; // round - this step may add a significant bit 81 a >>= 2; // dump Q and R 82 // a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits 83 if (a & ((du_int)1 << DBL_MANT_DIG)) 84 { 85 a >>= 1; 86 ++e; 87 } 88 // a is now rounded to DBL_MANT_DIG bits 89 } 90 else 91 { 92 a <<= (DBL_MANT_DIG - sd); 93 // a is now rounded to DBL_MANT_DIG bits 94 } 95 double_bits fb; 96 fb.u.high = ((e + 1023) << 20) | // exponent 97 ((su_int)(a >> 32) & 0x000FFFFF); // mantissa-high 98 fb.u.low = (su_int)a; // mantissa-low 99 return fb.f; 100} 101#endif