Path: blob/master/libs/compiler-rt/lib/builtins/floatundidf.c
4395 views
/* ===-- floatundidf.c - Implement __floatundidf ---------------------------===1*2* The LLVM Compiler Infrastructure3*4* This file is dual licensed under the MIT and the University of Illinois Open5* Source Licenses. See LICENSE.TXT for details.6*7* ===----------------------------------------------------------------------===8*9* This file implements __floatundidf for the compiler_rt library.10*11* ===----------------------------------------------------------------------===12*/1314/* Returns: convert a to a double, rounding toward even. */1516/* Assumption: double is a IEEE 64 bit floating point type17* du_int is a 64 bit integral type18*/1920/* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */2122#include "int_lib.h"2324#ifndef __SOFT_FP__25/* Support for systems that have hardware floating-point; we'll set the inexact flag26* as a side-effect of this computation.27*/2829COMPILER_RT_ABI double30__floatundidf(du_int a)31{32static const double twop52 = 4503599627370496.0; // 0x1.0p5233static const double twop84 = 19342813113834066795298816.0; // 0x1.0p8434static const double twop84_plus_twop52 = 19342813118337666422669312.0; // 0x1.00000001p843536union { uint64_t x; double d; } high = { .d = twop84 };37union { uint64_t x; double d; } low = { .d = twop52 };3839high.x |= a >> 32;40low.x |= a & UINT64_C(0x00000000ffffffff);4142const double result = (high.d - twop84_plus_twop52) + low.d;43return result;44}4546#else47/* Support for systems that don't have hardware floating-point; there are no flags to48* set, and we don't want to code-gen to an unknown soft-float implementation.49*/5051COMPILER_RT_ABI double52__floatundidf(du_int a)53{54if (a == 0)55return 0.0;56const unsigned N = sizeof(du_int) * CHAR_BIT;57int sd = N - __builtin_clzll(a); /* number of significant digits */58int e = sd - 1; /* exponent */59if (sd > DBL_MANT_DIG)60{61/* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx62* finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR63* 1234567890123456789012345664* 1 = msb 1 bit65* P = bit DBL_MANT_DIG-1 bits to the right of 166* Q = bit DBL_MANT_DIG bits to the right of 167* R = "or" of all bits to the right of Q68*/69switch (sd)70{71case DBL_MANT_DIG + 1:72a <<= 1;73break;74case DBL_MANT_DIG + 2:75break;76default:77a = (a >> (sd - (DBL_MANT_DIG+2))) |78((a & ((du_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0);79};80/* finish: */81a |= (a & 4) != 0; /* Or P into R */82++a; /* round - this step may add a significant bit */83a >>= 2; /* dump Q and R */84/* a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits */85if (a & ((du_int)1 << DBL_MANT_DIG))86{87a >>= 1;88++e;89}90/* a is now rounded to DBL_MANT_DIG bits */91}92else93{94a <<= (DBL_MANT_DIG - sd);95/* a is now rounded to DBL_MANT_DIG bits */96}97double_bits fb;98fb.u.s.high = ((e + 1023) << 20) | /* exponent */99((su_int)(a >> 32) & 0x000FFFFF); /* mantissa-high */100fb.u.s.low = (su_int)a; /* mantissa-low */101return fb.f;102}103#endif104105#if defined(__ARM_EABI__)106#if defined(COMPILER_RT_ARMHF_TARGET)107AEABI_RTABI double __aeabi_ul2d(du_int a) {108return __floatundidf(a);109}110#else111AEABI_RTABI double __aeabi_ul2d(du_int a) COMPILER_RT_ALIAS(__floatundidf);112#endif113#endif114115116