flt_add.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. /* $Id$ */
  6. #include "flt_misc.h"
  7. void
  8. flt_add(e1,e2,e3)
  9. register flt_arith *e1,*e2,*e3;
  10. {
  11. /* Add two extended numbers e1 and e2, and put the result
  12. in e3
  13. */
  14. flt_arith ce1, ce2;
  15. int diff;
  16. flt_status = 0;
  17. if ((e2->m1 | e2->m2) == 0L) {
  18. *e3 = *e1;
  19. return;
  20. }
  21. if ((e1->m1 | e1->m2) == 0L) {
  22. *e3 = *e2;
  23. return;
  24. }
  25. ce2 = *e2;
  26. ce1 = *e1;
  27. e1 = &ce1;
  28. e2 = &ce2;
  29. /* adjust mantissas to equal power */
  30. diff = e2->flt_exp - e1->flt_exp;
  31. if (diff < 0) {
  32. diff = -diff;
  33. e2->flt_exp += diff;
  34. flt_b64_sft(&(e2->flt_mantissa), diff);
  35. }
  36. else if (diff > 0) {
  37. e1->flt_exp += diff;
  38. flt_b64_sft(&(e1->flt_mantissa), diff);
  39. }
  40. if (e1->flt_sign != e2->flt_sign) {
  41. /* e2 + e1 = e2 - (-e1) */
  42. int tmp = ucmp(e1->m1, e2->m1);
  43. int tmp2 = ucmp(e1->m2, e2->m2);
  44. if (tmp > 0 || (tmp == 0 && tmp2 > 0)) {
  45. /* abs(e1) > abs(e2) */
  46. if (tmp2 < 0) {
  47. e1->m1 -= 1; /* carry in */
  48. }
  49. e1->m1 -= e2->m1;
  50. e1->m2 -= e2->m2;
  51. *e3 = *e1;
  52. }
  53. else {
  54. if (tmp2 > 0)
  55. e2->m1 -= 1; /* carry in */
  56. e2->m1 -= e1->m1;
  57. e2->m2 -= e1->m2;
  58. *e3 = *e2;
  59. }
  60. }
  61. else {
  62. *e3 = *e2;
  63. if (flt_b64_add(&e3->flt_mantissa,&e1->flt_mantissa)) {/* addition carry */
  64. flt_b64_sft(&e3->flt_mantissa, 1);
  65. e3->m1 |= 0x80000000L; /* set max bit */
  66. e3->flt_exp++; /* increase the exponent */
  67. }
  68. }
  69. flt_nrm(e3);
  70. flt_chk(e3);
  71. }
  72. void
  73. flt_sub(e1,e2,e3)
  74. flt_arith *e1,*e2,*e3;
  75. {
  76. e2->flt_sign = ! e2->flt_sign;
  77. flt_add(e1,e2,e3);
  78. e2->flt_sign = ! e2->flt_sign;
  79. }