sp_tint.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* IEEE754 floating point arithmetic
  2. * single precision
  3. */
  4. /*
  5. * MIPS floating point support
  6. * Copyright (C) 1994-2000 Algorithmics Ltd.
  7. *
  8. * ########################################################################
  9. *
  10. * This program is free software; you can distribute it and/or modify it
  11. * under the terms of the GNU General Public License (Version 2) as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  17. * for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * ########################################################################
  24. */
  25. #include <linux/kernel.h>
  26. #include "ieee754sp.h"
  27. int ieee754sp_tint(ieee754sp x)
  28. {
  29. COMPXSP;
  30. CLEARCX;
  31. EXPLODEXSP;
  32. FLUSHXSP;
  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 ieee754si_xcpt(ieee754si_indef(), "sp_tint", 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 >= 31) {
  46. /* look for valid corner case */
  47. if (xe == 31 && xs && xm == SP_HIDDEN_BIT)
  48. return -0x80000000;
  49. /* Set invalid. We will only use overflow for floating
  50. point overflow */
  51. SETCX(IEEE754_INVALID_OPERATION);
  52. return ieee754si_xcpt(ieee754si_indef(), "sp_tint", x);
  53. }
  54. /* oh gawd */
  55. if (xe > SP_MBITS) {
  56. xm <<= xe - SP_MBITS;
  57. } else {
  58. u32 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. } else {
  68. /* Shifting a u32 32 times does not work,
  69. * so we do it in two steps. Be aware that xe
  70. * may be -1 */
  71. residue = xm << (xe + 1);
  72. residue <<= 31 - SP_MBITS;
  73. round = (residue >> 31) != 0;
  74. sticky = (residue << 1) != 0;
  75. xm >>= SP_MBITS - xe;
  76. }
  77. odd = (xm & 0x1) != 0x0;
  78. switch (ieee754_csr.rm) {
  79. case IEEE754_RN:
  80. if (round && (sticky || odd))
  81. xm++;
  82. break;
  83. case IEEE754_RZ:
  84. break;
  85. case IEEE754_RU: /* toward +Infinity */
  86. if ((round || sticky) && !xs)
  87. xm++;
  88. break;
  89. case IEEE754_RD: /* toward -Infinity */
  90. if ((round || sticky) && xs)
  91. xm++;
  92. break;
  93. }
  94. if ((xm >> 31) != 0) {
  95. /* This can happen after rounding */
  96. SETCX(IEEE754_INVALID_OPERATION);
  97. return ieee754si_xcpt(ieee754si_indef(), "sp_tint", x);
  98. }
  99. if (round || sticky)
  100. SETCX(IEEE754_INEXACT);
  101. }
  102. if (xs)
  103. return -xm;
  104. else
  105. return xm;
  106. }
  107. unsigned int ieee754sp_tuns(ieee754sp x)
  108. {
  109. ieee754sp hb = ieee754sp_1e31();
  110. /* what if x < 0 ?? */
  111. if (ieee754sp_lt(x, hb))
  112. return (unsigned) ieee754sp_tint(x);
  113. return (unsigned) ieee754sp_tint(ieee754sp_sub(x, hb)) |
  114. ((unsigned) 1 << 31);
  115. }