/* TEST 2 EXERCISER PROGRAM FOR THE MULTI-LAYER PERCEPTRON TIME TRIALS AGAINST FLOATING-POINT DOUBLE BENCHMARK Author: Robert John Morton YE572246C December 1997 */ #include #include #include #define NI 77 // number of inputs to the neuron int I[] = {11376, 13425, 17920, 30226, 28763, 18940, 15329, 11376, 13425, 17920, 30226, 28763, 18940, 15329, 11376, 13425, 17920, 30226, 28763, 18940, 15329, 11376, 13425, 17920, 30226, 28763, 18940, 15329, 11376, 13425, 17920, 30226, 28763, 18940, 15329, 11376, 13425, 17920, 30226, 28763, 18940, 15329, 11376, 13425, 17920, 30226, 28763, 18940, 15329, 11376, 13425, 17920, 30226, 28763, 18940, 15329, 11376, 13425, 17920, 30226, 28763, 18940, 15329, 11376, 13425, 17920, 30226, 28763, 18940, 15329, 11376, 13425, 17920, 30226, 28763, 18940, 15329 }; //inputs int W[] = {12345, 21345, 31245, 16730, 31662, 25460, 13557, 12345, 21345, 31245, 16730, 31662, 25460, 13557, 12345, 21345, 31245, 16730, 31662, 25460, 13557, 12345, 21345, 31245, 16730, 31662, 25460, 13557, 12345, 21345, 31245, 16730, 31662, 25460, 13557, 12345, 21345, 31245, 16730, 31662, 25460, 13557, 12345, 21345, 31245, 16730, 31662, 25460, 13557, 12345, 21345, 31245, 16730, 31662, 25460, 13557, 12345, 21345, 31245, 16730, 31662, 25460, 13557, 12345, 21345, 31245, 16730, 31662, 25460, 13557, 12345, 21345, 31245, 16730, 31662, 25460, 13557 }; //weights double GetTime() { struct timeb TimeData; ftime(&TimeData); return((double)(TimeData.millitm) / 1000 + TimeData.time); } double Dfun() { int k, i; double x, t = GetTime(); for(k = 0; k < 10000; k++) { for(x = 0, i = 0; i < NI; i++) x += (double)(*(I + i)) * *(W + i); x /= NI * 32768; } return(GetTime() - t); } double Lfun1(void) { int k, i, a; long x, Hi, Lo; double t = GetTime(); for(k = 0; k < 10000; k++) { for(Hi = 0, Lo = 0, i = 0; i < NI; i++) { x = (long)*(I + i) * *(W + i); Lo += x & 0xFFFF; Hi += x >> 16; } a = ((Hi << 1) + (Lo >> 15)) / NI; } return(GetTime() - t); } double Lfun2(void) { int k, i, a, *pi = I, *pw = W; long x, Hi, Lo; double t = GetTime(); for(k = 0; k < 10000; k++) { for(Hi = 0, Lo = 0, i = 0; i < NI; i++) { x = (long)*(pi+ i) * *(pw + i); Lo += x & 0xFFFF; Hi += x >> 16; } a = ((Hi << 1) + (Lo >> 15)) / NI; } return(GetTime() - t); } double Lfun3(void) { int k, i, a, *pi = I, *pw = W; long x, Hi, Lo; double t = GetTime(); for(k = 0; k < 10000; k++) { for(Hi = 0, Lo = 0, i = 0; i < NI; i++) { x = (long)*pi++ * *pw++; Lo += x & 0xFFFF; Hi += x >> 16; } a = ((Hi << 1) + (Lo >> 15)) / NI; } return(GetTime() - t); } double Lfun4(void) { int k, i, a, *pi = I, *pw = W; long x, Hi, Lo; double t = GetTime(); for(k = 0; k < 10000; k++) { for(Hi = 0, Lo = 0, i = 0; i < NI; i++, pi++, pw++) { x = (long)*pi * *pw; Lo += x & 0xFFFF; Hi += x >> 16; } a = ((Hi << 1) + (Lo >> 15)) / NI; } return(GetTime() - t); } double Afun(void) { int k, i, AL, ni, w; unsigned int q; long Hi, Lo; double t = GetTime(); for(k = 0; k < 10000; k++) { for(Hi = 0, Lo = 0, i = 0; i < NI; i++) { ni = *(I + i); w = *(W + i); _asm { //IN-LINE ASSEMBLER CODE mov ax, ni ; load initerger input into AX register imul w ; do signed multiply with interger weight mov ni, dx ; put ms word into unsigned interger p mov q, ax ; put ls word into unsigned variable q } Hi += ni; //add ms word into High accumulator Lo += q; //add ls word into Low accumulator } AL = ((Hi << 1) + (Lo >> 15)) / NI; } return(GetTime() - t); } main() { FILE *fh; char s[1024], *p = s; long o; int c, i; double T1, T2, T3, T4, T5, T6; for(T1 = GetTime() + 10; GetTime() < T1;); //wait 10 seconds printf("\n\nTiming...\n"); T1 = Dfun(); T2 = Lfun1(); T3 = Lfun2(); T4 = Lfun3(); T5 = Lfun4(); T6 = Afun(); i = sprintf(s,"TIME TRIALS AGAINST FLOATING-POINT DOUBLE BENCHMARK:\n"); i += sprintf(s + i," Floating-point double %6.2f secs (Benchmark) \n", T1); i += sprintf(s + i," 4) 'C' using *(I+i) %6.2f secs (%6.2f times as fast)\n", T2, T1/T2); i += sprintf(s + i," 4) 'C' using *(pi+i) %6.2f secs (%6.2f times as fast)\n", T3, T1/T3); i += sprintf(s + i," 4) 'C' using *pi++ %6.2f secs (%6.2f times as fast)\n", T4, T1/T4); i += sprintf(s + i," 4) 'C' for(..., pw++) %6.2f secs (%6.2f times as fast)\n", T5, T1/T5); i += sprintf(s + i," 5) asm using *(I+i) %6.2f secs (%6.2f times as fast)\n", T6, T1/T6); sprintf(s + i,"Note: time trials were only done on the methods that worked.\n\n\n"); fh = fopen("results.txt","a"); //open the file for appending for(p = s; (c = *p) > '\0'; p++) //while character is not a null putc(c, fh); //write it to the file fclose(fh); //close the file }