floatuntidf.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* ===-- floatuntidf.c - Implement __floatuntidf ---------------------------===
  2. *
  3. * The LLVM Compiler Infrastructure
  4. *
  5. * This file is dual licensed under the MIT and the University of Illinois Open
  6. * Source Licenses. See LICENSE.TXT for details.
  7. *
  8. * ===----------------------------------------------------------------------===
  9. *
  10. * This file implements __floatuntidf for the compiler_rt library.
  11. *
  12. * ===----------------------------------------------------------------------===
  13. */
  14. #include "int_lib.h"
  15. #ifdef CRT_HAS_128BIT
  16. /* Returns: convert a to a double, rounding toward even. */
  17. /* Assumption: double is a IEEE 64 bit floating point type
  18. * tu_int is a 128 bit integral type
  19. */
  20. /* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */
  21. COMPILER_RT_ABI double
  22. __floatuntidf(tu_int a)
  23. {
  24. if (a == 0)
  25. return 0.0;
  26. const unsigned N = sizeof(tu_int) * CHAR_BIT;
  27. int sd = N - __clzti2(a); /* number of significant digits */
  28. int e = sd - 1; /* exponent */
  29. if (sd > DBL_MANT_DIG)
  30. {
  31. /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
  32. * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
  33. * 12345678901234567890123456
  34. * 1 = msb 1 bit
  35. * P = bit DBL_MANT_DIG-1 bits to the right of 1
  36. * Q = bit DBL_MANT_DIG bits to the right of 1
  37. * R = "or" of all bits to the right of Q
  38. */
  39. switch (sd)
  40. {
  41. case DBL_MANT_DIG + 1:
  42. a <<= 1;
  43. break;
  44. case DBL_MANT_DIG + 2:
  45. break;
  46. default:
  47. a = (a >> (sd - (DBL_MANT_DIG+2))) |
  48. ((a & ((tu_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0);
  49. };
  50. /* finish: */
  51. a |= (a & 4) != 0; /* Or P into R */
  52. ++a; /* round - this step may add a significant bit */
  53. a >>= 2; /* dump Q and R */
  54. /* a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits */
  55. if (a & ((tu_int)1 << DBL_MANT_DIG))
  56. {
  57. a >>= 1;
  58. ++e;
  59. }
  60. /* a is now rounded to DBL_MANT_DIG bits */
  61. }
  62. else
  63. {
  64. a <<= (DBL_MANT_DIG - sd);
  65. /* a is now rounded to DBL_MANT_DIG bits */
  66. }
  67. double_bits fb;
  68. fb.u.s.high = ((e + 1023) << 20) | /* exponent */
  69. ((su_int)(a >> 32) & 0x000FFFFF); /* mantissa-high */
  70. fb.u.s.low = (su_int)a; /* mantissa-low */
  71. return fb.f;
  72. }
  73. #endif /* CRT_HAS_128BIT */