sp_tint.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. int ieee754sp_tint(union ieee754sp x)
  23. {
  24. u32 residue;
  25. int round;
  26. int sticky;
  27. int odd;
  28. COMPXSP;
  29. ieee754_clearcx();
  30. EXPLODEXSP;
  31. FLUSHXSP;
  32. switch (xc) {
  33. case IEEE754_CLASS_SNAN:
  34. case IEEE754_CLASS_QNAN:
  35. ieee754_setcx(IEEE754_INVALID_OPERATION);
  36. return ieee754si_indef();
  37. case IEEE754_CLASS_INF:
  38. ieee754_setcx(IEEE754_INVALID_OPERATION);
  39. return ieee754si_overflow(xs);
  40. case IEEE754_CLASS_ZERO:
  41. return 0;
  42. case IEEE754_CLASS_DNORM:
  43. case IEEE754_CLASS_NORM:
  44. break;
  45. }
  46. if (xe >= 31) {
  47. /* look for valid corner case */
  48. if (xe == 31 && xs && xm == SP_HIDDEN_BIT)
  49. return -0x80000000;
  50. /* Set invalid. We will only use overflow for floating
  51. point overflow */
  52. ieee754_setcx(IEEE754_INVALID_OPERATION);
  53. return ieee754si_overflow(xs);
  54. }
  55. /* oh gawd */
  56. if (xe > SP_FBITS) {
  57. xm <<= xe - SP_FBITS;
  58. } else {
  59. if (xe < -1) {
  60. residue = xm;
  61. round = 0;
  62. sticky = residue != 0;
  63. xm = 0;
  64. } else {
  65. /* Shifting a u32 32 times does not work,
  66. * so we do it in two steps. Be aware that xe
  67. * may be -1 */
  68. residue = xm << (xe + 1);
  69. residue <<= 31 - SP_FBITS;
  70. round = (residue >> 31) != 0;
  71. sticky = (residue << 1) != 0;
  72. xm >>= SP_FBITS - xe;
  73. }
  74. odd = (xm & 0x1) != 0x0;
  75. switch (ieee754_csr.rm) {
  76. case FPU_CSR_RN:
  77. if (round && (sticky || odd))
  78. xm++;
  79. break;
  80. case FPU_CSR_RZ:
  81. break;
  82. case FPU_CSR_RU: /* toward +Infinity */
  83. if ((round || sticky) && !xs)
  84. xm++;
  85. break;
  86. case FPU_CSR_RD: /* toward -Infinity */
  87. if ((round || sticky) && xs)
  88. xm++;
  89. break;
  90. }
  91. if ((xm >> 31) != 0) {
  92. /* This can happen after rounding */
  93. ieee754_setcx(IEEE754_INVALID_OPERATION);
  94. return ieee754si_overflow(xs);
  95. }
  96. if (round || sticky)
  97. ieee754_setcx(IEEE754_INEXACT);
  98. }
  99. if (xs)
  100. return -xm;
  101. else
  102. return xm;
  103. }