/
BigIntPrimeThread.cs
340 lines (304 loc) · 15.2 KB
/
BigIntPrimeThread.cs
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//-----------------------------------------------------------------------
// <copyright file="BigIntPrimeThread.cs" company="Joey Woodson">
// Copyright © 2009 Joey Woodson. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace PrimeNumbers
{
/// <summary>
/// Contains methods used by each thread to determine whether a <see cref="BigInt"/> is prime.
/// </summary>
public class BigIntPrimeThread
{
/// <summary>
/// The <see cref="BigInt"/> to check for primality.
/// </summary>
private BigInt numberToCheck = new BigInt();
/// <summary>
/// The square root of <see cref="numberToCheck"/>.
/// </summary>
private BigInt squareRoot = new BigInt();
/// <summary>
/// The first number that <see cref="numberToCheck"/> is to be tested for divisibility by.
/// </summary>
private BigInt divisor = new BigInt();
/// <summary>
/// The number to advance <see cref="divisor"/> by after every divisibility test.
/// </summary>
private uint stride;
/// <summary>
/// The <see cref="PrimeTester"/> that provides the thread-shared property <see cref="PrimeTester.Result"/>.
/// </summary>
private PrimeTester tester;
/// <summary>
/// The int representing the number of counters to be used.
/// </summary>
private int numberOfCounters;
/// <summary>
/// The counters to be used in testing <see cref="numberToCheck"/> for primality.
/// </summary>
private Counter[] counters;
/// <summary>
/// Initializes a new instance of the <see cref="BigIntPrimeThread"/> class.
/// </summary>
/// <param name="numberToCheck">The <see cref="BigInt"/> to test for primality.</param>
/// <param name="squareRoot">The square root of <paramref name="numberToCheck"/>.</param>
/// <param name="divisor">The first number that <paramref name="numberToCheck"/> is to be tested for divisibility by.</param>
/// <param name="stride">The number to advance <paramref name="divisor"/> by after every divisibility test.</param>
/// <param name="tester">The <see cref="PrimeTester"/> that provides the thread-shared property <see cref="PrimeTester.Result"/>.</param>
public BigIntPrimeThread(ref BigInt numberToCheck, ref BigInt squareRoot, ref BigInt divisor, uint stride, PrimeTester tester)
{
this.stride = stride;
this.tester = tester;
NativeMethods.InitializeAndSet(ref this.numberToCheck, ref numberToCheck);
NativeMethods.InitializeAndSet(ref this.squareRoot, ref squareRoot);
NativeMethods.InitializeAndSet(ref this.divisor, ref divisor);
}
/// <summary>
/// Initializes a new instance of the <see cref="BigIntPrimeThread"/> class.
/// </summary>
/// <param name="numberToCheck">The <see cref="BigInt"/> to test for primality.</param>
/// <param name="squareRoot">The square root of <paramref name="numberToCheck"/>.</param>
/// <param name="divisor">The first number that <paramref name="numberToCheck"/> is to be tested for divisibility by.</param>
/// <param name="stride">The number to advance <paramref name="divisor"/> by after every divisibility test.</param>
/// <param name="tester">The <see cref="PrimeTester"/> that provides the thread-shared property <see cref="PrimeTester.Result"/>.</param>
/// <param name="primes">A uint[] with the first n primes in it (n being the number of counters desired).</param>
public BigIntPrimeThread(ref BigInt numberToCheck, ref BigInt squareRoot, ref BigInt divisor, uint stride, PrimeTester tester, uint[] primes)
{
this.stride = stride;
this.tester = tester;
NativeMethods.InitializeAndSet(ref this.numberToCheck, ref numberToCheck);
NativeMethods.InitializeAndSet(ref this.squareRoot, ref squareRoot);
NativeMethods.InitializeAndSet(ref this.divisor, ref divisor);
if (primes != null)
{
this.numberOfCounters = primes.Length;
this.counters = new Counter[this.numberOfCounters];
BigInt temp = new BigInt();
NativeMethods.Initialize(ref temp);
for (int i = 0; i < this.numberOfCounters; i++)
{
NativeMethods.Subtract(ref temp, ref divisor, stride);
this.counters[i].Prime = primes[i];
this.counters[i].Value = NativeMethods.ModFloor(ref temp, ref temp, primes[i]);
this.counters[i].Stride = stride % primes[i];
}
NativeMethods.Clear(ref temp);
}
}
/// <summary>
/// Tests <see cref="numberToCheck"/> for primality.
/// </summary>
public unsafe void TestPrime()
{
if (this.numberOfCounters == 0)
{
this.TestPrimeNoCounters();
}
else
{
BigInt localNumberToCheck = new BigInt();
BigInt localSquareRoot = new BigInt();
BigInt localDivisor = new BigInt();
NativeMethods.InitializeAndSet(ref localNumberToCheck, ref this.numberToCheck);
NativeMethods.InitializeAndSet(ref localSquareRoot, ref this.squareRoot);
NativeMethods.InitializeAndSet(ref localDivisor, ref this.divisor);
NativeMethods.Clear(ref this.numberToCheck);
NativeMethods.Clear(ref this.squareRoot);
NativeMethods.Clear(ref this.divisor);
int localNumberOfCounters = this.numberOfCounters;
uint localStride = this.stride;
uint maximumDivisor = uint.MaxValue - localStride;
Counter[] counterArray = (Counter[])this.counters.Clone();
fixed (Counter* firstCounter = &counterArray[0], lastCounter = &counterArray[localNumberOfCounters - 1])
{
bool counterTriggered;
Counter* c = firstCounter;
if (NativeMethods.Compare(ref localDivisor, maximumDivisor) <= 0)
{
uint uintDivisor = NativeMethods.ToUint(ref localDivisor);
while (uintDivisor < maximumDivisor)
{
if (this.tester.Result != 0)
{
NativeMethods.Clear(ref localNumberToCheck);
NativeMethods.Clear(ref localSquareRoot);
NativeMethods.Clear(ref localDivisor);
return;
}
counterTriggered = false;
c = firstCounter;
while (c <= lastCounter)
{
c->Value += c->Stride;
if (c->Value >= c->Prime)
{
c->Value -= c->Prime;
if (c->Value == 0)
{
counterTriggered = true;
}
}
c++;
}
if (!counterTriggered)
{
if (NativeMethods.Compare(ref localSquareRoot, uintDivisor) < 0)
{
NativeMethods.Clear(ref localNumberToCheck);
NativeMethods.Clear(ref localSquareRoot);
NativeMethods.Clear(ref localDivisor);
return;
}
if (NativeMethods.Divisible(ref localNumberToCheck, uintDivisor) != 0)
{
this.tester.Result = 1;
BigInt temp = new BigInt();
NativeMethods.InitializeAndSet(ref temp, uintDivisor);
lock (this.tester)
{
this.tester.BigIntFactor = temp;
}
NativeMethods.Clear(ref temp);
NativeMethods.Clear(ref localNumberToCheck);
NativeMethods.Clear(ref localSquareRoot);
NativeMethods.Clear(ref localDivisor);
return;
}
}
uintDivisor += localStride;
}
NativeMethods.Set(ref localDivisor, uintDivisor);
}
while (true)
{
if (this.tester.Result != 0)
{
NativeMethods.Clear(ref localNumberToCheck);
NativeMethods.Clear(ref localSquareRoot);
NativeMethods.Clear(ref localDivisor);
return;
}
counterTriggered = false;
c = firstCounter;
while (c <= lastCounter)
{
c->Value += c->Stride;
if (c->Value >= c->Prime)
{
c->Value -= c->Prime;
if (c->Value == 0)
{
counterTriggered = true;
}
}
c++;
}
if (!counterTriggered)
{
if (NativeMethods.Compare(ref localDivisor, ref localSquareRoot) > 0)
{
NativeMethods.Clear(ref localNumberToCheck);
NativeMethods.Clear(ref localSquareRoot);
NativeMethods.Clear(ref localDivisor);
return;
}
if (NativeMethods.Divisible(ref localNumberToCheck, ref localDivisor) != 0)
{
this.tester.Result = 1;
lock (this.tester)
{
this.tester.BigIntFactor = localDivisor;
}
NativeMethods.Clear(ref localNumberToCheck);
NativeMethods.Clear(ref localSquareRoot);
NativeMethods.Clear(ref localDivisor);
return;
}
}
NativeMethods.Add(ref localDivisor, ref localDivisor, localStride);
}
}
}
}
/// <summary>
/// Tests <see cref="numberToCheck"/> for primality with no counters.
/// </summary>
public void TestPrimeNoCounters()
{
BigInt localNumberToCheck = new BigInt();
BigInt localSquareRoot = new BigInt();
BigInt localDivisor = new BigInt();
NativeMethods.InitializeAndSet(ref localNumberToCheck, ref this.numberToCheck);
NativeMethods.InitializeAndSet(ref localSquareRoot, ref this.squareRoot);
NativeMethods.InitializeAndSet(ref localDivisor, ref this.divisor);
NativeMethods.Clear(ref this.numberToCheck);
NativeMethods.Clear(ref this.squareRoot);
NativeMethods.Clear(ref this.divisor);
int localNumberOfCounters = this.numberOfCounters;
uint localStride = this.stride;
uint maximumDivisor = uint.MaxValue - localStride;
if (NativeMethods.Compare(ref localDivisor, maximumDivisor) <= 0)
{
uint uintDivisor = NativeMethods.ToUint(ref localDivisor);
while (uintDivisor < maximumDivisor)
{
if (this.tester.Result != 0)
{
NativeMethods.Clear(ref localNumberToCheck);
NativeMethods.Clear(ref localSquareRoot);
NativeMethods.Clear(ref localDivisor);
return;
}
if (NativeMethods.Compare(ref localSquareRoot, uintDivisor) < 0)
{
NativeMethods.Clear(ref localNumberToCheck);
NativeMethods.Clear(ref localSquareRoot);
NativeMethods.Clear(ref localDivisor);
return;
}
if (NativeMethods.Divisible(ref localNumberToCheck, uintDivisor) != 0)
{
this.tester.Result = 1;
BigInt temp = new BigInt();
NativeMethods.InitializeAndSet(ref temp, uintDivisor);
lock (this.tester)
{
this.tester.BigIntFactor = temp;
}
NativeMethods.Clear(ref temp);
NativeMethods.Clear(ref localNumberToCheck);
NativeMethods.Clear(ref localSquareRoot);
NativeMethods.Clear(ref localDivisor);
return;
}
uintDivisor += localStride;
}
NativeMethods.Set(ref localDivisor, uintDivisor);
}
while (this.tester.Result == 0)
{
if (NativeMethods.Compare(ref localDivisor, ref localSquareRoot) > 0)
{
NativeMethods.Clear(ref localNumberToCheck);
NativeMethods.Clear(ref localSquareRoot);
NativeMethods.Clear(ref localDivisor);
return;
}
if (NativeMethods.Divisible(ref localNumberToCheck, ref localDivisor) != 0)
{
this.tester.Result = 1;
lock (this.tester)
{
this.tester.BigIntFactor = localDivisor;
}
NativeMethods.Clear(ref localNumberToCheck);
NativeMethods.Clear(ref localSquareRoot);
NativeMethods.Clear(ref localDivisor);
return;
}
NativeMethods.Add(ref localDivisor, ref localDivisor, localStride);
}
}
}
}