13cab2bb3Spatrick //===-- floatundidf.c - Implement __floatundidf ---------------------------===// 23cab2bb3Spatrick // 33cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 43cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information. 53cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 63cab2bb3Spatrick // 73cab2bb3Spatrick //===----------------------------------------------------------------------===// 83cab2bb3Spatrick // 93cab2bb3Spatrick // This file implements __floatundidf for the compiler_rt library. 103cab2bb3Spatrick // 113cab2bb3Spatrick //===----------------------------------------------------------------------===// 123cab2bb3Spatrick 133cab2bb3Spatrick // Returns: convert a to a double, rounding toward even. 143cab2bb3Spatrick 153cab2bb3Spatrick // Assumption: double is a IEEE 64 bit floating point type 163cab2bb3Spatrick // du_int is a 64 bit integral type 173cab2bb3Spatrick 183cab2bb3Spatrick // seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm 193cab2bb3Spatrick // mmmm 203cab2bb3Spatrick 213cab2bb3Spatrick #include "int_lib.h" 223cab2bb3Spatrick 23d89ec533Spatrick #ifndef __SOFTFP__ 243cab2bb3Spatrick // Support for systems that have hardware floating-point; we'll set the inexact 253cab2bb3Spatrick // flag as a side-effect of this computation. 263cab2bb3Spatrick __floatundidf(du_int a)273cab2bb3SpatrickCOMPILER_RT_ABI double __floatundidf(du_int a) { 283cab2bb3Spatrick static const double twop52 = 4503599627370496.0; // 0x1.0p52 293cab2bb3Spatrick static const double twop84 = 19342813113834066795298816.0; // 0x1.0p84 303cab2bb3Spatrick static const double twop84_plus_twop52 = 313cab2bb3Spatrick 19342813118337666422669312.0; // 0x1.00000001p84 323cab2bb3Spatrick 333cab2bb3Spatrick union { 343cab2bb3Spatrick uint64_t x; 353cab2bb3Spatrick double d; 363cab2bb3Spatrick } high = {.d = twop84}; 373cab2bb3Spatrick union { 383cab2bb3Spatrick uint64_t x; 393cab2bb3Spatrick double d; 403cab2bb3Spatrick } low = {.d = twop52}; 413cab2bb3Spatrick 423cab2bb3Spatrick high.x |= a >> 32; 433cab2bb3Spatrick low.x |= a & UINT64_C(0x00000000ffffffff); 443cab2bb3Spatrick 453cab2bb3Spatrick const double result = (high.d - twop84_plus_twop52) + low.d; 463cab2bb3Spatrick return result; 473cab2bb3Spatrick } 483cab2bb3Spatrick 493cab2bb3Spatrick #else 503cab2bb3Spatrick // Support for systems that don't have hardware floating-point; there are no 513cab2bb3Spatrick // flags to set, and we don't want to code-gen to an unknown soft-float 523cab2bb3Spatrick // implementation. 533cab2bb3Spatrick __floatundidf(du_int a)543cab2bb3SpatrickCOMPILER_RT_ABI double __floatundidf(du_int a) { 553cab2bb3Spatrick if (a == 0) 563cab2bb3Spatrick return 0.0; 573cab2bb3Spatrick const unsigned N = sizeof(du_int) * CHAR_BIT; 583cab2bb3Spatrick int sd = N - __builtin_clzll(a); // number of significant digits 593cab2bb3Spatrick int e = sd - 1; // exponent 603cab2bb3Spatrick if (sd > DBL_MANT_DIG) { 613cab2bb3Spatrick // start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx 623cab2bb3Spatrick // finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR 633cab2bb3Spatrick // 12345678901234567890123456 643cab2bb3Spatrick // 1 = msb 1 bit 653cab2bb3Spatrick // P = bit DBL_MANT_DIG-1 bits to the right of 1 663cab2bb3Spatrick // Q = bit DBL_MANT_DIG bits to the right of 1 673cab2bb3Spatrick // R = "or" of all bits to the right of Q 683cab2bb3Spatrick switch (sd) { 693cab2bb3Spatrick case DBL_MANT_DIG + 1: 703cab2bb3Spatrick a <<= 1; 713cab2bb3Spatrick break; 723cab2bb3Spatrick case DBL_MANT_DIG + 2: 733cab2bb3Spatrick break; 743cab2bb3Spatrick default: 753cab2bb3Spatrick a = (a >> (sd - (DBL_MANT_DIG + 2))) | 763cab2bb3Spatrick ((a & ((du_int)(-1) >> ((N + DBL_MANT_DIG + 2) - sd))) != 0); 773cab2bb3Spatrick }; 783cab2bb3Spatrick // finish: 793cab2bb3Spatrick a |= (a & 4) != 0; // Or P into R 803cab2bb3Spatrick ++a; // round - this step may add a significant bit 813cab2bb3Spatrick a >>= 2; // dump Q and R 823cab2bb3Spatrick // a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits 833cab2bb3Spatrick if (a & ((du_int)1 << DBL_MANT_DIG)) { 843cab2bb3Spatrick a >>= 1; 853cab2bb3Spatrick ++e; 863cab2bb3Spatrick } 873cab2bb3Spatrick // a is now rounded to DBL_MANT_DIG bits 883cab2bb3Spatrick } else { 893cab2bb3Spatrick a <<= (DBL_MANT_DIG - sd); 903cab2bb3Spatrick // a is now rounded to DBL_MANT_DIG bits 913cab2bb3Spatrick } 923cab2bb3Spatrick double_bits fb; 931f9cb04fSpatrick fb.u.s.high = ((su_int)(e + 1023) << 20) | // exponent 943cab2bb3Spatrick ((su_int)(a >> 32) & 0x000FFFFF); // mantissa-high 953cab2bb3Spatrick fb.u.s.low = (su_int)a; // mantissa-low 963cab2bb3Spatrick return fb.f; 973cab2bb3Spatrick } 983cab2bb3Spatrick #endif 993cab2bb3Spatrick 1003cab2bb3Spatrick #if defined(__ARM_EABI__) 1013cab2bb3Spatrick #if defined(COMPILER_RT_ARMHF_TARGET) __aeabi_ul2d(du_int a)1023cab2bb3SpatrickAEABI_RTABI double __aeabi_ul2d(du_int a) { return __floatundidf(a); } 1033cab2bb3Spatrick #else 1043cab2bb3Spatrick COMPILER_RT_ALIAS(__floatundidf, __aeabi_ul2d) 1053cab2bb3Spatrick #endif 1063cab2bb3Spatrick #endif 107*810390e3Srobert 108*810390e3Srobert #if defined(__MINGW32__) && defined(__arm__) 109*810390e3Srobert COMPILER_RT_ALIAS(__floatundidf, __u64tod) 110*810390e3Srobert #endif 111