flt_div.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. (c) copyright 1989 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. /* $Header$ */
  6. #include "flt_misc.h"
  7. void
  8. flt_div(e1,e2,e3)
  9. register flt_arith *e1,*e2,*e3;
  10. {
  11. long result[2];
  12. register long *lp;
  13. unsigned short u[9], v[5];
  14. register int j;
  15. register unsigned short *u_p = u;
  16. int maxv = 4;
  17. flt_status = 0;
  18. e3->flt_sign = e1->flt_sign ^ e2->flt_sign;
  19. if ((e2->m1 | e2->m2) == 0) {
  20. flt_status = FLT_DIV0;
  21. e3->flt_exp = EXT_MAX;
  22. e3->m1 = 0x80000000;
  23. e3->m2 = 0L;
  24. return;
  25. }
  26. if ((e1->m1 | e1->m2) == 0) { /* 0 / anything == 0 */
  27. e3->flt_exp = 0; /* make sure */
  28. e3->m1 = e3->m2 = 0L;
  29. e3->flt_sign = 0;
  30. return;
  31. }
  32. e3->flt_exp = e1->flt_exp - e2->flt_exp;
  33. u[4] = (e1->m2 & 1) << 15;
  34. flt_b64_sft(&(e1->flt_mantissa),1);
  35. flt_split(e1, u);
  36. u[5] = 0; u[6] = 0; u[7] = 0;
  37. flt_split(e2, &v[1]);
  38. while (! v[maxv]) maxv--;
  39. result[0] = 0;
  40. result[1] = 0;
  41. lp = result;
  42. /*
  43. * Use an algorithm of Knuth (The art of programming, Seminumerical
  44. * algorithms), to divide u by v. u and v are both seen as numbers
  45. * with base 65536.
  46. */
  47. for (j = 0; j <= 3; j++, u_p++) {
  48. long q_est, temp;
  49. long v1 = v[1];
  50. if (j == 2) lp++;
  51. if (u_p[0] == 0 && u_p[1] < v[1]) continue;
  52. temp = ((long)u_p[0] << 16) + u_p[1];
  53. if (u_p[0] >= v[1]) {
  54. q_est = 0x0000FFFFL;
  55. }
  56. else if (v[1] == 1) {
  57. q_est = temp;
  58. }
  59. else if (temp >= 0) {
  60. q_est = temp / v1;
  61. }
  62. else {
  63. long rem;
  64. q_est = (0x7FFFFFFF/v1)+((temp&0x7FFFFFFF)/v1);
  65. rem = (0x7FFFFFFF%v1)+((temp&0x7FFFFFFF)%v1)+1;
  66. while (rem >= v1) {
  67. q_est++;
  68. rem -= v1;
  69. }
  70. }
  71. temp -= q_est * v1;
  72. while (!(temp&0xFFFF0000) &&
  73. ucmp((long)v[2]*q_est,(temp<<16)+(long)u_p[2]) > 0) {
  74. q_est--;
  75. temp += v1;
  76. }
  77. /* Now, according to Knuth, we have an estimate of the
  78. quotient, that is either correct or one too big, but
  79. almost always correct.
  80. */
  81. if (q_est != 0) {
  82. int i;
  83. long k = 0;
  84. int borrow = 0;
  85. for (i = maxv; i > 0; i--) {
  86. long tmp = q_est * (long)v[i] + k + borrow;
  87. unsigned short md = tmp & 0xFFFF;
  88. borrow = (md > u_p[i]);
  89. u_p[i] -= md;
  90. k = (tmp >> 16) & 0xFFFF;
  91. }
  92. k += borrow;
  93. borrow = (long)u_p[0] < k;
  94. u_p[0] -= k;
  95. if (borrow) {
  96. /* So, this does not happen often; the estimate
  97. was one too big; correct this
  98. */
  99. q_est--;
  100. borrow = 0;
  101. for (i = maxv; i > 0; i--) {
  102. long tmp
  103. = v[i]+(long)u_p[i]+borrow;
  104. u_p[i] = tmp & 0xFFFF;
  105. borrow = (tmp >> 16) & 0xFFFF;
  106. }
  107. u_p[0] += borrow;
  108. }
  109. *lp |= (j & 1) ? q_est : (q_est<<16);
  110. }
  111. }
  112. e3->m1 = result[0];
  113. e3->m2 = result[1];
  114. flt_nrm(e3);
  115. flt_chk(e3);
  116. }