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