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