sp_tlong.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* IEEE754 floating point arithmetic
  2. * single precision
  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 "ieee754sp.h"
  22. #include "ieee754dp.h"
  23. s64 ieee754sp_tlong(union ieee754sp x)
  24. {
  25. u32 residue;
  26. int round;
  27. int sticky;
  28. int odd;
  29. COMPXDP; /* <-- need 64-bit mantissa tmp */
  30. ieee754_clearcx();
  31. EXPLODEXSP;
  32. FLUSHXSP;
  33. switch (xc) {
  34. case IEEE754_CLASS_SNAN:
  35. case IEEE754_CLASS_QNAN:
  36. ieee754_setcx(IEEE754_INVALID_OPERATION);
  37. return ieee754di_indef();
  38. case IEEE754_CLASS_INF:
  39. ieee754_setcx(IEEE754_INVALID_OPERATION);
  40. return ieee754di_overflow(xs);
  41. case IEEE754_CLASS_ZERO:
  42. return 0;
  43. case IEEE754_CLASS_DNORM:
  44. case IEEE754_CLASS_NORM:
  45. break;
  46. }
  47. if (xe >= 63) {
  48. /* look for valid corner case */
  49. if (xe == 63 && xs && xm == SP_HIDDEN_BIT)
  50. return -0x8000000000000000LL;
  51. /* Set invalid. We will only use overflow for floating
  52. point overflow */
  53. ieee754_setcx(IEEE754_INVALID_OPERATION);
  54. return ieee754di_overflow(xs);
  55. }
  56. /* oh gawd */
  57. if (xe > SP_FBITS) {
  58. xm <<= xe - SP_FBITS;
  59. } else if (xe < SP_FBITS) {
  60. if (xe < -1) {
  61. residue = xm;
  62. round = 0;
  63. sticky = residue != 0;
  64. xm = 0;
  65. } else {
  66. residue = xm << (32 - SP_FBITS + xe);
  67. round = (residue >> 31) != 0;
  68. sticky = (residue << 1) != 0;
  69. xm >>= SP_FBITS - xe;
  70. }
  71. odd = (xm & 0x1) != 0x0;
  72. switch (ieee754_csr.rm) {
  73. case FPU_CSR_RN:
  74. if (round && (sticky || odd))
  75. xm++;
  76. break;
  77. case FPU_CSR_RZ:
  78. break;
  79. case FPU_CSR_RU: /* toward +Infinity */
  80. if ((round || sticky) && !xs)
  81. xm++;
  82. break;
  83. case FPU_CSR_RD: /* toward -Infinity */
  84. if ((round || sticky) && xs)
  85. xm++;
  86. break;
  87. }
  88. if ((xm >> 63) != 0) {
  89. /* This can happen after rounding */
  90. ieee754_setcx(IEEE754_INVALID_OPERATION);
  91. return ieee754di_overflow(xs);
  92. }
  93. if (round || sticky)
  94. ieee754_setcx(IEEE754_INEXACT);
  95. }
  96. if (xs)
  97. return -xm;
  98. else
  99. return xm;
  100. }