-
Notifications
You must be signed in to change notification settings - Fork 0
/
Llr25_01_24.c
17125 lines (14899 loc) · 572 KB
/
Llr25_01_24.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
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;\
\
/* Check for excessive roundoff error */\
\
if (MAXERR > maxroundoff) {\
lasterr_point = J;\
if (J == last_bit[K] &&\
MAXERR == last_maxerr[K] && !abonroundoff && !care/*will_try_larger_fft*/) {\
clearline(100);\
OutputBoth (ERROK);\
GWERROR = 0;\
clearline(100);\
IniWriteInt(INI_FILE, (char*)"Error_Count", error_count = IniGetInt(INI_FILE, (char*)"Error_Count", 0) + 1);\
if (error_count > MAX_ERROR_COUNT) {\
OutputBoth (ERRMSG9);\
IniWriteString(INI_FILE, (char*)"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, MAXERR, 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, (char*)"Error_Count", NULL);\
last_bit[K] = 0;\
last_maxerr[K] = 0.0;\
}\
else {\
last_bit[K] = J;\
last_maxerr[K] = MAXERR;\
}\
maxerr_recovery_mode[K] = FALSE;\
sleep5 = FALSE;\
restarting = TRUE;\
goto error;\
}\
}\
\
if (ERRCHK) {\
if (MAXERR < reallyminerr && J > 30)\
reallyminerr = MAXERR;\
if (MAXERR > reallymaxerr)\
reallymaxerr = MAXERR;\
}
extern void print (double*, int); // debugging tool...
int it = 0; //cuda ; init to zero, JP 22/06/17
ssize_t wc = 0; // default return value for write function. JP 29/11/20
unsigned long format;
// Some ABC format strings
char ckstring[] = "(2^$a$b)^2-2"; // Carol/Kynea
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
char fkpstring[] = ""$LLF"*$a^$b+%d";
char fkmstring[] = ""$LLF"*$a^$b-%d";
// Fixed b and c forms for k*b^n+c
char fbpstring[] = "$a*"$LLF"^$b+%d";
char fbmstring[] = "$a*"$LLF"^$b-%d";
// 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
char fkastring[] = ""$LLF"*$a^$b$c";
// Fixed b forms for k*b^n+c
char fbastring[] = "$a*"$LLF"^$b$c";
// 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 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()
/* 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 20/07/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
#define _log2(x) log(x)/log(2)
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};
// int fftlen = 0;
int ERRCHK = 0;
unsigned int PRIORITY = 1;
unsigned int CPU_AFFINITY = 99;
unsigned int CPU_MASK = 0;
unsigned int 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 NUM_BACKUP_FILES = 3; // was 3
int NUM_JACOBI_BACKUP_FILES = 2; // was 2
int RUN_ON_BATTERY = 1;
int TRAY_ICON = TRUE;
int HIDE_ICON = FALSE;
int mul_final = 0;
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;
int TWO_BACKUP_FILES = 1;
int HIGH_RES_TIMER = 0;
int usingDivPhi_m = 0;
int string_rep_truncated = FALSE;
int recovering = FALSE;
unsigned long CPU_SPEED = 25;
int ZERO_PADDED_FFT;
int GWERROR = 0;
double maxdiffmult = 1.0;
/* PRP and LLR global variables */
#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 */
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 */
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 */
struct work_unit *w = NULL; // Work to do data as described in Prime95
/* Other gwypnum globals */
giant testn, testnp;
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 string_rep[80];
char sgd[sgkbufsize];
char sgb[sgkbufsize];
char bpfstring[sgkbufsize];
char cMAXBPD[10]={0};
/*
#ifndef X86_64
void setupf();
int factor64();
void psetupf();
int pfactor64();
#endif
void* aligned_malloc (unsigned long, unsigned long);
void aligned_free (void *);
*/
static unsigned long last_bit[10] = {0};
static double last_suminp[10] = {0.0};
static double last_sumout[10] = {0.0};
static double last_maxerr[10] = {0.0};
static double maxroundoff = 0.40;
double MAXBPD = 37.0;
// static double fpart = 0.0;
static unsigned long mask;
extern int zcomplex; // CUDA
extern int cufftonly; // CUDA
unsigned int fermat_only = FALSE;
unsigned int strong = TRUE;
unsigned int quotient = FALSE;
unsigned int vrbareix = FALSE;
unsigned int dualtest = FALSE;
unsigned int bpsw = 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 = FALSE;
unsigned int general = FALSE;
unsigned int eps2 = FALSE;
unsigned int vdebug = 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 nbfftinc = 0; // Number of required FFT increments.
unsigned int maxfftinc = 3; // Maximum accepted FFT increments.
unsigned int aborted = FALSE;
unsigned int abonroundoff = FALSE;
unsigned int will_try_larger_fft = FALSE;
unsigned int checknumber = 0;
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};
unsigned int lasterr_point = 0;
unsigned long primolimit = 30000;
unsigned long ifnear = FALSE;
unsigned long maxaprcl = 400;
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
int nbdg = 0, nbdg1, nbdg2;
unsigned long globalb = 2; // base of the candidate in a global
double globalk = 1.0; // k value of the candidate in a global
double pcfftlim = 0.5;
int readlg, writelg; // Values returned by low level I/O
// 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 x,
unsigned long y);
unsigned long ulbitlen (unsigned long);
unsigned long ulbitval (unsigned long, unsigned long);
// int ispower (unsigned long,
// unsigned long);
/* Utility output routines */
void LineFeed ()
{
#ifndef WIN32
OutputStr ((char*)"\r");
#elif defined (_CONSOLE)
OutputStr ((char*)"\r");
#else
OutputStr ((char*)"\n");
#endif
}
void OutputNum (
unsigned long num)
{
char buf[20];
sprintf (buf, "%lu", num);
OutputStr (buf);
}
#define MAX_ERROR_COUNT 5
/* Routines missing from GMP */
#define mpz_add_si(d,s,addin) if (addin >= 0) mpz_add_ui(d,s,(unsigned int)addin); else mpz_sub_ui(d,s,(unsigned int)(-addin));
#define mpz_mul_d(d,s,flt) { mpz_t t; mpz_init_set_d(t,flt); mpz_mul(d,s,t); mpz_clear(t); }
#define mpz_eq(a,b) mpz_cmp(a,b) == 0
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 larger FFT length...\n";
char ERRMSG9[] = "Too much errors ; Restarting with 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(int n) { // Debugging tool...
char buf[100];
sprintf(buf, "OK until number %d\n", n);
if (verbose)
OutputBoth (buf);
else
OutputStr (buf);
}
// giant divg, gcdg and invert functions implementations using GNU MP library functions
void gwypuldivg (unsigned long divisor, giant theg) {
mpz_t rop, op1, op2;
mpz_init (rop); // allocate mpz memory
mpz_init (op1);
mpz_init (op2);
gtompz (theg, op1); // theg ==> op1
mpz_set_ui (op2, divisor);
mpz_divexact (rop, op1, op2);
mpztog (rop, theg);
mpz_clear (rop);
mpz_clear (op1);
mpz_clear (op2);
}
void gwypdivg (giant divisor, giant theg) {
mpz_t rop, op1, op2;
mpz_init (rop); // allocate mpz memory
mpz_init (op1);
mpz_init (op2);
gtompz (theg, op1); // theg ==> op1
gtompz (divisor,op2); // divisor ==> op2
mpz_divexact (rop, op1, op2);
mpztog (rop, theg); // rop ==> theg
mpz_clear (rop);
mpz_clear (op1);
mpz_clear (op2);
}
unsigned long gwypgcdui (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 gwypgcdg (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 gwypinvg (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);
}
//*******************************************************************************
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
}
void clearbuf (char *b) {
int i;
for (i=0;i<strlen (b);i++)
b[i] = ' ';
b[i] = '\0';
}
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 (
double *output_frequency, /* Calculated adjustment to ITER_OUTPUT */
double *output_title_frequency)/* Calculated adjustment to ITER_OUTPUT for title */
{
int title_freq;
// double exp, temp;
*output_frequency = 1.0;
/* Calculate the title frequency as a fraction of the output frequency */
title_freq = (int) IniGetInt (INI_FILE, (char*)"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);
}
// The eight timer routines are now in the gwypnum.cu file
void OutputTimeStamp ()
{
time_t this_time;
char tbuf[40], buf[40];
time (&this_time);
strcpy (tbuf, ctime (&this_time)+4);
tbuf[12] = 0;
sprintf (buf, "[%s] ", tbuf);
OutputStr (buf);
}
/* Determine if a small number is prime */
int isPrime (
unsigned long p)
{
unsigned long i;
if (p < 2) // 1 is not prime.
return (FALSE);
for (i = 2; (i < 65536) && (i*i <= p); i = (i + 1) | 1)
// Avoid an overflow when computing i*i ...
if (p % i == 0)
return (FALSE);
return (TRUE);
}
/* Determine the names of the INI files */
void nameIniFiles (
int named_ini_files)
{
char buf[120];
if (named_ini_files < 0) {
strcpy (INI_FILE, "llr.ini");
strcpy (RESFILE, "lresults.txt");
strcpy (LOGFILE, "lprime.log");
strcpy (EXTENSION, "");
}
else {
sprintf (INI_FILE, "llr%04d.ini", named_ini_files);
sprintf (RESFILE, "lresu%04d.txt", named_ini_files);
sprintf (LOGFILE, "lprim%04d.log", named_ini_files);
sprintf (EXTENSION, ".%03d", named_ini_files);
}
/* Let the user rename these files and pick a different working directory */
IniGetString (INI_FILE, (char*)"WorkingDir", buf, sizeof(buf), NULL);
IniGetString (INI_FILE, (char*)"results.txt", RESFILE, 80, RESFILE);
IniGetString (INI_FILE, (char*)"prime.log", LOGFILE, 80, LOGFILE);
IniGetString (INI_FILE, (char*)"prime.ini", INI_FILE, 80, INI_FILE);
if (buf[0]) {
if (_chdir (buf))
printf ("Could not change the working directory...\n");
;
IniFileOpen (INI_FILE, 0);
}
}
/* Read the INI files */
void readIniFiles ()
{
int temp;
const char *p;
char cpustring[100];
// getCpuInfo ();
PRECISION = (unsigned int) IniGetInt (INI_FILE, (char*)"PercentPrecision", 2);
if (PRECISION > 6)
PRECISION = 6;
ITER_OUTPUT = IniGetInt (INI_FILE, (char*)"OutputIterations", 10000);
if (ITER_OUTPUT <= 0)
ITER_OUTPUT = 1;
ITER_OUTPUT_RES = IniGetInt (INI_FILE,
(char*)"ResultsFileIterations", 99999999);
if (ITER_OUTPUT_RES < 1000)
ITER_OUTPUT_RES = 1000;
DISK_WRITE_TIME = IniGetInt (INI_FILE, (char*)"DiskWriteTime", 30);
TWO_BACKUP_FILES = (int) IniGetInt (INI_FILE,
(char*)"TwoBackupFiles", 1);
RUN_ON_BATTERY = (int) IniGetInt (INI_FILE, (char*)"RunOnBattery",
1);
temp = (int) IniGetInt (INI_FILE, (char*)"ErrorCheck", 0);
ERRCHK = (temp != 0);
PRIORITY = (unsigned int) IniGetInt (INI_FILE, (char*)"Priority",
1);
CPU_AFFINITY = (unsigned int) IniGetInt (INI_FILE,
(char*)"Affinity", 99);
if (CPU_AFFINITY!=99) {
CPU_MASK = 1<<CPU_AFFINITY;
// p = IniSectionGetNthStringRaw (INI_FILE, NULL, "Affinity", 1);
IniGetString (INI_FILE, (char*)"Affinity", cpustring, 100, (char*)"99");
p = strchr (cpustring, ',');
while (p != NULL) {
p++;
sscanf (p, "%d", &CPU_AFFINITY);
CPU_MASK |= 1<<CPU_AFFINITY;
p = strchr (p, ',');
}
printf ("CPU_MASK = %d\n", CPU_MASK);
}
HIDE_ICON = (int) IniGetInt (INI_FILE, (char*)"HideIcon", 0);
TRAY_ICON = (int) IniGetInt (INI_FILE, (char*)"TrayIcon", 1);
/* Guess the CPU type if it isn't known. Otherwise, validate it. */
// getCpuInfo ();
/* Other oddball options */
CUMULATIVE_TIMING = IniGetInt (INI_FILE, (char*)"CumulativeTiming",
0);
// HIGH_RES_TIMER = isHighResTimerAvailable ();
}
/*----------------------------------------------------------------------
| Portable routines to read and write ini files! NOTE: These only
| work if you open no more than 5 ini files. Also you must not
| change the working directory at any time during program execution.
+---------------------------------------------------------------------*/
struct IniLine {
char *keyword;
char *value;
int active;
};
struct IniCache {
char *filename;
int immediate_writes;
int dirty;
unsigned int num_lines;
unsigned int array_size;
struct IniLine **lines;
};
void growIniLineArray (
struct IniCache *p)
{
struct IniLine **newlines;
if (p->num_lines != p->array_size) return;
newlines = (struct IniLine **)
malloc ((p->num_lines + 100) * sizeof (struct IniLine **));
if (p->num_lines) {
memcpy (newlines, p->lines, p->num_lines * sizeof (struct IniLine *));
gwypfree (p->lines);
}
p->lines = newlines;
p->array_size = p->num_lines + 100;
}
struct IniCache *openIniFile (
char *filename,
int forced_read)
{
static struct IniCache *cache[10] = {0};
struct IniCache *p;
FILE *fd;
unsigned int i;
char line[80];
char *val;
/* See if file is cached */
for (i = 0; i < 10; i++) {
p = cache[i];
if (p == NULL) {
p = (struct IniCache *) malloc (sizeof (struct IniCache));
p->filename = (char *) malloc (strlen (filename) + 1);
strcpy (p->filename, filename);
p->immediate_writes = 1;
p->dirty = 0;
p->num_lines = 0;
p->array_size = 0;
p->lines = NULL;
forced_read = 1;
cache[i] = p;
break;
}
if (strcmp (filename, p->filename) == 0)
break;
}
/* Skip reading the ini file if appropriate */
if (!forced_read) return (p);
if (p->dirty) return (p);
/* Free the data if we've already read some in */
for (i = 0; i < p->num_lines; i++) {
gwypfree (p->lines[i]->keyword);
gwypfree (p->lines[i]->value);
gwypfree (p->lines[i]);
}
p->num_lines = 0;
/* Read the IniFile */
fd = fopen (filename, "r");
if (fd == NULL)
return (p);
while (fgets (line, 80, fd)) {
if (line[strlen(line)-1] == '\n')
line[strlen(line)-1] = 0;
if (line[0] == 0)
continue;
if (line[strlen(line)-1] == '\r')
line[strlen(line)-1] = 0;
if (line[0] == 0)
continue;
val = strchr (line, '=');
if (val == NULL) {
char buf[130];
sprintf (buf, "Illegal line in INI file: %s\n", line);
OutputSomewhere (buf);
continue;
}
*val++ = 0;
growIniLineArray (p);
/* Allocate and fill in a new line structure */
i = p->num_lines++;
p->lines[i] = (struct IniLine *) malloc (sizeof (struct IniLine));
p->lines[i]->keyword = (char *) malloc (strlen (line) + 1);
p->lines[i]->value = (char *) malloc (strlen (val) + 1);
p->lines[i]->active = TRUE;
strcpy (p->lines[i]->keyword, line);
strcpy (p->lines[i]->value, val);
}
fclose (fd);
return (p);
}
void writeIniFile (
struct IniCache *p)
{
int fd;
unsigned int j;
char buf[100];
/* Delay writing the file unless this INI file is written */
/* to immediately */
if (!p->immediate_writes) {
p->dirty = 1;
return;
}
/* Create and write out the INI file */
fd = _open (p->filename, _O_CREAT | _O_TRUNC | _O_WRONLY | _O_TEXT, 0666);
if (fd < 0) return;
for (j = 0; j < p->num_lines; j++) {
strcpy (buf, p->lines[j]->keyword);
strcat (buf, "=");
strcat (buf, p->lines[j]->value);
strcat (buf, "\n");
wc = _write (fd, buf, strlen (buf));
}
p->dirty = 0;
_close (fd);
}
void save_IniFile (char *filename, char *savedfilename) {
struct IniCache *p;
p = openIniFile (filename, 1); // open and read the source IniFile.
p->filename = savedfilename;
// put the target filename in the structure.
writeIniFile (p); // Write the target.
p->filename = filename; // Restore the structure in memory.
}
void truncated_strcpy (
char *buf,
unsigned int bufsize,
char *val)
{
if (strlen (val) >= bufsize) {
memcpy (buf, val, bufsize-1);
buf[bufsize-1] = 0;
}
else {
strcpy (buf, val);
}
}
void IniGetString (
char *filename,
char *keyword,
char *val,
unsigned int val_bufsize,
char *default_val)
{
struct IniCache *p;
unsigned int i;
/* Open ini file */
p = openIniFile (filename, 1);
/* Look for the keyword */
for (i = 0; ; i++) {
if (i == p->num_lines) {
if (default_val == NULL) {
val[0] = 0;
}
else {
truncated_strcpy (val, val_bufsize, default_val);
}
return;
}
if (p->lines[i]->active &&
stricmp (keyword, p->lines[i]->keyword) == 0)
break;
}
/* Copy info from the line structure to the user buffers */
truncated_strcpy (val, val_bufsize, p->lines[i]->value);
}
long IniGetInt (
char *filename,
char *keyword,
long default_val)
{
char buf[20], defval[20];
sprintf (defval, "%ld", default_val);
IniGetString (filename, keyword, buf, 20, defval);
return (atol (buf));
}
void IniWriteString (
char *filename,
char *keyword,
char *val)
{
struct IniCache *p;
unsigned int i, j;
/* Open ini file */
p = openIniFile (filename, 1);
/* Look for the keyword */
for (i = 0; ; i++) {
if (i == p->num_lines ||
stricmp (p->lines[i]->keyword, "Time") == 0) {
/* Ignore request if we are deleting line */
if (val == NULL) return;
/* Make sure the line array has room for the new line */
growIniLineArray (p);
/* Shuffle entries down to make room for this entry */
for (j = p->num_lines; j > i; j--)
p->lines[j] = p->lines[j-1];
/* Allocate and fill in a new line structure */
p->lines[i] = (struct IniLine *) malloc (sizeof (struct IniLine));
p->lines[i]->keyword = (char *) malloc (strlen (keyword) + 1);
strcpy (p->lines[i]->keyword, keyword);
p->lines[i]->value = NULL;
p->num_lines++;
break;
}
if (p->lines[i]->active &&
stricmp (keyword, p->lines[i]->keyword) == 0) {
if (val != NULL && strcmp (val, p->lines[i]->value) == 0)