⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mybp.c

📁 This code : implemetation of branch predictors gag gap gshare
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
/* 
	11=ST=3
    10=WT=2
    01=WN=1
    00=SN=0 */



void initialize_pht(int);
int power_two(int);
int hist_bits;
int correct_predictions, mispredictions;
int *pht_table;
int bhr;
int pht_table_size;
int gshare_bits,gsel_bits;
FILE *f1;

int main()
{
  int t_or_nt, addr_br_instr, br_target,masking_val_addr;
  int predicted_taken, pht_index, masking_value;
  int addr_br_lowerbits,bhr_lowerbits,bhr_upperbits;
  printf("\n Enter Gshare bits");
  scanf("%d",&gshare_bits);
  printf("\n Enter Gsel bits");
  scanf("%d",&gsel_bits);
  hist_bits=gshare_bits+gsel_bits;
  masking_value = power_two(hist_bits) - 1;
  masking_val_addr=power_two(gshare_bits)-1;
  pht_table_size=power_two(hist_bits);
  f1 = fopen("history.txt", "r")

    if(f1 == NULL)
    {
		printf("FILE NOT OPENED\n");
	}
   	
    initialize_pht(pht_table_size);
	printf("\n The size of PHT %d",pht_table_size);
    bhr=0;
    while(fscanf(f1, "%x %x %d", &addr_br_instr, &br_target, &t_or_nt) == 3)
	{
     	addr_br_lowerbits=addr_br_instr & masking_val_addr;
		bhr_lowerbits=bhr & masking_val_addr;
		bhr_upperbits= (bhr>>gshare_bits)<<gsel_bits;
		pht_index=bhr_upperbits+(addr_br_lowerbits ^ bhr_lowerbits);
		
		if(pht_table[pht_index] >= 2)
		{
			/* Predicted TAKEN if counter value >= 2 */
			predicted_taken = 1;
		}
		else
		{
			/* Predicted NOT TAKEN if counter value < 2 */
			predicted_taken = 0;
		}

		/* Check predicted value against actual branch outcome and increment correct_predictions or mispredictions */
		if (predicted_taken == t_or_nt)
		{
			correct_predictions++;
		}
		else
		{
			mispredictions++;
		}
		/* Modify contents of BHR based on actual outcome */
		bhr = (bhr << 1) + t_or_nt;

		/* Mask the new value of BHR so that we retain only hist_bits in BHR */
		bhr= (bhr) & masking_value;

		/* Increment or decrement the corresponding PHT entry depending on branch outcome */
		if (t_or_nt)
		{
			pht_table[pht_index] = pht_table[pht_index] + 1;
		}
		else
		{
			pht_table[pht_index] = pht_table[pht_index] - 1;
		}
		//saturate counter 
		if (pht_table[pht_index] > 3)
		{
			pht_table[pht_index] = 3;
		}
		else if (pht_table[pht_index] < 0)
		{
			pht_table[pht_index] = 0;
		}
           
	}
	printf("\nGshare+Gsel \n");
	
	printf("Number of Correct Predictions = %d\n", correct_predictions);
	printf("Number of Mispredictions = %d\n", mispredictions);
	printf("Total Predictions = %d\n", correct_predictions + mispredictions);
	printf("Misprediction Rate = %f\n%%", (float) mispredictions * 100.0 / ((float)correct_predictions + (float)mispredictions));
    fclose(f1);
    return 0;
}

void initialize_pht(int num_entries)
{
	int count;

	/* Allocating memory for the PHT entries */
	pht_table = malloc(num_entries * sizeof(int));
	//printf("num_entries: %d",num_entries);
	for (count = 0; count < num_entries; count++)
	{
		/* Initializing all counters to 10 */
		pht_table[count] = 2;
	}
}

int power_two(int num)
{
	int power_of_two = 1, count;

	for (count = 0; count < num; count++)
	{
		power_of_two *= 2;

	}

	return (power_of_two);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -