sp_maddf.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * IEEE754 floating point arithmetic
  3. * single precision: MADDF.f (Fused Multiply Add)
  4. * MADDF.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. static union ieee754sp _sp_maddf(union ieee754sp z, union ieee754sp x,
  16. union ieee754sp y, enum maddf_flags flags)
  17. {
  18. int re;
  19. int rs;
  20. unsigned int rm;
  21. u64 rm64;
  22. u64 zm64;
  23. int s;
  24. COMPXSP;
  25. COMPYSP;
  26. COMPZSP;
  27. EXPLODEXSP;
  28. EXPLODEYSP;
  29. EXPLODEZSP;
  30. FLUSHXSP;
  31. FLUSHYSP;
  32. FLUSHZSP;
  33. ieee754_clearcx();
  34. /*
  35. * Handle the cases when at least one of x, y or z is a NaN.
  36. * Order of precedence is sNaN, qNaN and z, x, y.
  37. */
  38. if (zc == IEEE754_CLASS_SNAN)
  39. return ieee754sp_nanxcpt(z);
  40. if (xc == IEEE754_CLASS_SNAN)
  41. return ieee754sp_nanxcpt(x);
  42. if (yc == IEEE754_CLASS_SNAN)
  43. return ieee754sp_nanxcpt(y);
  44. if (zc == IEEE754_CLASS_QNAN)
  45. return z;
  46. if (xc == IEEE754_CLASS_QNAN)
  47. return x;
  48. if (yc == IEEE754_CLASS_QNAN)
  49. return y;
  50. if (zc == IEEE754_CLASS_DNORM)
  51. SPDNORMZ;
  52. /* ZERO z cases are handled separately below */
  53. switch (CLPAIR(xc, yc)) {
  54. /*
  55. * Infinity handling
  56. */
  57. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_ZERO):
  58. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_INF):
  59. ieee754_setcx(IEEE754_INVALID_OPERATION);
  60. return ieee754sp_indef();
  61. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_INF):
  62. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_INF):
  63. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_NORM):
  64. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_DNORM):
  65. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_INF):
  66. if ((zc == IEEE754_CLASS_INF) &&
  67. ((!(flags & MADDF_NEGATE_PRODUCT) && (zs != (xs ^ ys))) ||
  68. ((flags & MADDF_NEGATE_PRODUCT) && (zs == (xs ^ ys))))) {
  69. /*
  70. * Cases of addition of infinities with opposite signs
  71. * or subtraction of infinities with same signs.
  72. */
  73. ieee754_setcx(IEEE754_INVALID_OPERATION);
  74. return ieee754sp_indef();
  75. }
  76. /*
  77. * z is here either not an infinity, or an infinity having the
  78. * same sign as product (x*y) (in case of MADDF.D instruction)
  79. * or product -(x*y) (in MSUBF.D case). The result must be an
  80. * infinity, and its sign is determined only by the value of
  81. * (flags & MADDF_NEGATE_PRODUCT) and the signs of x and y.
  82. */
  83. if (flags & MADDF_NEGATE_PRODUCT)
  84. return ieee754sp_inf(1 ^ (xs ^ ys));
  85. else
  86. return ieee754sp_inf(xs ^ ys);
  87. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_ZERO):
  88. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_NORM):
  89. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_DNORM):
  90. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_ZERO):
  91. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_ZERO):
  92. if (zc == IEEE754_CLASS_INF)
  93. return ieee754sp_inf(zs);
  94. if (zc == IEEE754_CLASS_ZERO) {
  95. /* Handle cases +0 + (-0) and similar ones. */
  96. if ((!(flags & MADDF_NEGATE_PRODUCT)
  97. && (zs == (xs ^ ys))) ||
  98. ((flags & MADDF_NEGATE_PRODUCT)
  99. && (zs != (xs ^ ys))))
  100. /*
  101. * Cases of addition of zeros of equal signs
  102. * or subtraction of zeroes of opposite signs.
  103. * The sign of the resulting zero is in any
  104. * such case determined only by the sign of z.
  105. */
  106. return z;
  107. return ieee754sp_zero(ieee754_csr.rm == FPU_CSR_RD);
  108. }
  109. /* x*y is here 0, and z is not 0, so just return z */
  110. return z;
  111. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_DNORM):
  112. SPDNORMX;
  113. /* fall through */
  114. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_DNORM):
  115. if (zc == IEEE754_CLASS_INF)
  116. return ieee754sp_inf(zs);
  117. SPDNORMY;
  118. break;
  119. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_NORM):
  120. if (zc == IEEE754_CLASS_INF)
  121. return ieee754sp_inf(zs);
  122. SPDNORMX;
  123. break;
  124. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_NORM):
  125. if (zc == IEEE754_CLASS_INF)
  126. return ieee754sp_inf(zs);
  127. /* continue to real computations */
  128. }
  129. /* Finally get to do some computation */
  130. /*
  131. * Do the multiplication bit first
  132. *
  133. * rm = xm * ym, re = xe + ye basically
  134. *
  135. * At this point xm and ym should have been normalized.
  136. */
  137. /* rm = xm * ym, re = xe+ye basically */
  138. assert(xm & SP_HIDDEN_BIT);
  139. assert(ym & SP_HIDDEN_BIT);
  140. re = xe + ye;
  141. rs = xs ^ ys;
  142. if (flags & MADDF_NEGATE_PRODUCT)
  143. rs ^= 1;
  144. /* Multiple 24 bit xm and ym to give 48 bit results */
  145. rm64 = (uint64_t)xm * ym;
  146. /* Shunt to top of word */
  147. rm64 = rm64 << 16;
  148. /* Put explicit bit at bit 62 if necessary */
  149. if ((int64_t) rm64 < 0) {
  150. rm64 = rm64 >> 1;
  151. re++;
  152. }
  153. assert(rm64 & (1 << 62));
  154. if (zc == IEEE754_CLASS_ZERO) {
  155. /*
  156. * Move explicit bit from bit 62 to bit 26 since the
  157. * ieee754sp_format code expects the mantissa to be
  158. * 27 bits wide (24 + 3 rounding bits).
  159. */
  160. rm = XSPSRS64(rm64, (62 - 26));
  161. return ieee754sp_format(rs, re, rm);
  162. }
  163. /* Move explicit bit from bit 23 to bit 62 */
  164. zm64 = (uint64_t)zm << (62 - 23);
  165. assert(zm64 & (1 << 62));
  166. /* Make the exponents the same */
  167. if (ze > re) {
  168. /*
  169. * Have to shift r fraction right to align.
  170. */
  171. s = ze - re;
  172. rm64 = XSPSRS64(rm64, s);
  173. re += s;
  174. } else if (re > ze) {
  175. /*
  176. * Have to shift z fraction right to align.
  177. */
  178. s = re - ze;
  179. zm64 = XSPSRS64(zm64, s);
  180. ze += s;
  181. }
  182. assert(ze == re);
  183. assert(ze <= SP_EMAX);
  184. /* Do the addition */
  185. if (zs == rs) {
  186. /*
  187. * Generate 64 bit result by adding two 63 bit numbers
  188. * leaving result in zm64, zs and ze.
  189. */
  190. zm64 = zm64 + rm64;
  191. if ((int64_t)zm64 < 0) { /* carry out */
  192. zm64 = XSPSRS1(zm64);
  193. ze++;
  194. }
  195. } else {
  196. if (zm64 >= rm64) {
  197. zm64 = zm64 - rm64;
  198. } else {
  199. zm64 = rm64 - zm64;
  200. zs = rs;
  201. }
  202. if (zm64 == 0)
  203. return ieee754sp_zero(ieee754_csr.rm == FPU_CSR_RD);
  204. /*
  205. * Put explicit bit at bit 62 if necessary.
  206. */
  207. while ((zm64 >> 62) == 0) {
  208. zm64 <<= 1;
  209. ze--;
  210. }
  211. }
  212. /*
  213. * Move explicit bit from bit 62 to bit 26 since the
  214. * ieee754sp_format code expects the mantissa to be
  215. * 27 bits wide (24 + 3 rounding bits).
  216. */
  217. zm = XSPSRS64(zm64, (62 - 26));
  218. return ieee754sp_format(zs, ze, zm);
  219. }
  220. union ieee754sp ieee754sp_maddf(union ieee754sp z, union ieee754sp x,
  221. union ieee754sp y)
  222. {
  223. return _sp_maddf(z, x, y, 0);
  224. }
  225. union ieee754sp ieee754sp_msubf(union ieee754sp z, union ieee754sp x,
  226. union ieee754sp y)
  227. {
  228. return _sp_maddf(z, x, y, MADDF_NEGATE_PRODUCT);
  229. }