floatuntisf.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* ===-- floatuntisf.c - Implement __floatuntisf ---------------------------===
  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 __floatuntisf for the compiler_rt library.
  11. *
  12. * ===----------------------------------------------------------------------===
  13. */
  14. #include "int_lib.h"
  15. #ifdef CRT_HAS_128BIT
  16. /* Returns: convert a to a float, rounding toward even. */
  17. /* Assumption: float is a IEEE 32 bit floating point type
  18. * tu_int is a 128 bit integral type
  19. */
  20. /* seee eeee emmm mmmm mmmm mmmm mmmm mmmm */
  21. COMPILER_RT_ABI float
  22. __floatuntisf(tu_int a)
  23. {
  24. if (a == 0)
  25. return 0.0F;
  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 > FLT_MANT_DIG)
  30. {
  31. /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
  32. * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
  33. * 12345678901234567890123456
  34. * 1 = msb 1 bit
  35. * P = bit FLT_MANT_DIG-1 bits to the right of 1
  36. * Q = bit FLT_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 FLT_MANT_DIG + 1:
  42. a <<= 1;
  43. break;
  44. case FLT_MANT_DIG + 2:
  45. break;
  46. default:
  47. a = (a >> (sd - (FLT_MANT_DIG+2))) |
  48. ((a & ((tu_int)(-1) >> ((N + FLT_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 FLT_MANT_DIG or FLT_MANT_DIG+1 bits */
  55. if (a & ((tu_int)1 << FLT_MANT_DIG))
  56. {
  57. a >>= 1;
  58. ++e;
  59. }
  60. /* a is now rounded to FLT_MANT_DIG bits */
  61. }
  62. else
  63. {
  64. a <<= (FLT_MANT_DIG - sd);
  65. /* a is now rounded to FLT_MANT_DIG bits */
  66. }
  67. float_bits fb;
  68. fb.u = ((e + 127) << 23) | /* exponent */
  69. ((su_int)a & 0x007FFFFF); /* mantissa */
  70. return fb.f;
  71. }
  72. #endif /* CRT_HAS_128BIT */