dp_maddf.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * IEEE754 floating point arithmetic
  3. * double 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 "ieee754dp.h"
  15. /* 128 bits shift right logical with rounding. */
  16. static void srl128(u64 *hptr, u64 *lptr, int count)
  17. {
  18. u64 low;
  19. if (count >= 128) {
  20. *lptr = *hptr != 0 || *lptr != 0;
  21. *hptr = 0;
  22. } else if (count >= 64) {
  23. if (count == 64) {
  24. *lptr = *hptr | (*lptr != 0);
  25. } else {
  26. low = *lptr;
  27. *lptr = *hptr >> (count - 64);
  28. *lptr |= (*hptr << (128 - count)) != 0 || low != 0;
  29. }
  30. *hptr = 0;
  31. } else {
  32. low = *lptr;
  33. *lptr = low >> count | *hptr << (64 - count);
  34. *lptr |= (low << (64 - count)) != 0;
  35. *hptr = *hptr >> count;
  36. }
  37. }
  38. static union ieee754dp _dp_maddf(union ieee754dp z, union ieee754dp x,
  39. union ieee754dp y, enum maddf_flags flags)
  40. {
  41. int re;
  42. int rs;
  43. unsigned int lxm;
  44. unsigned int hxm;
  45. unsigned int lym;
  46. unsigned int hym;
  47. u64 lrm;
  48. u64 hrm;
  49. u64 lzm;
  50. u64 hzm;
  51. u64 t;
  52. u64 at;
  53. int s;
  54. COMPXDP;
  55. COMPYDP;
  56. COMPZDP;
  57. EXPLODEXDP;
  58. EXPLODEYDP;
  59. EXPLODEZDP;
  60. FLUSHXDP;
  61. FLUSHYDP;
  62. FLUSHZDP;
  63. ieee754_clearcx();
  64. /*
  65. * Handle the cases when at least one of x, y or z is a NaN.
  66. * Order of precedence is sNaN, qNaN and z, x, y.
  67. */
  68. if (zc == IEEE754_CLASS_SNAN)
  69. return ieee754dp_nanxcpt(z);
  70. if (xc == IEEE754_CLASS_SNAN)
  71. return ieee754dp_nanxcpt(x);
  72. if (yc == IEEE754_CLASS_SNAN)
  73. return ieee754dp_nanxcpt(y);
  74. if (zc == IEEE754_CLASS_QNAN)
  75. return z;
  76. if (xc == IEEE754_CLASS_QNAN)
  77. return x;
  78. if (yc == IEEE754_CLASS_QNAN)
  79. return y;
  80. if (zc == IEEE754_CLASS_DNORM)
  81. DPDNORMZ;
  82. /* ZERO z cases are handled separately below */
  83. switch (CLPAIR(xc, yc)) {
  84. /*
  85. * Infinity handling
  86. */
  87. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_ZERO):
  88. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_INF):
  89. ieee754_setcx(IEEE754_INVALID_OPERATION);
  90. return ieee754dp_indef();
  91. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_INF):
  92. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_INF):
  93. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_NORM):
  94. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_DNORM):
  95. case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_INF):
  96. if ((zc == IEEE754_CLASS_INF) &&
  97. ((!(flags & MADDF_NEGATE_PRODUCT) && (zs != (xs ^ ys))) ||
  98. ((flags & MADDF_NEGATE_PRODUCT) && (zs == (xs ^ ys))))) {
  99. /*
  100. * Cases of addition of infinities with opposite signs
  101. * or subtraction of infinities with same signs.
  102. */
  103. ieee754_setcx(IEEE754_INVALID_OPERATION);
  104. return ieee754dp_indef();
  105. }
  106. /*
  107. * z is here either not an infinity, or an infinity having the
  108. * same sign as product (x*y) (in case of MADDF.D instruction)
  109. * or product -(x*y) (in MSUBF.D case). The result must be an
  110. * infinity, and its sign is determined only by the value of
  111. * (flags & MADDF_NEGATE_PRODUCT) and the signs of x and y.
  112. */
  113. if (flags & MADDF_NEGATE_PRODUCT)
  114. return ieee754dp_inf(1 ^ (xs ^ ys));
  115. else
  116. return ieee754dp_inf(xs ^ ys);
  117. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_ZERO):
  118. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_NORM):
  119. case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_DNORM):
  120. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_ZERO):
  121. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_ZERO):
  122. if (zc == IEEE754_CLASS_INF)
  123. return ieee754dp_inf(zs);
  124. if (zc == IEEE754_CLASS_ZERO) {
  125. /* Handle cases +0 + (-0) and similar ones. */
  126. if ((!(flags & MADDF_NEGATE_PRODUCT)
  127. && (zs == (xs ^ ys))) ||
  128. ((flags & MADDF_NEGATE_PRODUCT)
  129. && (zs != (xs ^ ys))))
  130. /*
  131. * Cases of addition of zeros of equal signs
  132. * or subtraction of zeroes of opposite signs.
  133. * The sign of the resulting zero is in any
  134. * such case determined only by the sign of z.
  135. */
  136. return z;
  137. return ieee754dp_zero(ieee754_csr.rm == FPU_CSR_RD);
  138. }
  139. /* x*y is here 0, and z is not 0, so just return z */
  140. return z;
  141. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_DNORM):
  142. DPDNORMX;
  143. /* fall through */
  144. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_DNORM):
  145. if (zc == IEEE754_CLASS_INF)
  146. return ieee754dp_inf(zs);
  147. DPDNORMY;
  148. break;
  149. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_NORM):
  150. if (zc == IEEE754_CLASS_INF)
  151. return ieee754dp_inf(zs);
  152. DPDNORMX;
  153. break;
  154. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_NORM):
  155. if (zc == IEEE754_CLASS_INF)
  156. return ieee754dp_inf(zs);
  157. /* continue to real computations */
  158. }
  159. /* Finally get to do some computation */
  160. /*
  161. * Do the multiplication bit first
  162. *
  163. * rm = xm * ym, re = xe + ye basically
  164. *
  165. * At this point xm and ym should have been normalized.
  166. */
  167. assert(xm & DP_HIDDEN_BIT);
  168. assert(ym & DP_HIDDEN_BIT);
  169. re = xe + ye;
  170. rs = xs ^ ys;
  171. if (flags & MADDF_NEGATE_PRODUCT)
  172. rs ^= 1;
  173. /* shunt to top of word */
  174. xm <<= 64 - (DP_FBITS + 1);
  175. ym <<= 64 - (DP_FBITS + 1);
  176. /*
  177. * Multiply 64 bits xm and ym to give 128 bits result in hrm:lrm.
  178. */
  179. lxm = xm;
  180. hxm = xm >> 32;
  181. lym = ym;
  182. hym = ym >> 32;
  183. lrm = DPXMULT(lxm, lym);
  184. hrm = DPXMULT(hxm, hym);
  185. t = DPXMULT(lxm, hym);
  186. at = lrm + (t << 32);
  187. hrm += at < lrm;
  188. lrm = at;
  189. hrm = hrm + (t >> 32);
  190. t = DPXMULT(hxm, lym);
  191. at = lrm + (t << 32);
  192. hrm += at < lrm;
  193. lrm = at;
  194. hrm = hrm + (t >> 32);
  195. /* Put explicit bit at bit 126 if necessary */
  196. if ((int64_t)hrm < 0) {
  197. lrm = (hrm << 63) | (lrm >> 1);
  198. hrm = hrm >> 1;
  199. re++;
  200. }
  201. assert(hrm & (1 << 62));
  202. if (zc == IEEE754_CLASS_ZERO) {
  203. /*
  204. * Move explicit bit from bit 126 to bit 55 since the
  205. * ieee754dp_format code expects the mantissa to be
  206. * 56 bits wide (53 + 3 rounding bits).
  207. */
  208. srl128(&hrm, &lrm, (126 - 55));
  209. return ieee754dp_format(rs, re, lrm);
  210. }
  211. /* Move explicit bit from bit 52 to bit 126 */
  212. lzm = 0;
  213. hzm = zm << 10;
  214. assert(hzm & (1 << 62));
  215. /* Make the exponents the same */
  216. if (ze > re) {
  217. /*
  218. * Have to shift y fraction right to align.
  219. */
  220. s = ze - re;
  221. srl128(&hrm, &lrm, s);
  222. re += s;
  223. } else if (re > ze) {
  224. /*
  225. * Have to shift x fraction right to align.
  226. */
  227. s = re - ze;
  228. srl128(&hzm, &lzm, s);
  229. ze += s;
  230. }
  231. assert(ze == re);
  232. assert(ze <= DP_EMAX);
  233. /* Do the addition */
  234. if (zs == rs) {
  235. /*
  236. * Generate 128 bit result by adding two 127 bit numbers
  237. * leaving result in hzm:lzm, zs and ze.
  238. */
  239. hzm = hzm + hrm + (lzm > (lzm + lrm));
  240. lzm = lzm + lrm;
  241. if ((int64_t)hzm < 0) { /* carry out */
  242. srl128(&hzm, &lzm, 1);
  243. ze++;
  244. }
  245. } else {
  246. if (hzm > hrm || (hzm == hrm && lzm >= lrm)) {
  247. hzm = hzm - hrm - (lzm < lrm);
  248. lzm = lzm - lrm;
  249. } else {
  250. hzm = hrm - hzm - (lrm < lzm);
  251. lzm = lrm - lzm;
  252. zs = rs;
  253. }
  254. if (lzm == 0 && hzm == 0)
  255. return ieee754dp_zero(ieee754_csr.rm == FPU_CSR_RD);
  256. /*
  257. * Put explicit bit at bit 126 if necessary.
  258. */
  259. if (hzm == 0) {
  260. /* left shift by 63 or 64 bits */
  261. if ((int64_t)lzm < 0) {
  262. /* MSB of lzm is the explicit bit */
  263. hzm = lzm >> 1;
  264. lzm = lzm << 63;
  265. ze -= 63;
  266. } else {
  267. hzm = lzm;
  268. lzm = 0;
  269. ze -= 64;
  270. }
  271. }
  272. t = 0;
  273. while ((hzm >> (62 - t)) == 0)
  274. t++;
  275. assert(t <= 62);
  276. if (t) {
  277. hzm = hzm << t | lzm >> (64 - t);
  278. lzm = lzm << t;
  279. ze -= t;
  280. }
  281. }
  282. /*
  283. * Move explicit bit from bit 126 to bit 55 since the
  284. * ieee754dp_format code expects the mantissa to be
  285. * 56 bits wide (53 + 3 rounding bits).
  286. */
  287. srl128(&hzm, &lzm, (126 - 55));
  288. return ieee754dp_format(zs, ze, lzm);
  289. }
  290. union ieee754dp ieee754dp_maddf(union ieee754dp z, union ieee754dp x,
  291. union ieee754dp y)
  292. {
  293. return _dp_maddf(z, x, y, 0);
  294. }
  295. union ieee754dp ieee754dp_msubf(union ieee754dp z, union ieee754dp x,
  296. union ieee754dp y)
  297. {
  298. return _dp_maddf(z, x, y, MADDF_NEGATE_PRODUCT);
  299. }