fp_arith.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /*
  2. fp_arith.c: floating-point math routines for the Linux-m68k
  3. floating point emulator.
  4. Copyright (c) 1998-1999 David Huggins-Daines.
  5. Somewhat based on the AlphaLinux floating point emulator, by David
  6. Mosberger-Tang.
  7. You may copy, modify, and redistribute this file under the terms of
  8. the GNU General Public License, version 2, or any later version, at
  9. your convenience.
  10. */
  11. #include "fp_emu.h"
  12. #include "multi_arith.h"
  13. #include "fp_arith.h"
  14. const struct fp_ext fp_QNaN =
  15. {
  16. .exp = 0x7fff,
  17. .mant = { .m64 = ~0 }
  18. };
  19. const struct fp_ext fp_Inf =
  20. {
  21. .exp = 0x7fff,
  22. };
  23. /* let's start with the easy ones */
  24. struct fp_ext *
  25. fp_fabs(struct fp_ext *dest, struct fp_ext *src)
  26. {
  27. dprint(PINSTR, "fabs\n");
  28. fp_monadic_check(dest, src);
  29. dest->sign = 0;
  30. return dest;
  31. }
  32. struct fp_ext *
  33. fp_fneg(struct fp_ext *dest, struct fp_ext *src)
  34. {
  35. dprint(PINSTR, "fneg\n");
  36. fp_monadic_check(dest, src);
  37. dest->sign = !dest->sign;
  38. return dest;
  39. }
  40. /* Now, the slightly harder ones */
  41. /* fp_fadd: Implements the kernel of the FADD, FSADD, FDADD, FSUB,
  42. FDSUB, and FCMP instructions. */
  43. struct fp_ext *
  44. fp_fadd(struct fp_ext *dest, struct fp_ext *src)
  45. {
  46. int diff;
  47. dprint(PINSTR, "fadd\n");
  48. fp_dyadic_check(dest, src);
  49. if (IS_INF(dest)) {
  50. /* infinity - infinity == NaN */
  51. if (IS_INF(src) && (src->sign != dest->sign))
  52. fp_set_nan(dest);
  53. return dest;
  54. }
  55. if (IS_INF(src)) {
  56. fp_copy_ext(dest, src);
  57. return dest;
  58. }
  59. if (IS_ZERO(dest)) {
  60. if (IS_ZERO(src)) {
  61. if (src->sign != dest->sign) {
  62. if (FPDATA->rnd == FPCR_ROUND_RM)
  63. dest->sign = 1;
  64. else
  65. dest->sign = 0;
  66. }
  67. } else
  68. fp_copy_ext(dest, src);
  69. return dest;
  70. }
  71. dest->lowmant = src->lowmant = 0;
  72. if ((diff = dest->exp - src->exp) > 0)
  73. fp_denormalize(src, diff);
  74. else if ((diff = -diff) > 0)
  75. fp_denormalize(dest, diff);
  76. if (dest->sign == src->sign) {
  77. if (fp_addmant(dest, src))
  78. if (!fp_addcarry(dest))
  79. return dest;
  80. } else {
  81. if (dest->mant.m64 < src->mant.m64) {
  82. fp_submant(dest, src, dest);
  83. dest->sign = !dest->sign;
  84. } else
  85. fp_submant(dest, dest, src);
  86. }
  87. return dest;
  88. }
  89. /* fp_fsub: Implements the kernel of the FSUB, FSSUB, and FDSUB
  90. instructions.
  91. Remember that the arguments are in assembler-syntax order! */
  92. struct fp_ext *
  93. fp_fsub(struct fp_ext *dest, struct fp_ext *src)
  94. {
  95. dprint(PINSTR, "fsub ");
  96. src->sign = !src->sign;
  97. return fp_fadd(dest, src);
  98. }
  99. struct fp_ext *
  100. fp_fcmp(struct fp_ext *dest, struct fp_ext *src)
  101. {
  102. dprint(PINSTR, "fcmp ");
  103. FPDATA->temp[1] = *dest;
  104. src->sign = !src->sign;
  105. return fp_fadd(&FPDATA->temp[1], src);
  106. }
  107. struct fp_ext *
  108. fp_ftst(struct fp_ext *dest, struct fp_ext *src)
  109. {
  110. dprint(PINSTR, "ftst\n");
  111. (void)dest;
  112. return src;
  113. }
  114. struct fp_ext *
  115. fp_fmul(struct fp_ext *dest, struct fp_ext *src)
  116. {
  117. union fp_mant128 temp;
  118. int exp;
  119. dprint(PINSTR, "fmul\n");
  120. fp_dyadic_check(dest, src);
  121. /* calculate the correct sign now, as it's necessary for infinities */
  122. dest->sign = src->sign ^ dest->sign;
  123. /* Handle infinities */
  124. if (IS_INF(dest)) {
  125. if (IS_ZERO(src))
  126. fp_set_nan(dest);
  127. return dest;
  128. }
  129. if (IS_INF(src)) {
  130. if (IS_ZERO(dest))
  131. fp_set_nan(dest);
  132. else
  133. fp_copy_ext(dest, src);
  134. return dest;
  135. }
  136. /* Of course, as we all know, zero * anything = zero. You may
  137. not have known that it might be a positive or negative
  138. zero... */
  139. if (IS_ZERO(dest) || IS_ZERO(src)) {
  140. dest->exp = 0;
  141. dest->mant.m64 = 0;
  142. dest->lowmant = 0;
  143. return dest;
  144. }
  145. exp = dest->exp + src->exp - 0x3ffe;
  146. /* shift up the mantissa for denormalized numbers,
  147. so that the highest bit is set, this makes the
  148. shift of the result below easier */
  149. if ((long)dest->mant.m32[0] >= 0)
  150. exp -= fp_overnormalize(dest);
  151. if ((long)src->mant.m32[0] >= 0)
  152. exp -= fp_overnormalize(src);
  153. /* now, do a 64-bit multiply with expansion */
  154. fp_multiplymant(&temp, dest, src);
  155. /* normalize it back to 64 bits and stuff it back into the
  156. destination struct */
  157. if ((long)temp.m32[0] > 0) {
  158. exp--;
  159. fp_putmant128(dest, &temp, 1);
  160. } else
  161. fp_putmant128(dest, &temp, 0);
  162. if (exp >= 0x7fff) {
  163. fp_set_ovrflw(dest);
  164. return dest;
  165. }
  166. dest->exp = exp;
  167. if (exp < 0) {
  168. fp_set_sr(FPSR_EXC_UNFL);
  169. fp_denormalize(dest, -exp);
  170. }
  171. return dest;
  172. }
  173. /* fp_fdiv: Implements the "kernel" of the FDIV, FSDIV, FDDIV and
  174. FSGLDIV instructions.
  175. Note that the order of the operands is counter-intuitive: instead
  176. of src / dest, the result is actually dest / src. */
  177. struct fp_ext *
  178. fp_fdiv(struct fp_ext *dest, struct fp_ext *src)
  179. {
  180. union fp_mant128 temp;
  181. int exp;
  182. dprint(PINSTR, "fdiv\n");
  183. fp_dyadic_check(dest, src);
  184. /* calculate the correct sign now, as it's necessary for infinities */
  185. dest->sign = src->sign ^ dest->sign;
  186. /* Handle infinities */
  187. if (IS_INF(dest)) {
  188. /* infinity / infinity = NaN (quiet, as always) */
  189. if (IS_INF(src))
  190. fp_set_nan(dest);
  191. /* infinity / anything else = infinity (with approprate sign) */
  192. return dest;
  193. }
  194. if (IS_INF(src)) {
  195. /* anything / infinity = zero (with appropriate sign) */
  196. dest->exp = 0;
  197. dest->mant.m64 = 0;
  198. dest->lowmant = 0;
  199. return dest;
  200. }
  201. /* zeroes */
  202. if (IS_ZERO(dest)) {
  203. /* zero / zero = NaN */
  204. if (IS_ZERO(src))
  205. fp_set_nan(dest);
  206. /* zero / anything else = zero */
  207. return dest;
  208. }
  209. if (IS_ZERO(src)) {
  210. /* anything / zero = infinity (with appropriate sign) */
  211. fp_set_sr(FPSR_EXC_DZ);
  212. dest->exp = 0x7fff;
  213. dest->mant.m64 = 0;
  214. return dest;
  215. }
  216. exp = dest->exp - src->exp + 0x3fff;
  217. /* shift up the mantissa for denormalized numbers,
  218. so that the highest bit is set, this makes lots
  219. of things below easier */
  220. if ((long)dest->mant.m32[0] >= 0)
  221. exp -= fp_overnormalize(dest);
  222. if ((long)src->mant.m32[0] >= 0)
  223. exp -= fp_overnormalize(src);
  224. /* now, do the 64-bit divide */
  225. fp_dividemant(&temp, dest, src);
  226. /* normalize it back to 64 bits and stuff it back into the
  227. destination struct */
  228. if (!temp.m32[0]) {
  229. exp--;
  230. fp_putmant128(dest, &temp, 32);
  231. } else
  232. fp_putmant128(dest, &temp, 31);
  233. if (exp >= 0x7fff) {
  234. fp_set_ovrflw(dest);
  235. return dest;
  236. }
  237. dest->exp = exp;
  238. if (exp < 0) {
  239. fp_set_sr(FPSR_EXC_UNFL);
  240. fp_denormalize(dest, -exp);
  241. }
  242. return dest;
  243. }
  244. struct fp_ext *
  245. fp_fsglmul(struct fp_ext *dest, struct fp_ext *src)
  246. {
  247. int exp;
  248. dprint(PINSTR, "fsglmul\n");
  249. fp_dyadic_check(dest, src);
  250. /* calculate the correct sign now, as it's necessary for infinities */
  251. dest->sign = src->sign ^ dest->sign;
  252. /* Handle infinities */
  253. if (IS_INF(dest)) {
  254. if (IS_ZERO(src))
  255. fp_set_nan(dest);
  256. return dest;
  257. }
  258. if (IS_INF(src)) {
  259. if (IS_ZERO(dest))
  260. fp_set_nan(dest);
  261. else
  262. fp_copy_ext(dest, src);
  263. return dest;
  264. }
  265. /* Of course, as we all know, zero * anything = zero. You may
  266. not have known that it might be a positive or negative
  267. zero... */
  268. if (IS_ZERO(dest) || IS_ZERO(src)) {
  269. dest->exp = 0;
  270. dest->mant.m64 = 0;
  271. dest->lowmant = 0;
  272. return dest;
  273. }
  274. exp = dest->exp + src->exp - 0x3ffe;
  275. /* do a 32-bit multiply */
  276. fp_mul64(dest->mant.m32[0], dest->mant.m32[1],
  277. dest->mant.m32[0] & 0xffffff00,
  278. src->mant.m32[0] & 0xffffff00);
  279. if (exp >= 0x7fff) {
  280. fp_set_ovrflw(dest);
  281. return dest;
  282. }
  283. dest->exp = exp;
  284. if (exp < 0) {
  285. fp_set_sr(FPSR_EXC_UNFL);
  286. fp_denormalize(dest, -exp);
  287. }
  288. return dest;
  289. }
  290. struct fp_ext *
  291. fp_fsgldiv(struct fp_ext *dest, struct fp_ext *src)
  292. {
  293. int exp;
  294. unsigned long quot, rem;
  295. dprint(PINSTR, "fsgldiv\n");
  296. fp_dyadic_check(dest, src);
  297. /* calculate the correct sign now, as it's necessary for infinities */
  298. dest->sign = src->sign ^ dest->sign;
  299. /* Handle infinities */
  300. if (IS_INF(dest)) {
  301. /* infinity / infinity = NaN (quiet, as always) */
  302. if (IS_INF(src))
  303. fp_set_nan(dest);
  304. /* infinity / anything else = infinity (with approprate sign) */
  305. return dest;
  306. }
  307. if (IS_INF(src)) {
  308. /* anything / infinity = zero (with appropriate sign) */
  309. dest->exp = 0;
  310. dest->mant.m64 = 0;
  311. dest->lowmant = 0;
  312. return dest;
  313. }
  314. /* zeroes */
  315. if (IS_ZERO(dest)) {
  316. /* zero / zero = NaN */
  317. if (IS_ZERO(src))
  318. fp_set_nan(dest);
  319. /* zero / anything else = zero */
  320. return dest;
  321. }
  322. if (IS_ZERO(src)) {
  323. /* anything / zero = infinity (with appropriate sign) */
  324. fp_set_sr(FPSR_EXC_DZ);
  325. dest->exp = 0x7fff;
  326. dest->mant.m64 = 0;
  327. return dest;
  328. }
  329. exp = dest->exp - src->exp + 0x3fff;
  330. dest->mant.m32[0] &= 0xffffff00;
  331. src->mant.m32[0] &= 0xffffff00;
  332. /* do the 32-bit divide */
  333. if (dest->mant.m32[0] >= src->mant.m32[0]) {
  334. fp_sub64(dest->mant, src->mant);
  335. fp_div64(quot, rem, dest->mant.m32[0], 0, src->mant.m32[0]);
  336. dest->mant.m32[0] = 0x80000000 | (quot >> 1);
  337. dest->mant.m32[1] = (quot & 1) | rem; /* only for rounding */
  338. } else {
  339. fp_div64(quot, rem, dest->mant.m32[0], 0, src->mant.m32[0]);
  340. dest->mant.m32[0] = quot;
  341. dest->mant.m32[1] = rem; /* only for rounding */
  342. exp--;
  343. }
  344. if (exp >= 0x7fff) {
  345. fp_set_ovrflw(dest);
  346. return dest;
  347. }
  348. dest->exp = exp;
  349. if (exp < 0) {
  350. fp_set_sr(FPSR_EXC_UNFL);
  351. fp_denormalize(dest, -exp);
  352. }
  353. return dest;
  354. }
  355. /* fp_roundint: Internal rounding function for use by several of these
  356. emulated instructions.
  357. This one rounds off the fractional part using the rounding mode
  358. specified. */
  359. static void fp_roundint(struct fp_ext *dest, int mode)
  360. {
  361. union fp_mant64 oldmant;
  362. unsigned long mask;
  363. if (!fp_normalize_ext(dest))
  364. return;
  365. /* infinities and zeroes */
  366. if (IS_INF(dest) || IS_ZERO(dest))
  367. return;
  368. /* first truncate the lower bits */
  369. oldmant = dest->mant;
  370. switch (dest->exp) {
  371. case 0 ... 0x3ffe:
  372. dest->mant.m64 = 0;
  373. break;
  374. case 0x3fff ... 0x401e:
  375. dest->mant.m32[0] &= 0xffffffffU << (0x401e - dest->exp);
  376. dest->mant.m32[1] = 0;
  377. if (oldmant.m64 == dest->mant.m64)
  378. return;
  379. break;
  380. case 0x401f ... 0x403e:
  381. dest->mant.m32[1] &= 0xffffffffU << (0x403e - dest->exp);
  382. if (oldmant.m32[1] == dest->mant.m32[1])
  383. return;
  384. break;
  385. default:
  386. return;
  387. }
  388. fp_set_sr(FPSR_EXC_INEX2);
  389. /* We might want to normalize upwards here... however, since
  390. we know that this is only called on the output of fp_fdiv,
  391. or with the input to fp_fint or fp_fintrz, and the inputs
  392. to all these functions are either normal or denormalized
  393. (no subnormals allowed!), there's really no need.
  394. In the case of fp_fdiv, observe that 0x80000000 / 0xffff =
  395. 0xffff8000, and the same holds for 128-bit / 64-bit. (i.e. the
  396. smallest possible normal dividend and the largest possible normal
  397. divisor will still produce a normal quotient, therefore, (normal
  398. << 64) / normal is normal in all cases) */
  399. switch (mode) {
  400. case FPCR_ROUND_RN:
  401. switch (dest->exp) {
  402. case 0 ... 0x3ffd:
  403. return;
  404. case 0x3ffe:
  405. /* As noted above, the input is always normal, so the
  406. guard bit (bit 63) is always set. therefore, the
  407. only case in which we will NOT round to 1.0 is when
  408. the input is exactly 0.5. */
  409. if (oldmant.m64 == (1ULL << 63))
  410. return;
  411. break;
  412. case 0x3fff ... 0x401d:
  413. mask = 1 << (0x401d - dest->exp);
  414. if (!(oldmant.m32[0] & mask))
  415. return;
  416. if (oldmant.m32[0] & (mask << 1))
  417. break;
  418. if (!(oldmant.m32[0] << (dest->exp - 0x3ffd)) &&
  419. !oldmant.m32[1])
  420. return;
  421. break;
  422. case 0x401e:
  423. if (oldmant.m32[1] & 0x80000000)
  424. return;
  425. if (oldmant.m32[0] & 1)
  426. break;
  427. if (!(oldmant.m32[1] << 1))
  428. return;
  429. break;
  430. case 0x401f ... 0x403d:
  431. mask = 1 << (0x403d - dest->exp);
  432. if (!(oldmant.m32[1] & mask))
  433. return;
  434. if (oldmant.m32[1] & (mask << 1))
  435. break;
  436. if (!(oldmant.m32[1] << (dest->exp - 0x401d)))
  437. return;
  438. break;
  439. default:
  440. return;
  441. }
  442. break;
  443. case FPCR_ROUND_RZ:
  444. return;
  445. default:
  446. if (dest->sign ^ (mode - FPCR_ROUND_RM))
  447. break;
  448. return;
  449. }
  450. switch (dest->exp) {
  451. case 0 ... 0x3ffe:
  452. dest->exp = 0x3fff;
  453. dest->mant.m64 = 1ULL << 63;
  454. break;
  455. case 0x3fff ... 0x401e:
  456. mask = 1 << (0x401e - dest->exp);
  457. if (dest->mant.m32[0] += mask)
  458. break;
  459. dest->mant.m32[0] = 0x80000000;
  460. dest->exp++;
  461. break;
  462. case 0x401f ... 0x403e:
  463. mask = 1 << (0x403e - dest->exp);
  464. if (dest->mant.m32[1] += mask)
  465. break;
  466. if (dest->mant.m32[0] += 1)
  467. break;
  468. dest->mant.m32[0] = 0x80000000;
  469. dest->exp++;
  470. break;
  471. }
  472. }
  473. /* modrem_kernel: Implementation of the FREM and FMOD instructions
  474. (which are exactly the same, except for the rounding used on the
  475. intermediate value) */
  476. static struct fp_ext *
  477. modrem_kernel(struct fp_ext *dest, struct fp_ext *src, int mode)
  478. {
  479. struct fp_ext tmp;
  480. fp_dyadic_check(dest, src);
  481. /* Infinities and zeros */
  482. if (IS_INF(dest) || IS_ZERO(src)) {
  483. fp_set_nan(dest);
  484. return dest;
  485. }
  486. if (IS_ZERO(dest) || IS_INF(src))
  487. return dest;
  488. /* FIXME: there is almost certainly a smarter way to do this */
  489. fp_copy_ext(&tmp, dest);
  490. fp_fdiv(&tmp, src); /* NOTE: src might be modified */
  491. fp_roundint(&tmp, mode);
  492. fp_fmul(&tmp, src);
  493. fp_fsub(dest, &tmp);
  494. /* set the quotient byte */
  495. fp_set_quotient((dest->mant.m64 & 0x7f) | (dest->sign << 7));
  496. return dest;
  497. }
  498. /* fp_fmod: Implements the kernel of the FMOD instruction.
  499. Again, the argument order is backwards. The result, as defined in
  500. the Motorola manuals, is:
  501. fmod(src,dest) = (dest - (src * floor(dest / src))) */
  502. struct fp_ext *
  503. fp_fmod(struct fp_ext *dest, struct fp_ext *src)
  504. {
  505. dprint(PINSTR, "fmod\n");
  506. return modrem_kernel(dest, src, FPCR_ROUND_RZ);
  507. }
  508. /* fp_frem: Implements the kernel of the FREM instruction.
  509. frem(src,dest) = (dest - (src * round(dest / src)))
  510. */
  511. struct fp_ext *
  512. fp_frem(struct fp_ext *dest, struct fp_ext *src)
  513. {
  514. dprint(PINSTR, "frem\n");
  515. return modrem_kernel(dest, src, FPCR_ROUND_RN);
  516. }
  517. struct fp_ext *
  518. fp_fint(struct fp_ext *dest, struct fp_ext *src)
  519. {
  520. dprint(PINSTR, "fint\n");
  521. fp_copy_ext(dest, src);
  522. fp_roundint(dest, FPDATA->rnd);
  523. return dest;
  524. }
  525. struct fp_ext *
  526. fp_fintrz(struct fp_ext *dest, struct fp_ext *src)
  527. {
  528. dprint(PINSTR, "fintrz\n");
  529. fp_copy_ext(dest, src);
  530. fp_roundint(dest, FPCR_ROUND_RZ);
  531. return dest;
  532. }
  533. struct fp_ext *
  534. fp_fscale(struct fp_ext *dest, struct fp_ext *src)
  535. {
  536. int scale, oldround;
  537. dprint(PINSTR, "fscale\n");
  538. fp_dyadic_check(dest, src);
  539. /* Infinities */
  540. if (IS_INF(src)) {
  541. fp_set_nan(dest);
  542. return dest;
  543. }
  544. if (IS_INF(dest))
  545. return dest;
  546. /* zeroes */
  547. if (IS_ZERO(src) || IS_ZERO(dest))
  548. return dest;
  549. /* Source exponent out of range */
  550. if (src->exp >= 0x400c) {
  551. fp_set_ovrflw(dest);
  552. return dest;
  553. }
  554. /* src must be rounded with round to zero. */
  555. oldround = FPDATA->rnd;
  556. FPDATA->rnd = FPCR_ROUND_RZ;
  557. scale = fp_conv_ext2long(src);
  558. FPDATA->rnd = oldround;
  559. /* new exponent */
  560. scale += dest->exp;
  561. if (scale >= 0x7fff) {
  562. fp_set_ovrflw(dest);
  563. } else if (scale <= 0) {
  564. fp_set_sr(FPSR_EXC_UNFL);
  565. fp_denormalize(dest, -scale);
  566. } else
  567. dest->exp = scale;
  568. return dest;
  569. }