1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
#include "os.h"
#include <mp.h>
#include "dat.h"
/* */
/* from knuth's 1969 seminumberical algorithms, pp 233-235 and pp 258-260 */
/* */
/* mpvecmul is an assembly language routine that performs the inner */
/* loop. */
/* */
/* the karatsuba trade off is set empiricly by measuring the algs on */
/* a 400 MHz Pentium II. */
/* */
/* karatsuba like (see knuth pg 258) */
/* prereq: p is already zeroed */
static void
mpkaratsuba(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *p)
{
mpdigit *t, *u0, *u1, *v0, *v1, *u0v0, *u1v1, *res, *diffprod;
int u0len, u1len, v0len, v1len, reslen;
int sign, n;
/* divide each piece in half */
n = alen/2;
if(alen&1)
n++;
u0len = n;
u1len = alen-n;
if(blen > n){
v0len = n;
v1len = blen-n;
} else {
v0len = blen;
v1len = 0;
}
u0 = a;
u1 = a + u0len;
v0 = b;
v1 = b + v0len;
/* room for the partial products */
t = mallocz(Dbytes*5*(2*n+1), 1);
if(t == nil)
sysfatal("mpkaratsuba: %r");
u0v0 = t;
u1v1 = t + (2*n+1);
diffprod = t + 2*(2*n+1);
res = t + 3*(2*n+1);
reslen = 4*n+1;
/* t[0] = (u1-u0) */
sign = 1;
if(mpveccmp(u1, u1len, u0, u0len) < 0){
sign = -1;
mpvecsub(u0, u0len, u1, u1len, u0v0);
} else
mpvecsub(u1, u1len, u0, u1len, u0v0);
/* t[1] = (v0-v1) */
if(mpveccmp(v0, v0len, v1, v1len) < 0){
sign *= -1;
mpvecsub(v1, v1len, v0, v1len, u1v1);
} else
mpvecsub(v0, v0len, v1, v1len, u1v1);
/* t[4:5] = (u1-u0)*(v0-v1) */
mpvecmul(u0v0, u0len, u1v1, v0len, diffprod);
/* t[0:1] = u1*v1 */
memset(t, 0, 2*(2*n+1)*Dbytes);
if(v1len > 0)
mpvecmul(u1, u1len, v1, v1len, u1v1);
/* t[2:3] = u0v0 */
mpvecmul(u0, u0len, v0, v0len, u0v0);
/* res = u0*v0<<n + u0*v0 */
mpvecadd(res, reslen, u0v0, u0len+v0len, res);
mpvecadd(res+n, reslen-n, u0v0, u0len+v0len, res+n);
/* res += u1*v1<<n + u1*v1<<2*n */
if(v1len > 0){
mpvecadd(res+n, reslen-n, u1v1, u1len+v1len, res+n);
mpvecadd(res+2*n, reslen-2*n, u1v1, u1len+v1len, res+2*n);
}
/* res += (u1-u0)*(v0-v1)<<n */
if(sign < 0)
mpvecsub(res+n, reslen-n, diffprod, u0len+v0len, res+n);
else
mpvecadd(res+n, reslen-n, diffprod, u0len+v0len, res+n);
memmove(p, res, (alen+blen)*Dbytes);
free(t);
}
#define KARATSUBAMIN 32
void
mpvecmul(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *p)
{
int i;
mpdigit d;
mpdigit *t;
/* both mpvecdigmuladd and karatsuba are fastest when a is the longer vector */
if(alen < blen){
i = alen;
alen = blen;
blen = i;
t = a;
a = b;
b = t;
}
if(blen == 0){
memset(p, 0, Dbytes*(alen+blen));
return;
}
if(alen >= KARATSUBAMIN && blen > 1){
/* O(n^1.585) */
mpkaratsuba(a, alen, b, blen, p);
} else {
/* O(n^2) */
for(i = 0; i < blen; i++){
d = b[i];
if(d != 0)
mpvecdigmuladd(a, alen, d, &p[i]);
}
}
}
void
mpmul(mpint *b1, mpint *b2, mpint *prod)
{
mpint *oprod;
oprod = nil;
if(prod == b1 || prod == b2){
oprod = prod;
prod = mpnew(0);
}
prod->top = 0;
mpbits(prod, (b1->top+b2->top+1)*Dbits);
mpvecmul(b1->p, b1->top, b2->p, b2->top, prod->p);
prod->top = b1->top+b2->top+1;
prod->sign = b1->sign*b2->sign;
mpnorm(prod);
if(oprod != nil){
mpassign(prod, oprod);
mpfree(prod);
}
}
|