sp_msubf.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * IEEE754 floating point arithmetic
  3. * single precision: MSUB.f (Fused Multiply Subtract)
  4. * MSUBF.fmt: FPR[fd] = FPR[fd] - (FPR[fs] x FPR[ft])
  5. *
  6. * MIPS floating point support
  7. * Copyright (C) 2015 Imagination Technologies, Ltd.
  8. * Author: Markos Chandras <markos.chandras@imgtec.com>
  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 as published by the
  12. * Free Software Foundation; version 2 of the License.
  13. */
  14. #include "ieee754sp.h"
  15. union ieee754sp ieee754sp_msubf(union ieee754sp z, union ieee754sp x,
  16. union ieee754sp y)
  17. {
  18. int re;
  19. int rs;
  20. unsigned rm;
  21. unsigned short lxm;
  22. unsigned short hxm;
  23. unsigned short lym;
  24. unsigned short hym;
  25. unsigned lrm;
  26. unsigned hrm;
  27. unsigned t;
  28. unsigned at;
  29. int s;
  30. COMPXSP;
  31. COMPYSP;
  32. u32 zm; int ze; int zs __maybe_unused; int zc;
  33. EXPLODEXSP;
  34. EXPLODEYSP;
  35. EXPLODESP(z, zc, zs, ze, zm)
  36. FLUSHXSP;
  37. FLUSHYSP;
  38. FLUSHSP(z, zc, zs, ze, zm);
  39. ieee754_clearcx();
  40. switch (zc) {
  41. case IEEE754_CLASS_SNAN:
  42. ieee754_setcx(IEEE754_INVALID_OPERATION);
  43. return ieee754sp_nanxcpt(z);
  44. case IEEE754_CLASS_DNORM:
  45. SPDNORMx(zm, ze);
  46. /* QNAN is handled separately below */
  47. }
  48. switch (CLPAIR(xc, yc)) {
  49. case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_SNAN):
  50. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_SNAN):
  51. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_SNAN):
  52. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_SNAN):
  53. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_SNAN):
  54. return ieee754sp_nanxcpt(y);
  55. case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_SNAN):
  56. case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_QNAN):
  57. case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_ZERO):
  58. case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_NORM):
  59. case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_DNORM):
  60. case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_INF):
  61. return ieee754sp_nanxcpt(x);
  62. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_QNAN):
  63. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_QNAN):
  64. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_QNAN):
  65. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_QNAN):
  66. return y;
  67. case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_QNAN):
  68. case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_ZERO):
  69. case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_NORM):
  70. case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_DNORM):
  71. case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_INF):
  72. return x;
  73. /*
  74. * Infinity handling
  75. */
  76. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_ZERO):
  77. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_INF):
  78. if (zc == IEEE754_CLASS_QNAN)
  79. return z;
  80. ieee754_setcx(IEEE754_INVALID_OPERATION);
  81. return ieee754sp_indef();
  82. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_INF):
  83. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_INF):
  84. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_NORM):
  85. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_DNORM):
  86. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_INF):
  87. if (zc == IEEE754_CLASS_QNAN)
  88. return z;
  89. return ieee754sp_inf(xs ^ ys);
  90. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_ZERO):
  91. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_NORM):
  92. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_DNORM):
  93. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_ZERO):
  94. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_ZERO):
  95. if (zc == IEEE754_CLASS_INF)
  96. return ieee754sp_inf(zs);
  97. /* Multiplication is 0 so just return z */
  98. return z;
  99. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_DNORM):
  100. SPDNORMX;
  101. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_DNORM):
  102. if (zc == IEEE754_CLASS_QNAN)
  103. return z;
  104. else if (zc == IEEE754_CLASS_INF)
  105. return ieee754sp_inf(zs);
  106. SPDNORMY;
  107. break;
  108. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_NORM):
  109. if (zc == IEEE754_CLASS_QNAN)
  110. return z;
  111. else if (zc == IEEE754_CLASS_INF)
  112. return ieee754sp_inf(zs);
  113. SPDNORMX;
  114. break;
  115. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_NORM):
  116. if (zc == IEEE754_CLASS_QNAN)
  117. return z;
  118. else if (zc == IEEE754_CLASS_INF)
  119. return ieee754sp_inf(zs);
  120. /* fall through to real compuation */
  121. }
  122. /* Finally get to do some computation */
  123. /*
  124. * Do the multiplication bit first
  125. *
  126. * rm = xm * ym, re = xe + ye basically
  127. *
  128. * At this point xm and ym should have been normalized.
  129. */
  130. /* rm = xm * ym, re = xe+ye basically */
  131. assert(xm & SP_HIDDEN_BIT);
  132. assert(ym & SP_HIDDEN_BIT);
  133. re = xe + ye;
  134. rs = xs ^ ys;
  135. /* shunt to top of word */
  136. xm <<= 32 - (SP_FBITS + 1);
  137. ym <<= 32 - (SP_FBITS + 1);
  138. /*
  139. * Multiply 32 bits xm, ym to give high 32 bits rm with stickness.
  140. */
  141. lxm = xm & 0xffff;
  142. hxm = xm >> 16;
  143. lym = ym & 0xffff;
  144. hym = ym >> 16;
  145. lrm = lxm * lym; /* 16 * 16 => 32 */
  146. hrm = hxm * hym; /* 16 * 16 => 32 */
  147. t = lxm * hym; /* 16 * 16 => 32 */
  148. at = lrm + (t << 16);
  149. hrm += at < lrm;
  150. lrm = at;
  151. hrm = hrm + (t >> 16);
  152. t = hxm * lym; /* 16 * 16 => 32 */
  153. at = lrm + (t << 16);
  154. hrm += at < lrm;
  155. lrm = at;
  156. hrm = hrm + (t >> 16);
  157. rm = hrm | (lrm != 0);
  158. /*
  159. * Sticky shift down to normal rounding precision.
  160. */
  161. if ((int) rm < 0) {
  162. rm = (rm >> (32 - (SP_FBITS + 1 + 3))) |
  163. ((rm << (SP_FBITS + 1 + 3)) != 0);
  164. re++;
  165. } else {
  166. rm = (rm >> (32 - (SP_FBITS + 1 + 3 + 1))) |
  167. ((rm << (SP_FBITS + 1 + 3 + 1)) != 0);
  168. }
  169. assert(rm & (SP_HIDDEN_BIT << 3));
  170. /* And now the subtraction */
  171. /* Flip sign of r and handle as add */
  172. rs ^= 1;
  173. assert(zm & SP_HIDDEN_BIT);
  174. /*
  175. * Provide guard,round and stick bit space.
  176. */
  177. zm <<= 3;
  178. if (ze > re) {
  179. /*
  180. * Have to shift y fraction right to align.
  181. */
  182. s = ze - re;
  183. SPXSRSYn(s);
  184. } else if (re > ze) {
  185. /*
  186. * Have to shift x fraction right to align.
  187. */
  188. s = re - ze;
  189. SPXSRSYn(s);
  190. }
  191. assert(ze == re);
  192. assert(ze <= SP_EMAX);
  193. if (zs == rs) {
  194. /*
  195. * Generate 28 bit result of adding two 27 bit numbers
  196. * leaving result in zm, zs and ze.
  197. */
  198. zm = zm + rm;
  199. if (zm >> (SP_FBITS + 1 + 3)) { /* carry out */
  200. SPXSRSX1(); /* shift preserving sticky */
  201. }
  202. } else {
  203. if (zm >= rm) {
  204. zm = zm - rm;
  205. } else {
  206. zm = rm - zm;
  207. zs = rs;
  208. }
  209. if (zm == 0)
  210. return ieee754sp_zero(ieee754_csr.rm == FPU_CSR_RD);
  211. /*
  212. * Normalize in extended single precision
  213. */
  214. while ((zm >> (SP_MBITS + 3)) == 0) {
  215. zm <<= 1;
  216. ze--;
  217. }
  218. }
  219. return ieee754sp_format(zs, ze, zm);
  220. }