summaryrefslogtreecommitdiff
path: root/numsort.c
diff options
context:
space:
mode:
authorMatt Turner <mattst88@gmail.com>2008-11-12 04:53:46 +0000
committerMatt Turner <mattst88@gmail.com>2008-11-12 04:53:46 +0000
commit8a639cb06e75d496a33f609b049dbf9e6145befc (patch)
treebfc0fb3a89e8b39f0e89546043c64ac204f02b3d /numsort.c
parent13434a06d5f7d34be5a78f4d8ab2db1a8b1d50e5 (diff)
Clean up numsort.c
git-svn-id: svn://mattst88.com/svn/cleanbench/trunk@11 0d43b9a7-5ab2-4d7b-af9d-f64450cef757
Diffstat (limited to 'numsort.c')
-rw-r--r--numsort.c321
1 files changed, 144 insertions, 177 deletions
diff --git a/numsort.c b/numsort.c
index 5756a31..6337037 100644
--- a/numsort.c
+++ b/numsort.c
@@ -1,11 +1,7 @@
#include <stdio.h>
-#include <stdint.h>
-/*
#include <stdlib.h>
-#include <string.h>
-#include <strings.h>
-#include <math.h>
-*/
+#include <stdint.h>
+
#include "nmglobal.h"
#include "nbench1.h"
@@ -15,110 +11,91 @@
** This test implements a heapsort algorithm, performed on an
** array of longs.
*/
+static unsigned long DoNumSortIteration(long *arraybase, unsigned long arraysize, unsigned int numarrays);
+static void LoadNumArrayWithRand(long *array, unsigned long arraysize, unsigned int numarrays);
+static inline void NumHeapSort(long *array, unsigned long bottom, unsigned long top);
+static inline void NumSift(long *array, unsigned long min, unsigned long max);
/**************
** DoNumSort **
***************
** This routine performs the CPU numeric sort test.
-** NOTE: Last version incorrectly stated that the routine
-** returned result in # of longword sorted per second.
-** Not so; the routine returns # of iterations per sec.
*/
void DoNumSort(void) {
/* Error context string pointer */
- const char *errorcontext = "CPU:Numeric Sort";
+ const char *context = "CPU:Numeric Sort";
/* Local pointer to global struct */
SortStruct * numsortstruct = &global_numsortstruct;
long accumtime; /* Accumulated time */
double iterations; /* Iteration counter */
- long *arraybase; /* Base pointers of array */
- int systemerror; /* For holding error codes */
-
-/*
-** See if we need to do self adjustment code.
-*/
-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)
- {
- /*
- ** Allocate space for arrays
- */
- arraybase=(long *)AllocateMemory(sizeof(long) *
- numsortstruct->numarrays * numsortstruct->arraysize,
- &systemerror);
- if(systemerror)
- { ReportError(errorcontext,systemerror);
- FreeMemory((void *)arraybase,
- &systemerror);
- }
-
- /*
- ** Do an iteration of the numeric sort. If the
- ** elapsed time is less than or equal to the permitted
- ** minimum, then allocate for more arrays and
- ** try again.
- */
- if(DoNumSortIteration(arraybase,
- numsortstruct->arraysize,
- numsortstruct->numarrays)>global_min_ticks)
- break; /* We're ok...exit */
+ long *arraybase = NULL; /* Base pointers of array */
+
+ /*
+ ** See if we need to do self adjustment code.
+ */
+ 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) {
+ arraybase = realloc(arraybase, numsortstruct->numarrays * numsortstruct->arraysize * sizeof(long));
+ if (!arraybase) {
+ fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context);
+ exit(1);
+ }
- FreeMemory((void *)arraybase,&systemerror);
- if(numsortstruct->numarrays++>NUMNUMARRAYS)
- { printf("CPU:NSORT -- NUMNUMARRAYS hit.\n");
- }
- }
-}
-else
-{ /*
- ** Allocate space for arrays
- */
- arraybase=(long *)AllocateMemory(sizeof(long) *
- numsortstruct->numarrays * numsortstruct->arraysize,
- &systemerror);
- if(systemerror)
- { ReportError(errorcontext,systemerror);
- FreeMemory((void *)arraybase,
- &systemerror);
- }
+ /*
+ ** Do an iteration of the numeric sort. If the
+ ** elapsed time is less than or equal to the permitted
+ ** minimum, then allocate for more arrays and
+ ** try again.
+ */
+ if ( DoNumSortIteration(arraybase, numsortstruct->arraysize, numsortstruct->numarrays)>global_min_ticks ) {
+ break;
+ }
-}
-/*
-** All's well if we get here. Repeatedly perform sorts until the
-** accumulated elapsed time is greater than # of seconds requested.
-*/
-accumtime = 0L;
-iterations = 0.0;
+ if(numsortstruct->numarrays > NUMNUMARRAYS) {
+ printf("CPU:NSORT -- NUMNUMARRAYS hit.\n");
+ }
+ ++numsortstruct->numarrays;
+ }
+ } else {
+ arraybase = malloc(numsortstruct->numarrays * numsortstruct->arraysize * sizeof(long));
+ if (!arraybase) {
+ fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context);
+ exit(1);
+ }
+ }
-do {
- accumtime+=DoNumSortIteration(arraybase,
- numsortstruct->arraysize,
- numsortstruct->numarrays);
- iterations+=(double)1.0;
-} while(TicksToSecs(accumtime)<numsortstruct->request_secs);
+ /*
+ ** All's well if we get here. Repeatedly perform sorts until the
+ ** accumulated elapsed time is greater than # of seconds requested.
+ */
+ accumtime = 0L;
+ iterations = 0.0;
-/*
-** Clean up, calculate results, and go home. Be sure to
-** show that we don't have to rerun adjustment code.
-*/
-FreeMemory((void *)arraybase,&systemerror);
+ do {
+ accumtime += DoNumSortIteration(arraybase, numsortstruct->arraysize, numsortstruct->numarrays);
+ iterations += 1.0;
+ } while ( TicksToSecs(accumtime) < numsortstruct->request_secs );
-numsortstruct->sortspersec=iterations *
- (double)numsortstruct->numarrays / TicksToFracSecs(accumtime);
+ /*
+ ** Clean up, calculate results, and go home. Be sure to
+ ** show that we don't have to rerun adjustment code.
+ */
+ free(arraybase);
-if(numsortstruct->adjust==0)
- numsortstruct->adjust=1;
+ numsortstruct->sortspersec=iterations * (double)numsortstruct->numarrays / TicksToFracSecs(accumtime);
+ if ( numsortstruct->adjust == 0 ) {
+ numsortstruct->adjust = 1;
+ }
}
/***********************
@@ -128,34 +105,33 @@ if(numsortstruct->adjust==0)
** sort benchmark. It returns the number of ticks
** elapsed for the iteration.
*/
-static unsigned long DoNumSortIteration(long *arraybase,
- unsigned long arraysize,
- unsigned int numarrays)
-{
-unsigned long elapsed; /* Elapsed ticks */
-unsigned long i;
-/*
-** Load up the array with random numbers
-*/
-LoadNumArrayWithRand(arraybase,arraysize,numarrays);
-
-/*
-** Start the stopwatch
-*/
-elapsed=StartStopwatch();
-
-/*
-** Execute a heap of heapsorts
-*/
-for(i=0;i<numarrays;i++)
- NumHeapSort(arraybase+i*arraysize,0L,arraysize-1L);
+static unsigned long DoNumSortIteration(long *arraybase, unsigned long arraysize, unsigned int numarrays) {
+ unsigned long elapsed; /* Elapsed ticks */
+ unsigned long i;
+
+ /*
+ ** Load up the array with random numbers
+ */
+ LoadNumArrayWithRand(arraybase, arraysize, numarrays);
+
+ /*
+ ** Start the stopwatch
+ */
+ elapsed = StartStopwatch();
+
+ /*
+ ** Execute a heap of heapsorts
+ */
+ for(i = 0; i < numarrays; i++ ) {
+ NumHeapSort(arraybase + i * arraysize, 0L, arraysize - 1L);
+ }
-/*
-** Get elapsed time
-*/
-elapsed=StopStopwatch(elapsed);
+ /*
+ ** Get elapsed time
+ */
+ elapsed = StopStopwatch(elapsed);
-return(elapsed);
+ return(elapsed);
}
/*************************
@@ -163,37 +139,33 @@ return(elapsed);
**************************
** 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
-*/
-/* randnum(13L); */
-randnum(13);
-
-/*
-** Load up first array with randoms
-*/
-for(i=0L;i<arraysize;i++)
- /* array[i]=randnum(0L); */
- array[i]=randnum((int32_t)0);
-
-/*
-** Now, if there's more than one array to load, copy the
-** first into each of the others.
-*/
-darray=array;
-while(--numarrays)
-{ darray+=arraysize;
- for(i=0L;i<arraysize;i++)
- darray[i]=array[i];
-}
+ long i; /* Used for index */
+ long *darray; /* Destination array pointer */
+ /*
+ ** Initialize the random number generator
+ */
+ randnum(13);
+
+ /*
+ ** Load up first array with randoms
+ */
+ for( i = 0; i < arraysize; i++ ) {
+ array[i] = randnum(0);
+ }
-return;
+ /*
+ ** Now, if there's more than one array to load, copy the
+ ** first into each of the others.
+ */
+ darray = array;
+ while ( --numarrays ) {
+ darray += arraysize;
+ memcpy(darray, array, arraysize * sizeof(long));
+ }
}
/****************
@@ -203,31 +175,29 @@ return;
** integers. Also pass in minimum and maximum offsets.
** This routine performs a heap sort on that array.
*/
-static void NumHeapSort(long *array,
- unsigned long bottom, /* Lower bound */
- unsigned long top) /* Upper bound */
-{
-unsigned long temp; /* Used to exchange elements */
-unsigned long i; /* Loop index */
+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)
- NumSift(array,i,top);
+ /*
+ ** First, build a heap in the array
+ */
+ for (i = (top / 2L); i > 0; i-- ) {
+ NumSift(array, i, top);
+ }
-/*
-** Repeatedly extract maximum from heap and place it at the
-** end of the array. When we get done, we'll have a sorted
-** array.
-*/
-for(i=top; i>0; --i)
-{ NumSift(array,bottom,i);
- temp=*array; /* Perform exchange */
- *array=*(array+i);
- *(array+i)=temp;
-}
-return;
+ /*
+ ** Repeatedly extract maximum from heap and place it at the
+ ** end of the array. When we get done, we'll have a sorted
+ ** array.
+ */
+ for ( i = top; i > 0; i-- ) {
+ NumSift(array, bottom, i);
+
+ temp = *array;
+ *array = *(array + i);
+ *(array + i) = temp;
+ }
}
/************
@@ -236,28 +206,25 @@ return;
** Peforms the sift operation on a numeric array,
** constructing a heap in the array.
*/
-static void NumSift(long *array,/* Array of numbers */
- unsigned long i, /* Minimum of array */
- unsigned long j) /* Maximum of array */
-{
+static inline void NumSift(long *array, unsigned long min, unsigned long max) {
unsigned long k;
unsigned long temp; /* Used for exchange */
- while ( ( i + i ) <= j ) {
- k = i + i;
- if ( k < j ) {
+ while ( ( min * 2 ) <= max ) {
+ k = min * 2;
+ if ( k < max ) {
if ( array[k] < array[k+1L] ) {
++k;
}
}
- if ( array[i] < array[k] ) {
+ if ( array[min] < array[k] ) {
temp = array[k];
- array[k] = array[i];
- array[i] = temp;
- i = k;
+ array[k] = array[min];
+ array[min] = temp;
+ min = k;
} else {
- i = j + 1;
+ min = max + 1;
}
}
}