1 //===-- floatdidf.c - Implement __floatdidf -------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements __floatdidf for the compiler_rt library. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "int_lib.h" 14 15 // Returns: convert a to a double, rounding toward even. 16 17 // Assumption: double is a IEEE 64 bit floating point type 18 // di_int is a 64 bit integral type 19 20 // seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm 21 // mmmm 22 23 #ifndef __SOFT_FP__ 24 // Support for systems that have hardware floating-point; we'll set the inexact 25 // flag as a side-effect of this computation. 26 27 COMPILER_RT_ABI double __floatdidf(di_int a) { 28 static const double twop52 = 4503599627370496.0; // 0x1.0p52 29 static const double twop32 = 4294967296.0; // 0x1.0p32 30 31 union { 32 int64_t x; 33 double d; 34 } low = {.d = twop52}; 35 36 const double high = (int32_t)(a >> 32) * twop32; 37 low.x |= a & INT64_C(0x00000000ffffffff); 38 39 const double result = (high - twop52) + low.d; 40 return result; 41 } 42 43 #else 44 // Support for systems that don't have hardware floating-point; there are no 45 // flags to set, and we don't want to code-gen to an unknown soft-float 46 // implementation. 47 48 COMPILER_RT_ABI double __floatdidf(di_int a) { 49 if (a == 0) 50 return 0.0; 51 const unsigned N = sizeof(di_int) * CHAR_BIT; 52 const di_int s = a >> (N - 1); 53 a = (a ^ s) - s; 54 int sd = N - __builtin_clzll(a); // number of significant digits 55 int e = sd - 1; // exponent 56 if (sd > DBL_MANT_DIG) { 57 // start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx 58 // finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR 59 // 12345678901234567890123456 60 // 1 = msb 1 bit 61 // P = bit DBL_MANT_DIG-1 bits to the right of 1 62 // Q = bit DBL_MANT_DIG bits to the right of 1 63 // R = "or" of all bits to the right of Q 64 switch (sd) { 65 case DBL_MANT_DIG + 1: 66 a <<= 1; 67 break; 68 case DBL_MANT_DIG + 2: 69 break; 70 default: 71 a = ((du_int)a >> (sd - (DBL_MANT_DIG + 2))) | 72 ((a & ((du_int)(-1) >> ((N + DBL_MANT_DIG + 2) - sd))) != 0); 73 }; 74 // finish: 75 a |= (a & 4) != 0; // Or P into R 76 ++a; // round - this step may add a significant bit 77 a >>= 2; // dump Q and R 78 // a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits 79 if (a & ((du_int)1 << DBL_MANT_DIG)) { 80 a >>= 1; 81 ++e; 82 } 83 // a is now rounded to DBL_MANT_DIG bits 84 } else { 85 a <<= (DBL_MANT_DIG - sd); 86 // a is now rounded to DBL_MANT_DIG bits 87 } 88 double_bits fb; 89 fb.u.s.high = ((su_int)s & 0x80000000) | // sign 90 ((su_int)(e + 1023) << 20) | // exponent 91 ((su_int)(a >> 32) & 0x000FFFFF); // mantissa-high 92 fb.u.s.low = (su_int)a; // mantissa-low 93 return fb.f; 94 } 95 #endif 96 97 #if defined(__ARM_EABI__) 98 #if defined(COMPILER_RT_ARMHF_TARGET) 99 AEABI_RTABI double __aeabi_l2d(di_int a) { return __floatdidf(a); } 100 #else 101 COMPILER_RT_ALIAS(__floatdidf, __aeabi_l2d) 102 #endif 103 #endif 104