-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmult.c
231 lines (208 loc) · 6.2 KB
/
mult.c
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include "common.h"
#include "arith.h"
#include "fft.h"
#include "mult.h"
void classical(uint8_t * restrict h, const uint8_t *f, const uint8_t *g, uint32_t n) {
memset(h, 0, 2*n * sizeof(uint8_t));
for(size_t i=0; i<n; i++) {
for(size_t j=0; j<n; j++) {
uint16_t t = (uint16_t)f[i] * (uint16_t)g[j];
for(int k=i+j; k<2*n && t; k++) {
if(((uint16_t)h[k] + (t & ((1<<8)-1)))>>8)
t += 1<<8;
h[k] += (uint8_t)t;
t >>= 8;
}
}
}
}
extern inline void classical64(uint8_t * restrict hh, const uint8_t *ff, const uint8_t *gg, uint32_t nn) {
assert(nn % 8 == 0);
memset(hh, 0, 2*nn * sizeof(uint8_t));
const uint64_t *f = (const uint64_t*)ff;
const uint64_t *g = (const uint64_t*)gg;
const uint64_t n = nn/8;
uint64_t *h = (uint64_t*)hh;
for(size_t i=0; i<n; i++) {
for(size_t j=0; j<n; j++) {
uint128_t t = (uint128_t)f[i] * (uint128_t)g[j];
for(int k=i+j; k<2*n && t; k++) {
t += (((uint128_t)h[k] + (uint64_t)t)>>64)<<64;
h[k] += (uint64_t)t;
t >>= 64;
}
}
}
}
void karatsuba(uint8_t * restrict h, const uint8_t *f, const uint8_t *g, uint32_t n) {
assert(n % 2 == 0);
memset(h, 0, 2*n);
if(n <= 32) {
classical64(h, f, g, n);
return;
}
const uint32_t m = n/2;
uint8_t *buf = malloc(n + 2*n + n + n);
uint8_t *z0 = buf;
uint8_t *z1 = buf + n;
uint8_t *z2 = buf + n + 2*n;
uint8_t *b1 = buf + n + 2*n + n;
uint8_t *b2 = buf + n + 2*n + n + m;
uint8_t flag = 0;
karatsuba(z0, f, g, m);
karatsuba(z2, f+m, g+m, m);
memcpy(b1, f+m, m);
memcpy(b2, g+m, m);
flag |= addc(b1, f, m) << 0;
flag |= addc(b2, g, m) << 1;
karatsuba(z1, b1, b2, m);
if(flag & 1) h[3*m] += addc(z1+m, b2, m);
if(flag & 2) h[3*m] += addc(z1+m, b1, m);
h[3*m] += flag == 0b11;
if(!subc(z1, z0, n)) assert(h[3*m]), h[3*m]--;
if(!subc(z1, z2, n)) assert(h[3*m]), h[3*m]--;
memcpy(h, z0, n);
h[3*m] += addc(h + m, z1, n);
assert(!addc(h + 2*m, z2, n));
free(buf);
}
static inline uint32_t optk(uint32_t n) {
switch(n) {
case 1<<7:
case 1<<8:
case 1<<9:
case 1<<10:
return 10;
case 1<<11:
return 11;
case 1<<12:
return 12;
case 1<<13:
return 6;
case 1<<14:
return 6;
case 1<<15:
return 9;
case 1<<16:
return 10;
case 1<<17:
return 10;
case 1<<18:
return 11;
case 1<<19:
return 11;
case 1<<20:
return 12;
case 1<<21:
return 12;
case 1<<22:
return 13;
case 1<<23:
return 13;
case 1<<24:
return 14;
case 1<<25:
return 12;
default:
return 14;
}
}
char nega(uint8_t * restrict h, const uint8_t *f, const uint8_t *g, uint32_t n) {
// len(h) == len(f) == len(g) == n
uint32_t sp, u, each, k = optk(n);
for(;k<18; k+=2) {
sp = 1<<k;
if (n <= sp) {
uint8_t *hh = alloca(2*n);
karatsuba(hh, f, g, n);
sub(hh, hh+n, n);
memcpy(h, hh, n);
return 1;
}
each = n/sp;
for(u=2; !(u*8 >= sp && 2*8*each+k-1<8*u); u*=2);
uint8_t *buf = (uint8_t*)malloc(u*sp * 2 + u * 5);
memset(buf, 0, u*sp * 2);
uint8_t *ff = buf;
uint8_t *gg = buf + u*sp;
uint8_t *hh = buf + u*sp * 2;
for(int i=0; i<sp; i++) {
memcpy(ff + i*u, f + i*each, each);
memcpy(gg + i*u, g + i*each, each);
}
if(!fft(ff, u, sp)) goto KINC;
if(!fft(gg, u, sp)) goto KINC;
for(uint32_t i=0; i<sp; i++) {
assert(u < n);
if(!nega(hh, ff + u*i, gg + u*i, u)) goto KINC;
memcpy(ff + u*i, hh, u * sizeof(uint8_t));
}
if(!ifft(ff, u, sp)) goto KINC;
memset(gg, 0, 2*u);
memset(gg+2*u+u, 0, u);
for(int i=0; i<sp; i++) {
memcpy(gg + 2*u, ff + i*u, u);
if(!add(gg, gg + 2*u, 2*u)) continue;
memcpy(h + i*each, gg, each);
memmove(gg, gg+each, 2*u-each);
memset(gg + 2*u-each, 0, each);
}
free(buf);
return 1;
KINC:
;
}
return 0;
}
char _mult(uint8_t * restrict h, const uint8_t *f, const uint8_t *g, uint32_t n, uint32_t k);
char mult(uint8_t * restrict h, const uint8_t *f, const uint8_t *g, uint32_t n) {
return _mult(h,f,g,n,optk(n));
}
char _mult(uint8_t * restrict h, const uint8_t *f, const uint8_t *g, uint32_t n, uint32_t k) {
// len(h) == 2 * len(f) == 2 * len(g) == 2 * n
uint32_t sp, u, each;
for(;k<18; k+=2) {
sp = 1<<k;
if (n <= sp) {
karatsuba(h, f, g, n);
return 1;
}
each = n/sp;
for(u=2; !(u*8 >= 2*sp && 2*8*each+k-1<8*u); u*=2);
uint8_t *buf = (uint8_t*)malloc(2*u*sp * 2 + 2*u * 5);
memset(buf, 0, 2*u*sp * 2 + 2*u);
uint8_t *ff = buf;
uint8_t *gg = buf + 2*u*sp;
uint8_t *hh = buf + 2*u*sp * 2;
for(int i=0; i<sp; i++) {
memcpy(ff + i*u, f + i*each, each);
memcpy(gg + i*u, g + i*each, each);
}
if(!fft(ff, u, 2*sp)) goto KINC;
if(!fft(gg, u, 2*sp)) goto KINC;
for(uint32_t i=0; i<2*sp; i++) {
assert(u < n);
if(!nega(hh, ff + u*i, gg + u*i, u)) goto KINC;
memcpy(ff + u*i, hh, u * sizeof(uint8_t));
}
if(!ifft(ff, u, 2*sp)) goto KINC;
memset(gg, 0, 2*u);
memset(gg+2*u+u, 0, u);
for(int i=0; i<2*sp; i++) {
memcpy(gg + 2*u, ff + i*u, u);
if(!add(gg, gg + 2*u, 2*u)) continue;
memcpy(h + i*each, gg, each);
memmove(gg, gg+each, 2*u-each);
memset(gg + 2*u-each, 0, each);
}
free(buf);
return 1;
KINC:
;
}
return 0;
}