dp_sub.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. * 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 "ieee754dp.h"
  22. union ieee754dp ieee754dp_sub(union ieee754dp x, union ieee754dp y)
  23. {
  24. int s;
  25. COMPXDP;
  26. COMPYDP;
  27. EXPLODEXDP;
  28. EXPLODEYDP;
  29. ieee754_clearcx();
  30. FLUSHXDP;
  31. FLUSHYDP;
  32. switch (CLPAIR(xc, yc)) {
  33. case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_SNAN):
  34. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_SNAN):
  35. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_SNAN):
  36. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_SNAN):
  37. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_SNAN):
  38. return ieee754dp_nanxcpt(y);
  39. case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_SNAN):
  40. case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_QNAN):
  41. case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_ZERO):
  42. case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_NORM):
  43. case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_DNORM):
  44. case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_INF):
  45. return ieee754dp_nanxcpt(x);
  46. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_QNAN):
  47. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_QNAN):
  48. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_QNAN):
  49. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_QNAN):
  50. return y;
  51. case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_QNAN):
  52. case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_ZERO):
  53. case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_NORM):
  54. case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_DNORM):
  55. case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_INF):
  56. return x;
  57. /*
  58. * Infinity handling
  59. */
  60. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_INF):
  61. if (xs != ys)
  62. return x;
  63. ieee754_setcx(IEEE754_INVALID_OPERATION);
  64. return ieee754dp_indef();
  65. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_INF):
  66. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_INF):
  67. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_INF):
  68. return ieee754dp_inf(ys ^ 1);
  69. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_ZERO):
  70. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_NORM):
  71. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_DNORM):
  72. return x;
  73. /*
  74. * Zero handling
  75. */
  76. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_ZERO):
  77. if (xs != ys)
  78. return x;
  79. else
  80. return ieee754dp_zero(ieee754_csr.rm == FPU_CSR_RD);
  81. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_ZERO):
  82. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_ZERO):
  83. return x;
  84. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_NORM):
  85. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_DNORM):
  86. /* quick fix up */
  87. DPSIGN(y) ^= 1;
  88. return y;
  89. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_DNORM):
  90. DPDNORMX;
  91. /* FALL THROUGH */
  92. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_DNORM):
  93. /* normalize ym,ye */
  94. DPDNORMY;
  95. break;
  96. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_NORM):
  97. /* normalize xm,xe */
  98. DPDNORMX;
  99. break;
  100. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_NORM):
  101. break;
  102. }
  103. /* flip sign of y and handle as add */
  104. ys ^= 1;
  105. assert(xm & DP_HIDDEN_BIT);
  106. assert(ym & DP_HIDDEN_BIT);
  107. /* provide guard,round and stick bit dpace */
  108. xm <<= 3;
  109. ym <<= 3;
  110. if (xe > ye) {
  111. /*
  112. * Have to shift y fraction right to align
  113. */
  114. s = xe - ye;
  115. ym = XDPSRS(ym, s);
  116. ye += s;
  117. } else if (ye > xe) {
  118. /*
  119. * Have to shift x fraction right to align
  120. */
  121. s = ye - xe;
  122. xm = XDPSRS(xm, s);
  123. xe += s;
  124. }
  125. assert(xe == ye);
  126. assert(xe <= DP_EMAX);
  127. if (xs == ys) {
  128. /* generate 28 bit result of adding two 27 bit numbers
  129. */
  130. xm = xm + ym;
  131. if (xm >> (DP_FBITS + 1 + 3)) { /* carry out */
  132. xm = XDPSRS1(xm); /* shift preserving sticky */
  133. xe++;
  134. }
  135. } else {
  136. if (xm >= ym) {
  137. xm = xm - ym;
  138. } else {
  139. xm = ym - xm;
  140. xs = ys;
  141. }
  142. if (xm == 0) {
  143. if (ieee754_csr.rm == FPU_CSR_RD)
  144. return ieee754dp_zero(1); /* round negative inf. => sign = -1 */
  145. else
  146. return ieee754dp_zero(0); /* other round modes => sign = 1 */
  147. }
  148. /* normalize to rounding precision
  149. */
  150. while ((xm >> (DP_FBITS + 3)) == 0) {
  151. xm <<= 1;
  152. xe--;
  153. }
  154. }
  155. return ieee754dp_format(xs, xe, xm);
  156. }