dp_tlong.c 3.0 KB

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