]>
Commit | Line | Data |
---|---|---|
fecd2382 RP |
1 | /* flonum_multip.c - multiply two flonums |
2 | Copyright (C) 1987, 1990, 1991 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 | /* static const char rcsid[] = "$Id$"; */ | |
22 | ||
23 | #include "flonum.h" | |
24 | ||
25 | /* plan for a . b => p(roduct) | |
26 | ||
27 | ||
28 | +-------+-------+-/ /-+-------+-------+ | |
29 | | a | a | ... | a | a | | |
30 | | A | A-1 | | 1 | 0 | | |
31 | +-------+-------+-/ /-+-------+-------+ | |
32 | ||
33 | ||
34 | +-------+-------+-/ /-+-------+-------+ | |
35 | | b | b | ... | b | b | | |
36 | | B | B-1 | | 1 | 0 | | |
37 | +-------+-------+-/ /-+-------+-------+ | |
38 | ||
39 | ||
40 | +-------+-------+-/ /-+-------+-/ /-+-------+-------+ | |
41 | | p | p | ... | p | ... | p | p | | |
42 | | A+B+1| A+B | | N | | 1 | 0 | | |
43 | +-------+-------+-/ /-+-------+-/ /-+-------+-------+ | |
44 | ||
45 | /^\ | |
46 | (carry) a .b ... | ... a .b a .b | |
47 | A B | 0 1 0 0 | |
48 | | | |
49 | ... | ... a .b | |
50 | | 1 0 | |
51 | | | |
52 | | ... | |
53 | | | |
54 | | | |
55 | | | |
56 | | ___ | |
57 | | \ | |
58 | +----- P = > a .b | |
59 | N /__ i j | |
60 | ||
61 | N = 0 ... A+B | |
62 | ||
63 | for all i,j where i+j=N | |
64 | [i,j integers > 0] | |
65 | ||
66 | a[], b[], p[] may not intersect. | |
67 | Zero length factors signify 0 significant bits: treat as 0.0. | |
68 | 0.0 factors do the right thing. | |
69 | Zero length product OK. | |
70 | ||
71 | I chose the ForTran accent "foo[bar]" instead of the C accent "*garply" | |
72 | because I felt the ForTran way was more intuitive. The C way would | |
73 | probably yield better code on most C compilers. Dean Elsner. | |
74 | (C style also gives deeper insight [to me] ... oh well ...) | |
75 | */ | |
76 | \f | |
77 | void flonum_multip (a, b, product) | |
78 | const FLONUM_TYPE *a; | |
79 | const FLONUM_TYPE *b; | |
80 | FLONUM_TYPE *product; | |
81 | { | |
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? */ | |
97 | ||
98 | if((a->sign!='-' && a->sign!='+') || (b->sign!='-' && b->sign!='+')) { | |
99 | /* ... | |
100 | Got to fail somehow. Any suggestions? */ | |
101 | product->sign=0; | |
102 | return; | |
103 | } | |
104 | product -> sign = (a->sign == b->sign) ? '+' : '-'; | |
105 | size_of_a = a -> leader - a -> low; | |
106 | size_of_b = b -> leader - b -> low; | |
107 | exponent = a -> exponent + b -> exponent; | |
108 | size_of_product = product -> high - product -> low; | |
109 | size_of_sum = size_of_a + size_of_b; | |
110 | extra_product_positions = size_of_product - size_of_sum; | |
111 | if (extra_product_positions < 0) | |
112 | { | |
113 | P = extra_product_positions; /* P < 0 */ | |
114 | exponent -= extra_product_positions; /* Increases exponent. */ | |
115 | } | |
116 | else | |
117 | { | |
118 | P = 0; | |
119 | } | |
120 | carry = 0; | |
121 | significant = 0; | |
122 | for (N = 0; | |
123 | N <= size_of_sum; | |
124 | N++) | |
125 | { | |
126 | work = carry; | |
127 | carry = 0; | |
128 | for (A = 0; | |
129 | A <= N; | |
130 | A ++) | |
131 | { | |
132 | B = N - A; | |
133 | if (A <= size_of_a && B <= size_of_b && B >= 0) | |
134 | { | |
135 | #ifdef TRACE | |
136 | printf("a:low[%d.]=%04x b:low[%d.]=%04x work_before=%08x\n", A, a->low[A], B, b->low[B], work); | |
137 | #endif | |
138 | work += a -> low [A] * b -> low [B]; | |
139 | carry += work >> LITTLENUM_NUMBER_OF_BITS; | |
140 | work &= LITTLENUM_MASK; | |
141 | #ifdef TRACE | |
142 | printf("work=%08x carry=%04x\n", work, carry); | |
143 | #endif | |
144 | } | |
145 | } | |
146 | significant |= work; | |
147 | if (significant || P<0) | |
148 | { | |
149 | if (P >= 0) | |
150 | { | |
151 | product -> low [P] = work; | |
152 | #ifdef TRACE | |
153 | printf("P=%d. work[p]:=%04x\n", P, work); | |
154 | #endif | |
155 | } | |
156 | P ++; | |
157 | } | |
158 | else | |
159 | { | |
160 | extra_product_positions ++; | |
161 | exponent ++; | |
162 | } | |
163 | } | |
164 | /* | |
165 | * [P]-> position # size_of_sum + 1. | |
166 | * This is where 'carry' should go. | |
167 | */ | |
168 | #ifdef TRACE | |
169 | printf("final carry =%04x\n", carry); | |
170 | #endif | |
171 | if (carry) | |
172 | { | |
173 | if (extra_product_positions > 0) | |
174 | { | |
175 | product -> low [P] = carry; | |
176 | } | |
177 | else | |
178 | { | |
179 | /* No room at high order for carry littlenum. */ | |
180 | /* Shift right 1 to make room for most significant littlenum. */ | |
181 | exponent ++; | |
182 | P --; | |
183 | for (q = product -> low + P; | |
184 | q >= product -> low; | |
185 | q --) | |
186 | { | |
187 | work = * q; | |
188 | * q = carry; | |
189 | carry = work; | |
190 | } | |
191 | } | |
192 | } | |
193 | else | |
194 | { | |
195 | P --; | |
196 | } | |
197 | product -> leader = product -> low + P; | |
198 | product -> exponent = exponent; | |
199 | } | |
200 | ||
201 | /* end: flonum_multip.c */ |