diff options
Diffstat (limited to 'numsort.c')
-rw-r--r-- | numsort.c | 57 |
1 files changed, 32 insertions, 25 deletions
@@ -1,10 +1,12 @@ #include <stdio.h> #include <stdlib.h> #include <stdint.h> +#include <math.h> #include "nmglobal.h" #include "nbench1.h" + /********************* ** NUMERIC HEAPSORT ** ********************** @@ -22,7 +24,9 @@ static inline void NumSift(long *array, unsigned long min, unsigned long max); ** This routine performs the CPU numeric sort test. */ -void DoNumSort(void) { +void +DoNumSort (void) +{ /* Error context string pointer */ const char *context = "CPU:Numeric Sort"; /* Local pointer to global struct */ @@ -35,15 +39,15 @@ void DoNumSort(void) { /* ** See if we need to do self adjustment code. */ - if(numsortstruct->adjust==0) { + if (numsortstruct->adjust == 0) { /* ** Self-adjustment code. The system begins by sorting 1 ** array. If it does that in no time, then two arrays ** are built and sorted. This process continues until ** enough arrays are built to handle the tolerance. */ - numsortstruct->numarrays=1; - while(1) { + numsortstruct->numarrays = 1; + while (1) { arraybase = realloc(arraybase, numsortstruct->numarrays * numsortstruct->arraysize * sizeof(long)); if (!arraybase) { fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context); @@ -56,12 +60,12 @@ void DoNumSort(void) { ** minimum, then allocate for more arrays and ** try again. */ - if ( DoNumSortIteration(arraybase, numsortstruct->arraysize, numsortstruct->numarrays)>global_min_ticks ) { + if (DoNumSortIteration(arraybase, numsortstruct->arraysize, numsortstruct->numarrays)>global_min_ticks) { break; } - if(numsortstruct->numarrays > NUMNUMARRAYS) { - printf("CPU:NSORT -- NUMNUMARRAYS hit.\n"); + if (numsortstruct->numarrays > NUMNUMARRAYS) { + puts("CPU:NSORT -- NUMNUMARRAYS hit."); } ++numsortstruct->numarrays; } @@ -91,9 +95,9 @@ void DoNumSort(void) { */ free(arraybase); - numsortstruct->sortspersec=iterations * (double)numsortstruct->numarrays / TicksToFracSecs(accumtime); + numsortstruct->sortspersec = iterations * (double)numsortstruct->numarrays / TicksToFracSecs(accumtime); - if ( numsortstruct->adjust == 0 ) { + if (numsortstruct->adjust == 0) { numsortstruct->adjust = 1; } } @@ -105,7 +109,9 @@ void DoNumSort(void) { ** sort benchmark. It returns the number of ticks ** elapsed for the iteration. */ -static unsigned long DoNumSortIteration(long *arraybase, unsigned long arraysize, unsigned int numarrays) { +static unsigned long +DoNumSortIteration(long *arraybase, unsigned long arraysize, unsigned int numarrays) +{ unsigned long elapsed; /* Elapsed ticks */ unsigned long i; @@ -122,7 +128,7 @@ static unsigned long DoNumSortIteration(long *arraybase, unsigned long arraysize /* ** Execute a heap of heapsorts */ - for(i = 0; i < numarrays; i++ ) { + for (i = 0; i < numarrays; i++) { NumHeapSort(arraybase + i * arraysize, 0L, arraysize - 1L); } @@ -139,21 +145,19 @@ static unsigned long DoNumSortIteration(long *arraybase, unsigned long arraysize ************************** ** Load up an array with random longs. */ -static void LoadNumArrayWithRand(long *array, /* Pointer to arrays */ +static void +LoadNumArrayWithRand(long *array, /* Pointer to arrays */ unsigned long arraysize, unsigned int numarrays) /* # of elements in array */ { long i; /* Used for index */ long *darray; /* Destination array pointer */ - /* - ** Initialize the random number generator - */ + + /* Initialize the random number generator */ randnum(13); - /* - ** Load up first array with randoms - */ - for( i = 0; i < arraysize; i++ ) { + /* Load up first array with randoms */ + for (i = 0; i < arraysize; i++) { array[i] = randnum(0); } @@ -162,7 +166,7 @@ static void LoadNumArrayWithRand(long *array, /* Pointer to arrays */ ** first into each of the others. */ darray = array; - while ( --numarrays ) { + while (--numarrays) { darray += arraysize; memcpy(darray, array, arraysize * sizeof(long)); } @@ -175,14 +179,16 @@ static void LoadNumArrayWithRand(long *array, /* Pointer to arrays */ ** integers. Also pass in minimum and maximum offsets. ** This routine performs a heap sort on that array. */ -static inline void NumHeapSort(long *array, unsigned long bottom, unsigned long top) { +static inline void +NumHeapSort(long *array, unsigned long bottom, unsigned long top) +{ unsigned long temp; /* Used to exchange elements */ unsigned long i; /* Loop index */ /* ** First, build a heap in the array */ - for (i = (top / 2L); i > 0; i-- ) { + for (i = (top / 2L); i > 0; i--) { NumSift(array, i, top); } @@ -191,7 +197,7 @@ static inline void NumHeapSort(long *array, unsigned long bottom, unsigned long ** end of the array. When we get done, we'll have a sorted ** array. */ - for ( i = top; i > 0; i-- ) { + for (i = top; i > 0; i--) { NumSift(array, bottom, i); temp = *array; @@ -206,7 +212,9 @@ static inline void NumHeapSort(long *array, unsigned long bottom, unsigned long ** Peforms the sift operation on a numeric array, ** constructing a heap in the array. */ -static inline void NumSift(long *array, unsigned long min, unsigned long max) { +static inline void +NumSift(long *array, unsigned long min, unsigned long max) +{ unsigned long k; unsigned long temp; /* Used for exchange */ @@ -228,4 +236,3 @@ static inline void NumSift(long *array, unsigned long min, unsigned long max) { } } } - |