flonum-mult.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* flonum_mult.c - multiply two flonums
  2. Copyright (C) 1987-2015 Free Software Foundation, Inc.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GAS is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GAS; see the file COPYING. If not, write to the Free
  14. Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. #include "ansidecl.h"
  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. void
  58. flonum_multip (const FLONUM_TYPE *a, const FLONUM_TYPE *b,
  59. FLONUM_TYPE *product)
  60. {
  61. int size_of_a; /* 0 origin */
  62. int size_of_b; /* 0 origin */
  63. int size_of_product; /* 0 origin */
  64. int size_of_sum; /* 0 origin */
  65. int extra_product_positions; /* 1 origin */
  66. unsigned long work;
  67. unsigned long carry;
  68. long exponent;
  69. LITTLENUM_TYPE *q;
  70. long significant; /* TRUE when we emit a non-0 littlenum */
  71. /* ForTran accent follows. */
  72. int P; /* Scan product low-order -> high. */
  73. int N; /* As in sum above. */
  74. int A; /* Which [] of a? */
  75. int B; /* Which [] of b? */
  76. if ((a->sign != '-' && a->sign != '+')
  77. || (b->sign != '-' && b->sign != '+'))
  78. {
  79. /* Got to fail somehow. Any suggestions? */
  80. product->sign = 0;
  81. return;
  82. }
  83. product->sign = (a->sign == b->sign) ? '+' : '-';
  84. size_of_a = a->leader - a->low;
  85. size_of_b = b->leader - b->low;
  86. exponent = a->exponent + b->exponent;
  87. size_of_product = product->high - product->low;
  88. size_of_sum = size_of_a + size_of_b;
  89. extra_product_positions = size_of_product - size_of_sum;
  90. if (extra_product_positions < 0)
  91. {
  92. P = extra_product_positions; /* P < 0 */
  93. exponent -= extra_product_positions; /* Increases exponent. */
  94. }
  95. else
  96. {
  97. P = 0;
  98. }
  99. carry = 0;
  100. significant = 0;
  101. for (N = 0; N <= size_of_sum; N++)
  102. {
  103. work = carry;
  104. carry = 0;
  105. for (A = 0; A <= N; A++)
  106. {
  107. B = N - A;
  108. if (A <= size_of_a && B <= size_of_b && B >= 0)
  109. {
  110. #ifdef TRACE
  111. printf ("a:low[%d.]=%04x b:low[%d.]=%04x work_before=%08x\n",
  112. A, a->low[A], B, b->low[B], work);
  113. #endif
  114. /* Watch out for sign extension! Without the casts, on
  115. the DEC Alpha, the multiplication result is *signed*
  116. int, which gets sign-extended to convert to the
  117. unsigned long! */
  118. work += (unsigned long) a->low[A] * (unsigned long) b->low[B];
  119. carry += work >> LITTLENUM_NUMBER_OF_BITS;
  120. work &= LITTLENUM_MASK;
  121. #ifdef TRACE
  122. printf ("work=%08x carry=%04x\n", work, carry);
  123. #endif
  124. }
  125. }
  126. significant |= work;
  127. if (significant || P < 0)
  128. {
  129. if (P >= 0)
  130. {
  131. product->low[P] = work;
  132. #ifdef TRACE
  133. printf ("P=%d. work[p]:=%04x\n", P, work);
  134. #endif
  135. }
  136. P++;
  137. }
  138. else
  139. {
  140. extra_product_positions++;
  141. exponent++;
  142. }
  143. }
  144. /* [P]-> position # size_of_sum + 1.
  145. This is where 'carry' should go. */
  146. #ifdef TRACE
  147. printf ("final carry =%04x\n", carry);
  148. #endif
  149. if (carry)
  150. {
  151. if (extra_product_positions > 0)
  152. product->low[P] = carry;
  153. else
  154. {
  155. /* No room at high order for carry littlenum. */
  156. /* Shift right 1 to make room for most significant littlenum. */
  157. exponent++;
  158. P--;
  159. for (q = product->low + P; q >= product->low; q--)
  160. {
  161. work = *q;
  162. *q = carry;
  163. carry = work;
  164. }
  165. }
  166. }
  167. else
  168. P--;
  169. product->leader = product->low + P;
  170. product->exponent = exponent;
  171. }