        -:    0:Source:winning-combo4.c
        -:    0:Graph:winning-combo4.gcno
        -:    0:Data:winning-combo4.gcda
        -:    0:Runs:1
        -:    0:Programs:1
        -:    1:#include <stdlib.h>
        -:    2:#include <stdio.h>
        -:    3:#include <string.h>
        -:    4:#include <stdint.h>
        -:    5:
        -:    6:#ifdef IS32BIT
        -:    7:#define ALGS 7
        -:    8:#else
        -:    9:#define ALGS 8
        -:   10:#endif
        -:   11:
        -:   12:#define RANGES 4
        -:   13:
        -:   14:#ifndef MAX_SIZE
        -:   15:#define MAX_SIZE 32768
        -:   16:#endif
        -:   17:
        -:   18:#ifndef UINT32_MAX
        -:   19:#define UINT32_MAX (4294967295U)
        -:   20:#endif
        -:   21:
        -:   22:/*
        -:   23:static void Winning_Alg(uint16_t *wins, uint16_t *winner){
        -:   24:	uint16_t b, c;
        -:   25:	for(b=c=0; b<ALGS; b++){
        -:   26:		if(wins[b] > c){
        -:   27:			*winner = b;
        -:   28:			c = wins[b];
        -:   29:		}
        -:   30:	}
        -:   31:}
        -:   32:*/
        -:   33:
        -:   34:
        4:   35:static void Print_Alg(uint16_t alg){
        4:   36:	if(alg>(ALGS-1)){
    #####:   37:		printf("error");
    #####:   38:		return;
        -:   39:	}
        -:   40:	uint8_t n1;
        -:   41:#ifdef IS32BIT
        -:   42:	const char *algs[] = {"byte_loop", "loop", "unrolled_loop", "rep_byte", "rep_4byte", "vector_loop", "libcall"};
        -:   43:#else
        4:   44:	const char *algs[] = {"byte_loop", "loop", "unrolled_loop", "rep_byte", "rep_4byte", "rep_8byte", "vector_loop", "libcall"};
        -:   45:#endif
        4:   46:	printf("\t%s", algs[alg]);
       27:   47:	for(n1=strlen(algs[alg]); n1<14; n1++)
       23:   48:		printf(" ");
        -:   49:}
        -:   50:
        1:   51:int main(int argc, char **argv){
        -:   52:	FILE *in;
        1:   53:	uint16_t a,b,c=0;
        1:   54:	size_t cur_wins = 0;
        1:   55:	size_t max_wins = 0;
        1:   56:	size_t cur_cycles = 0;
        1:   57:	uint16_t winning_starts[RANGES] = {0,0,0,0};
        1:   58:	uint16_t winning_algs[RANGES] = {0,0,0,0};
        -:   59://make dynamic
        -:   60://	uint32_t data[ALGS][MAX_SIZE];
        -:   61://	uint32_t fastest[MAX_SIZE];
        -:   62:	uint32_t *data[ALGS];
        -:   63:	uint32_t *fastest;
        1:   64:	uint32_t read_buffer_size = 64;
        -:   65:	char *read_buffer;
        1:   66:	uint16_t start_a = 0;
        1:   67:	uint16_t start_b = 0;
        1:   68:	uint16_t start_c = 0;
        1:   69:	uint16_t start_d = 0;
        1:   70:	const uint16_t min_start_a = 0;
        1:   71:	const uint16_t min_start_b = 1;
        1:   72:	const uint16_t min_start_c = 2;
        1:   73:	const uint16_t min_start_d = 3;
        1:   74:	const uint16_t max_end_d = MAX_SIZE;
        1:   75:	uint8_t alg_a = 0;
        1:   76:	uint16_t winners[RANGES] = {0,0,0,0};
        -:   77:	uint16_t wins[RANGES-1][ALGS];
        -:   78:	uint16_t wins_T[RANGES][ALGS];
        -:   79:	uint16_t wins_B[2][ALGS];
        -:   80:
        -:   81:	//if(argc != 1)
        -:   82:	//	max_end_d = atoi(argv[1]);
        1:   83:	printf("Testing to %u\n", max_end_d);
        -:   84:
        9:   85:	for(a=0; a<ALGS; a++){
        8:   86:		data[a] = (uint32_t *)malloc(sizeof(uint32_t)*max_end_d);
        -:   87:	}
        1:   88:	fastest = (uint32_t *)malloc(sizeof(uint32_t)*max_end_d);
        1:   89:	read_buffer = (char *)malloc(read_buffer_size + 1);
        1:   90:	(void)memset( (void *)read_buffer, '\0', read_buffer_size + 1);
        1:   91:	(void)memset((void *)wins, '\0', sizeof(uint16_t)*(RANGES-1)*ALGS);
        1:   92:	(void)memset((void *)wins_T, '\0', sizeof(uint16_t)*RANGES*ALGS);
        -:   93:
        9:   94:	for(a=0; a<8; a++){
        -:   95:#ifdef IS32BIT
        -:   96:		if(a == 5)
        -:   97:			continue;
        -:   98:#endif
        8:   99:		(void)sprintf(read_buffer, "results/r%u.txt", a+1);
        8:  100:		in = fopen(read_buffer, "r");
        8:  101:		b = 0;
        8:  102:		(void)memset( (void *)read_buffer, '\0', read_buffer_size + 1);
     8208:  103:		while( fgets(read_buffer, read_buffer_size, in) != NULL){
     8200:  104:			if(b == max_end_d)
        -:  105:				break;
    16384:  106:			data[c][b] = atoi(read_buffer);
     8192:  107:			b++;
        -:  108:		}
        8:  109:		fclose(in);
        8:  110:		c++;
        -:  111:	}
        1:  112:	free(read_buffer);
        -:  113:
        -:  114:	//put only the times of the fastest in new array
     1025:  115:	for(a=0; a<max_end_d; a++){
        -:  116:		cur_cycles = UINT32_MAX;
     8192:  117:		for(b=0; b<ALGS; b++){
     8192:  118:			if(data[b][a]<cur_cycles){
     3878:  119:				cur_cycles = data[b][a];
     3878:  120:				c = b;
        -:  121:			}
        -:  122:		}
     1024:  123:		fastest[a] = c;
        -:  124:	}
        -:  125:
        8:  126:	for(a=0; a<ALGS; a++)
        8:  127:		free(data[a]);
        -:  128:
        -:  129:	//calculate wins for each alg of each range on start (NOTE: will be 1 position checked unless mins_start_* changed)
        -:  130:	//except range d where max_end_d - min_start_d are checked
     1021:  131:	for(start_d = min_start_d; start_d<max_end_d; start_d++){
     8168:  132:		for(alg_a=0; alg_a<ALGS; alg_a++){
     8168:  133:			if(fastest[start_d] == alg_a)
     1021:  134:				wins_T[min_start_d][alg_a]++;
        -:  135:		}
        -:  136:	}
        -:  137:
        1:  138:        for(start_c = min_start_c; start_c<min_start_d; start_c++){
        8:  139:                for(alg_a=0; alg_a<ALGS; alg_a++){
        8:  140:                        if(fastest[start_c] == alg_a)
        1:  141:                                wins[min_start_c][alg_a]++;
        -:  142:                }
        -:  143:        }
        -:  144:
        1:  145:        for(start_b = min_start_b; start_b<min_start_c; start_b++){
        8:  146:                for(alg_a=0; alg_a<ALGS; alg_a++){
        8:  147:                        if(fastest[start_b] == alg_a)
        1:  148:                                wins[min_start_b][alg_a]++;
        -:  149:                }
        -:  150:        }
        -:  151:
        1:  152:        for(start_a = min_start_a; start_a<min_start_b; start_a++){
        8:  153:                for(alg_a=0; alg_a<ALGS; alg_a++){
        8:  154:                        if(fastest[start_a] == alg_a)
        1:  155:                                wins[min_start_a][alg_a]++;
        -:  156:                }
        -:  157:        }
        -:  158:
        1:  159:	(void)memcpy((void *)&wins_B[0][0], (const void *)&wins[0][0], sizeof(uint16_t)*ALGS);
        1:  160:	(void)memcpy((void *)&wins_B[1][0], (const void *)&wins[1][0], sizeof(uint16_t)*ALGS);
        -:  161:
        -:  162:	//start range d
     1022:  163:	for(start_d = min_start_d; start_d<max_end_d; start_d++){
        -:  164://		printf("%u/MAX_SIZE\n", start_d);
     1021:  165:		(void)memcpy((void *)&wins_T[2][0], (const void *)&wins[2][0], sizeof(uint16_t)*ALGS);
        -:  166:		//pick d winning alg up front
        -:  167:
     9189:  168:		for(b=c=0; b<ALGS; b++){
     8168:  169:			if(wins_T[3][b] > c){
     2024:  170:				winners[3] = b;
     2024:  171:				c = wins_T[3][b];
        -:  172:			}
        -:  173:		}
        -:  174:
        -:  175://		Winning_Alg(&wins_T[3][0], &winners[3]);
        -:  176:		//wins_B
     1021:  177:		(void)memcpy((void *)&wins[1][0], (const void *)&wins_B[1][0], sizeof(uint16_t)*ALGS);
        -:  178:
        -:  179:		//start range c
   522752:  180:		for(start_c = min_start_c; start_c<start_d; start_c++){
   521731:  181:			(void)memcpy((void *)&wins_T[0][0], (const void *)&wins[0][0], sizeof(uint16_t)*ALGS);
   521731:  182:			(void)memcpy((void *)&wins_T[1][0], (const void *)&wins[1][0], sizeof(uint16_t)*ALGS);
        -:  183:			//pick c winning alg
        -:  184:
  4695579:  185:			for(b=c=0; b<ALGS; b++){
  4173848:  186:				if(wins_T[2][b] > c){
   856638:  187:					winners[2] = b;
   856638:  188:					c = wins_T[2][b];
        -:  189:				}
        -:  190:			}
        -:  191:
        -:  192://			Winning_Alg(&wins_T[2][0], &winners[2]);
        -:  193:			//start range b
177910271:  194:			for(start_b = min_start_b; start_b<start_c; start_b++){
        -:  195:
        -:  196:				//pick b and a winning algs
1423282168:  197:				for(a=b=c=0; b<ALGS; b++){
1423282168:  198:					if(wins_T[0][b] > c){
286563985:  199:						winners[0] = b;
286563985:  200:						c = wins_T[0][b];
        -:  201:					}
1423282168:  202:					if(wins_T[1][b] > a){
264777053:  203:						winners[1] = b;
264777053:  204:						a = wins_T[1][b];
        -:  205:					}
        -:  206:				}
        -:  207:
        -:  208:
        -:  209://				Winning_Alg(&wins_T[0][0], &winners[0]);
        -:  210://				Winning_Alg(&wins_T[1][0], &winners[1]);
        -:  211:
711641084:  212:				for(a=cur_wins=0; a<RANGES; a++)
711641084:  213:					cur_wins += wins_T[a][winners[a]];
        -:  214:
        -:  215:				//cur_wins = wins_T[0][winners[0]] + wins_T[1][winners[1]] + wins_T[2][winners[2]] + wins_T[3][winners[3]];
177910271:  216:				a = fastest[start_b];
177910271:  217:				wins_T[1][a]--;
177910271:  218:				wins_T[0][a]++;
        -:  219:
177910271:  220:				if(cur_wins < max_wins)//{
177908733:  221:					continue;
        -:  222:				//winning_starts[0] = start_a;
     1538:  223:				winning_starts[1] = start_b;
     1538:  224:				winning_starts[2] = start_c;
     1538:  225:				winning_starts[3] = start_d;
        -:  226:				//for(a=0; a<RANGES; a++)
        -:  227:				//	winning_algs[a] = winners[a];
     1538:  228:				(void)memcpy((void *)winning_algs, (const void *)winners, sizeof(uint16_t)*RANGES);
     1538:  229:				max_wins = cur_wins;
        -:  230:				//}
        -:  231:				//a = fastest[start_b];
        -:  232:				//wins_T[1][a]--;
        -:  233:				//wins_T[0][a]++;
        -:  234:			}
        -:  235:			//selectively decremember alg in range c that gained a win
   521731:  236:			a = fastest[start_c];
   521731:  237:			wins_T[2][a]--;
   521731:  238:			wins[1][a]++;
        -:  239:		}
        -:  240:		//selectively decrement alg that lost a win
     1021:  241:		a = fastest[start_d];
     1021:  242:		wins_T[3][a]--;
     1021:  243:		wins[2][a]++;
        -:  244:	}
        1:  245:	free(fastest);
        -:  246:
        1:  247:	printf("config with most wins in first %u:\n", max_end_d);
        1:  248:	printf("\twinning_alg\t\tstart\n");
        5:  249:	for(a=0; a<RANGES; a++){
        4:  250:		Print_Alg(winning_algs[a]);
        4:  251:		printf("(%u)\t\t%u\n", winning_algs[a], winning_starts[a]);
        -:  252:
        -:  253:	}
        -:  254:
        -:  255:	return 0;
        -:  256:}
