| 1 | /* flonum_mult.c - multiply two flonums | 
|---|
| 2 | Copyright 1987, 1990, 1991, 1992, 1995, 2000 | 
|---|
| 3 | Free Software Foundation, Inc. | 
|---|
| 4 |  | 
|---|
| 5 | This file is part of Gas, the GNU Assembler. | 
|---|
| 6 |  | 
|---|
| 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. | 
|---|
| 13 |  | 
|---|
| 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.  */ | 
|---|
| 21 |  | 
|---|
| 22 | #include <ansidecl.h> | 
|---|
| 23 | #include "flonum.h" | 
|---|
| 24 |  | 
|---|
| 25 | /*      plan for a . b => p(roduct) | 
|---|
| 26 |  | 
|---|
| 27 | +-------+-------+-/   /-+-------+-------+ | 
|---|
| 28 | | a     | a     |  ...  | a     | a     | | 
|---|
| 29 | |  A    |  A-1  |       |  1    |  0    | | 
|---|
| 30 | +-------+-------+-/   /-+-------+-------+ | 
|---|
| 31 |  | 
|---|
| 32 | +-------+-------+-/   /-+-------+-------+ | 
|---|
| 33 | | b     | b     |  ...  | b     | b     | | 
|---|
| 34 | |  B    |  B-1  |       |  1    |  0    | | 
|---|
| 35 | +-------+-------+-/   /-+-------+-------+ | 
|---|
| 36 |  | 
|---|
| 37 | +-------+-------+-/   /-+-------+-/   /-+-------+-------+ | 
|---|
| 38 | | p     | p     |  ...  | p     |  ...  | p     | p     | | 
|---|
| 39 | |  A+B+1|  A+B  |       |  N    |       |  1    |  0    | | 
|---|
| 40 | +-------+-------+-/   /-+-------+-/   /-+-------+-------+ | 
|---|
| 41 |  | 
|---|
| 42 | /^\ | 
|---|
| 43 | (carry) a .b       ...      |      ...   a .b    a .b | 
|---|
| 44 | A  B                |             0  1    0  0 | 
|---|
| 45 | | | 
|---|
| 46 | ...         |      ...   a .b | 
|---|
| 47 | |                 1  0 | 
|---|
| 48 | | | 
|---|
| 49 | |          ... | 
|---|
| 50 | | | 
|---|
| 51 | | | 
|---|
| 52 | | | 
|---|
| 53 | |                 ___ | 
|---|
| 54 | |                 \ | 
|---|
| 55 | +-----  P  =   >  a .b | 
|---|
| 56 | N         /__  i  j | 
|---|
| 57 |  | 
|---|
| 58 | N = 0 ... A+B | 
|---|
| 59 |  | 
|---|
| 60 | for all i,j where i+j=N | 
|---|
| 61 | [i,j integers > 0] | 
|---|
| 62 |  | 
|---|
| 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. | 
|---|
| 67 |  | 
|---|
| 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 ...)  */ | 
|---|
| 72 |  | 
|---|
| 73 |  | 
|---|
| 74 | void | 
|---|
| 75 | flonum_multip (a, b, product) | 
|---|
| 76 | const FLONUM_TYPE *a; | 
|---|
| 77 | const FLONUM_TYPE *b; | 
|---|
| 78 | FLONUM_TYPE *product; | 
|---|
| 79 | { | 
|---|
| 80 | int size_of_a;                /* 0 origin  */ | 
|---|
| 81 | int size_of_b;                /* 0 origin  */ | 
|---|
| 82 | int size_of_product;          /* 0 origin  */ | 
|---|
| 83 | int size_of_sum;              /* 0 origin  */ | 
|---|
| 84 | int extra_product_positions;  /* 1 origin  */ | 
|---|
| 85 | unsigned long work; | 
|---|
| 86 | unsigned long carry; | 
|---|
| 87 | long exponent; | 
|---|
| 88 | LITTLENUM_TYPE *q; | 
|---|
| 89 | long significant;             /* TRUE when we emit a non-0 littlenum  */ | 
|---|
| 90 | /* ForTran accent follows.  */ | 
|---|
| 91 | int P;                        /* Scan product low-order -> high.  */ | 
|---|
| 92 | int N;                        /* As in sum above.  */ | 
|---|
| 93 | int A;                        /* Which [] of a?  */ | 
|---|
| 94 | int B;                        /* Which [] of b?  */ | 
|---|
| 95 |  | 
|---|
| 96 | if ((a->sign != '-' && a->sign != '+') | 
|---|
| 97 | || (b->sign != '-' && b->sign != '+')) | 
|---|
| 98 | { | 
|---|
| 99 | /* Got to fail somehow.  Any suggestions?  */ | 
|---|
| 100 | product->sign = 0; | 
|---|
| 101 | return; | 
|---|
| 102 | } | 
|---|
| 103 | product->sign = (a->sign == b->sign) ? '+' : '-'; | 
|---|
| 104 | size_of_a = a->leader - a->low; | 
|---|
| 105 | size_of_b = b->leader - b->low; | 
|---|
| 106 | exponent = a->exponent + b->exponent; | 
|---|
| 107 | size_of_product = product->high - product->low; | 
|---|
| 108 | size_of_sum = size_of_a + size_of_b; | 
|---|
| 109 | extra_product_positions = size_of_product - size_of_sum; | 
|---|
| 110 | if (extra_product_positions < 0) | 
|---|
| 111 | { | 
|---|
| 112 | P = extra_product_positions;      /* P < 0  */ | 
|---|
| 113 | exponent -= extra_product_positions;      /* Increases exponent.  */ | 
|---|
| 114 | } | 
|---|
| 115 | else | 
|---|
| 116 | { | 
|---|
| 117 | P = 0; | 
|---|
| 118 | } | 
|---|
| 119 | carry = 0; | 
|---|
| 120 | significant = 0; | 
|---|
| 121 | for (N = 0; N <= size_of_sum; N++) | 
|---|
| 122 | { | 
|---|
| 123 | work = carry; | 
|---|
| 124 | carry = 0; | 
|---|
| 125 | for (A = 0; A <= N; A++) | 
|---|
| 126 | { | 
|---|
| 127 | B = N - A; | 
|---|
| 128 | if (A <= size_of_a && B <= size_of_b && B >= 0) | 
|---|
| 129 | { | 
|---|
| 130 | #ifdef TRACE | 
|---|
| 131 | printf ("a:low[%d.]=%04x b:low[%d.]=%04x work_before=%08x\n", | 
|---|
| 132 | A, a->low[A], B, b->low[B], work); | 
|---|
| 133 | #endif | 
|---|
| 134 | /* Watch out for sign extension!  Without the casts, on | 
|---|
| 135 | the DEC Alpha, the multiplication result is *signed* | 
|---|
| 136 | int, which gets sign-extended to convert to the | 
|---|
| 137 | unsigned long!  */ | 
|---|
| 138 | work += (unsigned long) a->low[A] * (unsigned long) 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 | /* [P]-> position # size_of_sum + 1. | 
|---|
| 165 | This is where 'carry' should go.  */ | 
|---|
| 166 | #ifdef TRACE | 
|---|
| 167 | printf ("final carry =%04x\n", carry); | 
|---|
| 168 | #endif | 
|---|
| 169 | if (carry) | 
|---|
| 170 | { | 
|---|
| 171 | if (extra_product_positions > 0) | 
|---|
| 172 | product->low[P] = carry; | 
|---|
| 173 | else | 
|---|
| 174 | { | 
|---|
| 175 | /* No room at high order for carry littlenum.  */ | 
|---|
| 176 | /* Shift right 1 to make room for most significant littlenum.  */ | 
|---|
| 177 | exponent++; | 
|---|
| 178 | P--; | 
|---|
| 179 | for (q = product->low + P; q >= product->low; q--) | 
|---|
| 180 | { | 
|---|
| 181 | work = *q; | 
|---|
| 182 | *q = carry; | 
|---|
| 183 | carry = work; | 
|---|
| 184 | } | 
|---|
| 185 | } | 
|---|
| 186 | } | 
|---|
| 187 | else | 
|---|
| 188 | P--; | 
|---|
| 189 | product->leader = product->low + P; | 
|---|
| 190 | product->exponent = exponent; | 
|---|
| 191 | } | 
|---|