floattisf.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* ===-- floattisf.c - Implement __floattisf -------------------------------===
  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 __floattisf 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. * ti_int is a 128 bit integral type
  19. */
  20. /* seee eeee emmm mmmm mmmm mmmm mmmm mmmm */
  21. COMPILER_RT_ABI float
  22. __floattisf(ti_int a)
  23. {
  24. if (a == 0)
  25. return 0.0F;
  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 > FLT_MANT_DIG)
  32. {
  33. /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
  34. * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
  35. * 12345678901234567890123456
  36. * 1 = msb 1 bit
  37. * P = bit FLT_MANT_DIG-1 bits to the right of 1
  38. * Q = bit FLT_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 FLT_MANT_DIG + 1:
  44. a <<= 1;
  45. break;
  46. case FLT_MANT_DIG + 2:
  47. break;
  48. default:
  49. a = ((tu_int)a >> (sd - (FLT_MANT_DIG+2))) |
  50. ((a & ((tu_int)(-1) >> ((N + FLT_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 FLT_MANT_DIG or FLT_MANT_DIG+1 bits */
  57. if (a & ((tu_int)1 << FLT_MANT_DIG))
  58. {
  59. a >>= 1;
  60. ++e;
  61. }
  62. /* a is now rounded to FLT_MANT_DIG bits */
  63. }
  64. else
  65. {
  66. a <<= (FLT_MANT_DIG - sd);
  67. /* a is now rounded to FLT_MANT_DIG bits */
  68. }
  69. float_bits fb;
  70. fb.u = ((su_int)s & 0x80000000) | /* sign */
  71. ((e + 127) << 23) | /* exponent */
  72. ((su_int)a & 0x007FFFFF); /* mantissa */
  73. return fb.f;
  74. }
  75. #endif /* CRT_HAS_128BIT */