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 rm;
  21. uint64_t rm64;
  22. uint64_t 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. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_DNORM):
  114. if (zc == IEEE754_CLASS_INF)
  115. return ieee754sp_inf(zs);
  116. SPDNORMY;
  117. break;
  118. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_NORM):
  119. if (zc == IEEE754_CLASS_INF)
  120. return ieee754sp_inf(zs);
  121. SPDNORMX;
  122. break;
  123. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_NORM):
  124. if (zc == IEEE754_CLASS_INF)
  125. return ieee754sp_inf(zs);
  126. /* fall through to real computations */
  127. }
  128. /* Finally get to do some computation */
  129. /*
  130. * Do the multiplication bit first
  131. *
  132. * rm = xm * ym, re = xe + ye basically
  133. *
  134. * At this point xm and ym should have been normalized.
  135. */
  136. /* rm = xm * ym, re = xe+ye basically */
  137. assert(xm & SP_HIDDEN_BIT);
  138. assert(ym & SP_HIDDEN_BIT);
  139. re = xe + ye;
  140. rs = xs ^ ys;
  141. if (flags & MADDF_NEGATE_PRODUCT)
  142. rs ^= 1;
  143. /* Multiple 24 bit xm and ym to give 48 bit results */
  144. rm64 = (uint64_t)xm * ym;
  145. /* Shunt to top of word */
  146. rm64 = rm64 << 16;
  147. /* Put explicit bit at bit 62 if necessary */
  148. if ((int64_t) rm64 < 0) {
  149. rm64 = rm64 >> 1;
  150. re++;
  151. }
  152. assert(rm64 & (1 << 62));
  153. if (zc == IEEE754_CLASS_ZERO) {
  154. /*
  155. * Move explicit bit from bit 62 to bit 26 since the
  156. * ieee754sp_format code expects the mantissa to be
  157. * 27 bits wide (24 + 3 rounding bits).
  158. */
  159. rm = XSPSRS64(rm64, (62 - 26));
  160. return ieee754sp_format(rs, re, rm);
  161. }
  162. /* Move explicit bit from bit 23 to bit 62 */
  163. zm64 = (uint64_t)zm << (62 - 23);
  164. assert(zm64 & (1 << 62));
  165. /* Make the exponents the same */
  166. if (ze > re) {
  167. /*
  168. * Have to shift r fraction right to align.
  169. */
  170. s = ze - re;
  171. rm64 = XSPSRS64(rm64, s);
  172. re += s;
  173. } else if (re > ze) {
  174. /*
  175. * Have to shift z fraction right to align.
  176. */
  177. s = re - ze;
  178. zm64 = XSPSRS64(zm64, s);
  179. ze += s;
  180. }
  181. assert(ze == re);
  182. assert(ze <= SP_EMAX);
  183. /* Do the addition */
  184. if (zs == rs) {
  185. /*
  186. * Generate 64 bit result by adding two 63 bit numbers
  187. * leaving result in zm64, zs and ze.
  188. */
  189. zm64 = zm64 + rm64;
  190. if ((int64_t)zm64 < 0) { /* carry out */
  191. zm64 = XSPSRS1(zm64);
  192. ze++;
  193. }
  194. } else {
  195. if (zm64 >= rm64) {
  196. zm64 = zm64 - rm64;
  197. } else {
  198. zm64 = rm64 - zm64;
  199. zs = rs;
  200. }
  201. if (zm64 == 0)
  202. return ieee754sp_zero(ieee754_csr.rm == FPU_CSR_RD);
  203. /*
  204. * Put explicit bit at bit 62 if necessary.
  205. */
  206. while ((zm64 >> 62) == 0) {
  207. zm64 <<= 1;
  208. ze--;
  209. }
  210. }
  211. /*
  212. * Move explicit bit from bit 62 to bit 26 since the
  213. * ieee754sp_format code expects the mantissa to be
  214. * 27 bits wide (24 + 3 rounding bits).
  215. */
  216. zm = XSPSRS64(zm64, (62 - 26));
  217. return ieee754sp_format(zs, ze, zm);
  218. }
  219. union ieee754sp ieee754sp_maddf(union ieee754sp z, union ieee754sp x,
  220. union ieee754sp y)
  221. {
  222. return _sp_maddf(z, x, y, 0);
  223. }
  224. union ieee754sp ieee754sp_msubf(union ieee754sp z, union ieee754sp x,
  225. union ieee754sp y)
  226. {
  227. return _sp_maddf(z, x, y, MADDF_NEGATE_PRODUCT);
  228. }