1 /* flonum_mult.c - multiply two flonums
2 Copyright 1987, 1990, 1991, 1992, 1995, 2000, 2002
3 Free Software Foundation, Inc.
5 This file is part of Gas, the GNU Assembler.
7 The GNU assembler is distributed in the hope that it will be
8 useful, but WITHOUT ANY WARRANTY. No author or distributor
9 accepts responsibility to anyone for the consequences of using it
10 or for whether it serves any particular purpose or works at all,
11 unless he says so in writing. Refer to the GNU Assembler General
12 Public License for full details.
14 Everyone is granted permission to copy, modify and redistribute
15 the GNU Assembler, but only under the conditions described in the
16 GNU Assembler General Public License. A copy of this license is
17 supposed to have been given to you along with the GNU Assembler
18 so you can know your rights and responsibilities. It should be
19 in a file named COPYING. Among other things, the copyright
20 notice and this notice must be preserved on all copies. */
25 /* plan for a . b => p(roduct)
27 +-------+-------+-/ /-+-------+-------+
28 | a | a | ... | a | a |
30 +-------+-------+-/ /-+-------+-------+
32 +-------+-------+-/ /-+-------+-------+
33 | b | b | ... | b | b |
35 +-------+-------+-/ /-+-------+-------+
37 +-------+-------+-/ /-+-------+-/ /-+-------+-------+
38 | p | p | ... | p | ... | p | p |
39 | A+B+1| A+B | | N | | 1 | 0 |
40 +-------+-------+-/ /-+-------+-/ /-+-------+-------+
43 (carry) a .b ... | ... a .b a .b
60 for all i,j where i+j=N
63 a[], b[], p[] may not intersect.
64 Zero length factors signify 0 significant bits: treat as 0.0.
65 0.0 factors do the right thing.
66 Zero length product OK.
68 I chose the ForTran accent "foo[bar]" instead of the C accent "*garply"
69 because I felt the ForTran way was more intuitive. The C way would
70 probably yield better code on most C compilers. Dean Elsner.
71 (C style also gives deeper insight [to me] ... oh well ...) */
74 flonum_multip (const FLONUM_TYPE
*a
, const FLONUM_TYPE
*b
,
77 int size_of_a
; /* 0 origin */
78 int size_of_b
; /* 0 origin */
79 int size_of_product
; /* 0 origin */
80 int size_of_sum
; /* 0 origin */
81 int extra_product_positions
; /* 1 origin */
86 long significant
; /* TRUE when we emit a non-0 littlenum */
87 /* ForTran accent follows. */
88 int P
; /* Scan product low-order -> high. */
89 int N
; /* As in sum above. */
90 int A
; /* Which [] of a? */
91 int B
; /* Which [] of b? */
93 if ((a
->sign
!= '-' && a
->sign
!= '+')
94 || (b
->sign
!= '-' && b
->sign
!= '+'))
96 /* Got to fail somehow. Any suggestions? */
100 product
->sign
= (a
->sign
== b
->sign
) ? '+' : '-';
101 size_of_a
= a
->leader
- a
->low
;
102 size_of_b
= b
->leader
- b
->low
;
103 exponent
= a
->exponent
+ b
->exponent
;
104 size_of_product
= product
->high
- product
->low
;
105 size_of_sum
= size_of_a
+ size_of_b
;
106 extra_product_positions
= size_of_product
- size_of_sum
;
107 if (extra_product_positions
< 0)
109 P
= extra_product_positions
; /* P < 0 */
110 exponent
-= extra_product_positions
; /* Increases exponent. */
118 for (N
= 0; N
<= size_of_sum
; N
++)
122 for (A
= 0; A
<= N
; A
++)
125 if (A
<= size_of_a
&& B
<= size_of_b
&& B
>= 0)
128 printf ("a:low[%d.]=%04x b:low[%d.]=%04x work_before=%08x\n",
129 A
, a
->low
[A
], B
, b
->low
[B
], work
);
131 /* Watch out for sign extension! Without the casts, on
132 the DEC Alpha, the multiplication result is *signed*
133 int, which gets sign-extended to convert to the
135 work
+= (unsigned long) a
->low
[A
] * (unsigned long) b
->low
[B
];
136 carry
+= work
>> LITTLENUM_NUMBER_OF_BITS
;
137 work
&= LITTLENUM_MASK
;
139 printf ("work=%08x carry=%04x\n", work
, carry
);
144 if (significant
|| P
< 0)
148 product
->low
[P
] = work
;
150 printf ("P=%d. work[p]:=%04x\n", P
, work
);
157 extra_product_positions
++;
161 /* [P]-> position # size_of_sum + 1.
162 This is where 'carry' should go. */
164 printf ("final carry =%04x\n", carry
);
168 if (extra_product_positions
> 0)
169 product
->low
[P
] = carry
;
172 /* No room at high order for carry littlenum. */
173 /* Shift right 1 to make room for most significant littlenum. */
176 for (q
= product
->low
+ P
; q
>= product
->low
; q
--)
186 product
->leader
= product
->low
+ P
;
187 product
->exponent
= exponent
;