nbench0.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 1,045 行 · 第 1/3 页

C
1,045
字号
                stdev);

        /*
        ** Is half interval 5% or less of mean?
        ** If so, we can go home.  Otherwise,
        ** we have to continue.
        */
        if(c_half_interval/ (*mean) <= (double)0.05)
                break;

        /*
        ** Go get a new score and see if it
        ** improves existing scores.
        */
        do {
                if(*numtries==10)
                        return(-1);
                (*funcpointer[fid])();
                *numtries+=1;
                newscore=getscore(fid);
        } while(seek_confidence(myscores,&newscore,
                &c_half_interval,mean,stdev)==0);
}

return(0);
}

/********************
** seek_confidence **
*********************
** Pass this routine an array of 5 scores PLUS a new score.
** This routine tries the new score in place of each of
** the other five scores to determine if the new score,
** when replacing one of the others, improves the confidence
** half-interval.
** Return 0 if failure.  Original 5 scores unchanged.
** Return -1 if success.  Also returns new half-interval,
** mean, and stand. dev.
*/
static int seek_confidence( double scores[5],
                double *newscore,
                double *c_half_interval,
                double *smean,
                double *sdev)
{
double sdev_to_beat;    /* Original sdev to be beaten */
double temp;            /* For doing a swap */
int is_beaten;          /* Indicates original was beaten */
int i;                  /* Index */

/*
** First calculate original standard deviation
*/
calc_confidence(scores,c_half_interval,smean,sdev);
sdev_to_beat=*sdev;
is_beaten=-1;

/*
** Try to beat original score.  We'll come out of this
** loop with a flag.
*/
for(i=0;i<5;i++)
{
        temp=scores[i];
        scores[i]=*newscore;
        calc_confidence(scores,c_half_interval,smean,sdev);
        scores[i]=temp;
        if(sdev_to_beat>*sdev)
        {       is_beaten=i;
                sdev_to_beat=*sdev;
        }
}

if(is_beaten!=-1)
{       scores[is_beaten]=*newscore;
        return(-1);
}
return(0);
}

/********************
** calc_confidence **
*********************
** Given a set of 5 scores, calculate the confidence
** half-interval.  We'l also return the sample mean and sample
** standard deviation.
** NOTE: This routines presumes a confidence of 95% and
** a confidence coefficient of .95
*/
static void calc_confidence( double scores[],    /* Array of scores */
                double *c_half_interval, /* Confidence half-int */
                double *smean,           /* Standard mean */
                double *sdev)            /* Sample stand dev */
{
int i;          /* Index */
/*
** First calculate mean.
*/
*smean=(scores[0]+scores[1]+scores[2]+scores[3]+scores[4])/
        (double)5.0;

/*
** Get standard deviation - first get variance
*/
*sdev=(double)0.0;
for(i=0;i<5;i++)
{       *sdev+=(scores[i]-(*smean))*(scores[i]-(*smean));
}
*sdev/=(double)4.0;
*sdev=sqrt(*sdev)/sqrt(5.0);

/*
** Now calculate the confidence half-interval.
** For a confidence level of 95% our confidence coefficient
** gives us a multiplying factor of 2.776
** (The upper .025 quartile of a t distribution with 4 degrees
** of freedom.)
*/
*c_half_interval=(double)2.776 * (*sdev);
return;
}

/*************
** getscore **
**************
** Return the score for a particular benchmark.
*/
static double getscore(int fid)
{

/*
** Fid tells us the function.  This is really a matter of
** doing the proper coercion.
*/
switch(fid)
{
        case TF_NUMSORT:
                return(global_numsortstruct.sortspersec);
        case TF_SSORT:
                return(global_strsortstruct.sortspersec);
        case TF_BITOP:
                return(global_bitopstruct.bitopspersec);
        case TF_FPEMU:
                return(global_emfloatstruct.emflops);
        case TF_FFPU:
                return(global_fourierstruct.fflops);
        case TF_ASSIGN:
                return(global_assignstruct.iterspersec);
        case TF_IDEA:
                return(global_ideastruct.iterspersec);
        case TF_HUFF:
                return(global_huffstruct.iterspersec);
        case TF_NNET:
                return(global_nnetstruct.iterspersec);
        case TF_LU:
                return(global_lustruct.iterspersec);
}
return((double)0.0);
}

/******************
** output_string **
*******************
** Displays a string on the screen.  Also, if the flag
** write_to_file is set, outputs the string to the output file.
** Note, this routine presumes that you've included a carriage
** return at the end of the buffer.
*/
static void output_string(char *buffer)
{

printf("%s",buffer);
if(write_to_file!=0)
        fprintf(global_ofile,"%s",buffer);
return;
}

