3
This describes the multiplication algorithm used by the MPI library.
5
This is basically a standard "schoolbook" algorithm. It is slow --
6
O(mn) for m = #a, n = #b -- but easy to implement and verify.
7
Basically, we run two nested loops, as illustrated here (R is the
11
for j <- 0 to (#b - 1)
12
for i <- 0 to (#a - 1)
13
w = (a[j] * b[i]) + k + c[i+j]
21
It is necessary that 'w' have room for at least two radix R digits.
22
The product of any two digits in radix R is at most:
24
(R - 1)(R - 1) = R^2 - 2R + 1
26
Since a two-digit radix-R number can hold R^2 - 1 distinct values,
27
this insures that the product will fit into the two-digit register.
29
To insure that two digits is enough for w, we must also show that
30
there is room for the carry-in from the previous multiplication, and
31
the current value of the product digit that is being recomputed.
32
Assuming each of these may be as big as R - 1 (and no larger,
33
certainly), two digits will be enough if and only if:
35
(R^2 - 2R + 1) + 2(R - 1) <= R^2 - 1
37
Solving this equation shows that, indeed, this is the case:
39
R^2 - 2R + 1 + 2R - 2 <= R^2 - 1
43
This suggests that a good radix would be one more than the largest
44
value that can be held in half a machine word -- so, for example, as
45
in this implementation, where we used a radix of 65536 on a machine
46
with 4-byte words. Another advantage of a radix of this sort is that
47
binary-level operations are easy on numbers in this representation.
49
Here's an example multiplication worked out longhand in radix-10,
50
using the above algorithm:
57
w = (a[jx] * b[ix]) + kin + c[ix + jx]
61
ix jx a[jx] b[ix] kin w c[i+j] kout 000000
62
0 0 9 9 0 81+0+0 1 8 000001
63
0 1 9 9 8 81+8+0 9 8 000091
64
0 2 9 9 8 81+8+0 9 8 000991
66
1 0 9 9 0 81+0+9 0 9 008901
67
1 1 9 9 9 81+9+9 9 9 008901
68
1 2 9 9 9 81+9+8 8 9 008901
70
2 0 9 9 0 81+0+9 0 9 098001
71
2 1 9 9 9 81+9+8 8 9 098001
72
2 2 9 9 9 81+9+9 9 9 098001
74
------------------------------------------------------------------
75
The contents of this file are subject to the Mozilla Public
76
License Version 1.1 (the "License"); you may not use this file
77
except in compliance with the License. You may obtain a copy of
78
the License at http://www.mozilla.org/MPL/
80
Software distributed under the License is distributed on an "AS
81
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
82
implied. See the License for the specific language governing
83
rights and limitations under the License.
85
The Original Code is the MPI Arbitrary Precision Integer Arithmetic
88
The Initial Developer of the Original Code is
89
Michael J. Fromberger <sting@linguist.dartmouth.edu>
91
Portions created by Michael J. Fromberger are
92
Copyright (C) 1998, 2000 Michael J. Fromberger. All Rights Reserved.
96
Alternatively, the contents of this file may be used under the
97
terms of the GNU General Public License Version 2 or later (the
98
"GPL"), in which case the provisions of the GPL are applicable
99
instead of those above. If you wish to allow use of your
100
version of this file only under the terms of the GPL and not to
101
allow others to use your version of this file under the MPL,
102
indicate your decision by deleting the provisions above and
103
replace them with the notice and other provisions required by
104
the GPL. If you do not delete the provisions above, a recipient
105
may use your version of this file under either the MPL or the GPL.
107
$Id: mul.txt,v 1.1 2000/07/14 00:44:35 nelsonb%netscape.com Exp $