dp_tlong.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* IEEE754 floating point arithmetic
  2. * double precision: common utilities
  3. */
  4. /*
  5. * MIPS floating point support
  6. * Copyright (C) 1994-2000 Algorithmics Ltd.
  7. *
  8. * ########################################################################
  9. *
  10. * This program is free software; you can distribute it and/or modify it
  11. * under the terms of the GNU General Public License (Version 2) as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  17. * for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * ########################################################################
  24. */
  25. #include "ieee754dp.h"
  26. s64 ieee754dp_tlong(ieee754dp x)
  27. {
  28. COMPXDP;
  29. CLEARCX;
  30. EXPLODEXDP;
  31. FLUSHXDP;
  32. switch (xc) {
  33. case IEEE754_CLASS_SNAN:
  34. case IEEE754_CLASS_QNAN:
  35. case IEEE754_CLASS_INF:
  36. SETCX(IEEE754_INVALID_OPERATION);
  37. return ieee754di_xcpt(ieee754di_indef(), "dp_tlong", x);
  38. case IEEE754_CLASS_ZERO:
  39. return 0;
  40. case IEEE754_CLASS_DNORM:
  41. case IEEE754_CLASS_NORM:
  42. break;
  43. }
  44. if (xe >= 63) {
  45. /* look for valid corner case */
  46. if (xe == 63 && xs && xm == DP_HIDDEN_BIT)
  47. return -0x8000000000000000LL;
  48. /* Set invalid. We will only use overflow for floating
  49. point overflow */
  50. SETCX(IEEE754_INVALID_OPERATION);
  51. return ieee754di_xcpt(ieee754di_indef(), "dp_tlong", x);
  52. }
  53. /* oh gawd */
  54. if (xe > DP_MBITS) {
  55. xm <<= xe - DP_MBITS;
  56. } else if (xe < DP_MBITS) {
  57. u64 residue;
  58. int round;
  59. int sticky;
  60. int odd;
  61. if (xe < -1) {
  62. residue = xm;
  63. round = 0;
  64. sticky = residue != 0;
  65. xm = 0;
  66. } else {
  67. /* Shifting a u64 64 times does not work,
  68. * so we do it in two steps. Be aware that xe
  69. * may be -1 */
  70. residue = xm << (xe + 1);
  71. residue <<= 63 - DP_MBITS;
  72. round = (residue >> 63) != 0;
  73. sticky = (residue << 1) != 0;
  74. xm >>= DP_MBITS - xe;
  75. }
  76. odd = (xm & 0x1) != 0x0;
  77. switch (ieee754_csr.rm) {
  78. case IEEE754_RN:
  79. if (round && (sticky || odd))
  80. xm++;
  81. break;
  82. case IEEE754_RZ:
  83. break;
  84. case IEEE754_RU: /* toward +Infinity */
  85. if ((round || sticky) && !xs)
  86. xm++;
  87. break;
  88. case IEEE754_RD: /* toward -Infinity */
  89. if ((round || sticky) && xs)
  90. xm++;
  91. break;
  92. }
  93. if ((xm >> 63) != 0) {
  94. /* This can happen after rounding */
  95. SETCX(IEEE754_INVALID_OPERATION);
  96. return ieee754di_xcpt(ieee754di_indef(), "dp_tlong", x);
  97. }
  98. if (round || sticky)
  99. SETCX(IEEE754_INEXACT);
  100. }
  101. if (xs)
  102. return -xm;
  103. else
  104. return xm;
  105. }
  106. u64 ieee754dp_tulong(ieee754dp x)
  107. {
  108. ieee754dp hb = ieee754dp_1e63();
  109. /* what if x < 0 ?? */
  110. if (ieee754dp_lt(x, hb))
  111. return (u64) ieee754dp_tlong(x);
  112. return (u64) ieee754dp_tlong(ieee754dp_sub(x, hb)) |
  113. (1ULL << 63);
  114. }