Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
//powee function declaration
float powe(float x,float y)
{
if(y==0)
return 1;
else
return(x*powe(x,y-1));
}
//bin to decimal conv function declaration
bin2dec(int *dec,int k,int *bp)
{
*dec=0;
int i;
for(i=k-1;i>=0;i--)
{*dec=*dec+(*(bp+i))*powe(2,k-1-i);}
return(1);
}
//main function
int main(int argc,char *argv[])
{
int r;
char type1[] = "GAg";
char type2[] = "GAp";
char type3[] = "PAg";
char type4[] = "Gshare";
//assigning 'r' a value which is based on the type of scheme required here
if(strcmp(argv[2],type1)==0)
r=1;
if(strcmp(argv[2],type2)==0)
r=2;
if(strcmp(argv[2],type3)==0)
r=3;
if(strcmp(argv[2],type4)==0)
r=4;
switch(r)
{
case 1: //GAg scheme
{
int i,k,j;
float hitrate;
k=atoi(argv[4]);
j=powe(k,2);
int bht[k], pht[j];
int branchaddr, targetaddr, tnt, dec;
int prediction;
//initialising the history bits to 0 and PHT entries to 01 as given
for(i=0;i<k;i++)
{bht[i]=0;}
for(i=0;i<j;i++)
{pht[i]=1;}
//accessing the given text file
FILE *fp1, *fp2;
int total=0,wrong=0,correct=0;
fp1=fopen("history.txt", "r");
fp2=fopen("output.txt", "w");
while(fscanf(fp1,"%x %x %d",&branchaddr,&targetaddr,&tnt) != EOF)
{
i=bin2dec(&dec,k,bht);
//doing a prediction here
if(pht[dec]==0 || pht[dec]==1)
{prediction==0;}
if(pht[dec]==2 || pht[dec]==3)
{prediction==1;}
total=total+1;
//checking if my prediction is ryt and inc the corresponding var
if(prediction==tnt)
{correct++;}
if(prediction!=tnt)
{wrong++;}
//am updating the counters based on the tnt values given.
if(tnt==1)
{
if(pht[dec]<3)
{pht[dec]++;}
}
if(tnt==0)
{
if(pht[dec]>0)
{pht[dec]--;}
}
//updating the bht-usng ryt shift reg
for(i=0; i<=k-1; i++)
{bht[i]=bht[i+1];
bht[k-1]=tnt;
}
i=bin2dec(&dec,k,bht);
fprintf(fp2,"%d %d\t %d\t %d %d %d %d\n",tnt,prediction,dec,pht[0],pht[1],pht[2],pht[3] );
}
//printing the outputs
printf("Total no of predictions made\n%d",total);
printf("no of correct\n%d no of wrong \n%d",correct, wrong);
hitrate=(correct/total)*100;
printf("hitrate is\n%f", hitrate);
break;
}
case 2://GAp scheme
{
int i,j,k,s,x,temp,n;
int prediction;
int total,correct,wrong;
float hitrate;
FILE *fp1,*fp2;
int branchaddr,targetaddr,tnt,dec;
k=atoi(argv[4]);
s=atoi(argv[6]);
j=powe(2,k);
x=s/j;
int bht[k];
int pht[j][x];
//intialising all the entries of pht to 1
for(i=0;i<j;i++)
{
for(n=0;n<x;n++)
{
pht[i][n]==0;
}
}
//initialising all the entries of bht to 0
for(i=0;i<k;i++)
{bht[i]=0;}
//accessing the text file
fp1=fopen("history.txt","r");
fp2=fopen("output.txt","w");
while (fscanf(fp1,"%x %x %d",&branchaddr,&targetaddr,&tnt) != EOF)
{
if(k==2 && s==32)
{temp=branchaddr&7;}
if(k==2 && s==64)
{temp=branchaddr&15;}
if(k==2 && s==128)
{temp=branchaddr&31;}
if(k==4 && s==32)
{temp=branchaddr&1;}
if(k==4 && s==64)
{temp=branchaddr&3;}
if(k==4 && s==128)
{temp=branchaddr&7;}
i=bin2dec(&dec,k,bht);
//doing a prediction here
if(pht[dec][temp]==0 || pht[dec][temp]==1)
{prediction==0;}
if(pht[dec][temp]==2 || pht[dec][temp]==3)
{prediction==1;}
total=total+1;
//checking if my prediction is ryt and inc the corresponding var
if(prediction==tnt)
{correct++;}
if(prediction!=tnt)
{wrong++;}
//am updating the counters based on the tnt values given.
if(tnt==1)
{
if(pht[dec][temp]<3)
{pht[dec][temp]++;}
}
if(tnt==0)
{
if(pht[dec][temp]>0)
{pht[dec][temp]--;}
}
//updating the bht-usng ryt shift reg
for(i=0; i<=k-1; i++)
{bht[i]=bht[i+1];}
bht[k-1]=tnt;
//printing the outputs
printf("Total no of predictions made\n%d",total);
printf("no of correct\n%d no of wrong \n%d",correct, wrong);
hitrate=(correct/total)*100;
printf("hitrate is\n%f", hitrate);
break;
}
case 3:
{
int i,j,k,s,temp,n;
int prediction;
int total,correct,wrong;
float hitrate;
FILE *fp1,*fp2;
int branchaddr,targetaddr,tnt,dec;
k=atoi(argv[4]);
s=atoi(argv[6]);
j=powe(2,k);
int pht[j];
temp=s/k;
int bht[temp][k];
//initialising the history bits to 0 and PHT entries to 01 as given
for(i=0;i<temp;i++)
{
for(n=0;n<k;n++)
{
bht[i][n]=0;
}
}
for(i=0;i<j;i++)
{pht[i]=1;}
//accessing the text file
fp1=fopen("history.txt","r");
fp2=fopen("output.txt","w");
while (fscanf(fp1,"%x %x %d",&branchaddr,&targetaddr,&tnt) != EOF)
{
if(s==32)
{temp=branchaddr&31;}
if(s==64)
{temp=branchaddr&63;}
if(s==128)
{temp=branchaddr&127;}
bin2dec(&dec,k,&bht[temp][0]);
int index=dec;
if(pht[index]==0 || pht[index]==1)
{prediction=0;}
if(pht[index]==2 || pht[index]==3)
{prediction=1;}
total=total+1;
if(tnt==prediction)
correct++;
if(tnt!=prediction)
wrong++;
//am updating the counters based on the tnt values given.
if(tnt==1)
{
if (pht[index]<3)
{
pht[index]++;
}
}
if(tnt==0)
{
if(pht[index]>0)
{pht[index]--;
}
}
//updating the bht-usng ryt shift reg
for(i=0;i<k-1;i++)
{bht[temp][i]=bht[temp][i+1];}
bht[temp][k-1]=tnt;
bin2dec(&dec,k,&bht[temp][0]);
//printing the outputs
printf("Total no of predictions made\n%d",total);
printf("no of correct\n%d no of wrong \n%d",correct, wrong);
hitrate=(correct/total)*100;
printf("hitrate is\n%f", hitrate);
break;
}
case 4:
{
int i,j,k,temp,n;
int prediction;
int total,correct,wrong;
int index;
float hitrate;
FILE *fp1,*fp2;
int branchaddr,targetaddr,tnt,dec;
k=atoi(argv[4]);
j=powe(2,k);
int pht[j],bht[k];
//initialising the history bits to 0 and PHT entries to 01 as given
for(i=0;i<k;i++)
{bht[i]=0;}
for(i=0;i<j;i++)
{pht[i]=1;}
while (fscanf(fp1,"%x %x %d",&branchaddr,&targetaddr,&tnt) != EOF)
{
if(k==4)
{temp=branchaddr&15;}
if(k==6)
{temp=branchaddr&63;}
if(k==7)
{temp=branchaddr&127;}
index=temp^dec;
//doing a prediction here
if(pht[index]==0 || pht[index]==1)
{prediction=0;}
if(pht[index]==2 || pht[index]==3)
{prediction=1;}
total=total+1;
if(tnt==prediction)
correct++;
if(tnt!=prediction)
wrong++;
//am updating the counters based on the tnt values given.
if(tnt==1)
{
if (pht[index]<3)
{
pht[index]++;
}
}
if(tnt==0)
{
if(pht[index]>0)
{pht[index]--;
}
}
//updating the bht-usng ryt shift reg
for(i=0;i<k-1;i++)
{bht[i]=bht[i+1];}
bht[k-1]=tnt;
i=bin2dec(&dec,k,bht);
}
//printing the outputs
printf("Total no of predictions made\n%d",total);
printf("no of correct\n%d no of wrong \n%d",correct, wrong);
hitrate=(correct/total)*100;
printf("hitrate is\n%f", hitrate);
break;
}
}
}
Look CAREFULLY!