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