floattidf.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* ===-- floattidf.c - Implement __floattidf -------------------------------===
  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 __floattidf 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. * ti_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. __floattidf(ti_int a)
  23. {
  24. if (a == 0)
  25. return 0.0;
  26. const unsigned N = sizeof(ti_int) * CHAR_BIT;
  27. const ti_int s = a >> (N-1);
  28. a = (a ^ s) - s;
  29. int sd = N - __clzti2(a); /* number of significant digits */
  30. int e = sd - 1; /* exponent */
  31. if (sd > DBL_MANT_DIG)
  32. {
  33. /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
  34. * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
  35. * 12345678901234567890123456
  36. * 1 = msb 1 bit
  37. * P = bit DBL_MANT_DIG-1 bits to the right of 1
  38. * Q = bit DBL_MANT_DIG bits to the right of 1
  39. * R = "or" of all bits to the right of Q
  40. */
  41. switch (sd)
  42. {
  43. case DBL_MANT_DIG + 1:
  44. a <<= 1;
  45. break;
  46. case DBL_MANT_DIG + 2:
  47. break;
  48. default:
  49. a = ((tu_int)a >> (sd - (DBL_MANT_DIG+2))) |
  50. ((a & ((tu_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0);
  51. };
  52. /* finish: */
  53. a |= (a & 4) != 0; /* Or P into R */
  54. ++a; /* round - this step may add a significant bit */
  55. a >>= 2; /* dump Q and R */
  56. /* a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits */
  57. if (a & ((tu_int)1 << DBL_MANT_DIG))
  58. {
  59. a >>= 1;
  60. ++e;
  61. }
  62. /* a is now rounded to DBL_MANT_DIG bits */
  63. }
  64. else
  65. {
  66. a <<= (DBL_MANT_DIG - sd);
  67. /* a is now rounded to DBL_MANT_DIG bits */
  68. }
  69. double_bits fb;
  70. fb.u.s.high = ((su_int)s & 0x80000000) | /* sign */
  71. ((e + 1023) << 20) | /* exponent */
  72. ((su_int)(a >> 32) & 0x000FFFFF); /* mantissa-high */
  73. fb.u.s.low = (su_int)a; /* mantissa-low */
  74. return fb.f;
  75. }
  76. #endif /* CRT_HAS_128BIT */