floatdidf.c 3.5 KB

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