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. */
22 static char rcsid[] = "$FreeBSD: src/gnu/usr.bin/as/flonum-mult.c,v 1.6 1999/08/27 23:34:15 peter Exp $";
27 /* plan for a . b => p(roduct)
30 +-------+-------+-/ /-+-------+-------+
31 | a | a | ... | a | a |
33 +-------+-------+-/ /-+-------+-------+
36 +-------+-------+-/ /-+-------+-------+
37 | b | b | ... | b | b |
39 +-------+-------+-/ /-+-------+-------+
42 +-------+-------+-/ /-+-------+-/ /-+-------+-------+
43 | p | p | ... | p | ... | p | p |
44 | A+B+1| A+B | | N | | 1 | 0 |
45 +-------+-------+-/ /-+-------+-/ /-+-------+-------+
48 (carry) a .b ... | ... a .b a .b
65 for all i,j where i+j=N
68 a[], b[], p[] may not intersect.
69 Zero length factors signify 0 significant bits: treat as 0.0.
70 0.0 factors do the right thing.
71 Zero length product OK.
73 I chose the ForTran accent "foo[bar]" instead of the C accent "*garply"
74 because I felt the ForTran way was more intuitive. The C way would
75 probably yield better code on most C compilers. Dean Elsner.
76 (C style also gives deeper insight [to me] ... oh well ...)
79 void flonum_multip (a, b, product)
84 int size_of_a; /* 0 origin */
85 int size_of_b; /* 0 origin */
86 int size_of_product; /* 0 origin */
87 int size_of_sum; /* 0 origin */
88 int extra_product_positions;/* 1 origin */
93 long significant; /* TRUE when we emit a non-0 littlenum */
94 /* ForTran accent follows. */
95 int P; /* Scan product low-order -> high. */
96 int N; /* As in sum above. */
97 int A; /* Which [] of a? */
98 int B; /* Which [] of b? */
100 if ((a->sign != '-' && a->sign != '+') || (b->sign != '-' && b->sign != '+')) {
102 Got to fail somehow. Any suggestions? */
106 product->sign = (a->sign == b->sign) ? '+' : '-';
107 size_of_a = a->leader - a->low;
108 size_of_b = b->leader - b->low;
109 exponent = a->exponent + b->exponent;
110 size_of_product = product->high - product->low;
111 size_of_sum = size_of_a + size_of_b;
112 extra_product_positions = size_of_product - size_of_sum;
113 if (extra_product_positions < 0)
115 P = extra_product_positions; /* P < 0 */
116 exponent -= extra_product_positions; /* Increases exponent. */
135 if (A <= size_of_a && B <= size_of_b && B >= 0)
138 printf("a:low[%d.]=%04x b:low[%d.]=%04x work_before=%08x\n", A, a->low[A], B, b->low[B], work);
140 work += a->low[A] * b->low[B];
141 carry += work >> LITTLENUM_NUMBER_OF_BITS;
142 work &= LITTLENUM_MASK;
144 printf("work=%08x carry=%04x\n", work, carry);
149 if (significant || P<0)
153 product->low[P] = work;
155 printf("P=%d. work[p]:=%04x\n", P, work);
162 extra_product_positions ++;
167 * [P]->position # size_of_sum + 1.
168 * This is where 'carry' should go.
171 printf("final carry =%04x\n", carry);
175 if (extra_product_positions > 0)
177 product->low[P] = carry;
181 /* No room at high order for carry littlenum. */
182 /* Shift right 1 to make room for most significant littlenum. */
185 for (q = product->low + P;
199 product->leader = product->low + P;
200 product->exponent = exponent;
203 /* end of flonum_mult.c */