forked from stream1972/gfnsieve_ocl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfnsvCUDA+.cpp
2651 lines (2165 loc) · 66.4 KB
/
gfnsvCUDA+.cpp
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#endif
#define PROG_NAME "GFNSvCUDA+"
#ifdef DEVICE_CUDA
#define PROG_VERSION "0.7"
#define PROG_COPY_RIGHT "2015 Anand Nair (anand.s.nair AT gmail)"
#else
#define PROG_VERSION "0.7.1"
#define PROG_COPY_RIGHT "2015 Anand Nair (anand.s.nair AT gmail)" "\n" \
"OpenCL port by Roman Trunov (stream AT proxyma ru)"
#endif
#define PROG_TITLE PROG_NAME " v" PROG_VERSION " (c) " PROG_COPY_RIGHT
#define __STDC_FORMAT_MACROS
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#if defined(_MSC_VER) && _MSC_VER <= 1600 /* At least MSVC 2010 does not have nor this nor "inttypes.h" */
#define PRIu64 "I64u"
#else
#include <inttypes.h>
#endif
#include <assert.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#if defined(DEVICE_CUDA) + defined(DEVICE_OPENCL) + defined(DEVICE_SIMULATION) != 1
#error One DEVICE_xxx hardware platform must be defined
#endif
#ifdef DEVICE_CUDA
#include <cuda.h>
#include <cuda_runtime.h>
#define cudaMalloc_ro cudaMalloc
#define cudaMalloc_rw cudaMalloc
#define cudaMemcpy_htd cudaMemcpy
#define cudaMemcpy_hth cudaMemcpy
#endif
#ifdef DEVICE_SIMULATION
#ifndef __NVCC__ // seems to be pre-included in NVCC
typedef enum
{
cudaSuccess,
cudaErrorMissingConfiguration,
cudaErrorMemoryAllocation
} cudaError_t;
#endif
#define cudaGetErrorString(n) "Fake CUDA error"
/* For simulation, all memory is allocated on PC using plain malloc/free */
static void cudaFreeHost_wrapper(void *p) { if (p) free(p); }
#define cudaFreeHost cudaFreeHost_wrapper
#define cudaFree cudaFreeHost
static cudaError_t cudaMalloc_wrapper(void **pHost, size_t size)
{
*pHost = malloc(size);
return *pHost ? cudaSuccess : cudaErrorMemoryAllocation;
}
#define cudaMalloc cudaMalloc_wrapper
#define cudaMalloc_ro cudaMalloc
#define cudaMalloc_rw cudaMalloc
#define cudaHostAlloc(p, n, f) cudaMalloc(p, n)
static cudaError_t cudaMemcpyWrapper(void *dst, const void *src, size_t count)
{
memcpy(dst, src, count);
return cudaSuccess;
}
#define cudaMemcpy_htd(dst, src, count, kind) cudaMemcpyWrapper(dst, src, count)
#define cudaMemcpy_hth(dst, src, count, kind) cudaMemcpyWrapper(dst, src, count)
#endif // DEVICE_SIMULATION
#define PETA 1000000000000000ull
// #define INIT_MODE // uncomment to enable special sieve init mode (handle ranges below 1P)
#ifndef BMAX_HARDCODED
// #define BMAX_HARDCODED 100000000 // 100M harcoded for PG
#define BMAX_HARDCODED 2000000000 // extra hard search PG
#endif
#ifdef INIT_MODE
/*
* Ranges below 1P will generate huge amount of factors. Output buffer must
* be able to store all of them. This is unneccessary for normal crunching
* (will only slow things down due to extra buffer copying)
*
* Even with this buffer, GFN-15 can be run at 'B5' only in 0-1P range!
* GFN-16 - at B6, and so on.
*/
#define RESULT_BUFFER_SIZE 2000000
#else // INIT_MODE
#define RESULT_BUFFER_SIZE 10000
#endif // INIT_MODE
#define RESULT_BUFFER_COUNT (RESULT_BUFFER_SIZE/2-1)
#if CUDART_VERSION >= 4000
#define SYNC_CALL cudaDeviceSynchronize
#define BLOCKING_SYNC cudaDeviceScheduleBlockingSync
#else
#define SYNC_CALL cudaThreadSynchronize
#define BLOCKING_SYNC cudaDeviceBlockingSync
#endif
typedef uint32_t U32;
typedef uint64_t U64;
typedef U64 u64;
typedef U32 u32;
typedef U32 HALF[3];
typedef U32 FULL[6];
typedef u64 MY_TICK;
#define TICKS_PER_SEC 1000
#ifdef PLATFORM_WIN32
#include <intrin.h>
#define MY_TIME GetTickCount
#endif
#if defined(PLATFORM_LINUX) || defined(PLATFORM_MAC)
#define MY_TIME my_custom_clock
static
U64 __emulu(U32 a, U32 b)
{
return (U64)a*b;
}
#endif
#ifdef DEVICE_OPENCL
#include "gsv_ocl.h"
#define cudaError_t cl_int
#define cudaSuccess CL_SUCCESS
#define cudaGetErrorString ocl_strerror
static cl_int cudaHostAlloc(void **pHost, size_t size, unsigned int flags)
{
(void) flags;
*pHost = malloc(size);
return *pHost ? CL_SUCCESS : CL_OUT_OF_HOST_MEMORY;
}
static void cudaFreeHost(void *p) { if (p) free(p); }
static cl_int oclCreateBufferGeneric(cl_mem *pmem, size_t size, unsigned flags, ocl_context_t *cont)
{
cl_int status;
*pmem = clCreateBuffer(cont->clcontext, flags, size, NULL, &status);
ocl_diagnose(status, "clCreateBuffer", cont);
return status;
}
#define cudaMalloc_ro(pp, size) oclCreateBufferGeneric((cl_mem *)(pp), size, CL_MEM_READ_ONLY, gd.device_ctx)
#define cudaMalloc_rw(pp, size) oclCreateBufferGeneric((cl_mem *)(pp), size, CL_MEM_READ_WRITE, gd.device_ctx)
#define cudaFree(p) ocl_diagnose( clReleaseMemObject(p), "clReleaseMemObject", gd.device_ctx )
static cl_int hostMemcpyWrapper(void *dest, const void *src, size_t size)
{
memcpy(dest, src, size);
return CL_SUCCESS;
}
static cl_int copyToDeviceWrapper(cl_mem dest, void *src, size_t size, ocl_context_t *cont)
{
cl_int status;
status = clEnqueueWriteBuffer(cont->cmdQueue, dest, CL_TRUE, 0, size, src, 0, NULL, NULL);
return ocl_diagnose(status, "clEnqueueWriteBuffer", cont);
}
#define cudaMemcpy_hth(dest, src, size, flags) hostMemcpyWrapper(dest, src, size)
#define cudaMemcpy_htd(dest, src, size, flags) copyToDeviceWrapper(dest, src, size, gd.device_ctx)
#endif // DEVICE_OPENCL
// Global data structure. Avoid namespace conflict with local variables.
struct global_data {
U32 n; // 18..24. might change later after safety analysis
U32 N; // 2^n
U32 N1; // 2^(n+1) -- factors are of the form k.N1+1
U64 k; // k from above. Limited to 2^51
double inv_k; // 1.0/k
double inv_N1; // 1.0/N1
double inv_f; // 1.0/k * 1.0/N1
double inv_f_scale;
U64 stat;
U64 factorcount;
HALF the_f, the_f_inverse;
U32 the_f_bits;
#if defined(DEVICE_CUDA) || defined(DEVICE_SIMULATION)
u64 *h_Factor_Mult_Ratio, *d_Factor_Mult_Ratio, *b_Factor_Mult_Ratio;
u32 *h_Factor_Mult_Ratio1, *d_Factor_Mult_Ratio1, *b_Factor_Mult_Ratio1;
u64 *h_Init , *d_Init;
u32 *h_Init1 , *d_Init1;
u32 *h_Result, *d_Result;
#endif
#ifdef DEVICE_OPENCL
/* Same as CUDA but device memory must have cl_mem type */
u64 *h_Factor_Mult_Ratio, *b_Factor_Mult_Ratio; cl_mem d_Factor_Mult_Ratio;
u32 *h_Factor_Mult_Ratio1, *b_Factor_Mult_Ratio1; cl_mem d_Factor_Mult_Ratio1;
u64 *h_Init; cl_mem d_Init;
u32 *h_Init1; cl_mem d_Init1;
u32 *h_Result; cl_mem d_Result;
#endif
MY_TICK starttime;
U32 startp_in_peta;
U32 endp_in_peta;
U32 bmax;
U32 candsInBuffer;
U32 factorsInBuffer;
char ckpt[80];
char fact[80];
int device_number;
#ifdef DEVICE_CUDA
cudaDeviceProp device_info;
#endif
#ifdef DEVICE_OPENCL
ocl_context_t *device_ctx;
u32 use_nvidia_workaround;
bool timer_activated;
#endif
U32 b_blocks_per_grid, blocks_per_grid;
} gd;
// Jacobi symbol
static
int jacobi(U32 m, U32 n)
{
int c = 1;
while((m & 1) == 0) { m >>= 1; c = -c; }
c &= (int) ((n ^ (n>>1) ^ 2) & 2) - 1;
if(m == 1) {
return c;
}
// quadratic reciprocity
c *= (int) (( n & m & 2 ) ^ 2) - 1;
return c * jacobi(n%m, m);
}
//******** S I E V I N G *********
#define SP_COUNT 82000
static
struct sieve_prime_t {
U32 p;
U32 x; // index into array
} sp[SP_COUNT];
#define MAX_PRIME 1024*1024
#define MAX_PRIME_SQRT 1024
#define bit_set(arr, ndx) arr[ndx >> 5] |= (1 << (ndx & 0x1F))
#define bit_test(arr, ndx) (arr[ndx >> 5] & (1 << (ndx & 0x1F)))
#define bit_clear(arr, ndx) arr[ndx >> 5] &= ~(1 << (ndx & 0x1F))
static
U32 invN1(U32 p)
{
U32 h = 1;
for(U32 i=0; i <= gd.n; i++)
{
if(h & 1) h += p;
h >>= 1; // h = h/2 (mod p)
}
return h;
}
static
void init_sieve_primes(U64 startk)
{
// calculate primes
U32 *sieve = (U32 *) malloc(MAX_PRIME/2);
memset(sieve, 0xFF, MAX_PRIME/2);
U32 count = 0, p, bit;
for(p=3, bit=p>>1; p < MAX_PRIME_SQRT; p += 2, bit++)
{
if( bit_test(sieve, bit) )
{
sp[count++].p = p;
for(U32 j=(p*p)>>1; j<MAX_PRIME/2; j += p)
{
bit_clear(sieve, j);
}
}
}
for( ; count < SP_COUNT; p += 2, bit++)
{
if( bit_test(sieve, bit) )
{
sp[count++].p = p;
}
}
free(sieve);
// compute x
for(U32 i=0; i<SP_COUNT; i++)
{
p = sp[i].p;
U32 modp = startk%p;
U32 x = invN1(p) + modp;
if(x >= p) x -= p;
if(x) x = p-x;
sp[i].x = x;
}
}
#define SIEVE_SIZE 16384
#define SIEVE_BITS (SIEVE_SIZE*sizeof(U32)*8)
static
U32 sieve[SIEVE_SIZE];
static
void sieve_iteration()
{
memset(sieve, 0xFF, sizeof(sieve));
for(U32 i=0; i < SP_COUNT; i++)
{
U32 p = sp[i].p, x = sp[i].x;
for(; x < SIEVE_BITS; x += p)
{
bit_clear(sieve, x);
}
sp[i].x = x - SIEVE_BITS;
}
}
//******** END S I E V I N G END *********
#if !defined PLATFORM_WIN32 && !defined PLATFORM_LINUX && !defined PLATFORM_MAC
#error No platforms defined
#endif
#define M_2_POW_32 4294967296.0
#define MASK_24 0xffffff
static void modFULLslow(const FULL a, HALF b);
static void expmodHALF(U32 b, HALF ret);
static void expmodHALFcheck(const HALF init, HALF ret);
static void calc_inverse(void);
static void calc_ratio(const HALF num, const HALF den, HALF ratio);
#ifdef PLATFORM_WIN32
#include <Windows.h>
static
volatile int term_requested = 0;
static BOOL WINAPI CtrlHandler( DWORD fdwCtrlType )
{
(void) fdwCtrlType;
term_requested = 1;
printf("\nTermination requested\n");
return TRUE;
}
#endif // PLATFORM_WIN32
#if defined(PLATFORM_LINUX) || defined(PLATFORM_MAC)
#include <signal.h>
static
MY_TICK my_custom_clock()
{
timespec t;
clock_gettime(CLOCK_REALTIME, &t);
return (MY_TICK) t.tv_sec * TICKS_PER_SEC + t.tv_nsec / (1000000000 / TICKS_PER_SEC);
}
static
volatile sig_atomic_t term_requested = 0;
static
void my_sig_handler(int sig)
{
term_requested = 1;
printf("\nTermination requested\n");
signal(sig, my_sig_handler);
}
#ifdef DEVICE_OPENCL
/* These functions are used to enable fine-grade scheduler on Windows (n=1ms tick),
* Linux (at least on my machine :) seems to be OK by default.
*/
static void timeBeginPeriod(int n) { (void) n; }
static void timeEndPeriod(int n) { (void) n; }
#endif
#endif // PLATFORM_LINUX || PLATFORM_MAC
static
U64 cvt_q(const HALF h)
{
return ((U64) h[1] << 32) + h[0];
}
static
U64 cvt_q_hi(const HALF h)
{
return ((U64) h[2] << 32) + h[1];
}
static
double cvt_dbl(const HALF h)
{
return ((double) h[2] * M_2_POW_32 + (double) h[1]) * M_2_POW_32 + (double) h[0];
}
static
void initHALF2(HALF a, U64 q, U32 d)
{
a[0] = (U32) q;
a[1] = (U32) (q >> 32);
a[2] = d;
}
static
void initHALF(HALF a, U64 q)
{
initHALF2(a, q, 0);
}
static
void copyHALF(HALF dest, const HALF src)
{
dest[0] = src[0], dest[1] = src[1], dest[2] = src[2];
}
static
void divmodHALFsingle(HALF a, U32 b, U32 &m) // a /= b
{
U64 q;
U64 r = 0;
for(int i=2; i >= 0; i--)
{
r = (r << 32) + a[i];
q = r/b; a[i] = (U32) q;
r -= q * b;
}
m = (U32) r;
}
#if 0 // Not used
static
void divHALFsingle(HALF a, U32 b) // a /= b
{
U32 dummy;
divmodHALFsingle(a,b,dummy);
}
#endif
static
void shrHALF(HALF a, U32 b) // a >>= b
{
while(b >= 32)
{
a[0] = a[1], a[1] = a[2], a[2] = 0;
b -= 32;
}
if(b > 0)
{
a[0] = (a[0] >> b) | (a[1] << (32-b));
a[1] = (a[1] >> b) | (a[2] << (32-b));
a[2] >>= b;
}
}
static
void shlHALF(HALF a, U32 b) // a <<= b
{
while(b >= 32)
{
a[2] = a[1], a[1] = a[0], a[0] = 0;
b -= 32;
}
if(b > 0)
{
a[2] = (a[2] << b) | (a[1] >> (32-b));
a[1] = (a[1] << b) | (a[0] >> (32-b));
a[0] <<= b;
}
}
#if 0 // Not used
static
void hiHALF(const FULL f, HALF h)
{
h[0] = f[3];
h[1] = f[4];
h[2] = f[5];
}
#endif
static
void loHALF(const FULL f, HALF h)
{
h[0] = f[0];
h[1] = f[1];
h[2] = f[2];
}
static
void mulHALF(const HALF a, const HALF b, FULL c)
{
U64 t;
U32 m;
HALF w;
m = b[0];
t = __emulu(m, a[0]); c[0] = (U32) t; t >>= 32;
t += __emulu(m, a[1]); w[0] = (U32) t; t >>= 32;
t += __emulu(m, a[2]); w[1] = (U32) t; w[2] = (U32) (t >> 32);
m = b[1];
t = __emulu(m, a[0]) + w[0]; c[1] = (U32) t; t >>= 32;
t += __emulu(m, a[1]) + w[1]; w[0] = (U32) t; t >>= 32;
t += __emulu(m, a[2]) + w[2]; w[1] = (U32) t; w[2] = (U32) (t >> 32);
m = b[2];
t = __emulu(m, a[0]) + w[0]; c[2] = (U32) t; t >>= 32;
t += __emulu(m, a[1]) + w[1]; c[3] = (U32) t; t >>= 32;
t += __emulu(m, a[2]) + w[2]; c[4] = (U32) t; c[5] = (U32) (t >> 32);
}
static
void mulHALFpartial(const HALF a, const HALF b, HALF c)
{
U64 t;
U32 m;
HALF w;
m = b[0];
t = __emulu(m, a[0]); c[0] = (U32) t; t >>= 32;
t += __emulu(m, a[1]); w[0] = (U32) t; t >>= 32;
t += __emulu(m, a[2]); w[1] = (U32) t;
m = b[1];
t = __emulu(m, a[0]) + w[0]; c[1] = (U32) t; t >>= 32;
t += __emulu(m, a[1]) + w[1];
m = b[2];
t += __emulu(m, a[0]); c[2] = (U32) t;
}
static
void mulHALFpartialhi(const HALF a, const HALF b, HALF c)
{
U64 t;
U32 m;
HALF w;
m = b[0];
t = __emulu(m, a[0]); t >>= 32;
t += __emulu(m, a[1]); w[0] = (U32) t; t >>= 32;
t += __emulu(m, a[2]); w[1] = (U32) t; w[2] = (U32) (t >> 32);
m = b[1];
t = __emulu(m, a[0]) + w[0]; t >>= 32;
t += __emulu(m, a[1]) + w[1]; w[0] = (U32) t; t >>= 32;
t += __emulu(m, a[2]) + w[2]; w[1] = (U32) t; w[2] = (U32) (t >> 32);
m = b[2];
if( m )
{
t = __emulu(m, a[0]) + w[0]; t >>= 32;
t += __emulu(m, a[1]) + w[1]; c[0] = (U32) t; t >>= 32;
t += __emulu(m, a[2]) + w[2]; c[1] = (U32) t; c[2] = (U32) (t >> 32);
}
else
{
c[0] = w[1];
c[1] = w[2];
c[2] = 0;
}
}
static
void squareHALF(const HALF a, FULL b)
{
U64 t;
U32 m;
U32 w1, w2, w3, w4;
// Calculate intermediate products.
m = a[0];
t = __emulu(m, a[1]); w1 = (U32) t; t >>= 32;
t += __emulu(m, a[2]); w2 = (U32) t; t >>= 32;
m = a[1];
t += __emulu(m, a[2]); w3 = (U32) t; w4 = (U32) (t >> 32);
// Final = square products + 2*intermediate products
m = a[0]; t = __emulu(m, m); b[0] = (U32) t; b[1] = (t >> 32);
m = a[1]; t = __emulu(m, m); b[2] = (U32) t; b[3] = (t >> 32);
m = a[2]; t = __emulu(m, m); b[4] = (U32) t; b[5] = (t >> 32);
t = __emulu(w1, 2) + b[1]; b[1] = (U32) t; t >>= 32;
t += __emulu(w2, 2) + b[2]; b[2] = (U32) t; t >>= 32;
t += __emulu(w3, 2) + b[3]; b[3] = (U32) t; t >>= 32;
t += __emulu(w4, 2) + b[4]; b[4] = (U32) t; t >>= 32;
t += b[5]; b[5] = (U32) t;
}
static
int cmpHALF(const HALF a, const HALF b)
{
if(a[2] > b[2]) return 1;
if(a[2] < b[2]) return -1;
if(a[1] > b[1]) return 1;
if(a[1] < b[1]) return -1;
if(a[0] > b[0]) return 1;
if(a[0] < b[0]) return -1;
return 0;
}
static
void addHALF(HALF a, const HALF b) // a += b
{
U64 t;
t = (U64) a[0] + b[0]; a[0] = (U32) t; t >>= 32;
t += (U64) a[1] + b[1]; a[1] = (U32) t; t >>= 32;
t += (U64) a[2] + b[2]; a[2] = (U32) t;
}
static
void incHALF(HALF a) // a++
{
if( ++a[0] == 0)
{
if( ++a[1] == 0)
{
++a[2];
}
}
}
static
int subHALF(HALF a, const HALF b)
{
U64 t;
t = (U64) a[0] - (U64) b[0]; a[0] = (U32) t; t = (t >> 32) & 1;
t = (U64) a[1] - (U64) b[1] - t; a[1] = (U32) t; t = (t >> 32) & 1;
t = (U64) a[2] - (U64) b[2] - t; a[2] = (U32) t; t = (t >> 32) & 1;
return (U32) t;
}
static
void mul_64_32(HALF a, U64 q, U32 b)
{
U32 q0 = (U32) q, q1 = (U32) (q >> 32);
U64 t;
t = __emulu(b, q0); a[0] = (U32) t; t >>= 32;
t += __emulu(b, q1); a[1] = (U32) t; a[2] = (U32) (t >> 32);
}
static
char *HALF2Str(const HALF a)
{
U64 q;
U32 r;
HALF tmp;
copyHALF(tmp, a);
divmodHALFsingle(tmp, 1000000000, r);
q = cvt_q(tmp);
static char buf[32] = "";
sprintf(buf, "%" PRIu64 "%09u", q, r);
return buf;
}
// result must fit within a FULL. no check performed.
static
void mulFULLsingle(const FULL a, const U32 b, FULL c) // c = a * b;
{
U64 t;
t = __emulu(b, a[0]); c[0] = (U32) t; t >>= 32;
t += __emulu(b, a[1]); c[1] = (U32) t; t >>= 32;
t += __emulu(b, a[2]); c[2] = (U32) t; t >>= 32;
t += __emulu(b, a[3]); c[3] = (U32) t; t >>= 32;
t += __emulu(b, a[4]); c[4] = (U32) t; t >>= 32;
t += __emulu(b, a[5]); c[5] = (U32) t;
}
static
int subFULL(const FULL a, const FULL b, FULL c) // c= a-b. borrow out is returned
{
U64 t;
t = (U64) a[0] - (U64) b[0]; c[0] = (U32) t; t = (t >> 32) & 1;
t = (U64) a[1] - (U64) b[1] - t; c[1] = (U32) t; t = (t >> 32) & 1;
t = (U64) a[2] - (U64) b[2] - t; c[2] = (U32) t; t = (t >> 32) & 1;
t = (U64) a[3] - (U64) b[3] - t; c[3] = (U32) t; t = (t >> 32) & 1;
t = (U64) a[4] - (U64) b[4] - t; c[4] = (U32) t; t = (t >> 32) & 1;
t = (U64) a[5] - (U64) b[5] - t; c[5] = (U32) t; t = (t >> 32) & 1;
return (int) (U32) t;
}
static
U32 msb(U32 x)
{
U32 b = 0;
if (x & 0xFFFF0000) { b |= 16; x >>= 16; }
if (x & 0xFF00) { b |= 8; x >>= 8; }
if (x & 0xF0) { b |= 4; x >>= 4; }
if (x & 0xC) { b |= 2; x >>= 2; }
if (x & 0x2) { b |= 1; }
return b;
}
static
U32 bitsHALF(const HALF a)
{
return 1 + (a[2] ? (msb(a[2])+64) : (msb(a[1])+32));
}
// process a single k. k is available in gd.k
static
void processk(U32 cand_per_fac)
{
initHALF(gd.the_f, gd.k); shlHALF(gd.the_f, gd.n+1); gd.the_f[0]++;
U32 p;
int qnr = 1;
// find QNR
for(U32 i=0; i < 128; i++) {
p = sp[i].p;
U32 modp = (((gd.k % p) << (gd.n+1)) + 1) % p;
if (jacobi(modp, p) == -1) {
qnr = -1;
break;
}
}
if(qnr != -1) {
// Too much questions asked
// printf("\nUnable to find qnr for %s\n", HALF2Str(gd.the_f));
return;
}
// calculate init (p^k) and multiplier (init^2)
calc_inverse();
HALF init, check;
expmodHALF(p, init);
expmodHALFcheck(init, check);
check[0]++;
if(cmpHALF(check, gd.the_f) != 0) // not a prime
return;
gd.h_Init[gd.candsInBuffer] = cvt_q(init);
gd.h_Init1[gd.candsInBuffer] = init[2];
gd.candsInBuffer++;
FULL finit2; HALF init2; squareHALF(init, finit2); modFULLslow(finit2, init2);
HALF ratio; calc_ratio(init2, gd.the_f, ratio);
// Previous versions always executed this loop.
// Now, with dynamic iteration count, this might not be executed.
if (cand_per_fac > 1)
{
U32 cands_left = cand_per_fac-1;
HALF tmp; copyHALF(tmp, init);
HALF q, r, s;
do {
mulHALFpartialhi(ratio, init, q);
incHALF(q);
mulHALFpartial(init, init2, r);
mulHALFpartial(gd.the_f, q, s);
if( subHALF(r, s) )
{
addHALF(r, gd.the_f);
}
copyHALF(init, r);
gd.h_Init[gd.candsInBuffer] = cvt_q(init);
gd.h_Init1[gd.candsInBuffer] = init[2];
gd.candsInBuffer++;
cands_left--;
} while (cands_left);
mulHALF(tmp, init, finit2); modFULLslow(finit2, init2);
calc_ratio(init2, gd.the_f, ratio);
}
gd.h_Factor_Mult_Ratio[gd.factorsInBuffer*3 ] = cvt_q(gd.the_f); gd.h_Factor_Mult_Ratio1[gd.factorsInBuffer*3 ] = gd.the_f[2];
gd.h_Factor_Mult_Ratio[gd.factorsInBuffer*3+1] = cvt_q(init2); gd.h_Factor_Mult_Ratio1[gd.factorsInBuffer*3+1] = init2[2];
gd.h_Factor_Mult_Ratio[gd.factorsInBuffer*3+2] = cvt_q_hi(ratio); gd.h_Factor_Mult_Ratio1[gd.factorsInBuffer*3+2] = ratio[0];
gd.factorsInBuffer++;
}
static
int Str2HALF(const char *s, HALF a, U32 * peta)
{
int len = strlen(s);
U32 hi_peta = 0;
U64 lo_peta = 0;
if(len > 24)
return 1;
for(int i=0; i < len; i++) {
if(s[i] < '0' || s[i] > '9')
return 1;
if(i < (len-15)) {
hi_peta = hi_peta * 10 + (s[i]-'0');
}
else {
lo_peta = lo_peta * 10 + (s[i]-'0');
}
}
mul_64_32(a, PETA, hi_peta);
HALF tmp; initHALF(tmp, lo_peta);
addHALF(a, tmp);
if (peta)
*peta = hi_peta;
return 0;
}
static
U64 peta_to_k(U32 p_in_peta)
{
HALF tmp;
mul_64_32(tmp, PETA, p_in_peta); shrHALF(tmp, gd.n+1);
return cvt_q(tmp);
}
static
void copyFULL(FULL dest, const FULL src)
{
for(int i=0; i < 6; i++)
dest[i] = src[i];
}
static
void shrFULL(FULL a, U32 b) // a >>= b
{
while(b >= 32)
{
for(int i=0; i < 5; i++)
a[i] = a[i+1];
a[5] = 0;
b -= 32;
}
if(b > 0)
{
for(int i=0; i < 5; i++)
a[i] = (a[i] >> b) | (a[i+1] << (32-b));
a[5] >>= b;
}
}
static
void modFULLslow(const FULL a, HALF b) // b = a mod k.N1+1
{
HALF a_hi, quot;
FULL tmp;
copyFULL(tmp, a);
shrFULL(tmp, gd.the_f_bits-1);
loHALF(tmp, a_hi);
mulHALF(a_hi, gd.the_f_inverse, tmp);
shrFULL(tmp, gd.the_f_bits+1);
loHALF(tmp, quot);
mulHALF(quot, gd.the_f, tmp);
subFULL(a, tmp, tmp);
loHALF(tmp, b);
while(cmpHALF(b, gd.the_f) >= 0)
subHALF(b, gd.the_f);
}
static
void expmodHALF(U32 b, HALF e /* ret */) // compute b^k mod (k*N1+1)
{
U64 q = 0x8000000000000000ull;
while ((q & gd.k) == 0) q >>= 1;
// HALF e;
FULL f;
initHALF(e, b);
q >>= 1;
while(q)
{
squareHALF(e, f);
if(q & gd.k)
{
mulFULLsingle(f, b, f);
}
modFULLslow(f, e);
q >>= 1;
}
// copyHALF( ret, e );
}
static
void expmodHALFcheck(const HALF init, HALF e /* ret */) // compute init^N mod (k*N1+1)
{
// HALF e;
FULL f;
copyHALF(e, init);
for(U32 i=0; i<gd.n; i++)
{
squareHALF(e, f);
modFULLslow(f, e);
}
// copyHALF( ret, e );
}
static
void calc_inverse()
{
gd.the_f_bits = bitsHALF(gd.the_f);
HALF num1; initHALF(num1, 1); shlHALF(num1, gd.the_f_bits);
initHALF(gd.the_f_inverse, 0);
for(U32 i=0; i<gd.the_f_bits+1; i++)
{
// Double num
shlHALF(gd.the_f_inverse, 1);
if(cmpHALF(num1, gd.the_f) >= 0)
{
subHALF(num1, gd.the_f);
gd.the_f_inverse[0] |= 1;
}