/* +-------------------------------------+ | Fibonacci to Galois NLFSR optimizer | | v1.1 - March 23, 2009 | | | | Jean-Michel Chabloz | | chabloz@kth.se | | KTH, Stockholm, Sweden | +-------------------------------------+ */ #include #include #include #include #define MAX_INPUT_SIZE 1024 /* maximum number of characters in the input string */ #define XOR_WEIGHT 2 /* weight of the XOR compared to AND */ #define FREEBINS_WEIGHT 1 /* importance given to the number of free bins while choosing which product should be placed next */ #define AHCOST_WEIGHT 1 /* importance given to the average hypotetical cost while choosing which product should be placed next */ #define PLACEMENT 1 /* 1 to activate deterministic AHCOST-FREEBINS placement, 0 to deactivate */ #define RANDOMPLACEMENT 0 /* 1 to activate random placement, 0 to deactivate */ #define INTPOSTPLACEMENT 1 /* 1 to activate intelligent postplacement, 0 to deactivate */ #define TECHNOLOGY 1 /* 0 - no critical path estimation, 1 - TSMC 90 nm */ #define VERBOSE 1 /* 1 for verbose output, 0 for non-verbose output */ typedef struct { int nterms; int *terms; int min_bin; double cost; int bin; int freebins; double ahcost; } prod; typedef struct { int nprods; int allowed; } bin; /* function prototypes */ int print_galois(prod*, bin*, int, int); double calculate_cost(int, int, prod*, bin*, int); int main(void); /* prints to screen the Galois configuration */ int print_galois(prod* products, bin* bins, int nprods, int nbins) { int index; int i; int j; int k; int first; int elements; first=1; for(i=nbins;i>=0;i--) { elements=0; for(k=0;k0) { printf("f%d =",i); if(i=0;i--) { elements=0; for(k=0;k0) { if(i!=nbins) elements++; //printf("f%d: %d products, cost %.2f\n",i,elements,calculate_cost(nprods,nbins,products,bins,i)); } } return 1; } /* calculates the cost of a single bin by doing a two-input AND/XOR optimal synthesis */ double calculate_cost(int np, int nb, prod* products, bin* bins, int bin_id) { int i; int j; int iter; int best1; int best2; int nprods; double* costs; nprods=bins[bin_id].nprods; costs=(double*)malloc((nprods)*sizeof(double)); if(bin_id==nb) j=0; else { costs[0]=0; j=1; } for(i=0;i=0) { if(best1==-1) best1=i; else { if( (best1!=-1) && (best2==-1) ) { if(costs[best1]<=costs[i]) best2=i; else { best2=best1; best1=i; } } else { if( (costs[i]<=costs[best1]) ) { best2=best1; best1=i; } else if( (costs[i]>costs[best1]) && (costs[i]1) { min=products[i].terms[0]; max=products[i].terms[0]; for(j=1;jmax) max=products[i].terms[j]; } if(tau<(max-min)) tau=(max-min); } } for(i=0;iproducts[i].terms[j]) min=products[i].terms[j]; if((nbins-min)=products[i].min_bin;j--) if(bins[j].allowed) products[i].freebins++; } //if(VERBOSE) printf("--------------------------------------------------------------------------------\n"); //if(VERBOSE) printf("Found %d products, tau is %d\n",nprods, tau); /* ------------------------------------ */ /* Deterministic Initial placement */ /* ------------------------------------ */ if(PLACEMENT) { unplaced_prods=nprods; //if(VERBOSE) printf("--------------------------------------------------------------------------------\n"); //if(VERBOSE) printf("Placing all products with a single allowed position\n"); for(i=0;i0;unplaced_prods--) { /* calculate average hypothetical costs, worst average hypotetical cost and worst free bins*/ max_ahcost=-1; max_freebins=nbins; for(i=0;i=products[i].min_bin;j--) if(bins[j].allowed) { products[i].bin=j; bins[j].nprods++; products[i].ahcost+=calculate_cost(nprods,nbins,products,bins,j); bins[j].nprods--; } products[i].ahcost=(products[i].ahcost/(products[i].freebins)); if(products[i].ahcost>max_ahcost) max_ahcost=products[i].ahcost; if(products[i].freebins max_tcost) ) { max_tcost=(products[i].ahcost/max_ahcost)*AHCOST_WEIGHT + (max_freebins/products[i].freebins)*FREEBINS_WEIGHT; priority_prod=i; } /* place the priority product in the best bin */ for(i=nbins;i>=products[priority_prod].min_bin;i--) if(bins[i].allowed) { oldbin=products[priority_prod].bin; products[priority_prod].bin=i; bins[i].nprods++; cost=calculate_cost(nprods,nbins,products,bins,i); bins[i].nprods--; products[priority_prod].bin=oldbin; if(i==nbins) { min_cost=cost; products[priority_prod].bin=i; } if(cost<=min_cost) { min_cost=cost; products[priority_prod].bin=i; } } (bins[products[priority_prod].bin].nprods)++; //if(VERBOSE) printf("--------------------------------------------------------------------------------\n"); //if(VERBOSE) printf("Product %d has been placed in f%d\n\n",priority_prod, products[priority_prod].bin); //if(VERBOSE) print_galois(products, bins, nprods, nbins); } } /* ------------------------------------ */ /* Random Initial placement */ /* ------------------------------------ */ if(RANDOMPLACEMENT) { //if(VERBOSE) printf("--------------------------------------------------------------------------------\n"); //if(VERBOSE) printf("Starting Random placement \n"); srand(time(NULL)); for(i=0;(i=products[i].min_bin) ) ok=1; } products[i].bin=r; bins[r].nprods++; //if(VERBOSE) printf("--------------------------------------------------------------------------------\n"); //if(VERBOSE) printf("Product %d has been placed in f%d\n\n",i, products[i].bin); //if(VERBOSE) print_galois(products, bins, nprods, nbins); } } /* ------------------------------------ */ /* Post-placement optimization */ /* ------------------------------------ */ if(INTPOSTPLACEMENT) { //if(VERBOSE) printf("--------------------------------------------------------------------------------\n"); //if(VERBOSE) printf("Starting postplacement optimization \n"); movedsome=1; while(movedsome) { movedsome=0; /* calculate maximum bin cost */ max_cost=-1; for(i=0;i<=nbins;i++) if((bins[i].allowed) && (calculate_cost(nprods,nbins,products,bins,i)>max_cost) ) max_cost=calculate_cost(nprods,nbins,products,bins,i); for(priority_bin=nbins;priority_bin>0;priority_bin--) if(bins[priority_bin].allowed && (calculate_cost(nprods,nbins,products,bins,priority_bin)==max_cost)) { moved=0; /* look for a critical product that would improve the critical path in the worst bins if moved */ for(i=0;(i<=nprods)&&(!moved);i++) if(products[i].bin==priority_bin) { oldbin=products[i].bin; products[i].bin=-1; bins[oldbin].nprods--; worth=(calculate_cost(nprods,nbins,products,bins,priority_bin)=products[j].min_bin) && (products[j].bin>=products[i].min_bin)) { products[i].bin=products[j].bin; products[j].bin=oldbin; aft_dst_cost=calculate_cost(nprods,nbins,products,bins,products[i].bin); aft_org_cost=calculate_cost(nprods,nbins,products,bins,oldbin); if((aft_dst_cost0)&&(!moved);j--) if((j!=oldbin) && (bins[j].allowed) && (j>=products[i].min_bin) && (calculate_cost(nprods,nbins,products,bins,j)=0;i--) if(calculate_cost(nprods,nbins,products,bins,i)>max_cost) max_cost=calculate_cost(nprods,nbins,products,bins,i); /* calculating the cost in the fibonacci form */ for(i=0;i