/***************
** show_stats **
****************
** This routine displays statistics for a particular benchmark.
** The benchmark is identified by its id.
*/
static void show_stats (int bid)
{
char buffer[80];        /* Display buffer */

switch(bid)
{
        case TF_NUMSORT:                /* Numeric sort */
                sprintf(buffer,"  Number of arrays: %d\n",
                        global_numsortstruct.numarrays);
                output_string(buffer);
                sprintf(buffer,"  Array size: %ld\n",
                        global_numsortstruct.arraysize);
                output_string(buffer);
                break;

        case TF_SSORT:          /* String sort */
                sprintf(buffer,"  Number of arrays: %d\n",
                        global_strsortstruct.numarrays);
                output_string(buffer);
                sprintf(buffer,"  Array size: %ld\n",
                        global_strsortstruct.arraysize);
                output_string(buffer);
                break;

        case TF_BITOP:          /* Bitmap operation */
                sprintf(buffer,"  Operations array size: %ld\n",
                        global_bitopstruct.bitoparraysize);
                output_string(buffer);
                sprintf(buffer,"  Bitfield array size: %ld\n",
                        global_bitopstruct.bitfieldarraysize);
                output_string(buffer);
                break;

        case TF_FPEMU:          /* Floating-point emulation */
                sprintf(buffer,"  Number of loops: %lu\n",
                        global_emfloatstruct.loops);
                output_string(buffer);
                sprintf(buffer,"  Array size: %lu\n",
                        global_emfloatstruct.arraysize);
                output_string(buffer);
                break;

        case TF_FFPU:           /* Fourier test */
                sprintf(buffer,"  Number of coefficients: %lu\n",
                        global_fourierstruct.arraysize);
                output_string(buffer);
                break;

        case TF_ASSIGN:
                sprintf(buffer,"  Number of arrays: %lu\n",
                        global_assignstruct.numarrays);
                output_string(buffer);
                break;

        case TF_IDEA:
                sprintf(buffer,"  Array size: %lu\n",
                        global_ideastruct.arraysize);
                output_string(buffer);
                sprintf(buffer," Number of loops: %lu\n",
                        global_ideastruct.loops);
                output_string(buffer);
                break;

        case TF_HUFF:
                sprintf(buffer,"  Array size: %lu\n",
                        global_huffstruct.arraysize);
                output_string(buffer);
                sprintf(buffer,"  Number of loops: %lu\n",
                        global_huffstruct.loops);
                output_string(buffer);
                break;

        case TF_NNET:
                sprintf(buffer,"  Number of loops: %lu\n",
                        global_nnetstruct.loops);
                output_string(buffer);
                break;

        case TF_LU:
                sprintf(buffer,"  Number of arrays: %lu\n",
                        global_lustruct.numarrays);
                output_string(buffer);
                break;
}
return;
}

/*
** Following code added for Mac stuff, so that we can emulate command
** lines.
*/

#ifdef MAC

/*****************
** UCommandLine **
******************
** Reads in a command line, and sets up argc and argv appropriately.
** Note that this routine uses gets() to read in the line.  This means
** you'd better not enter more than 128 characters on a command line, or
** things will overflow, and oh boy...
*/
void UCommandLine(void)
{
printf("Enter command line\n:");
gets((char *)Uargbuff);
UParse();
return;
}

/***********
** UParse **
************
** Parse the pseudo command-line.  This code appeared as part of the
** Small-C library in Dr. Dobb's ToolBook of C.
** It expects the following globals:
** argc = arg count
** argv = Pointer to array of char pointers
** Uargbuff = Character array that holds the arguments.  Should be 129 bytes long.
** Udummy1 = This is a 2-byte buffer that holds a "*", and acts as the first
**  argument in the argument list.  This maintains compatibility with other
**  C's, though it does not provide access to the executable filename.
** This routine allows for up to 20 individual command-line arguments.
** Also note that this routine does NOT allow for redirection.
*/
void UParse(void)
{
unsigned char *ptr;

argc=0;         /* Start arg count */
Udummy[0]='*';  /* Set dummy first argument */
Udummy[1]='\0';
argv[argc++]=(char *)Udummy;

ptr=Uargbuff;           /* Start pointer */
while(*ptr)
{
        if(isspace(*ptr))
        {       ++ptr;
                continue;
        }
        if(argc<20) argv[argc++]=(char *)ptr;
        ptr=UField(ptr);
}
return;
}
/***********
** UField **
************
** Isolate the next command-line field.
*/
unsigned char *UField(unsigned char *ptr)
{
while(*ptr)
{       if(isspace(*ptr))
        {       *ptr=(unsigned char)NULL;
                return(++ptr);
        }
        ++ptr;
}
return(ptr);
}
#endif

⌨️ 快捷键说明

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