2000-09-11 Kazu Hirata <kazu@hxi.com>
[deliverable/binutils-gdb.git] / gas / flonum-mult.c
CommitLineData
252b5132
RH
1/* flonum_mult.c - multiply two flonums
2 Copyright (C) 1987, 1990, 1991, 1992 Free Software Foundation, Inc.
3
4 This file is part of Gas, the GNU Assembler.
5
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.
12
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. */
20
21#include <ansidecl.h>
22#include "flonum.h"
23
24/* plan for a . b => p(roduct)
f851444e 25
252b5132
RH
26 +-------+-------+-/ /-+-------+-------+
27 | a | a | ... | a | a |
28 | A | A-1 | | 1 | 0 |
29 +-------+-------+-/ /-+-------+-------+
f851444e 30
252b5132
RH
31 +-------+-------+-/ /-+-------+-------+
32 | b | b | ... | b | b |
33 | B | B-1 | | 1 | 0 |
34 +-------+-------+-/ /-+-------+-------+
f851444e 35
252b5132
RH
36 +-------+-------+-/ /-+-------+-/ /-+-------+-------+
37 | p | p | ... | p | ... | p | p |
38 | A+B+1| A+B | | N | | 1 | 0 |
39 +-------+-------+-/ /-+-------+-/ /-+-------+-------+
f851444e 40
252b5132
RH
41 /^\
42 (carry) a .b ... | ... a .b a .b
43 A B | 0 1 0 0
44 |
45 ... | ... a .b
46 | 1 0
47 |
48 | ...
49 |
50 |
51 |
52 | ___
53 | \
54 +----- P = > a .b
55 N /__ i j
f851444e 56
252b5132 57 N = 0 ... A+B
f851444e 58
252b5132
RH
59 for all i,j where i+j=N
60 [i,j integers > 0]
f851444e 61
252b5132
RH
62 a[], b[], p[] may not intersect.
63 Zero length factors signify 0 significant bits: treat as 0.0.
64 0.0 factors do the right thing.
65 Zero length product OK.
f851444e 66
252b5132
RH
67 I chose the ForTran accent "foo[bar]" instead of the C accent "*garply"
68 because I felt the ForTran way was more intuitive. The C way would
69 probably yield better code on most C compilers. Dean Elsner.
f851444e 70 (C style also gives deeper insight [to me] ... oh well ...) */
252b5132 71\f
f851444e 72void
252b5132
RH
73flonum_multip (a, b, product)
74 const FLONUM_TYPE *a;
75 const FLONUM_TYPE *b;
76 FLONUM_TYPE *product;
77{
f851444e
NC
78 int size_of_a; /* 0 origin */
79 int size_of_b; /* 0 origin */
80 int size_of_product; /* 0 origin */
81 int size_of_sum; /* 0 origin */
82 int extra_product_positions; /* 1 origin */
252b5132
RH
83 unsigned long work;
84 unsigned long carry;
85 long exponent;
86 LITTLENUM_TYPE *q;
87 long significant; /* TRUE when we emit a non-0 littlenum */
f851444e
NC
88 /* ForTran accent follows. */
89 int P; /* Scan product low-order -> high. */
252b5132 90 int N; /* As in sum above. */
f851444e
NC
91 int A; /* Which [] of a? */
92 int B; /* Which [] of b? */
252b5132 93
f851444e
NC
94 if ((a->sign != '-' && a->sign != '+')
95 || (b->sign != '-' && b->sign != '+'))
252b5132 96 {
f851444e 97 /* Got to fail somehow. Any suggestions? */
252b5132
RH
98 product->sign = 0;
99 return;
100 }
101 product->sign = (a->sign == b->sign) ? '+' : '-';
102 size_of_a = a->leader - a->low;
103 size_of_b = b->leader - b->low;
104 exponent = a->exponent + b->exponent;
105 size_of_product = product->high - product->low;
106 size_of_sum = size_of_a + size_of_b;
107 extra_product_positions = size_of_product - size_of_sum;
108 if (extra_product_positions < 0)
109 {
f851444e
NC
110 P = extra_product_positions; /* P < 0 */
111 exponent -= extra_product_positions; /* Increases exponent. */
252b5132
RH
112 }
113 else
114 {
115 P = 0;
116 }
117 carry = 0;
118 significant = 0;
119 for (N = 0; N <= size_of_sum; N++)
120 {
121 work = carry;
122 carry = 0;
123 for (A = 0; A <= N; A++)
124 {
125 B = N - A;
126 if (A <= size_of_a && B <= size_of_b && B >= 0)
127 {
128#ifdef TRACE
f851444e
NC
129 printf ("a:low[%d.]=%04x b:low[%d.]=%04x work_before=%08x\n",
130 A, a->low[A], B, b->low[B], work);
252b5132
RH
131#endif
132 /* Watch out for sign extension! Without the casts, on
133 the DEC Alpha, the multiplication result is *signed*
134 int, which gets sign-extended to convert to the
135 unsigned long! */
136 work += (unsigned long) a->low[A] * (unsigned long) b->low[B];
137 carry += work >> LITTLENUM_NUMBER_OF_BITS;
138 work &= LITTLENUM_MASK;
139#ifdef TRACE
140 printf ("work=%08x carry=%04x\n", work, carry);
141#endif
142 }
143 }
144 significant |= work;
145 if (significant || P < 0)
146 {
147 if (P >= 0)
148 {
149 product->low[P] = work;
150#ifdef TRACE
151 printf ("P=%d. work[p]:=%04x\n", P, work);
152#endif
153 }
154 P++;
155 }
156 else
157 {
158 extra_product_positions++;
159 exponent++;
160 }
161 }
f851444e
NC
162 /* [P]-> position # size_of_sum + 1.
163 This is where 'carry' should go. */
252b5132
RH
164#ifdef TRACE
165 printf ("final carry =%04x\n", carry);
166#endif
167 if (carry)
168 {
169 if (extra_product_positions > 0)
f851444e 170 product->low[P] = carry;
252b5132
RH
171 else
172 {
f851444e
NC
173 /* No room at high order for carry littlenum. */
174 /* Shift right 1 to make room for most significant littlenum. */
252b5132
RH
175 exponent++;
176 P--;
177 for (q = product->low + P; q >= product->low; q--)
178 {
179 work = *q;
180 *q = carry;
181 carry = work;
182 }
183 }
184 }
185 else
f851444e 186 P--;
252b5132
RH
187 product->leader = product->low + P;
188 product->exponent = exponent;
189}
This page took 0.0835090000000001 seconds and 4 git commands to generate.