dp_maddf.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. 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 lxm;
  44. unsigned hxm;
  45. unsigned lym;
  46. unsigned 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. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_DNORM):
  144. if (zc == IEEE754_CLASS_INF)
  145. return ieee754dp_inf(zs);
  146. DPDNORMY;
  147. break;
  148. case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_NORM):
  149. if (zc == IEEE754_CLASS_INF)
  150. return ieee754dp_inf(zs);
  151. DPDNORMX;
  152. break;
  153. case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_NORM):
  154. if (zc == IEEE754_CLASS_INF)
  155. return ieee754dp_inf(zs);
  156. /* fall through to real computations */
  157. }
  158. /* Finally get to do some computation */
  159. /*
  160. * Do the multiplication bit first
  161. *
  162. * rm = xm * ym, re = xe + ye basically
  163. *
  164. * At this point xm and ym should have been normalized.
  165. */
  166. assert(xm & DP_HIDDEN_BIT);
  167. assert(ym & DP_HIDDEN_BIT);
  168. re = xe + ye;
  169. rs = xs ^ ys;
  170. if (flags & MADDF_NEGATE_PRODUCT)
  171. rs ^= 1;
  172. /* shunt to top of word */
  173. xm <<= 64 - (DP_FBITS + 1);
  174. ym <<= 64 - (DP_FBITS + 1);
  175. /*
  176. * Multiply 64 bits xm and ym to give 128 bits result in hrm:lrm.
  177. */
  178. /* 32 * 32 => 64 */
  179. #define DPXMULT(x, y) ((u64)(x) * (u64)y)
  180. lxm = xm;
  181. hxm = xm >> 32;
  182. lym = ym;
  183. hym = ym >> 32;
  184. lrm = DPXMULT(lxm, lym);
  185. hrm = DPXMULT(hxm, hym);
  186. t = DPXMULT(lxm, hym);
  187. at = lrm + (t << 32);
  188. hrm += at < lrm;
  189. lrm = at;
  190. hrm = hrm + (t >> 32);
  191. t = DPXMULT(hxm, lym);
  192. at = lrm + (t << 32);
  193. hrm += at < lrm;
  194. lrm = at;
  195. hrm = hrm + (t >> 32);
  196. /* Put explicit bit at bit 126 if necessary */
  197. if ((int64_t)hrm < 0) {
  198. lrm = (hrm << 63) | (lrm >> 1);
  199. hrm = hrm >> 1;
  200. re++;
  201. }
  202. assert(hrm & (1 << 62));
  203. if (zc == IEEE754_CLASS_ZERO) {
  204. /*
  205. * Move explicit bit from bit 126 to bit 55 since the
  206. * ieee754dp_format code expects the mantissa to be
  207. * 56 bits wide (53 + 3 rounding bits).
  208. */
  209. srl128(&hrm, &lrm, (126 - 55));
  210. return ieee754dp_format(rs, re, lrm);
  211. }
  212. /* Move explicit bit from bit 52 to bit 126 */
  213. lzm = 0;
  214. hzm = zm << 10;
  215. assert(hzm & (1 << 62));
  216. /* Make the exponents the same */
  217. if (ze > re) {
  218. /*
  219. * Have to shift y fraction right to align.
  220. */
  221. s = ze - re;
  222. srl128(&hrm, &lrm, s);
  223. re += s;
  224. } else if (re > ze) {
  225. /*
  226. * Have to shift x fraction right to align.
  227. */
  228. s = re - ze;
  229. srl128(&hzm, &lzm, s);
  230. ze += s;
  231. }
  232. assert(ze == re);
  233. assert(ze <= DP_EMAX);
  234. /* Do the addition */
  235. if (zs == rs) {
  236. /*
  237. * Generate 128 bit result by adding two 127 bit numbers
  238. * leaving result in hzm:lzm, zs and ze.
  239. */
  240. hzm = hzm + hrm + (lzm > (lzm + lrm));
  241. lzm = lzm + lrm;
  242. if ((int64_t)hzm < 0) { /* carry out */
  243. srl128(&hzm, &lzm, 1);
  244. ze++;
  245. }
  246. } else {
  247. if (hzm > hrm || (hzm == hrm && lzm >= lrm)) {
  248. hzm = hzm - hrm - (lzm < lrm);
  249. lzm = lzm - lrm;
  250. } else {
  251. hzm = hrm - hzm - (lrm < lzm);
  252. lzm = lrm - lzm;
  253. zs = rs;
  254. }
  255. if (lzm == 0 && hzm == 0)
  256. return ieee754dp_zero(ieee754_csr.rm == FPU_CSR_RD);
  257. /*
  258. * Put explicit bit at bit 126 if necessary.
  259. */
  260. if (hzm == 0) {
  261. /* left shift by 63 or 64 bits */
  262. if ((int64_t)lzm < 0) {
  263. /* MSB of lzm is the explicit bit */
  264. hzm = lzm >> 1;
  265. lzm = lzm << 63;
  266. ze -= 63;
  267. } else {
  268. hzm = lzm;
  269. lzm = 0;
  270. ze -= 64;
  271. }
  272. }
  273. t = 0;
  274. while ((hzm >> (62 - t)) == 0)
  275. t++;
  276. assert(t <= 62);
  277. if (t) {
  278. hzm = hzm << t | lzm >> (64 - t);
  279. lzm = lzm << t;
  280. ze -= t;
  281. }
  282. }
  283. /*
  284. * Move explicit bit from bit 126 to bit 55 since the
  285. * ieee754dp_format code expects the mantissa to be
  286. * 56 bits wide (53 + 3 rounding bits).
  287. */
  288. srl128(&hzm, &lzm, (126 - 55));
  289. return ieee754dp_format(zs, ze, lzm);
  290. }
  291. union ieee754dp ieee754dp_maddf(union ieee754dp z, union ieee754dp x,
  292. union ieee754dp y)
  293. {
  294. return _dp_maddf(z, x, y, 0);
  295. }
  296. union ieee754dp ieee754dp_msubf(union ieee754dp z, union ieee754dp x,
  297. union ieee754dp y)
  298. {
  299. return _dp_maddf(z, x, y, MADDF_NEGATE_PRODUCT);
  300. }