dp_tlong.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. * This program is free software; you can distribute it and/or modify it
  9. * under the terms of the GNU General Public License (Version 2) as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  15. * for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. #include "ieee754dp.h"
  22. s64 ieee754dp_tlong(union ieee754dp x)
  23. {
  24. u64 residue;
  25. int round;
  26. int sticky;
  27. int odd;
  28. COMPXDP;
  29. ieee754_clearcx();
  30. EXPLODEXDP;
  31. FLUSHXDP;
  32. switch (xc) {
  33. case IEEE754_CLASS_SNAN:
  34. case IEEE754_CLASS_QNAN:
  35. case IEEE754_CLASS_INF:
  36. ieee754_setcx(IEEE754_INVALID_OPERATION);
  37. return ieee754di_indef();
  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. ieee754_setcx(IEEE754_INVALID_OPERATION);
  51. return ieee754di_indef();
  52. }
  53. /* oh gawd */
  54. if (xe > DP_FBITS) {
  55. xm <<= xe - DP_FBITS;
  56. } else if (xe < DP_FBITS) {
  57. if (xe < -1) {
  58. residue = xm;
  59. round = 0;
  60. sticky = residue != 0;
  61. xm = 0;
  62. } else {
  63. /* Shifting a u64 64 times does not work,
  64. * so we do it in two steps. Be aware that xe
  65. * may be -1 */
  66. residue = xm << (xe + 1);
  67. residue <<= 63 - DP_FBITS;
  68. round = (residue >> 63) != 0;
  69. sticky = (residue << 1) != 0;
  70. xm >>= DP_FBITS - xe;
  71. }
  72. odd = (xm & 0x1) != 0x0;
  73. switch (ieee754_csr.rm) {
  74. case FPU_CSR_RN:
  75. if (round && (sticky || odd))
  76. xm++;
  77. break;
  78. case FPU_CSR_RZ:
  79. break;
  80. case FPU_CSR_RU: /* toward +Infinity */
  81. if ((round || sticky) && !xs)
  82. xm++;
  83. break;
  84. case FPU_CSR_RD: /* toward -Infinity */
  85. if ((round || sticky) && xs)
  86. xm++;
  87. break;
  88. }
  89. if ((xm >> 63) != 0) {
  90. /* This can happen after rounding */
  91. ieee754_setcx(IEEE754_INVALID_OPERATION);
  92. return ieee754di_indef();
  93. }
  94. if (round || sticky)
  95. ieee754_setcx(IEEE754_INEXACT);
  96. }
  97. if (xs)
  98. return -xm;
  99. else
  100. return xm;
  101. }