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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
/* mpn_mulmid -- middle product
Contributed by David Harvey.
THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE. IT IS ONLY
SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
GUARANTEED THAT IT'LL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
Copyright 2011 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any
later version.
or both in parallel, as here.
The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received copies of the GNU General Public License and the
GNU Lesser General Public License along with the GNU MP Library. If not,
see https://www.gnu.org/licenses/. */
#include "gmp-impl.h"
#define CHUNK (200 + MULMID_TOOM42_THRESHOLD)
void
mpn_mulmid (mp_ptr rp,
mp_srcptr ap, mp_size_t an,
mp_srcptr bp, mp_size_t bn)
{
mp_size_t rn, k;
mp_ptr scratch, temp;
ASSERT (an >= bn);
ASSERT (bn >= 1);
ASSERT (! MPN_OVERLAP_P (rp, an - bn + 3, ap, an));
ASSERT (! MPN_OVERLAP_P (rp, an - bn + 3, bp, bn));
if (bn < MULMID_TOOM42_THRESHOLD)
{
/* region not tall enough to make toom42 worthwhile for any portion */
if (an < CHUNK)
{
/* region not too wide either, just call basecase directly */
mpn_mulmid_basecase (rp, ap, an, bp, bn);
return;
}
/* Region quite wide. For better locality, use basecase on chunks:
AAABBBCC..
.AAABBBCC.
..AAABBBCC
*/
k = CHUNK - bn + 1; /* number of diagonals per chunk */
/* first chunk (marked A in the above diagram) */
mpn_mulmid_basecase (rp, ap, CHUNK, bp, bn);
/* remaining chunks (B, C, etc) */
an -= k;
while (an >= CHUNK)
{
mp_limb_t t0, t1, cy;
ap += k, rp += k;
t0 = rp[0], t1 = rp[1];
mpn_mulmid_basecase (rp, ap, CHUNK, bp, bn);
ADDC_LIMB (cy, rp[0], rp[0], t0); /* add back saved limbs */
MPN_INCR_U (rp + 1, k + 1, t1 + cy);
an -= k;
}
if (an >= bn)
{
/* last remaining chunk */
mp_limb_t t0, t1, cy;
ap += k, rp += k;
t0 = rp[0], t1 = rp[1];
mpn_mulmid_basecase (rp, ap, an, bp, bn);
ADDC_LIMB (cy, rp[0], rp[0], t0);
MPN_INCR_U (rp + 1, an - bn + 2, t1 + cy);
}
return;
}
/* region is tall enough for toom42 */
rn = an - bn + 1;
if (rn < MULMID_TOOM42_THRESHOLD)
{
/* region not wide enough to make toom42 worthwhile for any portion */
TMP_DECL;
if (bn < CHUNK)
{
/* region not too tall either, just call basecase directly */
mpn_mulmid_basecase (rp, ap, an, bp, bn);
return;
}
/* Region quite tall. For better locality, use basecase on chunks:
AAAAA....
.AAAAA...
..BBBBB..
...BBBBB.
....CCCCC
*/
TMP_MARK;
temp = TMP_ALLOC_LIMBS (rn + 2);
/* first chunk (marked A in the above diagram) */
bp += bn - CHUNK, an -= bn - CHUNK;
mpn_mulmid_basecase (rp, ap, an, bp, CHUNK);
/* remaining chunks (B, C, etc) */
bn -= CHUNK;
while (bn >= CHUNK)
{
ap += CHUNK, bp -= CHUNK;
mpn_mulmid_basecase (temp, ap, an, bp, CHUNK);
mpn_add_n (rp, rp, temp, rn + 2);
bn -= CHUNK;
}
if (bn)
{
/* last remaining chunk */
ap += CHUNK, bp -= bn;
mpn_mulmid_basecase (temp, ap, rn + bn - 1, bp, bn);
mpn_add_n (rp, rp, temp, rn + 2);
}
TMP_FREE;
return;
}
/* we're definitely going to use toom42 somewhere */
if (bn > rn)
{
/* slice region into chunks, use toom42 on all chunks except possibly
the last:
AA....
.AA...
..BB..
...BB.
....CC
*/
TMP_DECL;
TMP_MARK;
temp = TMP_ALLOC_LIMBS (rn + 2 + mpn_toom42_mulmid_itch (rn));
scratch = temp + rn + 2;
/* first chunk (marked A in the above diagram) */
bp += bn - rn;
mpn_toom42_mulmid (rp, ap, bp, rn, scratch);
/* remaining chunks (B, C, etc) */
bn -= rn;
while (bn >= rn)
{
ap += rn, bp -= rn;
mpn_toom42_mulmid (temp, ap, bp, rn, scratch);
mpn_add_n (rp, rp, temp, rn + 2);
bn -= rn;
}
if (bn)
{
/* last remaining chunk */
ap += rn, bp -= bn;
mpn_mulmid (temp, ap, rn + bn - 1, bp, bn);
mpn_add_n (rp, rp, temp, rn + 2);
}
TMP_FREE;
}
else
{
/* slice region into chunks, use toom42 on all chunks except possibly
the last:
AAABBBCC..
.AAABBBCC.
..AAABBBCC
*/
TMP_DECL;
TMP_MARK;
scratch = TMP_ALLOC_LIMBS (mpn_toom42_mulmid_itch (bn));
/* first chunk (marked A in the above diagram) */
mpn_toom42_mulmid (rp, ap, bp, bn, scratch);
/* remaining chunks (B, C, etc) */
rn -= bn;
while (rn >= bn)
{
mp_limb_t t0, t1, cy;
ap += bn, rp += bn;
t0 = rp[0], t1 = rp[1];
mpn_toom42_mulmid (rp, ap, bp, bn, scratch);
ADDC_LIMB (cy, rp[0], rp[0], t0); /* add back saved limbs */
MPN_INCR_U (rp + 1, bn + 1, t1 + cy);
rn -= bn;
}
TMP_FREE;
if (rn)
{
/* last remaining chunk */
mp_limb_t t0, t1, cy;
ap += bn, rp += bn;
t0 = rp[0], t1 = rp[1];
mpn_mulmid (rp, ap, rn + bn - 1, bp, bn);
ADDC_LIMB (cy, rp[0], rp[0], t0);
MPN_INCR_U (rp + 1, rn + 1, t1 + cy);
}
}
}
|