dp_mul.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_mul(union ieee754dp x, union ieee754dp y)
  23. {
  24. int re;
  25. int rs;
  26. u64 rm;
  27. unsigned lxm;
  28. unsigned hxm;
  29. unsigned lym;
  30. unsigned hym;
  31. u64 lrm;
  32. u64 hrm;
  33. u64 t;
  34. u64 at;
  35. COMPXDP;
  36. COMPYDP;
  37. EXPLODEXDP;
  38. EXPLODEYDP;
  39. ieee754_clearcx();
  40. FLUSHXDP;
  41. FLUSHYDP;
  42. switch (CLPAIR(xc, yc)) {
  43. case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_SNAN):
  44. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_SNAN):
  45. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_SNAN):
  46. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_SNAN):
  47. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_SNAN):
  48. return ieee754dp_nanxcpt(y);
  49. case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_SNAN):
  50. case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_QNAN):
  51. case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_ZERO):
  52. case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_NORM):
  53. case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_DNORM):
  54. case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_INF):
  55. return ieee754dp_nanxcpt(x);
  56. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_QNAN):
  57. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_QNAN):
  58. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_QNAN):
  59. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_QNAN):
  60. return y;
  61. case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_QNAN):
  62. case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_ZERO):
  63. case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_NORM):
  64. case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_DNORM):
  65. case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_INF):
  66. return x;
  67. /*
  68. * Infinity handling
  69. */
  70. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_ZERO):
  71. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_INF):
  72. ieee754_setcx(IEEE754_INVALID_OPERATION);
  73. return ieee754dp_indef();
  74. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_INF):
  75. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_INF):
  76. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_NORM):
  77. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_DNORM):
  78. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_INF):
  79. return ieee754dp_inf(xs ^ ys);
  80. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_ZERO):
  81. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_NORM):
  82. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_DNORM):
  83. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_ZERO):
  84. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_ZERO):
  85. return ieee754dp_zero(xs ^ ys);
  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. /* rm = xm * ym, re = xe+ye basically */
  98. assert(xm & DP_HIDDEN_BIT);
  99. assert(ym & DP_HIDDEN_BIT);
  100. re = xe + ye;
  101. rs = xs ^ ys;
  102. /* shunt to top of word */
  103. xm <<= 64 - (DP_FBITS + 1);
  104. ym <<= 64 - (DP_FBITS + 1);
  105. /*
  106. * Multiply 64 bits xm, ym to give high 64 bits rm with stickness.
  107. */
  108. /* 32 * 32 => 64 */
  109. #define DPXMULT(x, y) ((u64)(x) * (u64)y)
  110. lxm = xm;
  111. hxm = xm >> 32;
  112. lym = ym;
  113. hym = ym >> 32;
  114. lrm = DPXMULT(lxm, lym);
  115. hrm = DPXMULT(hxm, hym);
  116. t = DPXMULT(lxm, hym);
  117. at = lrm + (t << 32);
  118. hrm += at < lrm;
  119. lrm = at;
  120. hrm = hrm + (t >> 32);
  121. t = DPXMULT(hxm, lym);
  122. at = lrm + (t << 32);
  123. hrm += at < lrm;
  124. lrm = at;
  125. hrm = hrm + (t >> 32);
  126. rm = hrm | (lrm != 0);
  127. /*
  128. * Sticky shift down to normal rounding precision.
  129. */
  130. if ((s64) rm < 0) {
  131. rm = (rm >> (64 - (DP_FBITS + 1 + 3))) |
  132. ((rm << (DP_FBITS + 1 + 3)) != 0);
  133. re++;
  134. } else {
  135. rm = (rm >> (64 - (DP_FBITS + 1 + 3 + 1))) |
  136. ((rm << (DP_FBITS + 1 + 3 + 1)) != 0);
  137. }
  138. assert(rm & (DP_HIDDEN_BIT << 3));
  139. return ieee754dp_format(rs, re, rm);
  140. }