From 27b6cc8fd26d7d3f31308a4137436e4f455c0daf Mon Sep 17 00:00:00 2001 From: Matt Turner Date: Sat, 15 Nov 2008 00:27:18 +0000 Subject: Continue replacing struct members with local variables git-svn-id: svn://mattst88.com/svn/cleanbench/trunk@47 0d43b9a7-5ab2-4d7b-af9d-f64450cef757 --- bitfield.c | 80 +++++++++++++++++++++++++++++++----------------------------- cleanbench.c | 3 --- nmglobal.h | 72 ------------------------------------------------------ numsort.c | 44 ++++++++++++++++++--------------- stringsort.c | 50 ++++++++++++++++++++----------------- 5 files changed, 92 insertions(+), 157 deletions(-) diff --git a/bitfield.c b/bitfield.c index fcab172..d87bfe7 100644 --- a/bitfield.c +++ b/bitfield.c @@ -14,9 +14,15 @@ ** BITFIELD OPERATIONS ** *************************/ -static clock_t DoBitfieldIteration(unsigned long *bitarraybase, - unsigned long *bitoparraybase, - long bitoparraysize, +#ifdef _LP64 +#define ARRAY_SIZE 16384L +#else +#define ARRAY_SIZE 32768L +#endif + +static clock_t DoBitfieldIteration(unsigned long *bitarray, + unsigned long *bitoparray, + long bitop_array_size, unsigned long *nbitops); static void ToggleBitRun(unsigned long *bitmap, unsigned long bit_addr, @@ -37,32 +43,28 @@ DoBitops(void) { const char* context = "CPU:Bitfields"; BitOpStruct* locbitopstruct = &global_bitopstruct; - unsigned long* bitarraybase = NULL; - unsigned long* bitoparraybase = NULL; + unsigned long* bitarray = NULL; + unsigned long* bitoparray = NULL; clock_t total_time = 0; int iterations = 0; unsigned long nbitops; static int is_adjusted = FALSE; + static int bitop_array_size = 30; if (is_adjusted == FALSE) { is_adjusted = TRUE; - bitarraybase = realloc(bitarraybase, locbitopstruct->bitfieldarraysize * sizeof(unsigned long)); - if (!bitarraybase) { + bitarray = realloc(bitarray, ARRAY_SIZE * sizeof(unsigned long)); + if (!bitarray) { fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context); exit(1); } - /* - ** Initialize bitfield operations array to [2,30] elements - */ - locbitopstruct->bitoparraysize = 30L; - while(1) { - bitoparraybase = malloc(locbitopstruct->bitoparraysize * 2 * sizeof(unsigned long)); - if (!bitoparraybase) { + bitoparray = malloc(bitop_array_size * 2 * sizeof(unsigned long)); + if (!bitoparray) { fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context); - free(bitarraybase); + free(bitarray); exit(1); } @@ -72,27 +74,27 @@ DoBitops(void) ** minimum, then de-allocate the array, reallocate a ** larger version, and try again. */ - if(DoBitfieldIteration(bitarraybase, bitoparraybase, locbitopstruct->bitoparraysize, &nbitops) > MINIMUM_TICKS) { + if(DoBitfieldIteration(bitarray, bitoparray, bitop_array_size, &nbitops) > MINIMUM_TICKS) { break; } - locbitopstruct->bitoparraysize += 100L; + bitop_array_size += 100; } } else { /* ** Don't need to do self adjustment, just allocate ** the array space. */ - bitarraybase = malloc(locbitopstruct->bitfieldarraysize * sizeof(unsigned long)); - if (!bitarraybase) { + bitarray = malloc(ARRAY_SIZE * sizeof(unsigned long)); + if (!bitarray) { fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context); exit(1); } - bitoparraybase = malloc(locbitopstruct->bitoparraysize * 2 * sizeof(unsigned long)); - if (!bitoparraybase) { + bitoparray = malloc(bitop_array_size * 2 * sizeof(unsigned long)); + if (!bitoparray) { fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context); - free(bitarraybase); + free(bitarray); exit(1); } } @@ -102,7 +104,7 @@ DoBitops(void) ** accumulated elapsed time is greater than # of seconds requested. */ do { - total_time += DoBitfieldIteration(bitarraybase, bitoparraybase, locbitopstruct->bitoparraysize,&nbitops); + total_time += DoBitfieldIteration(bitarray, bitoparray, bitop_array_size,&nbitops); iterations += nbitops; } while (total_time < MINIMUM_SECONDS * CLOCKS_PER_SEC); @@ -111,8 +113,8 @@ DoBitops(void) ** Also, set adjustment flag to show that we don't have ** to do self adjusting in the future. */ - free(bitarraybase); - free(bitoparraybase); + free(bitarray); + free(bitoparray); locbitopstruct->results = (double)(iterations * CLOCKS_PER_SEC) / (double)total_time; } @@ -124,7 +126,7 @@ DoBitops(void) ** Return the # of ticks accumulated by the operation. */ static clock_t -DoBitfieldIteration(unsigned long *bitarraybase, unsigned long *bitoparraybase, long bitoparraysize, unsigned long *nbitops) +DoBitfieldIteration(unsigned long *bitarray, unsigned long *bitoparray, long bitop_array_size, unsigned long *nbitops) { clock_t start, stop; long i; @@ -152,37 +154,37 @@ DoBitfieldIteration(unsigned long *bitarraybase, unsigned long *bitoparraybase, */ randnum(13); - for (i = 0; i < global_bitopstruct.bitfieldarraysize; i++) { + for (i = 0; i < ARRAY_SIZE; i++) { #ifdef _LP64 - *(bitarraybase+i) = 0x5555555555555555UL; + *(bitarray+i) = 0x5555555555555555UL; #else - *(bitarraybase+i) = 0x55555555UL; + *(bitarray+i) = 0x55555555UL; #endif } randnum(13); - for (i = 0; i < bitoparraysize; i++) { + for (i = 0; i < bitop_array_size; i++) { /* First item is offset */ - *(bitoparraybase + i + i) = bitoffset = abs_randwc((int32_t)262140); + *(bitoparray + i + i) = bitoffset = abs_randwc((int32_t)262140); /* Next item is run length */ - *nbitops += *(bitoparraybase + i + i + 1L) = abs_randwc((int32_t)262140 - bitoffset); + *nbitops += *(bitoparray + i + i + 1L) = abs_randwc((int32_t)262140 - bitoffset); } start = clock(); - for(i = 0; i < bitoparraysize; i++) { + for(i = 0; i < bitop_array_size; i++) { switch(i % 3) { case 2: /* Complement run of bits */ - FlipBitRun(bitarraybase, - *(bitoparraybase+i+i), - *(bitoparraybase+i+i+1)); + FlipBitRun(bitarray, + *(bitoparray+i+i), + *(bitoparray+i+i+1)); break; default: - ToggleBitRun(bitarraybase, - *(bitoparraybase+i+i), - *(bitoparraybase+i+i+1), + ToggleBitRun(bitarray, + *(bitoparray+i+i), + *(bitoparray+i+i+1), !i); break; } diff --git a/cleanbench.c b/cleanbench.c index 57f9bf7..93a6c82 100644 --- a/cleanbench.c +++ b/cleanbench.c @@ -81,9 +81,6 @@ for (i = 0; i < NUMTESTS; i++) { ** Initialize test data structures to default ** values. */ -global_numsortstruct.arraysize=NUMARRAYSIZE; -global_strsortstruct.arraysize=STRINGARRAYSIZE; -global_bitopstruct.bitfieldarraysize=BITFARRAYSIZE; global_emfloatstruct.arraysize=EMFARRAYSIZE; global_ideastruct.arraysize=IDEAARRAYSIZE; global_huffstruct.arraysize=HUFFARRAYSIZE; diff --git a/nmglobal.h b/nmglobal.h index 2f1018d..90fcf63 100644 --- a/nmglobal.h +++ b/nmglobal.h @@ -35,84 +35,12 @@ #define MINIMUM_TICKS 60 #define MINIMUM_SECONDS 5 /* Minimum number of seconds to run each test */ -/***************** -** NUMERIC SORT ** -*****************/ -/* -** DEFINES -*/ - -/* -** The following constant, NUMNUMARRAYS (no, it is not a -** Peter Sellers joke) is the maximum number of arrays -** that can be built by the numeric sorting benchmark -** before it gives up. This maximum is dependent on the -** amount of memory in the system. -*/ -/*#define NUMNUMARRAYS 1000*/ -#define NUMNUMARRAYS 10000 - -/* -** The following constant NUMARRAYSIZE determines the -** default # of elements in each numeric array. Ordinarily -** this is something you shouldn't fool with, though as -** with most of the constants here, it is adjustable. -*/ -#define NUMARRAYSIZE 8111L - - -/* -** TYPEDEFS -*/ typedef struct { double results; /* # of sort iterations per sec */ - unsigned long arraysize; /* # of elements in array */ } SortStruct; -/**************** -** STRING SORT ** -***************** -** Note: The string sort benchmark uses the same structure to -** communicate parameters as does the numeric sort benchmark. -** (i.e., SortStruct...see above. -*/ - -/* -** DEFINES -*/ -/* -** The following constant STRINGARRAYSIZE determines -** the default # of bytes allocated to each string array. -** Though the actual size can be pre-set from the command -** file, this constant should be left unchanged. -*/ -#define STRINGARRAYSIZE 8111L - -/************************ -** BITFIELD OPERATIONS ** -************************* -*/ - -/* -** DEFINES -*/ - -/* -** Following field sets the size of the bitfield array (in longs). -*/ -#ifdef _LP64 -#define BITFARRAYSIZE 16384L -#else -#define BITFARRAYSIZE 32768L -#endif - -/* -** TYPEDEFS -*/ typedef struct { double results; /* # of bitfield ops per sec */ - unsigned long bitoparraysize; /* Total # of bitfield ops */ - unsigned long bitfieldarraysize; /* Bit field array size */ } BitOpStruct; /**************************** diff --git a/numsort.c b/numsort.c index 690d5a6..b9765cf 100644 --- a/numsort.c +++ b/numsort.c @@ -16,8 +16,15 @@ ** This test implements a heapsort algorithm, performed on an ** array of longs. */ -static clock_t DoNumSortIteration(long *arraybase, unsigned long arraysize, unsigned int num_arrays); -static void LoadNumArrayWithRand(long *array, unsigned long arraysize, unsigned int num_arrays); + +/* +** The following constant NUMARRAYSIZE determines the +** default # of elements in each numeric array +*/ +#define ARRAY_SIZE 8111L + +static clock_t DoNumSortIteration(long *array, unsigned int num_arrays); +static void LoadNumArrayWithRand(long *array, unsigned int num_arrays); static inline void NumHeapSort(long *array, unsigned long bottom, unsigned long top); static inline void NumSift(long *array, unsigned long min, unsigned long max); @@ -32,7 +39,7 @@ DoNumSort (void) { const char* context = "CPU:Numeric Sort"; SortStruct* numsortstruct = &global_numsortstruct; - long* arraybase = NULL; + long* array = NULL; clock_t total_time = 0; int iterations = 0; static int num_arrays = 1; @@ -48,8 +55,8 @@ DoNumSort (void) */ while (1) { - arraybase = realloc(arraybase, num_arrays * numsortstruct->arraysize * sizeof(long)); - if (!arraybase) { + array = realloc(array, num_arrays * ARRAY_SIZE * sizeof(long)); + if (!array) { fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context); exit(1); } @@ -60,18 +67,15 @@ DoNumSort (void) ** minimum, then allocate for more arrays and ** try again. */ - if (DoNumSortIteration(arraybase, numsortstruct->arraysize, num_arrays) > MINIMUM_TICKS) { + if (DoNumSortIteration(array, num_arrays) > MINIMUM_TICKS) { break; } - if (num_arrays > NUMNUMARRAYS) { - puts("CPU:NSORT -- NUMNUMARRAYS hit."); - } ++num_arrays; } } else { - arraybase = malloc(num_arrays * numsortstruct->arraysize * sizeof(long)); - if (!arraybase) { + array = malloc(num_arrays * ARRAY_SIZE * sizeof(long)); + if (!array) { fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context); exit(1); } @@ -83,11 +87,11 @@ DoNumSort (void) */ do { - total_time += DoNumSortIteration(arraybase, numsortstruct->arraysize, num_arrays); + total_time += DoNumSortIteration(array, num_arrays); ++iterations; } while (total_time < MINIMUM_SECONDS * CLOCKS_PER_SEC); - free(arraybase); + free(array); numsortstruct->results = (double)(iterations * num_arrays * CLOCKS_PER_SEC) / (double)total_time; } @@ -100,7 +104,7 @@ DoNumSort (void) ** elapsed for the iteration. */ static clock_t -DoNumSortIteration(long *arraybase, unsigned long arraysize, unsigned int num_arrays) +DoNumSortIteration(long *array, unsigned int num_arrays) { clock_t start, stop; unsigned long i; @@ -108,12 +112,12 @@ DoNumSortIteration(long *arraybase, unsigned long arraysize, unsigned int num_ar /* ** Load up the array with random numbers */ - LoadNumArrayWithRand(arraybase, arraysize, num_arrays); + LoadNumArrayWithRand(array, num_arrays); start = clock(); for (i = 0; i < num_arrays; i++) { - NumHeapSort(&arraybase[i], 0L, arraysize - 1L); + NumHeapSort(&array[i], 0, ARRAY_SIZE - 1); } stop = clock(); @@ -127,7 +131,7 @@ DoNumSortIteration(long *arraybase, unsigned long arraysize, unsigned int num_ar ** Load up an array with random longs. */ static void -LoadNumArrayWithRand(long *array, unsigned long arraysize, unsigned int num_arrays) +LoadNumArrayWithRand(long *array, unsigned int num_arrays) { long i; /* Used for index */ long *darray; /* Destination array pointer */ @@ -136,7 +140,7 @@ LoadNumArrayWithRand(long *array, unsigned long arraysize, unsigned int num_arra randnum(13); /* Load up first array with randoms */ - for (i = 0; i < arraysize; i++) { + for (i = 0; i < ARRAY_SIZE; i++) { array[i] = randnum(0); } @@ -146,8 +150,8 @@ LoadNumArrayWithRand(long *array, unsigned long arraysize, unsigned int num_arra */ darray = array; while (--num_arrays) { - darray += arraysize; - memcpy(darray, array, arraysize * sizeof(long)); + darray += ARRAY_SIZE; + memcpy(darray, array, ARRAY_SIZE * sizeof(long)); } } diff --git a/stringsort.c b/stringsort.c index a91ff6d..71e6045 100644 --- a/stringsort.c +++ b/stringsort.c @@ -14,13 +14,18 @@ ** STRING HEAPSORT ** ********************/ -static clock_t DoStringSortIteration(unsigned char *arraybase, - unsigned int num_arrays, - unsigned long arraysize); +/* +** The following constant STRINGARRAYSIZE determines +** the default # of bytes allocated to each string array. +*/ +#define ARRAY_SIZE 8111L + + +static clock_t DoStringSortIteration(unsigned char *array, + unsigned int num_arrays); static unsigned long *LoadStringArray(unsigned char *strarray, unsigned int num_arrays, - unsigned long *strings, - unsigned long arraysize); + unsigned long *strings); static void stradjust(unsigned long *optrarray, unsigned char *strarray, unsigned long nstrings, @@ -49,7 +54,7 @@ void DoStringSort(void) { const char* context = "CPU:String Sort"; SortStruct* strsortstruct = &global_strsortstruct; - unsigned char* arraybase = NULL; + unsigned char* array = NULL; clock_t total_time = 0; int iterations = 0; static int num_arrays = 1; @@ -66,8 +71,8 @@ void DoStringSort(void) ** bytes to protect memory as strings move around ** (this can happen during string adjustment) */ - arraybase = realloc(arraybase, (strsortstruct->arraysize + 100) * num_arrays); - if (!arraybase) { + array = realloc(array, (ARRAY_SIZE + 100) * num_arrays); + if (!array) { fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context); exit(1); } @@ -78,7 +83,7 @@ void DoStringSort(void) ** minimum, then de-allocate the array, reallocate a ** an additional array, and try again. */ - if (DoStringSortIteration(arraybase, num_arrays, strsortstruct->arraysize) > MINIMUM_TICKS) { + if (DoStringSortIteration(array, num_arrays) > MINIMUM_TICKS) { break; } @@ -89,19 +94,19 @@ void DoStringSort(void) ** We don't have to perform self adjustment code. ** Simply allocate the space for the array. */ - arraybase = malloc((strsortstruct->arraysize + 100) * num_arrays); - if (!arraybase) { + array = malloc((ARRAY_SIZE + 100) * num_arrays); + if (!array) { fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context); exit(1); } } do { - total_time += DoStringSortIteration(arraybase, num_arrays, strsortstruct->arraysize); + total_time += DoStringSortIteration(array, num_arrays); iterations += num_arrays; } while (total_time < MINIMUM_SECONDS * CLOCKS_PER_SEC); - free(arraybase); + free(array); strsortstruct->results = (double)(iterations * CLOCKS_PER_SEC) / (double)total_time; } @@ -115,7 +120,7 @@ void DoStringSort(void) ** array. */ static clock_t -DoStringSortIteration(unsigned char *arraybase, unsigned int num_arrays,unsigned long arraysize) +DoStringSortIteration(unsigned char *array, unsigned int num_arrays) { unsigned long *optrarray; /* Offset pointer array */ clock_t start, stop; @@ -127,21 +132,21 @@ DoStringSortIteration(unsigned char *arraybase, unsigned int num_arrays,unsigned /* ** Load up the array(s) with random numbers */ - optrarray=LoadStringArray(arraybase,num_arrays,&nstrings,arraysize); + optrarray=LoadStringArray(array,num_arrays,&nstrings); /* ** Set temp base pointers...they will be modified as the ** benchmark proceeds. */ tempobase = optrarray; - tempsbase = arraybase; + tempsbase = array; start = clock(); for(i = 0; i < num_arrays; i++) { StrHeapSort(tempobase, tempsbase, nstrings, 0L, nstrings - 1); tempobase += nstrings; /* Advance base pointers */ - tempsbase += arraysize+100; + tempsbase += ARRAY_SIZE + 100; } stop = clock(); @@ -162,8 +167,7 @@ DoStringSortIteration(unsigned char *arraybase, unsigned int num_arrays,unsigned */ static unsigned long *LoadStringArray(unsigned char *strarray, /* String array */ unsigned int num_arrays, /* # of arrays */ - unsigned long *nstrings, /* # of strings */ - unsigned long arraysize) /* Size of array */ + unsigned long *nstrings) /* # of strings */ { unsigned char *tempsbase; /* Temporary string base pointer */ unsigned long *optrarray; /* Local for pointer */ @@ -200,8 +204,8 @@ do */ /* stringlength=(unsigned char)((1+abs_randwc(76L)) & 0xFFL);*/ stringlength=(unsigned char)((1+abs_randwc((int32_t)76)) & 0xFFL); - if((unsigned long)stringlength+curroffset+1L>=arraysize) - { stringlength=(unsigned char)((arraysize-curroffset-1L) & + if((unsigned long)stringlength+curroffset+1L>=ARRAY_SIZE) + { stringlength=(unsigned char)((ARRAY_SIZE-curroffset-1L) & 0xFF); fullflag=1; /* Indicates a full */ } @@ -237,8 +241,8 @@ do k=1; tempsbase=strarray; while(k