-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLlr.c
More file actions
18967 lines (16272 loc) · 589 KB
/
Llr.c
File metadata and controls
18967 lines (16272 loc) · 589 KB
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
/*----------------------------------------------------------------------
| This file contains routines and global variables that are common for
| all operating systems the program has been ported to. It is included
| in one of the source code files of each port. See Llr.h for the
| common #defines and common routine definitions.
+---------------------------------------------------------------------*/
#define CHECK_IF_ANY_ERROR(X,J,N,K) \
checknumber = K;\
restarting = FALSE;\
will_try_larger_fft = FALSE;\
/* If the sum of the output values is an error (such as infinity) */\
/* then raise an error. */\
\
if (gw_test_illegal_sumout (gwdata)) {\
sprintf (buf, ERRMSG0L, J, N, ERRMSG1A);\
clearline(100);\
OutputBoth (buf);\
sleep5 = TRUE;\
goto error;\
}\
\
/* Check that the sum of the input numbers squared is approximately */\
/* equal to the sum of unfft results. Since this check may not */\
/* be perfect, check for identical results after a restart. */\
\
if (gw_test_mismatched_sums (gwdata)) {\
double suminp, sumout;\
lasterr_point = J;\
suminp = gwsuminp (gwdata, X);\
sumout = gwsumout (gwdata, X);\
if (J == last_bit[K] &&\
suminp == last_suminp[K] &&\
sumout == last_sumout[K]) {\
clearline(100);\
OutputBoth (ERROK);\
saving = 1;\
} else {\
char msg[80], sic[80], soc[80];\
sprintf (sic, "%.16g", suminp);\
sprintf (soc, "%.16g", sumout);\
if (strcmp(sic, soc)) {\
sprintf (msg, ERRMSG1B, suminp, sumout);\
sprintf (buf, ERRMSG0L, J, N, msg);\
clearline(100);\
OutputBoth (buf);\
last_bit[K] = J;\
last_suminp[K] = suminp;\
last_sumout[K] = sumout;\
sleep5 = TRUE;\
goto error;\
}\
}\
}\
\
/* Check for excessive roundoff error */\
\
if (gw_get_maxerr(gwdata) > maxroundoff) {\
lasterr_point = J;\
if (J == last_bit[K] &&\
gw_get_maxerr(gwdata) == last_maxerr[K] && !abonroundoff && !care) {\
clearline(100);\
OutputBoth (ERROK);\
gwdata->GWERROR = 0;\
clearline(100);\
IniWriteInt(INI_FILE, "Error_Count", error_count = IniGetInt(INI_FILE, "Error_Count", 0) + 1);\
if (error_count > MAX_ERROR_COUNT) {\
OutputBoth (ERRMSG9);\
IniWriteString(INI_FILE, "Error_Count", NULL);\
will_try_larger_fft = TRUE;\
last_bit[K] = 0;\
last_maxerr[K] = 0.0;\
maxerr_recovery_mode[K] = FALSE;\
}\
else {\
OutputBoth (ERRMSG6);\
maxerr_recovery_mode[K] = TRUE;\
}\
sleep5 = FALSE;\
restarting = TRUE;\
goto error;\
} else {\
char msg[80];\
sprintf (msg, ERRMSG1C, gw_get_maxerr(gwdata), maxroundoff);\
sprintf (buf, ERRMSG0L, J, N, msg);\
clearline(100);\
OutputBoth (buf);\
if (J == last_bit[K])\
will_try_larger_fft = TRUE;\
if (will_try_larger_fft) {\
OutputBoth (ERRMSG8);\
IniWriteString(INI_FILE, "Error_Count", NULL);\
last_bit[K] = 0;\
last_maxerr[K] = 0.0;\
}\
else {\
last_bit[K] = J;\
last_maxerr[K] = gw_get_maxerr(gwdata);\
}\
maxerr_recovery_mode[K] = FALSE;\
sleep5 = FALSE;\
restarting = TRUE;\
goto error;\
}\
}\
\
if (ERRCHK) {\
if (gw_get_maxerr(gwdata) < reallyminerr && J > 30)\
reallyminerr = gw_get_maxerr(gwdata);\
if (gw_get_maxerr(gwdata) > reallymaxerr)\
reallymaxerr = gw_get_maxerr(gwdata);\
}
unsigned long format;
// Some ABC format strings
char ckstring[] = "(2^$a$b)^2-2"; // Carol/Kynea
char eckstring[] = "($a^$b$c)^2-2"; // Extended Carol/Kynea or Extended Square minus two
char cwstring[] = "$a*$b^$a$c"; // Cullen/Woodall
char ffstring[] = "$a*2^$b+1"; // FermFact output
char ffmstring[] = "$a*2^$b-1"; // Lei output
char gmstring[] = "4^$a+1"; // Gaussian Mersenne norms
char gfstring[] = "$a^$b+1"; // Special primality test for generalized Fermat numbers
char spstring[] = "(2^$a+1)/3"; // Special SPRP test for Wagstaff numbers
char dpstring[] = "DivPhi($a*$b^$c+1)"; // like-PRP test for DivPhi(...,2)
char repustring[] = "(10^$a-1)/9"; // PRP test for repunits numbers
char grepustring[] = "($a^$b-1)/($a-1)";// PRP test for generalized repunits numbers
char diffnumpstring[] = "$a^$b-$a^$c+%d";// If $b>$c, it is [$a^($b-$c)-1]*$a^$c+%d so, form K*B^N+C
char diffnummstring[] = "$a^$b-$a^$c-%d";// If $b>$c, it is [$a^($b-$c)-1]*$a^$c-%d so, form K*B^N+C
char diffnumstring[] = "$a^$b-$a^$c$d"; // General diffnum format
// Fixed k and c forms for k*b^n+c
#if defined (__linux__) || defined (__FreeBSD__) || defined (__APPLE__)
char fkpstring[] = "%qi*$a^$b+%d";
char fkmstring[] = "%qi*$a^$b-%d";
#else
char fkpstring[] = "%I64u*$a^$b+%d";
char fkmstring[] = "%I64u*$a^$b-%d";
#endif
// Fixed b and c forms for k*b^n+c
#if defined (__linux__) || defined (__FreeBSD__) || defined (__APPLE__)
char fbpstring[] = "$a*%qi^$b+%d";
char fbmstring[] = "$a*%qi^$b-%d";
#else
char fbpstring[] = "$a*%I64u^$b+%d";
char fbmstring[] = "$a*%I64u^$b-%d";
#endif
// Fixed n and c forms for k*b^n+c
char fnpstring[] = "$a*$b^%lu+%d";
char fnmstring[] = "$a*$b^%lu-%d";
// Fixed k forms for k*b^n+c
#if defined (__linux__) || defined (__FreeBSD__) || defined (__APPLE__)
char fkastring[] = "%qi*$a^$b$c";
#else
char fkastring[] = "%I64u*$a^$b$c";
#endif
// Fixed b forms for k*b^n+c
#if defined (__linux__) || defined (__FreeBSD__) || defined (__APPLE__)
char fbastring[] = "$a*%qi^$b$c";
#else
char fbastring[] = "$a*%I64u^$b$c";
#endif
// Fixed n forms for k*b^n+c
char fnastring[] = "$a*$b^%lu$c";
// Fixed c forms for k*b^n+c
char abcpstring[] = "$a*$b^$c+%d";
char abcmstring[] = "$a*$b^$c-%d";
// General k*b^n+c format
char abcastring[] = "$a*$b^$c$d";
// k*b^n+c format with k, b, c fixed
char abcnstring[] = "%lu*%lu^n%d";
// General (k*b^n+c)/d format
char abcadstring[] = "($a*$b^$c$d)/$e";
// (k*b^n+c)/d format with k, b, c, d fixed
char abcndstring[] = "(%lu*%lu^n%d)/%lu";
// Test the primality of a number given as a string
char numberstring[] = "$a";
// Test if $a is a base $b Wieferich prime
char wftstring[] = "$a$b";
// Search for base $c Wieferich primes in the range $a to $b
char wfsstring[] = "$a$b$c";
#define ABCSP 8 // (2^n+1)/3 ABC format
#define ABCVARAQS 19 // k, b, n, c and divisor specified on each input line or header
#define ABCVARAS 18 // k, b, n, and c specified on each input line or header
#define ABCDP 28 // Format used for DivPhi()
#define ABCECK 29 // Format used for Extended square minus two
/* Process a number from newpgen output file */
/* NEWPGEN output files use the mask as defined below: */
#define MODE_PLUS 0x01 /* k.b^n+1 */
#define MODE_MINUS 0x02 /* k.b^n-1 */
#define MODE_2PLUS 0x04 /* k.b^(n+1)+1 (*) */
#define MODE_2MINUS 0x08 /* k.b^(n+1)-1 (*) */
#define MODE_4PLUS 0x10 /* k.b^(n+2)+1 (*) */
#define MODE_4MINUS 0x20 /* k.b^(n+2)-1 (*) */
#define MODE_PRIMORIAL 0x40 /* PRIMORIAL - can't handle this */
#define MODE_PLUS5 0x80 /* k.b^n+5 */
#define MODE_2MINUS3 0x2000 /* 2k.b^n-3 JP 10/02/23 */
#define MODE_AP 0x200 /* 2^n+2k-1 */
#define MODE_PLUS7 0x800 /* k.b^n+7 */
#define MODE_2PLUS3 0x1000 /* 2k.b^n+3 */
#define MODE_DUAL 0x8000
#define MODE_PLUS_DUAL 0x8001 /* b^n+k */
#define MODE_MINUS_DUAL 0x8002 /* b^n-k */
#define MODE_NOTGENERALISED 0x400
/* Define the world/group/owner read/write attributes for creating files */
/* I've always used 0666 in Unix (everyone gets R/W access), but MSVC 8 */
/* now refuses to work with that setting -- insisting on 0600 instead. */
#ifdef _WIN32
#define CREATE_FILE_ACCESS 0600
#else
#define CREATE_FILE_ACCESS 0666
#endif
#define IBSIZE 300
char greatbuf[10001] = {0};
char INI_FILE[80] = {0};
char SVINI_FILE[80] = {0};
char RESFILE[80] = {0};
char LOGFILE[80] = {0};
char EXTENSION[8] = {0};;
char HARDWARE_GUID[33] = {0};
char CPU_AFFINITYSTRING[80] = {0};
int volatile ERRCHK = 0;
unsigned int PRIORITY = 1;
unsigned int CPU_AFFINITY = 99;
unsigned int CPU_AFFINITYMAX;
unsigned int CPU_AFFINITYINC = 1;
unsigned __int64 CPU_MASK = 0;
EXTERNC unsigned int volatile CPU_TYPE = 0;
unsigned long volatile ITER_OUTPUT = 0;
unsigned long volatile ITER_OUTPUT_RES = 99999999;
unsigned long volatile DISK_WRITE_TIME = 30;
unsigned long INTERIM_FILES = 0;
unsigned long INTERIM_RESIDUES = 0;
int CLASSIC_OUTPUT = 0;
int OUTPUT_ROUNDOFF = 0;
int CUMULATIVE_ROUNDOFF = 1;
int TWO_BACKUP_FILES = 1;
int NUM_BACKUP_FILES = 3;
int NUM_JACOBI_BACKUP_FILES = 2;
int RUN_ON_BATTERY = 1;
int TRAY_ICON = TRUE;
int HIDE_ICON = FALSE;
double UNOFFICIAL_CPU_SPEED = 0.0;
unsigned int WORKTODO_COUNT = 0;/* Count of valid work lines */
unsigned int ROLLING_AVERAGE = 0;
unsigned int PRECISION = 2;
unsigned long NUM_CPUS = 1; /* Number of CPUs/Cores in the computer */
unsigned long NUM_WORKER_THREADS = 1;
hwloc_topology_t hwloc_topology; /* Hardware topology */
uint32_t CPU_TOTAL_L1_CACHE_SIZE = 0; /* Sum of all the L1 caches in KB as determined by hwloc */
uint32_t CPU_TOTAL_L2_CACHE_SIZE = 0; /* Sum of all the L2 caches in KB as determined by hwloc */
uint32_t CPU_TOTAL_L3_CACHE_SIZE = 0; /* Sum of all the L3 caches in KB as determined by hwloc */
uint32_t CPU_TOTAL_L4_CACHE_SIZE = 0; /* Sum of all the L4 caches in KB as determined by hwloc */
uint32_t CPU_NUM_L1_CACHES = 0; /* Number of L1 caches as determined by hwloc */
uint32_t CPU_NUM_L2_CACHES = 0; /* Number of L2 caches as determined by hwloc */
uint32_t CPU_NUM_L3_CACHES = 0; /* Number of L3 caches as determined by hwloc */
uint32_t CPU_NUM_L4_CACHES = 0; /* Number of L4 caches as determined by hwloc */
int CPU_L2_CACHE_INCLUSIVE = -1; /* 1 if inclusive, 0 if exclusive, -1 if not known */
int CPU_L3_CACHE_INCLUSIVE = -1; /* 1 if inclusive, 0 if exclusive, -1 if not known */
int CPU_L4_CACHE_INCLUSIVE = -1; /* 1 if inclusive, 0 if exclusive, -1 if not known */
unsigned int NUM_NUMA_NODES = 1; /* Number of NUMA nodes in the computer */
unsigned int NUM_THREADING_NODES = 1; /* Number of nodes where it might be beneficial to keep a worker's threads in the same node */
int CUMULATIVE_TIMING = 0;
int HIGH_RES_TIMER = 0;
int usingDivPhi_m = 0;
int RDTSC_TIMING = 1;
int snfft = 0;
extern double MAXDIFF;
double maxdiffmult = 1.0;
/* PRP and LLR global variables */
#if defined (_CONSOLE)
HANDLE hConsole; // Handle to Console attributes
#endif
#define sgkbufsize 20000
giant N = NULL; /* Number being tested */
giant NP = NULL; /* Number being tested */
giant M = NULL; /* Gaussian Mersenne modulus = N*NP */
giant gk = NULL; /* k multiplier may be a large integer... */
giant gb = NULL; /* Generalized Fermat base may be a large integer... */
unsigned long Nlen = 0; /* Bit length of number being LLRed or PRPed */
unsigned long klen = 0; /* Number of bits of k multiplier */
unsigned long n_orig; /* exponent associated to initial base sgb */
unsigned long OLDFFTLEN = 0; /* previous value of FFTLEN, used by setuponly option */
unsigned long ndiff = 0;/* used for b^n-b^m+c number processing */
unsigned long gformat; /* used for b^n-b^m+c number processing */
unsigned long possiblyfalsenegative = 0;
struct work_unit *w = NULL; // Work to do data as described in Prime95
/* Global variables for factoring */
#ifndef WIN32
EXTERNC unsigned long _CPU_FLAGS = 0;
#endif
EXTERNC unsigned long FACPASS = 0;
EXTERNC unsigned long FACTEST = 0;
EXTERNC unsigned long FACHSW = 0; /* High word of found factor */
EXTERNC unsigned long FACMSW = 0; /* Middle word of found factor */
EXTERNC unsigned long FACLSW = 0; /* Low word of found factor */
EXTERNC void *FACDATA = NULL; /* factor data is kept in a global */
EXTERNC void *SRCARG = NULL;
/* Other gwnum globals */
EXTERNC void gw_random_number (gwhandle*, gwnum);/* Generate random FFT data */
giant testn, testnp, testf, testx;
unsigned long facn = 0, facnp = 0;
int resn = 0, resnp = 0;
char facnstr[80], facnpstr[80];
char m_pgen_input[IBSIZE], m_pgen_output[IBSIZE], oldm_pgen_input[IBSIZE];
char keywords[10][IBSIZE], values[10][IBSIZE];
char multiplier[IBSIZE], base[IBSIZE], exponent[IBSIZE], exponent2[IBSIZE], addin[IBSIZE], divisors[IBSIZE];
char inifilebuf[IBSIZE];
char sgd[sgkbufsize];
char sgb[sgkbufsize];
char bpfstring[sgkbufsize];
#if !defined(X86_64) && !defined(_WIN64)
void setupf();
int factor64();
void psetupf();
int pfactor64();
#endif
//void* aligned_malloc (unsigned long, unsigned long);
void aligned_free (void *);
void md5 (
char output[33],
const char *string);
/* Calculate the 32-byte hex string for the hardware GUID. We use the */
/* output of the CPUID function to generate this. We don't include the */
/* cache size information because when a new processor comes out the CPUID */
/* does not recognize the cache size. When a new version of prime95 is */
/* released that does recognize the cache size a different hardware GUID */
/* would be generated. */
void calc_hardware_guid (void)
{
char buf[500];
sprintf (buf, "%s%d", CPU_BRAND, CPU_SIGNATURE);
md5 (HARDWARE_GUID, buf);
/* Sometimes a user might want to run the program on several machines. */
/* Typically this is done by carrying the program and files around on a */
/* portable media such as a USB memory stick. In this case, */
/* we need to defeat the code that automatically detects hardware changes. */
/* The FixedHardwareUID INI option tells us to get the Windows and */
/* hardware hashes from the INI file rather than calculating them. */
if (IniGetInt (INI_FILE, "FixedHardwareUID", 0)) {
IniGetString (INI_FILE, "HardwareGUID", HARDWARE_GUID, sizeof (HARDWARE_GUID), HARDWARE_GUID);
IniWriteString (INI_FILE, "HardwareGUID", HARDWARE_GUID);
}
}
static unsigned long last_bit[10] = {0}; // Bit or iter. currently tested on operaton N° index.
static double last_suminp[10] = {0.0};
static double last_sumout[10] = {0.0};
static double last_maxerr[10] = {0.0}; // Current Roundoff on operation N° index.
static double maxroundoff = 0.40; // Largest acceptable Roundoff Error
static double fpart = 0.0;
static unsigned long mask;
unsigned int Fermat_only = FALSE;
unsigned int strong = FALSE;
unsigned int quotient = FALSE;
unsigned int vrbareix = FALSE;
unsigned int dualtest = FALSE;
unsigned int bpsw = FALSE;
unsigned int verbose = FALSE;
unsigned int setuponly = FALSE;
unsigned int nosaving = FALSE;
unsigned int abonillsum = FALSE;
unsigned int abonmismatch = FALSE;
unsigned int testgm = FALSE;
unsigned int testgq = FALSE;
unsigned int testfac = FALSE;
unsigned int nofac = TRUE;
unsigned int general = FALSE;
unsigned int eps2 = FALSE;
unsigned int debug = FALSE;
unsigned int restarting = FALSE; // Actually set by Roundoff error condition ; reset by the macro.
unsigned int care = FALSE; // Set after a careful iteration ; reset after a normal one.
unsigned int care_limit = 30; // Limit for careful iterations 24/05/21
unsigned int postfft = TRUE;
unsigned int nbfftinc = 0; // Number of required FFT increments.
unsigned int maxfftinc = 5; // Maximum accepted FFT increments.
unsigned int aborted = FALSE;
unsigned int generic = FALSE;
unsigned int abonroundoff = FALSE; // Abort the test in a Roundoff error occurs.
unsigned int will_try_larger_fft = FALSE;
// Set if unrecoverable error or too much errors ; reset by the macro.
unsigned int checknumber = 0;
// N° of currently checked gwnum operation ; only displayed in abort message
unsigned int error_count = 0; // Number of reproducible errors that previously occured.
unsigned int sleep5 = FALSE, showdigits = FALSE;
unsigned int maxerr_recovery_mode [10] = {0};
// Set if a reproducible error occured in the current operation.
unsigned int lasterr_point = 0; // Last bit or iteration that caused an error.
unsigned long primolimit = 30000;
unsigned long nextifnear = FALSE;
unsigned long maxaprcl = 200;
unsigned long interimFiles, interimResidues, throttle, facfrom, facto;
unsigned long factored = 0, eliminated = 0;
unsigned long pdivisor = 1000000, pquotient = 1;
unsigned long bpf[30], bpc[30], vpf[30]; // Base prime factors, cofactors, power of p.f.
unsigned long *t = NULL,*ta = NULL; // Precrible arrays, dynamically allocated.
unsigned long smallprime[168] = // Small primes < 1000
{2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,
73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,
179, 181, 191, 193, 197, 199, 211, 223, 227, 229,
233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
353, 359, 367, 373, 379, 383, 389, 397, 401, 409,
419, 421, 431, 433, 439, 443, 449, 457, 461, 463,
467, 479, 487, 491, 499, 503, 509, 521, 523, 541,
547, 557, 563, 569, 571, 577, 587, 593, 599, 601,
607, 613, 617, 619, 631, 641, 643, 647, 653, 659,
661, 673, 677, 683, 691, 701, 709, 719, 727, 733,
739, 743, 751, 757, 761, 769, 773, 787, 797, 809,
811, 821, 823, 827, 829, 839, 853, 857, 859, 863,
877, 881, 883, 887, 907, 911, 919, 929, 937, 941,
947, 953, 967, 971, 977, 983, 991, 997};
unsigned long smallphi[10] = {1,2,4,6,10,12,16,18,22,28};
giantstruct* gbpf[30] = {NULL}; // Large integer prime factors
giantstruct* gbpc[30] = {NULL}; // Large integer cofactors
unsigned long nrestarts = 0; // Nb. of restarts for an N+1 or N-1 prime test
unsigned long nbdg = 0, globalb = 2; // number of digits ; base of the candidate in a global
double globalk = 1.0; // k value of the candidate in a global
double pcfftlim = 0.5; // limit for gwnear_fft_limit function to return TRUE.
int readlg, writelg; // Values returned by low level I/O
int factorized; // indicates successful factorization.
double timers[10] = {0.0}; /* Up to five separate timers */
double smargin = 0.0;
int is_valid_double(double);
int genProthBase(giant, uint32_t);
long generalLucasBase(giant , uint32_t *, uint32_t *);
unsigned long gcd (
unsigned long,
unsigned long);
int ispower (unsigned long,
unsigned long);
/* Utility output routines */
void LineFeed ()
{
#ifndef WIN32
OutputStr ("\r");
#elif defined (_CONSOLE)
OutputStr ("\r");
#else
OutputStr ("\n");
#endif
}
void OutputNum (
unsigned long num)
{
char buf[20];
sprintf (buf, "%lu", num);
OutputStr (buf);
}
#define MAX_ERROR_COUNT 5
char ERROK[] = "Disregard last error. Result is reproducible and thus not a hardware problem.\n";
char ERRMSG0L[] = "Iter: %ld/%ld, %s";
char ERRMSG0[] = "Bit: %ld/%ld, %s";
char ERRMSG1A[] = "ERROR: ILLEGAL SUMOUT\n";
char ERRMSG1B[] = "ERROR: SUM(INPUTS) != SUM(OUTPUTS), %.16g != %.16g\n";
char ERRMSG1C[] = "ERROR: ROUND OFF (%.10g) > %.10g\n";
char ERRMSG2[] = "Possible hardware failure, consult the readme file.\n";
char ERRMSG3[] = "Continuing from last save file.\n";
char ERRMSG4[] = "Waiting five minutes before restarting.\n";
char ERRMSG5[] = "Fatal Error, Check Number = %d, test of %s aborted\n";
char ERRMSG6[] = "For added safety, redoing iteration using a slower, more reliable method.\n";
char ERRMSG7[] = "Fatal Error, divisibility test of %d^(N-1)-1 aborted\n";
char ERRMSG8[] = "Unrecoverable error, Restarting with next larger FFT length...\n";
//char ERRMSG9[] = "Too much errors while using the standard method ; continuing with a slower, more reliable one.\n";
char ERRMSG9[] = "Too much errors ; Restarting with next larger FFT length...\n";
char ERRMSG60[] = "ERROR: Comparing double-check values failed. Rolling back to iteration %lu.\n";
char ERRMSG70[] = "ERROR: Comparing Gerbicz checksum values failed. Rolling back to iteration %lu.\n";
char ERRMSG80[] = "ERROR: Invalid FFT data. Restarting from last save file.\n";
char ERRMSG90[] = "ERROR: Invalid PRP state. Restarting from last save file.\n";
char WRITEFILEERR[] = "Error writing intermediate file: %s\n";
void trace(unsigned long n) { // Debugging tool...
char buf[100];
sprintf(buf, "OK until number %lu\n", n);
if (verbose)
OutputBoth (buf);
else
OutputStr (buf);
}
void Writefactors (giant, char*, char*);
// giant gcd and invert functions implementations using GNU MP library functions
unsigned long gwpgcdui (giant g, unsigned long x) {
mpz_t rop, op1;
unsigned long result;
mpz_init (rop); // allocate mpz memory
mpz_init (op1);
gtompz (g, op1); // g ==> op1
result = mpz_gcd_ui (rop, op1, x); // returns zero if x is zero, but then, gcd is g...
mpz_clear (rop); // free mpz memory
mpz_clear (op1);
return (result);
}
void gwpgcdg (giant g1, giant g2) {
mpz_t rop, op1, op2;
mpz_init (rop); // allocate mpz memory
mpz_init (op1);
mpz_init (op2);
gtompz (g1, op1); // g1 ==> op1
gtompz (g2, op2); // g2 ==> op2
mpz_gcd (rop, op1, op2); // gcd (op1, op2) ==> rop
mpztog (rop, g2); // rop ==> g2
mpz_clear (rop); // free mpz memory
mpz_clear (op1);
mpz_clear (op2);
}
int gwpinvg (giant g1, giant g2) {
int result;
mpz_t rop, op1, op2;
mpz_init (rop); // allocate mpz memory
mpz_init (op1);
mpz_init (op2);
gtompz (g1, op1); // g1 ==> op1
gtompz (g2, op2); // g2 ==> op2
result = mpz_invert (rop, op2, op1); // invert of op2 mod op1 ==> rop
if (result == 0) {
mpz_gcd (rop, op1, op2);
OutputBoth ((char*)"inverse does not exist, so, gcd is returned in second operand...\n");
}
mpztog (rop, g2); // rop ==> g2
mpz_clear (rop); // free mpz memory
mpz_clear (op1);
mpz_clear (op2);
return (result);
}
unsigned long hasasmalldivisor (giant x, unsigned long lim) {
unsigned long i, divisor, rem = 69;
if (lim > 997)
return (0);
for (i = 0 ; smallprime[i] <= lim ; i++) {
divisor = smallprime[i];
rem = gmodi (divisor, x);
if (rem == 0)
return (divisor);
}
return (0);
}
//*******************************************************************************
void clearline (int size) {
char buf[256];
int i;
for (i=0; i<256; i++)
buf[i] = '\0';
for (i=0; i<size; i++)
buf[i] = ' ';
buf[size-1] = '\r';
#if !defined(WIN32) || defined(_CONSOLE)
OutputStr(buf);
#endif
}
int digitstrcmp (const char *s1, const char *s2) {
if (strlen (s1) < strlen (s2))
return (-1);
else if (strlen (s1) > strlen (s2))
return (1);
else
return (strcmp (s1, s2));
}
int SleepFive ()
{
int i;
OutputStr (ERRMSG4);
BlinkIcon (10); /* Blink icon for 10 seconds */
Sleep (10000);
ChangeIcon (IDLE_ICON); /* Idle icon for rest of 5 minutes */
for (i = 0; i < 290; i++) {
Sleep (1000);
if (escapeCheck ()) return (FALSE);
}
ChangeIcon (WORKING_ICON); /* And back to the working icon */
return (TRUE);
}
/* Generate the scaling factors for ITER_OUTPUT in the rare cases where the user */
/* has used some undoc.txt settings to change how often the title is output or to */
/* make the frequency roughly the same in all windows even if using different FFT sizes. */
void calc_output_frequencies (
gwhandle *gwdata, /* Handle to the gwnum code */
double *output_frequency, /* Calculated adjustment to ITER_OUTPUT */
double *output_title_frequency)/* Calculated adjustment to ITER_OUTPUT for title */
{
int scaled_freq, title_freq;
double exp, temp;
/* Check the flag that says scale ITER_OUTPUT so that messages */
/* appear at roughly same rate for all FFT sizes (scale factor */
/* should be 1.0 if testing M50000000). */
scaled_freq = (int) IniGetInt (INI_FILE, "ScaleOutputFrequency", 0);
if (!scaled_freq) {
*output_frequency = 1.0;
} else {
*output_frequency = gwmap_to_timing (1.0, 2, 50000000, -1) /
gwmap_to_timing (gwdata->k, gwdata->b, gwdata->n, gwdata->c);
if (gwget_num_threads (gwdata) > 1 && NUM_WORKER_THREADS < NUM_CPUS)
*output_frequency /= 1.8 * (gwget_num_threads (gwdata) - 1);
/* For prettier output (outputs likely to be a multiple of a power of 10), round the */
/* output frequency to the nearest (10,15,20,25,30,40,...90) times a power of ten */
exp = floor (_log10 (*output_frequency));
temp = *output_frequency * pow (10.0, -exp);
if (temp < 1.25) temp = 1.0;
else if (temp <1.75) temp = 1.5;
else if (temp < 2.25) temp = 2.0;
else if (temp < 2.75) temp = 2.5;
else temp = floor (temp + 0.5);
*output_frequency = temp * pow (10.0, exp);
}
/* Calculate the title frequency as a fraction of the output frequency */
title_freq = (int) IniGetInt (INI_FILE, "TitleOutputFrequency", 1);
if (title_freq < 1) title_freq = 1;
*output_title_frequency = *output_frequency / (double) title_freq;
}
/* Truncate a percentage to the requested number of digits. */
/* Truncating prevents 99.5% from showing up as 100% complete. */
double trunc_percent (
double percent)
{
if (percent > 100.0) percent = 100.0;
percent -= 0.5 * pow (10.0, - (double) PRECISION);
if (percent < 0.0) return (0.0);
return (percent);
}
// Test if a string contains only valid digits.
int isDigitString(char *s) {
while (*s) {
if (!isdigit(*s)) return (FALSE);
s++;
}
return (TRUE);
}
/* Routines used to time code chunks */
void clear_timers () {
int i;
for (i = 0; i < 10; i++) timers[i] = 0.0;
}
void clear_timer (
int i)
{
timers[i] = 0.0;
}
void start_timer (
int i)
{
if (i > 4)
return;
if (timers[i+5] != 0.0) // to avoid double start...
return;
if (HIGH_RES_TIMER) {
timers[i] -= getHighResTimer ();
} /* else if (CPU_FLAGS & CPU_RDTSC) { // 16/06/12 CPU_SPEED no more used...
uint32_t hi, lo;
rdtsc (&hi, &lo);
timers[i] -= (double) hi * 4294967296.0 + lo;
} */ else {
struct _timeb timeval;
_ftime (&timeval);
timers[i] -= (double) timeval.time * 1000.0 + timeval.millitm;
}
timers[i+5] = 1.0; // to show that timers[i] is already started
}
void end_timer (
int i)
{
if (i > 4)
return;
if (timers[i+5] == 0.0) // to avoid double end...
return;
if (HIGH_RES_TIMER) {
timers[i] += getHighResTimer ();
} /* else if (CPU_FLAGS & CPU_RDTSC) { // 16/06/12 CPU_SPEED no more used...
uint32_t hi, lo;
rdtsc (&hi, &lo);
timers[i] += (double) hi * 4294967296.0 + lo;
} */ else {
struct _timeb timeval;
_ftime (&timeval);
timers[i] += (double) timeval.time * 1000.0 + timeval.millitm;
}
timers[i+5] = 0.0; // to show that timers[i] is ended
}
void divide_timer (
int i,
int j)
{
timers[i] = timers[i] / j;
}
double timer_value (
int i)
{
if (HIGH_RES_TIMER)
return (timers[i] / getHighResTimerFrequency ());
// else if (CPU_FLAGS & CPU_RDTSC)
// return (timers[i] / CPU_SPEED / 1000000.0); // 16/06/12 CPU_SPEED no more used...
else
return (timers[i] / 1000.0);
}
#define TIMER_NL 0x1
#define TIMER_CLR 0x2
#define TIMER_OPT_CLR 0x4
#define TIMER_MS 0x8
void print_timer (
int i,
int flags)
{
char buf[40];
double t;
t = timer_value (i);
if (t >= 1.0)
sprintf (buf, "%.3f sec.", t);
else
sprintf (buf, "%.3f ms.", t * 1000.0);
OutputStr (buf);
if (flags & TIMER_NL) LineFeed();
if (flags & TIMER_CLR) timers[i] = 0.0;
if ((flags & TIMER_OPT_CLR) && !CUMULATIVE_TIMING) timers[i] = 0.0;
}
void write_timer ( // JP 23/11/07
char* buf,
int i,
int flags)
{
double t;
t = timer_value (i);
if (t >= 1.0)
sprintf (buf, "%.3f sec.", t);
else
sprintf (buf, "%.3f ms.", t * 1000.0);
if (flags & TIMER_NL)
strcat(buf, "\n");
if (flags & TIMER_CLR) timers[i] = 0.0;
if ((flags & TIMER_OPT_CLR) && !CUMULATIVE_TIMING) timers[i] = 0.0;
}
void OutputTimeStamp ()
{
time_t this_time;
char tbuf[40], buf[40];
// if (TIMESTAMPING) {
time (&this_time);
strcpy (tbuf, ctime (&this_time)+4);
tbuf[12] = 0;
sprintf (buf, "[%s] ", tbuf);
OutputStr (buf);
// }
}
void OutputBTimeStamp ()
{
time_t this_time;
char tbuf[40], buf[40];
// if (TIMESTAMPING) {
time (&this_time);
strcpy (tbuf, ctime (&this_time)+4);
tbuf[12] = 0;
sprintf (buf, "[%s]\n", tbuf);
OutputBoth (buf);
// }
}
/* Determine the CPU speed either empirically or by user overrides. */
/* getCpuType must be called prior to calling this routine. */
void getCpuSpeed (void)
{
int temp, old_cpu_speed, report_new_cpu_speed;
/* Guess the CPU speed using the RDTSC instruction */
guessCpuSpeed ();
/* Now let the user override the cpu speed from the local.ini file */
temp = IniGetInt (INI_FILE, "CpuSpeed", 99);
if (temp != 99) CPU_SPEED = temp;
/* Make sure the cpu speed is reasonable */
if (CPU_SPEED > 50000) CPU_SPEED = 50000;
if (CPU_SPEED < 25) CPU_SPEED = 25;
/* Set the unofficial CPU speed. The unofficial CPU speed is the */
/* last CPU speed measurement. The official CPU speed is the one */
/* reported to the server. */
UNOFFICIAL_CPU_SPEED = CPU_SPEED;
/* If CPU speed is much less than the official CPU speed, then set a new */
/* official CPU speed only after several slower measurements. */
/* The reason for this is that erroneously (due to one aberrant CPU speed */
/* calculation) reducing the speed we report to the server may result */
/* in erroneously unreserving exponents. */
report_new_cpu_speed = FALSE;
old_cpu_speed = IniGetInt (INI_FILE, "OldCpuSpeed", 0);
if (CPU_SPEED < (double) old_cpu_speed * 0.97) {
if (IniGetInt (INI_FILE, "NewCpuSpeedCount", 0) <= 5) {
if (CPU_SPEED > (double) IniGetInt (INI_FILE, "NewCpuSpeed", 0))
IniWriteInt (INI_FILE, "NewCpuSpeed", (int) (CPU_SPEED + 0.5));
IniWriteInt (INI_FILE, "NewCpuSpeedCount", IniGetInt (INI_FILE, "NewCpuSpeedCount", 0) + 1);
CPU_SPEED = old_cpu_speed;
} else {
if (CPU_SPEED < (double) IniGetInt (INI_FILE, "NewCpuSpeed", 0))
CPU_SPEED = (double) IniGetInt (INI_FILE, "NewCpuSpeed", 0);
report_new_cpu_speed = TRUE;
}
}
/* If CPU speed is close to last reported CPU speed, then use it. */
/* tell the server, recalculate new completion dates, and reset the */
/* rolling average. Don't do this on the first run (before the Welcome */
/* dialog has been displayed). */
else if (CPU_SPEED < (double) old_cpu_speed * 1.03) {
IniWriteInt (INI_FILE, "NewCpuSpeedCount", 0);
IniWriteInt (INI_FILE, "NewCpuSpeed", 0);
}
/* If CPU speed is much larger than the speed reported to the server, then */
/* use this new speed and tell the server. */
else {
report_new_cpu_speed = TRUE;
}
/* Report a new CPU speed. Remember the new CPU speed, tell the server, */
/* recalculate new completion dates, and reset the rolling average in */
/* such a way as to reduce the chance of spurious unreserves. Don't */
/* do this on the first run (before the Welcome dialog has been displayed). */
if (report_new_cpu_speed) {
IniWriteInt (INI_FILE, "OldCpuSpeed", (int) (CPU_SPEED + 0.5));
IniWriteInt (INI_FILE, "NewCpuSpeedCount", 0);
IniWriteInt (INI_FILE, "NewCpuSpeed", 0);
if (old_cpu_speed) {
if (WORKTODO_COUNT) {
ROLLING_AVERAGE = (int) (ROLLING_AVERAGE * old_cpu_speed / CPU_SPEED);
if (ROLLING_AVERAGE < 1000) ROLLING_AVERAGE = 1000;
}
else
ROLLING_AVERAGE = 1000;
IniWriteInt (INI_FILE, "RollingAverage", ROLLING_AVERAGE);
IniWriteInt (INI_FILE, "RollingStartTime", 0);
}
}
}
/* Set the CPU flags based on the CPUID instruction. Also, the advanced */
/* user can override our guesses. */
void getCpuInfo (void)
{
int depth, i, temp;
/* Get the CPU info using CPUID instruction */
guessCpuType ();
/* New in version 29! Use hwloc info to determine NUM_CPUS and CPU_HYPERTHREADS. Also get number of NUMA nodes */
/* which we may use later on to allocate memory from the proper NUMA node. */
/* We still allow overriding these settings using the INI file. */
NUM_CPUS = hwloc_get_nbobjs_by_type (hwloc_topology, HWLOC_OBJ_CORE);
if (NUM_CPUS < 1) NUM_CPUS = hwloc_get_nbobjs_by_type (hwloc_topology, HWLOC_OBJ_PU);
if (NUM_CPUS < 1) NUM_CPUS = 1; // Shouldn't happen
CPU_HYPERTHREADS = hwloc_get_nbobjs_by_type (hwloc_topology, HWLOC_OBJ_PU) / NUM_CPUS;
if (CPU_HYPERTHREADS < 1) CPU_HYPERTHREADS = 1; // Shouldn't happen
NUM_NUMA_NODES = hwloc_get_nbobjs_by_type (hwloc_topology, HWLOC_OBJ_NUMANODE);
if (NUM_NUMA_NODES < 1 || NUM_CPUS % NUM_NUMA_NODES != 0) NUM_NUMA_NODES = 1;
/* New in version 29.5, get L1/L2/L3/L4 total cache size for use in determining torture test FFT sizes. */
/* Overwrite cpuid's linesize and associativity with hwloc's */
CPU_TOTAL_L1_CACHE_SIZE = CPU_NUM_L1_CACHES = 0;
CPU_TOTAL_L2_CACHE_SIZE = CPU_NUM_L2_CACHES = 0;
CPU_TOTAL_L3_CACHE_SIZE = CPU_NUM_L3_CACHES = 0;
CPU_TOTAL_L4_CACHE_SIZE = CPU_NUM_L4_CACHES = 0;
CPU_L2_CACHE_INCLUSIVE = -1;
CPU_L3_CACHE_INCLUSIVE = -1;
CPU_L4_CACHE_INCLUSIVE = -1;
#if HWLOC_API_VERSION >= 0x00020000
for (depth = 0; depth < hwloc_topology_get_depth (hwloc_topology); depth++) {
for (i = 0; i < (int) hwloc_get_nbobjs_by_depth (hwloc_topology, depth); i++) {
hwloc_obj_t obj;
const char *inclusive;
obj = hwloc_get_obj_by_depth (hwloc_topology, depth, i);
if (obj == NULL || obj->attr == NULL) break; // can't happen
if (obj->type == HWLOC_OBJ_L1CACHE) {
CPU_TOTAL_L1_CACHE_SIZE += (uint32_t) (obj->attr->cache.size >> 10);
CPU_NUM_L1_CACHES++;
if (obj->attr->cache.linesize > 0) CPU_L1_CACHE_LINE_SIZE = obj->attr->cache.linesize;