dp_tint.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. * ########################################################################
  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 "ieee754dp.h"
  27. int ieee754dp_tint(ieee754dp x)
  28. {
  29. COMPXDP;
  30. CLEARCX;
  31. EXPLODEXDP;
  32. FLUSHXDP;
  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(), "dp_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. /* Set invalid. We will only use overflow for floating
  47. point overflow */
  48. SETCX(IEEE754_INVALID_OPERATION);
  49. return ieee754si_xcpt(ieee754si_indef(), "dp_tint", x);
  50. }
  51. /* oh gawd */
  52. if (xe > DP_MBITS) {
  53. xm <<= xe - DP_MBITS;
  54. } else if (xe < DP_MBITS) {
  55. u64 residue;
  56. int round;
  57. int sticky;
  58. int odd;
  59. if (xe < -1) {
  60. residue = xm;
  61. round = 0;
  62. sticky = residue != 0;
  63. xm = 0;
  64. } else {
  65. residue = xm << (64 - DP_MBITS + xe);
  66. round = (residue >> 63) != 0;
  67. sticky = (residue << 1) != 0;
  68. xm >>= DP_MBITS - xe;
  69. }
  70. /* Note: At this point upper 32 bits of xm are guaranteed
  71. to be zero */
  72. odd = (xm & 0x1) != 0x0;
  73. switch (ieee754_csr.rm) {
  74. case IEEE754_RN:
  75. if (round && (sticky || odd))
  76. xm++;
  77. break;
  78. case IEEE754_RZ:
  79. break;
  80. case IEEE754_RU: /* toward +Infinity */
  81. if ((round || sticky) && !xs)
  82. xm++;
  83. break;
  84. case IEEE754_RD: /* toward -Infinity */
  85. if ((round || sticky) && xs)
  86. xm++;
  87. break;
  88. }
  89. /* look for valid corner case 0x80000000 */
  90. if ((xm >> 31) != 0 && (xs == 0 || xm != 0x80000000)) {
  91. /* This can happen after rounding */
  92. SETCX(IEEE754_INVALID_OPERATION);
  93. return ieee754si_xcpt(ieee754si_indef(), "dp_tint", x);
  94. }
  95. if (round || sticky)
  96. SETCX(IEEE754_INEXACT);
  97. }
  98. if (xs)
  99. return -xm;
  100. else
  101. return xm;
  102. }
  103. unsigned int ieee754dp_tuns(ieee754dp x)
  104. {
  105. ieee754dp hb = ieee754dp_1e31();
  106. /* what if x < 0 ?? */
  107. if (ieee754dp_lt(x, hb))
  108. return (unsigned) ieee754dp_tint(x);
  109. return (unsigned) ieee754dp_tint(ieee754dp_sub(x, hb)) |
  110. ((unsigned) 1 << 31);
  111. }