flonum-mult.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* flonum_multip.c - multiply two flonums
  2. Copyright (C) 1987 Free Software Foundation, Inc.
  3. This file is part of Gas, the GNU Assembler.
  4. The GNU assembler is distributed in the hope that it will be
  5. useful, but WITHOUT ANY WARRANTY. No author or distributor
  6. accepts responsibility to anyone for the consequences of using it
  7. or for whether it serves any particular purpose or works at all,
  8. unless he says so in writing. Refer to the GNU Assembler General
  9. Public License for full details.
  10. Everyone is granted permission to copy, modify and redistribute
  11. the GNU Assembler, but only under the conditions described in the
  12. GNU Assembler General Public License. A copy of this license is
  13. supposed to have been given to you along with the GNU Assembler
  14. so you can know your rights and responsibilities. It should be
  15. in a file named COPYING. Among other things, the copyright
  16. notice and this notice must be preserved on all copies. */
  17. #include "flonum.h"
  18. /* plan for a . b => p(roduct)
  19. +-------+-------+-/ /-+-------+-------+
  20. | a | a | ... | a | a |
  21. | A | A-1 | | 1 | 0 |
  22. +-------+-------+-/ /-+-------+-------+
  23. +-------+-------+-/ /-+-------+-------+
  24. | b | b | ... | b | b |
  25. | B | B-1 | | 1 | 0 |
  26. +-------+-------+-/ /-+-------+-------+
  27. +-------+-------+-/ /-+-------+-/ /-+-------+-------+
  28. | p | p | ... | p | ... | p | p |
  29. | A+B+1| A+B | | N | | 1 | 0 |
  30. +-------+-------+-/ /-+-------+-/ /-+-------+-------+
  31. /^\
  32. (carry) a .b ... | ... a .b a .b
  33. A B | 0 1 0 0
  34. |
  35. ... | ... a .b
  36. | 1 0
  37. |
  38. | ...
  39. |
  40. |
  41. |
  42. | ___
  43. | \
  44. +----- P = > a .b
  45. N /__ i j
  46. N = 0 ... A+B
  47. for all i,j where i+j=N
  48. [i,j integers > 0]
  49. a[], b[], p[] may not intersect.
  50. Zero length factors signify 0 significant bits: treat as 0.0.
  51. 0.0 factors do the right thing.
  52. Zero length product OK.
  53. I chose the ForTran accent "foo[bar]" instead of the C accent "*garply"
  54. because I felt the ForTran way was more intuitive. The C way would
  55. probably yield better code on most C compilers. Dean Elsner.
  56. (C style also gives deeper insight [to me] ... oh well ...)
  57. */
  58. void
  59. flonum_multip (a, b, product)
  60. FLONUM_TYPE * a,
  61. * b,
  62. * product;
  63. {
  64. int size_of_a; /* 0 origin */
  65. int size_of_b; /* 0 origin */
  66. int size_of_product; /* 0 origin */
  67. int size_of_sum; /* 0 origin */
  68. int extra_product_positions;/* 1 origin */
  69. unsigned long int work;
  70. unsigned long int carry;
  71. long int exponent;
  72. LITTLENUM_TYPE * q;
  73. long int significant; /* TRUE when we emit a non-0 littlenum */
  74. /* ForTran accent follows. */
  75. int P; /* Scan product low-order -> high. */
  76. int N; /* As in sum above. */
  77. int A; /* Which [] of a? */
  78. int B; /* Which [] of b? */
  79. if((a->sign!='-' && a->sign!='+') || (b->sign!='-' && b->sign!='+')) {
  80. /* ...
  81. Got to fail somehow. Any suggestions? */
  82. product->sign=0;
  83. return;
  84. }
  85. product -> sign = (a->sign == b->sign) ? '+' : '-';
  86. size_of_a = a -> leader - a -> low;
  87. size_of_b = b -> leader - b -> low;
  88. exponent = a -> exponent + b -> exponent;
  89. size_of_product = product -> high - product -> low;
  90. size_of_sum = size_of_a + size_of_b;
  91. extra_product_positions = size_of_product - size_of_sum;
  92. if (extra_product_positions < 0)
  93. {
  94. P = extra_product_positions; /* P < 0 */
  95. exponent -= extra_product_positions; /* Increases exponent. */
  96. }
  97. else
  98. {
  99. P = 0;
  100. }
  101. carry = 0;
  102. significant = 0;
  103. for (N = 0;
  104. N <= size_of_sum;
  105. N++)
  106. {
  107. work = carry;
  108. carry = 0;
  109. for (A = 0;
  110. A <= N;
  111. A ++)
  112. {
  113. B = N - A;
  114. if (A <= size_of_a && B <= size_of_b && B >= 0)
  115. {
  116. #ifdef TRACE
  117. printf("a:low[%d.]=%04x b:low[%d.]=%04x work_before=%08x\n", A, a->low[A], B, b->low[B], work);
  118. #endif
  119. work += a -> low [A] * b -> low [B];
  120. carry += work >> LITTLENUM_NUMBER_OF_BITS;
  121. work &= LITTLENUM_MASK;
  122. #ifdef TRACE
  123. printf("work=%08x carry=%04x\n", work, carry);
  124. #endif
  125. }
  126. }
  127. significant |= work;
  128. if (significant)
  129. {
  130. if (P >= 0)
  131. {
  132. product -> low [P] = work;
  133. #ifdef TRACE
  134. printf("P=%d. work[p]:=%04x\n", P, work);
  135. #endif
  136. }
  137. P ++;
  138. }
  139. else
  140. {
  141. extra_product_positions ++;
  142. exponent ++;
  143. }
  144. }
  145. /*
  146. * [P]-> position # size_of_sum + 1.
  147. * This is where 'carry' should go.
  148. */
  149. #ifdef TRACE
  150. printf("final carry =%04x\n", carry);
  151. #endif
  152. if (carry)
  153. {
  154. if (extra_product_positions > 0)
  155. {
  156. product -> low [P] = carry;
  157. }
  158. else
  159. {
  160. /* No room at high order for carry littlenum. */
  161. /* Shift right 1 to make room for most significant littlenum. */
  162. exponent ++;
  163. P --;
  164. for (q = product -> low + P;
  165. q >= product -> low;
  166. q --)
  167. {
  168. work = * q;
  169. * q = carry;
  170. carry = work;
  171. }
  172. }
  173. }
  174. else
  175. {
  176. P --;
  177. }
  178. product -> leader = product -> low + P;
  179. product -> exponent = exponent;
  180. }
  181. /* end: flonum_multip.c */