sp_tlong.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. case IEEE754_CLASS_INF:
  37. ieee754_setcx(IEEE754_INVALID_OPERATION);
  38. return ieee754di_indef();
  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 == SP_HIDDEN_BIT)
  48. return -0x8000000000000000LL;
  49. /* Set invalid. We will only use overflow for floating
  50. point overflow */
  51. ieee754_setcx(IEEE754_INVALID_OPERATION);
  52. return ieee754di_indef();
  53. }
  54. /* oh gawd */
  55. if (xe > SP_FBITS) {
  56. xm <<= xe - SP_FBITS;
  57. } else if (xe < SP_FBITS) {
  58. if (xe < -1) {
  59. residue = xm;
  60. round = 0;
  61. sticky = residue != 0;
  62. xm = 0;
  63. } else {
  64. residue = xm << (32 - SP_FBITS + xe);
  65. round = (residue >> 31) != 0;
  66. sticky = (residue << 1) != 0;
  67. xm >>= SP_FBITS - xe;
  68. }
  69. odd = (xm & 0x1) != 0x0;
  70. switch (ieee754_csr.rm) {
  71. case FPU_CSR_RN:
  72. if (round && (sticky || odd))
  73. xm++;
  74. break;
  75. case FPU_CSR_RZ:
  76. break;
  77. case FPU_CSR_RU: /* toward +Infinity */
  78. if ((round || sticky) && !xs)
  79. xm++;
  80. break;
  81. case FPU_CSR_RD: /* toward -Infinity */
  82. if ((round || sticky) && xs)
  83. xm++;
  84. break;
  85. }
  86. if ((xm >> 63) != 0) {
  87. /* This can happen after rounding */
  88. ieee754_setcx(IEEE754_INVALID_OPERATION);
  89. return ieee754di_indef();
  90. }
  91. if (round || sticky)
  92. ieee754_setcx(IEEE754_INEXACT);
  93. }
  94. if (xs)
  95. return -xm;
  96. else
  97. return xm;
  98. }