-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiperspective.h
1678 lines (1288 loc) · 44.9 KB
/
multiperspective.h
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
/*
* Multiperspective Perceptron Predictor
* Daniel A. Jimenez
*
* This file simulates the 64.25KB "multiperspective perceptron predictor" for CBP2015.
*
*/
#ifndef _MULTIPERSPECTIVE_H_
#define _MULTIPERSPECTIVE_H_
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <inttypes.h>
#include <math.h>
// this class contains information that is kept between prediction and update
// the only thing it has here is the prediction. it should be subclassed to hold
// other data e.g. indices in tables, branch PC, etc.
class branch_info {
bool _prediction;
public:
unsigned int address;
branch_info (void) { }
// set the prediction for this branch
void prediction (bool p) {
_prediction = p;
}
// return the prediction for this branch
bool prediction (void) {
return _prediction;
}
};
// this class represents a branch predictor
class branch_predictor {
public:
branch_predictor (void) { }
// the first parameter is the branch address
// the second parameter is true if the branch was ever taken
// the third parameter is true if the branch was ever not taken
// the return value is a pointer to a branch_info object that gives
// the prediction and presumably contains information needed to
// update the predictor
virtual branch_info *lookup (unsigned int pc) = 0;
// the first parameter is the branch info returned by the corresponding predict
// the second parameter is the outcome of the branch
virtual void update (branch_info *p, unsigned int target, bool taken, int type = 0) = 0;
// if this returns false, then we will do lookup even for always/never taken branches
virtual bool filter_always_never (void) { return true; }
virtual void nonconditional_branch (unsigned int pc) { }
virtual const char *name (bool) { return ""; }
virtual ~branch_predictor (void) { }
};
class multiperspective_perceptron;
class PREDICTOR {
multiperspective_perceptron *ex;
branch_info *u;
public:
PREDICTOR(void);
bool GetPrediction (uint64_t PC);
void UpdatePredictor (uint64_t PC, OpType opType, bool resolveDir, bool predDir, uint64_t branchTarget);
void TrackOtherInst (uint64_t PC, OpType opType, bool branchDir, uint64_t branchTarget);
};
//#define VERBOSE
#define MAX_TABLES 64
typedef short int weight;
class multiperspective_perceptron_update : public branch_info {
public:
unsigned int pc;
unsigned short int hpc, pc2;
int yout;
bool filtered;
multiperspective_perceptron_update (void) { }
};
struct bestpair {
int index;
int mpreds;
};
// feature types
enum history_type {
GHIST = 1, // global pattern history
ACYCLIC = 2, // "acylic history" - put the most recent history of pc into array[pc%parameter] and use this array as a feature
MODHIST = 3, // "modulo history" - shift history bit in if PC%modulus==0
BIAS = 4, // bias of this branch
RECENCY = 5, // hash of a recency stack of PCs
IMLI = 6, // inner most loop iteration counter(s)
PATH = 7, // path history
LOCAL = 8, // local history (pshare)
MODPATH = 9, // like modhist but with path history
GHISTPATH = 10, // (path history << 1) | global history
GHISTMODPATH = 11, // (mod path history << 1) | global history
BLURRYPATH = 12, // "page" history sort of
RECENCYPOS = 13, // position of this PC in the recency stack
SGHISTPATH = 14, // alternate ghistpath inspired by strided sampling
MAXTYPE = 16
};
int paircmp (const bestpair *a, const bestpair *b) {
return a->mpreds - b->mpreds;
}
// this struct represents one feature that is an input to the hashed
// perceptron predictor
struct history_spec {
// the type of the feature
enum history_type type;
// up to three parameters for this feature
int p1, p2, p3;
// a coefficient multiplied by the weight value
double coeff;
// the size of the array of weights for this feature
int
size,
// bit width of weights for this feature (5 or 6)
width;
};
// an entry in the "branch filter", a structure that keeps track of whether
// a branch has been take or not taken so far
struct filter_entry {
bool seen_taken, seen_untaken;
filter_entry (void) {
seen_taken = false;
seen_untaken = false;
}
};
// main class for the prediction code, contained in Jimenez's little branch
// prediction infrastructure
class multiperspective_perceptron : public branch_predictor {
public:
// some values carried from prediction to update. there's no good
// reason that some stuff is put in here and other stuff isn't. it
// just happened that way as a historical artifact of supporting
// speculative update for previous predictors.
multiperspective_perceptron_update u;
// for simplicity we keep stuff in large arrays and then only use
// the part of them needed for the predictor. these defines give the
// maximum sizes of the arrays. they are very large to allow for flexible
// search of the design space, but the actual tuned values are represented
// in variables passed as parameters to the constructor and fit within the
// given hardware budgets
#define MAX_PATHHIST 4096
#define MAX_GHIST 4096
#define MAX_LG_TABLE_SIZE 13
#define MAX_TABLE_SIZE (1<<MAX_LG_TABLE_SIZE)
#define MAX_FILTER 65536
#define MAX_LOCAL_HISTORIES 4096
#define MAX_ACYCLIC 20
#define MAX_MOD 10
#define MAX_BLURRY 16
#define MAX_BLURRY2 16
#define MAX_ASSOC 256
// STATE THAT COUNTS AGAINST THE HARDWARE BUDGET. these variables
// are the part of the predictor that contain mutable state that
// persists from one prediction to the next. the parts of these variables
// that are actually used by the predictor count against the hardware budget.
// seed for random number generator
unsigned int my_seed;
// branch filter
filter_entry filter_table[MAX_FILTER];
// table of per-branch (local) histories
unsigned int local_histories[MAX_LOCAL_HISTORIES];
// counter for adaptive theta setting
int tc;
int occupancy;
// tables of weight magnitudes
weight tables[MAX_TABLES][MAX_TABLE_SIZE];
// tables of weight signs
bool sign_bits[MAX_TABLES][MAX_TABLE_SIZE][2];
// global history bits divided into block_size bits per array element
unsigned int
ghist_words[MAX_GHIST/MAX_LG_TABLE_SIZE+1];
// count of mispredictions per table
int mpreds[MAX_TABLES];
// acyclic histories
bool acyclic_histories[MAX_ACYCLIC][32];
// alternate acyclic histories that use path instead of pattern history
unsigned int acyclic2_histories[MAX_ACYCLIC][32];
// modulo pattern history
bool mod_histories[MAX_MOD][MAX_GHIST];
// modulo path history
unsigned short int modpath_histories[MAX_MOD][MAX_PATHHIST];
// "page" history
unsigned int blurrypath_histories[MAX_BLURRY][MAX_BLURRY2];
// recency stack of recenctly visited PCs (hashed)
unsigned int short recency_stack[MAX_ASSOC];
// the last global history outcome
bool last_ghist_bit;
// history of recent PCs (hashed)
unsigned int short path_history[MAX_PATHHIST];
// innermost loop iteration counters; 4 versions
unsigned int imli_counter1, imli_counter2, imli_counter3, imli_counter4;
// RUN-TIME CONSTANTS THAT DO NOT COUNT AGAINST HARDWARE BUDGET
int
// many different moduli could be used for building
// modulo history. these arrays keep track of which moduli
// are actually used in the features for this predictor
// so only the relevant tables are updated for performance
// reasons. in a real implementation, only those tables would
// be implemented.
modhist_indices[MAX_MOD],
modpath_indices[MAX_MOD],
modpath_lengths[MAX_MOD],
modhist_lengths[MAX_MOD];
int
// maximum global history required by any feature
ghist_length,
// maximum modulo history required by any feature
modghist_length,
// maximum path length required by any feature
path_length,
// total bits used by the predictor (for sanity check)
totalbits,
// associativity of the recency stack, derived from the
// maximum depth any RECENCY feature hashes
assoc;
// number of modulo histories and modulo path histories
int nmodhist_histories, nmodpath_histories;
// insert an item into an array without duplication. used for
// building the _indices arrays
int insert (int *v, int *n, int x) {
for (int i=0; i<*n; i++) if (v[i] == x) return i;
v[(*n)] = x;
return (*n)++;
}
// analyze the specification of the features to see what the
// various maximum history lengths are, how much space is taken by
// various history structures, and how much space is left over in
// the hardware budget for tables
void analyze_spec (void) {
bool
// true if at least one feature requires the recency stack
doing_recency = false,
// true if at least one feature uses local history
doing_local = false;
int
// how many bits are allocated to the IMLI counters
imli_counter_bits[4];
// initially assume a recency stack of depth 0
assoc = 0;
// accounting for bits for the blurry path history
int blurrypath_bits[MAX_BLURRY][MAX_BLURRY2];
// accouting for the bits for the acyclic path history
bool acyclic_bits[MAX_ACYCLIC][32][2];
// set these accounting arrays to 0 initially
memset (blurrypath_bits, 0, sizeof (blurrypath_bits));
memset (imli_counter_bits, 0, sizeof (imli_counter_bits));
memset (acyclic_bits, 0, sizeof (acyclic_bits));
// initially assume very short histories, later find out
// what the maximum values are from the features
ghist_length = 1;
modghist_length = 1;
nmodhist_histories = 0;
path_length = 1;
// go through each feature in the specification finding the requirements
for (int i=0; i<num_tables; i++) {
// find the maximum associativity of the recency stack required
if (specs[i].type == RECENCY || specs[i].type == RECENCYPOS) {
if (assoc < specs[i].p1) assoc = specs[i].p1;
}
// find out how much and what kind of history is needed for acyclic feature
if (specs[i].type == ACYCLIC) {
for (int j=0; j<specs[i].p1+2; j++) {
acyclic_bits[specs[i].p1][j][!specs[i].p3] = true;
}
}
// do we need local history?
if (specs[i].type == LOCAL) doing_local = true;
// how many IMLI counter bits do we need for different versions of IMLI
if (specs[i].type == IMLI) imli_counter_bits[specs[i].p1-1] = 32;
// do we require a recency stack?
if (specs[i].type == RECENCY || specs[i].type == RECENCYPOS) doing_recency = true;
// count blurry path bits (assuming full 32-bit addresses less shifted bits)
if (specs[i].type == BLURRYPATH) for (int j=0; j<specs[i].p2; j++) blurrypath_bits[specs[i].p1][j] = 32 - specs[i].p1;
// if we are doing modulo history, figure out which and how much history we need
if (specs[i].type == MODHIST || specs[i].type == GHISTMODPATH) {
int j = insert (modhist_indices, &nmodhist_histories, specs[i].p1);
if (modhist_lengths[j] < specs[i].p2+1) modhist_lengths[j] = specs[i].p2 + 1;
if (specs[i].p2 >= modghist_length) modghist_length = specs[i].p2+1;
}
}
// figure out how much history we need for modulo path, modulo+global path, and regular path
nmodpath_histories = 0;
for (int i=0; i<num_tables; i++) {
if (specs[i].type == MODPATH || specs[i].type == GHISTMODPATH) {
int j = insert (modpath_indices, &nmodpath_histories, specs[i].p1);
if (modpath_lengths[j] < specs[i].p2+1) modpath_lengths[j] = specs[i].p2 + 1;
if (path_length <= specs[i].p2) path_length = specs[i].p2 + 1;
}
if (specs[i].type == PATH) {
if (path_length <= specs[i].p1) path_length = specs[i].p1 + 1;
}
}
// how much global history and global path history do we need
for (int i=0; i<num_tables; i++) {
switch (specs[i].type) {
case GHIST:
if (ghist_length <= specs[i].p2) ghist_length = specs[i].p2 + 1;
break;
case GHISTPATH:
if (ghist_length <= specs[i].p1) ghist_length = specs[i].p1 + 1;
if (path_length <= specs[i].p1) path_length = specs[i].p1 + 1;
break;
default: ;
}
}
// sanity check
assert (ghist_length <= MAX_GHIST);
assert (modghist_length <= MAX_GHIST);
// account for IMLI counters
for (int i=0; i<4; i++) totalbits += imli_counter_bits[i];
// account for ghist bits
totalbits += ghist_length;
// account for global path bits (represented as an array of 16 bit integers)
totalbits += path_length * 16;
// account for misprediction monitoring counters if the "threshold" idea is being used
if (threshold >= 0) totalbits += tunebits * num_tables;
// plus one bit to record each prediction (can avoid by recomputing)
// totalbits += num_tables;
// account for modulo history bits
for (int i=0; i<nmodhist_histories; i++) totalbits += modhist_lengths[i];
// account for modulo path bits
for (int i=0; i<nmodpath_histories; i++) totalbits += 16 * modpath_lengths[i];
// account for local histories
if (doing_local) totalbits += local_history_length * nlocal_histories;
// account for recency stack
if (doing_recency) totalbits += assoc * 16;
// account for blurry path bits
for (int i=0; i<MAX_BLURRY; i++) for (int j=0; j<MAX_BLURRY2; j++) totalbits += blurrypath_bits[i][j];
// account for filter bits
totalbits += num_filter * 2;
// account for acyclic bits
for (int i=0; i<MAX_ACYCLIC; i++) for (int j=0; j<32; j++) for (int k=0; k<2; k++) totalbits += acyclic_bits[i][j][k];
// how many bits are left for the tables?
int remaining = budgetbits - totalbits;
// count the tables that have already been assigned sizes
int num_sized = 0;
for (int i=0; i<num_tables; i++) {
if (table_sizes[i] != 0) {
int sz = table_sizes[i] * (specs[i].width + (n_sign_bits-1));
totalbits += sz;
remaining -= sz;
num_sized++;
}
}
// whatever is left, we divide among the rest of the tables
int table_size_bits = (remaining / (num_tables-num_sized));
for (int i=0; i<num_tables; i++) {
// if a table doesn't have a size yet, give it one and count those bits
if (!table_sizes[i]) {
int my_table_size = table_size_bits / (specs[i].width + (n_sign_bits-1)); // extra sign bits
table_sizes[i] = my_table_size;
totalbits += my_table_size * (specs[i].width + (n_sign_bits-1));
}
}
#ifdef VERBOSE
printf ("%d bits of metadata so far, %d left out of %d total budget\n", totalbits, remaining, budgetbits);
printf ("table size is %d bits, ", table_size_bits);
int my_table_size = table_size_bits / (5 + (n_sign_bits-1));
printf ("%d entries for 5 bit, ", my_table_size);
my_table_size = table_size_bits / (6 + (n_sign_bits-1));
printf ("%d entries for 6 bit\n", my_table_size);
printf ("%d total bits (%0.2fKB)\n", totalbits, totalbits / 8192.0);
#endif
}
// these variables set from parameters
history_spec *specs;
int
num_tables, // number of features (each one gets its own table)
budgetbits, // hardware budget in bits
nlocal_histories, // number of local histories
local_history_length, // local history length
theta; // initial threshold for adaptive theta adjusting
double
fudge; // fudge factor to multiply by perceptron output
int
*xlat, // transfer function for 6-bit weights (5-bit magnitudes)
*xlat4, // transfer function for 5-bit weights (4-bit magnitudes)
pcbit, // bit from the PC to use for hashing global history
pcshift, // shift for hashing PC
block_size, // number of ghist bits in a "block"; this is the width of an initial hash of ghist
num_filter; // number of entries in the filter; 0 means don't use the filter
bool
hash_taken; // should we hash the taken/not taken value with a PC bit?
int
n_sign_bits, // number of sign bits per magnitude
table_sizes[MAX_TABLES],// size of each table
extra_rounds; // number of extra rounds of training a single weight on a low-confidence prediction
unsigned int
record_mask; // which histories are updated with filtered branch outcomes
int
speed, // speed for adaptive theta training
threshold, // threshold for deciding low/high confidence
tunebits; // number of bits in misprediction counters
bool
tuneonly; // if true, only count mispredictions of low-confidence branches
int
nbest, // use this many of the top performing tables on a low-confidence branch
bias0, // bias perceptron output this much on all-bits-zero local history
bias1, // bias perceptron output this much on all-bits-one local history
biasmostly0, // bias perceptron output this much on almost-all-bits-zero local history
biasmostly1, // bias perceptron output this much on almost-all-bits-one local history
hshift; // how much to shift initial feauture hash before XORing with PC bits
int
decay; // whether and how often to decay a random weight
unsigned long long int
imli_mask1, // which tables should have their indices hashed with the first IMLI counter
imli_mask4, // which tables should have their indices hashed with the fourth IMLI counter
recencypos_mask; // which tables should have their indices hashed with the recency position
// constructor; parameters described above
multiperspective_perceptron (
history_spec *_specs, int _num_tables = 38, int _budgetbits = 65536 * 8 + 2048, int _nlocal_histories = 512,
int _local_history_length = 11, int _theta = 10, double _fudge = 0.26, int *_xlat = NULL, int *_xlat4 = NULL,
int _pcbit = 2, int _pcshift = -10, int _block_size = 21, int _num_filter = 18000, bool _hash_taken = false,
int _n_sign_bits = 2, int _extra_rounds = 1, unsigned int _record_mask= 191, int _speed = 9, int _threshold = 0,
int _tunebits = 30, bool _tuneonly = false, int _nbest = 1, int _bias0 = 0, int _bias1 = 0, int _biasmostly0 = 0,
int _biasmostly1 = 0, int _hshift = 0, int _decay = 0, unsigned long long int _imli_mask1 = 0,
unsigned long long int _imli_mask4 = 0, unsigned long long int _recencypos_mask = 0) :
specs(_specs), num_tables(_num_tables), budgetbits(_budgetbits), nlocal_histories(_nlocal_histories),
local_history_length(_local_history_length), theta(_theta), fudge(_fudge), xlat(_xlat), xlat4(_xlat4), pcbit(_pcbit), pcshift(_pcshift),
block_size(_block_size), num_filter(_num_filter), hash_taken(_hash_taken), n_sign_bits(_n_sign_bits), extra_rounds(_extra_rounds),
record_mask(_record_mask), speed(_speed), threshold( _threshold), tunebits(_tunebits), tuneonly(_tuneonly), nbest(_nbest), bias0(_bias0),
bias1(_bias1), biasmostly0(_biasmostly0), biasmostly1(_biasmostly1), hshift(_hshift), decay(_decay), imli_mask1(_imli_mask1),
imli_mask4(_imli_mask4), recencypos_mask (_recencypos_mask) {
// initially nothing in the filter
occupancy = 0;
// copy sizes of tables from feature specifications into a convenient array
for (int i=0; i<num_tables; i++) table_sizes[i] = specs[i].size;
// no bits used so far
totalbits = 0;
// seed the random number generator
my_seed = 0xdeadbeef;
// initialize various structures to 0
imli_counter1 = 0;
imli_counter2 = 0;
imli_counter3 = 0;
imli_counter4 = 0;
memset (modhist_lengths, 0, sizeof (modhist_lengths));
memset (modpath_lengths, 0, sizeof (modpath_lengths));
// analyze the specification to figure out how many bits are allocated to what
analyze_spec ();
// set more structures to 0
memset (ghist_words, 0, sizeof (ghist_words));
memset (tables, 0, sizeof (tables));
memset (sign_bits, 0, sizeof (sign_bits));
memset (acyclic_histories, 0, sizeof (acyclic_histories));
memset (acyclic2_histories, 0, sizeof (acyclic2_histories));
memset (local_histories, 0, sizeof (local_histories));
memset (mod_histories, 0, sizeof (mod_histories));
memset (modpath_histories, 0, sizeof (modpath_histories));
memset (recency_stack, 0, sizeof (recency_stack));
memset (path_history, 0, sizeof (path_history));
memset (blurrypath_histories, 0, sizeof (blurrypath_histories));
// threshold counter for adaptive theta training
tc = 0;
memset (mpreds, 0, sizeof (mpreds));
// initialize tables and signs
for (int i=0; i<num_tables; i++) {
for (int j=0; j<table_sizes[i]; j++) {
for (int k=0; k<n_sign_bits; k++)
sign_bits[i][j][k] = (i & 1) | (k & 1);
tables[i][j] = 0;
}
}
}
// insert a (shifted) PC into the recency stack with LRU replacement
void insert_recency (unsigned int pc) {
int i = 0;
for (i=0; i<assoc; i++) {
if (recency_stack[i] == pc) break;
}
if (i == assoc) {
i = assoc-1;
recency_stack[i] = pc;
}
int j;
unsigned int b = recency_stack[i];
for (j=i; j>=1; j--) recency_stack[j] = recency_stack[j-1];
recency_stack[0] = b;
}
// hash a PC
unsigned int hash_pc (unsigned int pc) {
if (pcshift < 0) {
return hash (pc, -pcshift);
} else if (pcshift < 11) {
unsigned int x = pc;
x ^= (pc >> pcshift);
return x;
} else {
return pc >> (pcshift-11);
}
}
// hash global history from position a to position b
unsigned int hash_ghist (int a, int b) {
unsigned int x = 0;
// am is the next multiple of block_size after a
int am = (((a/block_size)*block_size)+block_size);
// bm is the previous multiple of block_size before b
int bm = (b/block_size)*block_size;
// the 0th bit of ghist_words[a/block_size] is the most recent bit.
// so the number of bits between a and am is the number to shift right?
// start out x as remainder bits from the beginning:
// x = [ . . . . . b b b b b ]
x += ghist_words[a/block_size] >> (a-am);
// add in bits from the middle
for (int i=am; i<bm; i+=block_size)
x += ghist_words[i/block_size];
// add in remainder bits from end:
// x += [ b b b b b . . . . . ]
unsigned int y = ghist_words[bm/block_size] & ((1<<(b-bm))-1);
x += y << (block_size-(b-bm));
return x;
}
// hash the items in the recency stack to a given depth, shifting
// by a certain amount each iteration, with two different ways of
// mixing the bits
unsigned int hash_recency (int depth, int shift, int style) {
if (style == -1) {
unsigned int x = 0;
for (int i=0; i<depth; i++) {
x <<= shift;
x += recency_stack[i];
}
return x;
} else {
unsigned int x = 0, k = 0;
for (int i=0; i<depth; i++) {
x ^= (!!(recency_stack[i] & (1<<shift))) << k;
k++;
k %= block_size;
}
return x;
}
}
// hash the "blurry" path history of a given scale to a given
// depth and given shifting parameter
unsigned int hash_blurry (int scale, int depth, int shiftdelta) {
if (shiftdelta == -1) shiftdelta = 0;
int sdint = shiftdelta >> 2;
int sdfrac = shiftdelta & 3;
unsigned int x = 0;
int shift = 0;
int count = 0;
for (int i=0; i<depth; i++) {
x += blurrypath_histories[scale][i] >> shift;
count++;
if (count == sdfrac) {
shift += sdint;
count = 0;
}
}
return x;
}
// hahs path history of a given depth, with a given shift, and in
// two different mixing styles
unsigned int hash_path (int depth, int shift, int style) {
if (style == -1) {
unsigned int x = 0;
for (int i=0; i<depth; i++) {
x <<= shift;
x += path_history[i];
}
return x;
} else {
unsigned int x = 0;
int bm = (depth/block_size)*block_size;
for (int i=0; i<bm; i+=block_size) {
for (int j=0; j<block_size; j++)
x ^= (!!(path_history[i+j] & (1<<shift))) << j;
}
int k = 0;
for (int i=bm; i<depth; i++)
x ^= (!!(path_history[i] & (1<<shift))) << k++;
return x;
}
}
// hash ghist together with path history, with a given shift
// and with two different mixing styles
unsigned int hash_ghistpath (int depth, int shift, int style) {
if (style == -1) {
unsigned int x = 0;
int bm = (depth/block_size)*block_size;
unsigned int w;
for (int i=0; i<bm; i+=block_size) {
w = ghist_words[i/block_size];
for (int j=0; j<block_size; j++) {
x <<= shift;
x += (path_history[i+j] << 1) | (w & 1);
w >>= 1;
}
}
w = ghist_words[bm/block_size];
for (int i=bm; i<depth; i++) {
x <<= shift;
x += (path_history[i] << 1) | (w & 1);
w >>= 1;
}
return x;
} else {
unsigned int x = 0;
int bm = (depth/block_size)*block_size;
unsigned int w = 0;
for (int i=0; i<bm; i+=block_size) {
w = ghist_words[i/block_size];
for (int j=0; j<block_size; j++) {
x ^= (!!(path_history[i+j] & (1<<shift))) << j;
x ^= (w & 1) << j;
w >>= 1;
}
}
w = ghist_words[bm/block_size];
int k = 0;
for (int i=bm; i<depth; i++) {
x ^= (!!(path_history[i] & (1<<shift))) << k;
x ^= (w & 1) << k;
w >>= 1;
k++;
}
return x;
}
}
// an alternate formulation of ghistpath: using ranges of path
// and ghist
unsigned int hash_sghistpath (int a, int b, int shift) {
unsigned int x = 0;
int bm = (b/block_size)*block_size;
unsigned int w;
for (int i=a; i<bm; i+=block_size) {
w = ghist_words[i/block_size];
for (int j=0; j<block_size; j++) {
x <<= shift;
x += (path_history[i+j] << 1) | (w & 1);
w >>= 1;
}
}
w = ghist_words[bm/block_size];
for (int i=bm; i<b; i++) {
x <<= shift;
x += (path_history[i] << 1) | (w & 1);
w >>= 1;
}
return x;
}
// hash acyclic histories with a given modulus, shift, and style
unsigned int hash_acyclic (int a, int shift, int style) {
unsigned int x = 0;
if (style == -1) {
unsigned int k = 0;
for (int i=0; i<a+2; i++) {
x ^= acyclic_histories[a][i] << k;
k++;
k %= block_size;
}
} else {
for (int i=0; i<a+2; i++) {
x <<= shift;
x += acyclic2_histories[a][i];
}
}
return x;
}
// hash modulo history with a given modulus, length, and style
unsigned int hash_modhist (int a, int b, int n) {
unsigned int x = 0, k = 0;
for (int i=0; i<b; i++) {
x ^= mod_histories[a][i] << k;
k++;
k %= n;
}
return x;
}
// hash modulo path history with a given modulus, depth, and shift
unsigned int hash_modpath (int a, int depth, int shift) {
unsigned int x = 0;
for (int i=0; i<depth; i++) {
x <<= shift;
x += modpath_histories[a][i];
}
return x;
}
// hash modulo path history together with modulo (outcome) history
unsigned int hash_ghistmodpath (int a, int depth, int shift) {
unsigned int x = 0;
for (int i=0; i<depth; i++) {
x <<= shift;
x += (modpath_histories[a][i] << 1) | mod_histories[a][i];
}
return x;
}
// hash the recency position where we find this PC
unsigned int hash_recencypos (unsigned int pc, int l, int t) {
// search for the PC
for (int i=0; i<l; i++) {
if (recency_stack[i] == pc) return i * table_sizes[t] / l;
}
// return last index in table on a miss
return table_sizes[t] - 1;
}
// use a history specification to call the corresponding history hash function
unsigned int get_hash (history_spec *s, unsigned int pc, unsigned int pc2, int t) {
unsigned int x = 0;
switch (s->type) {
case SGHISTPATH:
x = hash_sghistpath (s->p1, s->p2, s->p3);
break;
case GHIST:
x = hash_ghist (s->p1, s->p2);
break;
case GHISTPATH:
x = hash_ghistpath (s->p1, s->p2, s->p3);
break;
case ACYCLIC:
x = hash_acyclic (s->p1, s->p2, s->p3);
break;
case MODHIST:
x = hash_modhist (s->p1, s->p2, block_size);
break;
case GHISTMODPATH:
x = hash_ghistmodpath (s->p1, s->p2, s->p3);
break;
case MODPATH:
x = hash_modpath (s->p1, s->p2, s->p3);
break;
case BIAS:
x = 0;
break;
case RECENCY:
x = hash_recency (s->p1, s->p2, s->p3);
break;
case IMLI:
if (s->p1 == 1) x = imli_counter1;
else if (s->p1 == 2) x = imli_counter2;
else if (s->p1 == 3) x = imli_counter3;
else if (s->p1 == 4) x = imli_counter4;
else assert (0);
break;
case PATH:
x = hash_path (s->p1, s->p2, s->p3);
break;
case LOCAL:
x = local_histories[hashlocal() % nlocal_histories];
if (s->p1 != -1) x &= ((1<<s->p1)-1);
break;
case BLURRYPATH:
x = hash_blurry (s->p1, s->p2, s->p3);
break;
case RECENCYPOS:
x = hash_recencypos (pc2, s->p1, t);
break;
default: assert (0);
}
return x;
}
// for quicksort
typedef int (*compar_t)(const void *, const void *);
// find the best performing subset of features (estimated by
// sorting by mispredictionr rate)
void findbest (int *bestpreds) {
if (threshold < 0) return;
bestpair pairs[num_tables];
for (int i=0; i<num_tables; i++) {
pairs[i].index = i;
pairs[i].mpreds = mpreds[i];
}
qsort (pairs, num_tables, sizeof (bestpair), (compar_t) paircmp);