dp_div.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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_div(union ieee754dp x, union ieee754dp y)
  23. {
  24. u64 rm;
  25. int re;
  26. u64 bm;
  27. COMPXDP;
  28. COMPYDP;
  29. EXPLODEXDP;
  30. EXPLODEYDP;
  31. ieee754_clearcx();
  32. FLUSHXDP;
  33. FLUSHYDP;
  34. switch (CLPAIR(xc, yc)) {
  35. case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_SNAN):
  36. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_SNAN):
  37. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_SNAN):
  38. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_SNAN):
  39. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_SNAN):
  40. return ieee754dp_nanxcpt(y);
  41. case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_SNAN):
  42. case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_QNAN):
  43. case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_ZERO):
  44. case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_NORM):
  45. case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_DNORM):
  46. case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_INF):
  47. return ieee754dp_nanxcpt(x);
  48. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_QNAN):
  49. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_QNAN):
  50. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_QNAN):
  51. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_QNAN):
  52. return y;
  53. case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_QNAN):
  54. case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_ZERO):
  55. case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_NORM):
  56. case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_DNORM):
  57. case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_INF):
  58. return x;
  59. /*
  60. * Infinity handling
  61. */
  62. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_INF):
  63. ieee754_setcx(IEEE754_INVALID_OPERATION);
  64. return ieee754dp_indef();
  65. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_INF):
  66. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_INF):
  67. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_INF):
  68. return ieee754dp_zero(xs ^ ys);
  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 ieee754dp_inf(xs ^ ys);
  73. /*
  74. * Zero handling
  75. */
  76. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_ZERO):
  77. ieee754_setcx(IEEE754_INVALID_OPERATION);
  78. return ieee754dp_indef();
  79. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_ZERO):
  80. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_ZERO):
  81. ieee754_setcx(IEEE754_ZERO_DIVIDE);
  82. return ieee754dp_inf(xs ^ ys);
  83. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_NORM):
  84. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_DNORM):
  85. return ieee754dp_zero(xs == ys ? 0 : 1);
  86. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_DNORM):
  87. DPDNORMX;
  88. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_DNORM):
  89. DPDNORMY;
  90. break;
  91. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_NORM):
  92. DPDNORMX;
  93. break;
  94. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_NORM):
  95. break;
  96. }
  97. assert(xm & DP_HIDDEN_BIT);
  98. assert(ym & DP_HIDDEN_BIT);
  99. /* provide rounding space */
  100. xm <<= 3;
  101. ym <<= 3;
  102. /* now the dirty work */
  103. rm = 0;
  104. re = xe - ye;
  105. for (bm = DP_MBIT(DP_FBITS + 2); bm; bm >>= 1) {
  106. if (xm >= ym) {
  107. xm -= ym;
  108. rm |= bm;
  109. if (xm == 0)
  110. break;
  111. }
  112. xm <<= 1;
  113. }
  114. rm <<= 1;
  115. if (xm)
  116. rm |= 1; /* have remainder, set sticky */
  117. assert(rm);
  118. /*
  119. * Normalise rm to rounding precision ?
  120. */
  121. while ((rm >> (DP_FBITS + 3)) == 0) {
  122. rm <<= 1;
  123. re--;
  124. }
  125. return ieee754dp_format(xs == ys ? 0 : 1, re, rm);
  126. }