primenums.c
23.7 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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
/* -----------------------------------------------------------------------------
* Copyright (C) 2019-2021 daiteq s.r.o. http://www.daiteq.com
*
* This program is distributed WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE.
*
* -----------------------------------------------------------------------------
* Filename : primenums.c
* Authors : Martin Danek
* Description : Sieve of Eratosthenes with SWAR operations
* Release :
* Version : 1.0
* Date : 27.4.2021
* -----------------------------------------------------------------------------
*/
#include <stdint.h>
#include <leon-myuart.h>
#include <my_printf.h>
#include <asm.h>
#include <asi.h>
//#include <sfmath.h>
#include <sysregs.h>
#include <lconf.h>
#include "swar.h"
void report_time(int clr) {
unsigned ticks_lo, ticks_hi, insns_lo, insns_hi;
static unsigned ticks_lo_acc, ticks_hi_acc, insns_lo_acc, insns_hi_acc;
sysregs_stop();
sysregs_read(&ticks_lo, &ticks_hi, &insns_lo, &insns_hi);
if (ticks_hi) {
printf("EXECUTED IN (%u * 2^32 + %u) TICKS AND (%u * 2^32 + %u) INSTRUCTIONS\n\n",ticks_hi, ticks_lo, insns_hi, insns_lo);
}
else {
printf("EXECUTED IN %u TICKS AND %u INSTRUCTIONS\n\n",ticks_lo, insns_lo);
}
ticks_lo_acc+=ticks_lo; ticks_hi_acc+=ticks_hi;
insns_lo_acc+=insns_lo; insns_hi_acc+=insns_hi;
if (clr) {
NL;
if (ticks_hi_acc) {
printf("ACCUMULATED VALUES (%u * 2^32 + %u) TICKS AND (%u * 2^32 + %u) INSTRUCTIONS\n\n", ticks_hi_acc, ticks_lo_acc, insns_hi_acc, insns_lo_acc);
}
else {
printf("ACCUMULATED VALUES %u TICKS AND %u INSTRUCTIONS\n\n",ticks_lo_acc, insns_lo_acc);
}
ticks_lo_acc=0; ticks_hi_acc=0;
insns_lo_acc=0; insns_hi_acc=0;
}
sysregs_start();
}
//A: --has-swar
// -----------------------------------------------------------------------------
// SWAR - macros
//#define SWAR_SET_ELM(tp,svar,index,value)
//svar = (svar ) |
//((value & ((1<<sizeofswar(tp))-1))<<(index*sizeofswar(tp)))
void SWAR_SET_ELM(void *swar, int idx, int val, int bitsz)
{
*((unsigned int *)swar) = ( (*((unsigned int *)swar)) & ~(((1<<bitsz)-1)<<(bitsz*idx))) | ((val & ((1<<bitsz)-1))<<(bitsz*idx));
}
//#define SWAR_GET_ELM(tp,svar,index)
int SWAR_GET_ELM(void *swar, int idx, int bitsz)
{
return ((*((unsigned int *)swar) >> (bitsz*idx)) & ((1<<bitsz)-1));
}
//#define SWAR_SET_ARR_ELM(tp,arry,index,value)
void SWAR_SET_ARR_ELM(void *arry, int idx, int val, int bitsz)
{
int pack = 32/bitsz;
int ofs = idx / pack;
int elidx = idx - ofs*pack;
unsigned int *pw = ((unsigned int *)arry) + ofs;
if (pack==1)
*pw=val;
else
SWAR_SET_ELM(pw, elidx, val, bitsz);
}
//#define SWAR_GET_ARR_ELM(tp,arry,index)
int SWAR_GET_ARR_ELM(void *arry, int idx, int bitsz)
{
int pack = 32/bitsz;
int ofs = idx / pack;
int elidx = idx - ofs*pack;
unsigned int *pw = ((unsigned int *)arry) + ofs;
if (pack==1)
return *pw;
else
return SWAR_GET_ELM(pw, elidx, bitsz);
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// BIT - macros
#define BIT_SET_ELM(word, pos, val) \
((val)?(word | (1<<pos)):(word & (~(1<<pos))))
#define BIT_GET_ELM(word, pos) \
(word & (1<<pos))
/* for clearing BSS section */
extern unsigned long __bss_start; /* start address for the .bss section. defined in linker script */
extern unsigned long _end; /* end address for the .bss section. defined in linker script */
#define VERBOSE /* */
#define RAWRAM
#define VERSION_NAIVE
#define VERSION_BITVEC
// #define VERSION_SWAR_INLINEASM
#define VERSION_SWAR
// #define PRINT_ITEMS
#ifdef VERSION_SWAR_INLINEASM
#define SLICESZ 1
#ifdef VERSION_SWAR
#error "INVALID CONFIGURATION - SWAR WHEN SWAR_INLINEASM"
#endif
#endif
#ifdef VERSION_SWAR
#define VERSION_SWAR_CHAR
#define VERSION_CHAR_SWAR
#define SLICESZ 8
#ifdef VERSION_SWAR_INLINEASM
#error "INVALID CONFIGURATION - SWAR_INLINEASM WHEN SWAR"
#endif
#endif
typedef unsigned int u8b __attribute__((subword(SLICESZ)));
typedef unsigned int u8b1 __attribute__((subword(SLICESZ,1)));
#define PACKING (32/SLICESZ)
#define MAXNUM 1300000
#define SQRTMAXNUM 1141 // ceil(sqrt(1300000))
// #define MAXNUM 256
// #define SQRTMAXNUM 16 // ceil(sqrt(256))
// #define MAXNUM 2048
// #define SQRTMAXNUM 46 // ceil(sqrt(2048))
#define MAXSLOTS ((MAXNUM-1)/PACKING+1)
#define MAXMASKS (MAXSLOTS<<1)
#define RAMSTART 0x50000000 // VC707 w/ mig
#define RAMSTART2 0x6000000 // VC707 w/ mig
void main(void) {
volatile unsigned acc;
volatile u8b redmask[2];
// #ifdef RAWRAM
// volatile u8b *swnumbers=RAMSTART;
// volatile u8b *swmasks=RAMSTART+(MAXSLOTS*4);
// #else
volatile u8b swnumbers[MAXSLOTS];
volatile u8b swmasks[MAXSLOTS];
// #endif
int i,j,k,l,m;
unsigned sz;
unsigned top;
unsigned slot,slotreg;
unsigned slotstart;
unsigned prime, first, last;
unsigned fin, cont;
unsigned sqr, val, res;
#ifdef VERSION_SWAR_INLINEASM
#ifdef RAWRAM
unsigned *a=RAMSTART;
#else
unsigned a[MAXNUM];
#endif
#else
#ifdef RAWRAM
unsigned char *a=RAMSTART;
#else
unsigned char a[MAXNUM];
#endif
#endif
// unsigned roots[MAXNUM];
#ifdef RAWRAM
unsigned *numbers=RAMSTART;
unsigned *masks=RAMSTART+(MAXSLOTS*4);
#else
unsigned numbers[MAXSLOTS];
unsigned masks[MAXMASKS];
#endif
/* reset BSS section to zero */
for(unsigned long *pMem = &__bss_start; pMem < &_end;) {
*(pMem++) = 0;
}
init_uart();
sysregs_init();
top = MAXNUM;
sz = sizeof(*a);
#ifdef VERBOSE
printf("\n\nConfiguration:\n");
#ifdef VERSION_NAIVE
printf(" VERSION_NAIVE\n");
printf(" sizeof(*a) = %dB\n",sz);
#endif
#ifdef VERSION_BITVEC
printf(" VERSION_BITVEC\n");
#endif
#ifdef VERSION_SWAR_INLINEASM
printf(" VERSION_SWAR_INLINEASM\n");
#endif
#ifdef VERSION_SWAR
printf(" VERSION_SWAR\n");
#endif
#ifdef VERSION_SWAR_CHAR
printf(" VERSION_SWAR_CHAR\n");
#endif
#ifdef VERSION_CHAR_SWAR
printf(" VERSION_CHAR_SWAR\n");
#endif
printf("\n");
printf(" MAXNUM = %d\n",top);
printf(" SQRTMAXNUM = %d\n",SQRTMAXNUM);
printf(" SLICESZ = %d\n",SLICESZ);
printf(" PACKING = %d\n",PACKING);
printf(" MAXSLOTS = %d\n",MAXSLOTS);
printf(" MAXMASKS = %d\n",MAXMASKS);
printf("\n\n\n");
#endif
#ifdef PRECOMP_SQRT
k=0;
roots[0]=1;
for (i=1;i<top;i++) {
roots[i]=i*i;
}
k=0;
while (roots[k]<top) {
k++;
}
#else
k=SQRTMAXNUM;
#endif
printf("primenums: sqrt(%d)=%d\n",top,k);
// -------------------------------------------------------------
#ifdef VERSION_NAIVE
printf("\n\n*** VERSION_NAIVE BEGIN ***\n\n");
printf("primenums: initializing array\n");
sysregs_init();
for (i=0;i<top;i++) {
*(a+i)=1;
}
report_time(0);
printf("primenums: eliminating composites\n");
sysregs_init();
j=2;
while(j<=SQRTMAXNUM) {
while (*(a+j)==0)
j++;
for (i=j;i<top;i=i+j)
if (i>j) {
*(a+i)=0;
}
j++;
}
report_time(0);
printf("primenums: counting primes...\n");
sysregs_init();
acc=0;
for (i=1;i<top;i++) {
if (*(a+i)==1) {
acc++;
}
}
report_time(1);
printf("primenums: primes found: %d\n\n",acc);
sysregs_init();
#ifdef PRINT_ITEMS
#ifdef VERBOSE
printf("primenums: printing primes\n");
#endif
for (i=1;i<top;i++)
#ifdef VERBOSE
if (*(a+i)==1) {
printf("%d\n",i);
}
#else
if (*(a+i)==1) {
printf("1");
}
else
printf("0");
#endif
printf("\n");
#endif
printf("\nprimenums: VERSION_NAIVE done\n\n\n");
#endif
// -------------------------------------------------------------
#ifdef VERSION_BITVEC
printf("\n\n*** VERSION_BITVEC BEGIN ***\n\n");
printf("primenums: initializing array\n");
sysregs_init();
numbers[0]=0xfffffffe;
for (i=1;i<(MAXSLOTS)+1;i++) {
numbers[i]=0xffffffff;
}
report_time(0);
printf("primenums: eliminating composites\n");
sysregs_init();
j=0;
l=0;
prime=0;
cont=1;
while(cont) {
// Get the next prime
while(numbers[j]==0) {
// printf("Skipping j=%d\n",j);
j++;
}
val=numbers[j];
while (!((val>>l)&1))
l++;
prime=PACKING*j+l+1; // the actual prime
// Compute masks, set the number of active masks
last=0;
first=1;
m=prime;
val=0;
slot=0;
fin=0;
slotreg=0;
slotstart=slot;
while ((slot<MAXSLOTS)&&(fin<2)) {
if (prime<=PACKING) {
if (!first)
val|=(1<<(m-1));
else
first=0;
// printf("m %d val %08X\n",m,val);
if (val&(1<<(PACKING-1))) {
fin++;
if (!slotreg) {
slotreg=slot+1;
// printf("slotreg is %d\n",slotreg);
}
}
m+=prime;
if (m>PACKING) {
// printf("masks[%d]=%08X\n",slot,val);
masks[slot++]=val;
m=m%PACKING;
val=0;
}
}
else {
if ((m-1-last)<(PACKING)) {
// the only non-zero word
val=(1<<(m-last-1));
last=PACKING-m+last;
if ((!slotstart)&&(!first))
slotstart=slot;
}
else {
val=0;
last+=PACKING;
}
if (val&(1<<(PACKING-1))) {
fin++;
if (!slotreg) {
slotreg=slot+1;
// printf("slotreg is %d\n",slotreg);
}
}
// printf("last %d m %d m-last %d\n",last,m,m-last);
// printf("masks[%d]=%08X\n",slot,val);
if (first)
masks[slot++]=0;
else
masks[slot++]=val;
if ((first)&&(val))
first=0;
}
}
if (slot>=MAXMASKS) {
printf("\nMAXIMUM NUMBER OF MASKS EXCEEDED - SLOT %d PRIME %d\n",slot,prime);
exit(1);
}
// printf("slot %d\n",slot);
// printf("prime %d\n",prime);
// for (i=0;i<slot;i++) {
// printf("masks[%d]=%08X\n",i,masks[i]);
// }
if (!slotreg) slotreg=MAXSLOTS;
for (i=0;i<slotreg;i++) {
numbers[i]&=(~masks[i%slot]);
}
for (i=slotreg;i<MAXSLOTS;i++) {
numbers[i]&=(~masks[i%slotreg+slotreg]);
}
l++;
if (l>=PACKING) {
l=0;
j++;
}
if (prime>k) {
cont=0;
}
}
numbers[0]|=1;
report_time(0);
printf("primenums: counting primes...\n");
sysregs_init();
acc=0;
m=0;
for (i=0;i<MAXSLOTS;i++) {
for (j=0;j<PACKING;j++) {
m++;
// if ((numbers[i]&(1<<j))&&(m<top)) {
if (numbers[i]&(1<<j)) {
acc++;
}
}
// printf("numbers[%d] %08X acc %d\n",i,numbers[i],acc);
}
report_time(1);
printf("primenums: primes found: %d\n\n",acc);
sysregs_init();
#ifdef PRINT_ITEMS
m=0;
for (i=0;i<MAXSLOTS;i++) {
for (j=0;j<PACKING;j++) {
m++;
if ((numbers[i]&(1<<j))&&(m<top)) {
printf("%d\n",m);
}
}
}
printf("\n");
#endif
printf("\nprimenums: VERSION_BITVEC done\n\n\n");
#endif
// -------------------------------------------------------------
#ifdef VERSION_SWAR_INLINEASM
printf("\n\n*** VERSION_SWAR_INLINEASM BEGIN ***\n\n");
printf("primenums: initializing array\n");
sysregs_init();
numbers[0]=0xfffffffe;
for (i=1;i<(MAXSLOTS)+1;i++) {
numbers[i]=0xffffffff;
}
report_time(0);
printf("primenums: eliminating composites\n");
sysregs_init();
j=0;
l=0;
prime=0;
cont=1;
while(cont) {
// Get the next prime
while(numbers[j]==0) {
printf("Skipping j=%d\n",j);
j++;
}
val=numbers[j];
while (!((val>>l)&1))
l++;
prime=PACKING*j+l+1; // the actual prime
// Compute masks, set the number of active masks
last=0;
first=1;
m=prime;
val=0;
slot=0;
fin=0;
slotreg=0;
slotstart=slot;
while ((slot<MAXSLOTS)&&(fin<2)) {
if (prime<=PACKING) {
if (!first)
val|=(1<<(m-1));
else
first=0;
if (val&(1<<(PACKING-1))) {
fin++;
if (!slotreg) {
slotreg=slot+1;
}
}
m+=prime;
if (m>PACKING) {
masks[slot++]=val;
m=m%PACKING;
val=0;
}
}
else {
if ((m-1-last)<(PACKING)) {
// the only non-zero word
val=(1<<(m-last-1));
last=PACKING-m+last;
if ((!slotstart)&&(!first))
slotstart=slot;
}
else {
val=0;
last+=PACKING;
}
if (val&(1<<(PACKING-1))) {
fin++;
if (!slotreg) {
slotreg=slot+1;
}
}
if (first)
masks[slot++]=0;
else
masks[slot++]=val;
if ((first)&&(val))
first=0;
}
}
if (slot>=MAXMASKS) {
printf("\nMAXIMUM NUMBER OF MASKS EXCEEDED - SLOT %d PRIME %d\n",slot,prime);
exit(1);
}
// printf("slot %d\n",slot);
// printf("prime %d\n",prime);
// for (i=0;i<slot;i++) {
// printf("masks[%d]=%08X\n",i,masks[i]);
// }
if (!slotreg) slotreg=MAXSLOTS;
for (i=0;i<slotreg;i++) {
numbers[i]&=(~masks[i%slot]);
}
for (i=slotreg;i<MAXSLOTS;i++) {
numbers[i]&=(~masks[i%slotreg+slotreg]);
}
l++;
if (l>=PACKING) {
l=0;
j++;
}
if (prime>k) {
cont=0;
}
}
numbers[0]|=1;
report_time(0);
printf("primenums: counting primes...\n");
sysregs_init();
acc=0;
swar_config(SW_OP_COR1b|SW_RED);
for (i=0;i<MAXSLOTS;i++) {
acc=exec_swar(0xffffffff,numbers[i]);
// printf("numbers[%d] %08X acc %d\n",i,numbers[i],acc);
}
report_time(1);
printf("primenums: primes found: %d\n\n",acc);
sysregs_init();
#ifdef PRINT_ITEMS
m=0;
for (i=0;i<MAXSLOTS;i++) {
for (j=0;j<PACKING;j++) {
m++;
if ((numbers[i]&(1<<j))&&(m<top)) {
printf("%d\n",m);
}
}
}
printf("\n");
#endif
printf("\nprimenums: VERSION_SWAR_INLINEASM done\n\n\n");
#endif
// -------------------------------------------------------------
#ifdef VERSION_SWAR
printf("\n\n*** VERSION_SWAR BEGIN ***\n\n");
printf("primenums: initializing array\n");
sysregs_init();
// swnumbers[0][0]=0;
SWAR_SET_ARR_ELM(swnumbers, 0, 0, SLICESZ);
for (j=1;j<PACKING;j++)
// swnumbers[0][j]=((1<<SLICESZ)-1);
SWAR_SET_ARR_ELM(swnumbers, j, ((1<<SLICESZ)-1), SLICESZ);
for (i=1;i<(MAXSLOTS)+1;i++) {
for (j=0;j<PACKING;j++)
// swnumbers[i][j]=((1<<SLICESZ)-1);
SWAR_SET_ARR_ELM(swnumbers, i*PACKING+j, ((1<<SLICESZ)-1), SLICESZ);
}
// for (i=0;i<(MAXSLOTS)+1;i++) {
// printf("swnumbers[%d]=%08X\n",i,*((int*)&swnumbers[i]));
// }
report_time(0);
printf("primenums: eliminating composites\n");
sysregs_init();
redmask[0]=0xffffffff;
j=0;
l=0;
prime=0;
cont=1;
while(cont) {
// Get the next prime
val=(*((int*)&swnumbers[j]));
// printf("j %d val %08X\n",j,val);
while(val==0) {
printf("Skipping j=%d val %08X\n",j,val);
j++;
l=0;
val=(*((int*)&swnumbers[j]));
}
// while (!((val>>l)&1)) {
while (!(SWAR_GET_ARR_ELM(swnumbers, PACKING*j+l, SLICESZ))) {
// printf("Skipping (SWAR_GET_ARR_ELM(swnumbers, %d*%d+%d, %d) %08X\n",PACKING,j,l,SLICESZ,SWAR_GET_ARR_ELM(swnumbers, PACKING*j+l, SLICESZ));
l++;
if (l==PACKING) {
l=0;
j++;
}
}
prime=PACKING*j+l+1; // the actual prime
// printf("current prime is %d\n",prime);
// Compute swmasks, set the number of active swmasks
last=0;
first=1;
m=prime;
val=0;
slot=0;
fin=0;
slotreg=0;
slotstart=slot;
while ((slot<MAXSLOTS)&&(fin<2)) {
if (prime<=PACKING) {
if (!first)
val|=(((1<<SLICESZ)-1)<<(SLICESZ*(m-1)));
else
first=0;
// printf("m %d val %08X\n",m,val);
if (val&(((1<<SLICESZ)-1)<<((PACKING-1)*SLICESZ))) {
fin++;
if (!slotreg) {
slotreg=slot+1;
// printf("slotreg is %d\n",slotreg);
}
}
m+=prime;
if (m>PACKING) {
// printf("swmasks[%d]=%08X\n",slot,~val);
swmasks[slot++]=~val;
m=m%PACKING;
val=0;
}
}
else {
if ((m-1-last)<(PACKING)) {
// the only non-zero word
val=(((1<<SLICESZ)-1)<<(SLICESZ*(m-last-1)));
last=PACKING-m+last;
if ((!slotstart)&&(!first))
slotstart=slot;
}
else {
val=0;
last+=PACKING;
}
if (val&(((1<<SLICESZ)-1)<<((PACKING-1)*SLICESZ))) {
fin++;
if (!slotreg) {
slotreg=slot+1;
// printf("slotreg is %d\n",slotreg);
}
}
// printf("last %d m %d m-last %d\n",last,m,m-last);
// printf("swmasks[%d]=%08X\n",slot,~val);
if (first)
swmasks[slot++]=~0;
else
swmasks[slot++]=~val;
if ((first)&&(val))
first=0;
}
}
if (slot>=MAXMASKS) {
printf("\nMAXIMUM NUMBER OF MASKS EXCEEDED - SLOT %d PRIME %d\n",slot,prime);
exit(1);
}
// printf("slotreg %d\n",slotreg);
if (!slotreg) {
slotreg=MAXSLOTS;
// printf("adjusted slotreg %d\n",slotreg);
}
// printf("slot %d\n",slot);
// printf("prime %d\n",prime);
// for (i=0;i<slot;i++) {
// printf("swmasks[%d]=%08X\n",i,*((int*)&swmasks[i]));
// }
res=0;
for (i=0;i<slotreg;i++) {
// printf("slot %d swnumbers[%d]=%08X swmasks[%d]=%08X\n",slot, i, *((int*)&swnumbers[i]),i%slot, (*((int*)&swmasks[i%slot])));
// res=*((int*)&swnumbers[i]);
// res&=((*((int*)&swmasks[i%slot])));
// swnumbers[i]=res;
swnumbers[i] = swnumbers[i] * swmasks[i%slot];
// printf("#1 swnumbers[%d]=%08X\n",i,*((int*)&swnumbers[i]));
}
for (i=slotreg;i<MAXSLOTS;i++) {
// printf("slot %d swnumbers[%d]=%08X swmasks[%d]=%08X\n",slot, i, *((int*)&swnumbers[i]),i%slotreg+slotreg, (*((int*)&swmasks[i%slotreg+slotreg])));
// res=*((int*)&swnumbers[i]);
// res&=((*((int*)&swmasks[i%slotreg+slotreg])));
// swnumbers[i]=res;
swnumbers[i] = swnumbers[i] * swmasks[i%slotreg+slotreg];
// printf("#2 swnumbers[%d]=%08X\n",i,*((int*)&swnumbers[i]));
}
// correct values back to all '1's
for (i=0;i<(MAXSLOTS);i++) {
// printf("#3.1 swnumbers[%d]=%08X * redmask[0]=%08X\n",i,*((int*)&swnumbers[i]),*((int*)&redmask[0]));
swnumbers[i] = swnumbers[i] * redmask[0];
// printf("#3.2 swnumbers[%d]=%08X\n",i,*((int*)&swnumbers[i]));
}
// for (i=0;i<(MAXSLOTS);i++) {
// printf("corr swnumbers[%d]=%08X\n",i,*((int*)&swnumbers[i]));
// }
l++;
if (l>=PACKING) {
l=0;
j++;
}
if (prime>k) {
// printf("setting cont=0 for prime=%d and k=%d\n",prime,k);
cont=0;
}
}
val=*((int*)&swnumbers[0]);
val|=((1<<SLICESZ)-1);
swnumbers[0]=val;
report_time(0);
printf("primenums: translating flags...\n");
sysregs_init();
// redmask[0]=0xffffffff; // moved at the beginning
// convert values to single 1's
for (i=0;i<MAXSLOTS;i++) {
swnumbers[i] = redmask[0] * swnumbers[i];
// printf("#4 swnumbers[%d]=%08X\n",i,*((int*)&swnumbers[i]));
}
report_time(0);
printf("primenums: counting primes...\n");
sysregs_init();
acc=0;
redmask[0]=redmask[0]*redmask[0];
// printf("#5 init redmask[0]=%08X\n",*((int*)&redmask[0]));
#pragma swar reduce
for (i=0;i<MAXSLOTS;i++) {
redmask[1] = redmask[0] * swnumbers[i];
acc+=*((int *)&redmask[1]);
// printf("#6 swnumbers[%d]=%08X redmask[1]=%08X acc=%d\n",i,*((int*)&swnumbers[i]),*((int*)&redmask[1]),acc);
}
report_time(1);
printf("primenums: primes found: %d\n\n",acc);
sysregs_init();
#ifdef PRINT_ITEMS
m=0;
for (i=0;i<MAXSLOTS;i++) {
for (j=0;j<PACKING;j++) {
m++;
if (((*((int*)&swnumbers[i]))&(1<<(SLICESZ*j)))&&(m<top)) {
printf("%d\n",m);
}
}
}
printf("\n");
#endif
printf("\nprimenums: VERSION_SWAR done\n\n\n");
#endif
// -------------------------------------------------------------
#ifdef VERSION_SWAR_CHAR
printf("\n\n*** VERSION_SWAR_CHAR BEGIN ***\n\n");
printf("primenums: initializing array\n");
sysregs_init();
*((char*)&swnumbers)=0;
for (j=1;j<PACKING*MAXSLOTS;j++) {
*(((char*)&swnumbers)+j)=1;
// printf("((char*)&swnumbers)+%d=%08X *(((char*)&swnumbers)+%d)=%08X\n",j,((char*)&swnumbers)+j,j,*(((char*)&swnumbers)+j));
}
// for (i=0;i<MAXSLOTS;i++) {
// printf("(((int*)&swnumbers)+%d)=%08X swnumbers[%d]=%08X\n",i,(((int*)&swnumbers)+i),i,*(((int*)&swnumbers)+i));
// }
report_time(0);
printf("primenums: eliminating composites\n");
sysregs_init();
j=1;
while(j<MAXSLOTS*PACKING) {
val=*(((char*)&swnumbers)+j);
while (val==0) {
// printf("Skipping ((char*)&swnumbers)+%d=%08X *(((char*)&swnumbers)+%d)=%08X val=%08X\n",j,((char*)&swnumbers)+j,j,*(((char*)&swnumbers)+j),val);
j++;
val=*(((char*)&swnumbers)+j);
}
// printf("prime %d\n",j+1);
for (i=j;i<top;i=i+j+1) {
if (i>j) {
*(((char*)&swnumbers)+i)=0;
// printf("Zeroed ((char*)&swnumbers)+%d=%08X *(((char*)&swnumbers)+%d)=%08X\n",i,((char*)&swnumbers)+i,i,*(((char*)&swnumbers)+i));
}
}
j++;
}
*((char*)&swnumbers)=1;
report_time(0);
// printf("primenums: counting primes - naive...\n");
// sysregs_init();
//
// acc=0;
// for (i=0;i<top;i++) {
// if (*(((char*)&swnumbers)+i)==1) {
// acc++;
// }
// }
// report_time(0);
// printf("primenums: primes found: %d\n\n",acc);
//
// printf("\n\n");
//
printf("primenums: counting primes - SWAR reduction...\n");
sysregs_init();
redmask[0]=0xffffffff;
// redmask[0]=0x01010101;
acc=0;
// printf("#5.0 acc=%d\n",acc); // when commented, the previous assignment acc=0 is not executed
// printf("#5.1 init redmask[0]=%08X\n",*((int*)&redmask[0]));
redmask[0]=redmask[0]*redmask[0];
// printf("#5.2 init redmask[0]=%08X\n",*((int*)&redmask[0]));
#pragma swar reduce
for (i=0;i<MAXSLOTS;i++) {
redmask[1] = redmask[0] * swnumbers[i];
acc+=*((int *)&redmask[1]);
// printf("#6 swnumbers[%d]=%08X redmask[1]=%08X acc=%d\n",i,*((int*)&swnumbers[i]),*((int*)&redmask[1]),acc);
}
report_time(1);
printf("primenums: primes found: %d\n\n",acc);
sysregs_init();
// #ifdef PRINT_ITEMS
// #ifdef VERBOSE
// printf("primenums: printing primes - naive\n");
// #endif
// for (i=1;i<top;i++)
// #ifdef VERBOSE
// if (*(a+i)==1) {
// printf("%d\n",i);
// }
// #else
// if (*(a+i)==1) {
// printf("1");
// }
// else
// printf("0");
// #endif
// printf("\n");
// #endif
#ifdef PRINT_ITEMS
#ifdef VERBOSE
printf("primenums: printing primes - SWAR reduction\n");
#endif
m=0;
for (i=0;i<MAXSLOTS;i++) {
for (j=0;j<PACKING;j++) {
m++;
if (((*((int*)&swnumbers[i]))&(1<<(SLICESZ*j)))&&(m<top)) {
printf("%d\n",m);
}
}
}
printf("\n");
#endif
printf("\nprimenums: VERSION_SWAR_CHAR done\n\n\n");
#endif
// -------------------------------------------------------------
#ifdef VERSION_CHAR_SWAR
printf("\n\n*** VERSION_CHAR_SWAR BEGIN ***\n\n");
printf("primenums: initializing array\n");
sysregs_init();
*(a)=0;
for (i=1;i<top;i++) {
*(a+i)=1;
}
report_time(0);
printf("primenums: eliminating composites\n");
sysregs_init();
j=1;
while(j<=SQRTMAXNUM) {
while (*(a+j)==0)
j++;
for (i=j;i<top;i=i+j+1)
if (i>j) {
*(a+i)=0;
}
j++;
}
*(a)=1;
report_time(0);
printf("primenums: counting primes - SWAR reduction...\n");
// sysregs_init();
// redmask[0]=0xffffffff;
redmask[0]=0x01010101;
acc=0;
// printf("#5.0 acc=%d\n",acc); // when commented, the previous assignment acc=0 is not executed
sysregs_init();
// printf("#5.1 init redmask[0]=%08X\n",*((int*)&redmask[0]));
// redmask[0]=redmask[0]*redmask[0];
// printf("#5.2 init redmask[0]=%08X\n",*((int*)&redmask[0]));
#pragma swar reduce
for (i=0;i<MAXSLOTS;i++) {
redmask[1] = redmask[0] * (*(u8b*)(((void*)a)+i*PACKING));
acc+=*((int *)&redmask[1]);
// printf("#6 *(a+%d*PACKING)=%08X redmask[1]=%08X acc=%d\n",i,*(int*)(((void*)a)+i*PACKING),*((int*)&redmask[1]),acc);
}
report_time(1);
printf("primenums: primes found: %d\n\n",acc);
sysregs_init();
#ifdef PRINT_ITEMS
#ifdef VERBOSE
printf("primenums: printing primes\n");
#endif
for (i=1;i<top;i++)
#ifdef VERBOSE
if (*(a+i)==1) {
printf("%d\n",i);
}
#else
if (*(a+i)==1) {
printf("1");
}
else
printf("0");
#endif
printf("\n");
#endif
printf("\nprimenums: VERSION_CHAR_SWAR done\n\n\n");
#endif
// -------------------------------------------------------------
// sysregs_stop();
// sysregs_read(&ticks_lo, &ticks_hi, &insns_lo, &insns_hi);
//
// if (ticks_hi) {
// m_print("TEST EXECUTED IN ("); i_print(ticks_hi); m_print(" * 2^32 + "); i_print(ticks_lo); m_print(") TICKS AND ("); i_print(insns_hi); m_print(" * 2^32 + "); i_print(insns_lo); m_print(" INSTRUCTIONS");NL;NL;
// }
// else {
// m_print("TEST EXECUTED IN "); i_print(ticks_lo); m_print(" TICKS AND "); i_print(insns_lo); m_print(" INSTRUCTIONS");NL;NL;
// }
m_print("\n*** END OF TEST ***\n");
__asm__ volatile (
"\t ta 1\n"
"\t nop\n"
"\t nop\n"
);
}