sp_mul.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* IEEE754 floating point arithmetic
  2. * single precision
  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 "ieee754sp.h"
  22. union ieee754sp ieee754sp_mul(union ieee754sp x, union ieee754sp y)
  23. {
  24. int re;
  25. int rs;
  26. unsigned rm;
  27. unsigned short lxm;
  28. unsigned short hxm;
  29. unsigned short lym;
  30. unsigned short hym;
  31. unsigned lrm;
  32. unsigned hrm;
  33. unsigned t;
  34. unsigned at;
  35. COMPXSP;
  36. COMPYSP;
  37. EXPLODEXSP;
  38. EXPLODEYSP;
  39. ieee754_clearcx();
  40. FLUSHXSP;
  41. FLUSHYSP;
  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 ieee754sp_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 ieee754sp_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 ieee754sp_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 ieee754sp_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 ieee754sp_zero(xs ^ ys);
  86. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_DNORM):
  87. SPDNORMX;
  88. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_DNORM):
  89. SPDNORMY;
  90. break;
  91. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_NORM):
  92. SPDNORMX;
  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 & SP_HIDDEN_BIT);
  99. assert(ym & SP_HIDDEN_BIT);
  100. re = xe + ye;
  101. rs = xs ^ ys;
  102. /* shunt to top of word */
  103. xm <<= 32 - (SP_FBITS + 1);
  104. ym <<= 32 - (SP_FBITS + 1);
  105. /*
  106. * Multiply 32 bits xm, ym to give high 32 bits rm with stickness.
  107. */
  108. lxm = xm & 0xffff;
  109. hxm = xm >> 16;
  110. lym = ym & 0xffff;
  111. hym = ym >> 16;
  112. lrm = lxm * lym; /* 16 * 16 => 32 */
  113. hrm = hxm * hym; /* 16 * 16 => 32 */
  114. t = lxm * hym; /* 16 * 16 => 32 */
  115. at = lrm + (t << 16);
  116. hrm += at < lrm;
  117. lrm = at;
  118. hrm = hrm + (t >> 16);
  119. t = hxm * lym; /* 16 * 16 => 32 */
  120. at = lrm + (t << 16);
  121. hrm += at < lrm;
  122. lrm = at;
  123. hrm = hrm + (t >> 16);
  124. rm = hrm | (lrm != 0);
  125. /*
  126. * Sticky shift down to normal rounding precision.
  127. */
  128. if ((int) rm < 0) {
  129. rm = (rm >> (32 - (SP_FBITS + 1 + 3))) |
  130. ((rm << (SP_FBITS + 1 + 3)) != 0);
  131. re++;
  132. } else {
  133. rm = (rm >> (32 - (SP_FBITS + 1 + 3 + 1))) |
  134. ((rm << (SP_FBITS + 1 + 3 + 1)) != 0);
  135. }
  136. assert(rm & (SP_HIDDEN_BIT << 3));
  137. return ieee754sp_format(rs, re, rm);
  138. }