summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Turner <mattst88@gmail.com>2008-11-15 02:37:34 +0000
committerMatt Turner <mattst88@gmail.com>2008-11-15 02:37:34 +0000
commitfd21c50949e99a978c211c46bc56852460f79bbc (patch)
tree8645b1669f19a07b5181d9ad5181aef2e0634fc2
parent5bded43a8b43a0da5abf953c37ffd6fcd77f4443 (diff)
Clean huffman struct. Replace with local static variables
git-svn-id: svn://mattst88.com/svn/cleanbench/trunk@50 0d43b9a7-5ab2-4d7b-af9d-f64450cef757
-rw-r--r--cleanbench.c8
-rw-r--r--huffman.c54
-rw-r--r--nmglobal.h32
3 files changed, 30 insertions, 64 deletions
diff --git a/cleanbench.c b/cleanbench.c
index 50b2c43..3b17ee7 100644
--- a/cleanbench.c
+++ b/cleanbench.c
@@ -77,12 +77,6 @@ for (i = 0; i < NUMTESTS; i++) {
tests_to_do[i] = TRUE;
}
-/*
-** Initialize test data structures to default
-** values.
-*/
-global_huffstruct.arraysize=HUFFARRAYSIZE;
-
puts("BYTEmark* Native Mode Benchmark ver. 2 (10/95)");
puts("Index-split by Andrew D. Balsa (11/97)");
puts("Linux/Unix* port by Uwe F. Mayer (12/96,11/97)");
@@ -90,7 +84,7 @@ puts("\nTEST : Iterations/sec. : Old Index : New Index");
puts(" : : Pentium 90* : AMD K6/233*");
puts("--------------------:------------------:-------------:------------");
-for(i=IDEA;i<NUMTESTS;i++)
+for(i=HUFFMAN;i<NUMTESTS;i++)
{
if(tests_to_do[i])
{ printf("%s :",ftestnames[i]);
diff --git a/huffman.c b/huffman.c
index 168f29a..7ebdd81 100644
--- a/huffman.c
+++ b/huffman.c
@@ -15,13 +15,23 @@
************************/
/*
-** DEFINES
+** This constant specifies the maximum number of Huffman
+** compression loops the system will try for. This keeps
+** the test from going off into the weeds. This is not
+** a critical constant, and can be increased if your
+** system is a real barn-burner.
*/
-#define EXCLUDED 32000L /* Big positive value */
+/*#define LOOP_MAX 50000L*/
+#define LOOP_MAX 500000L
/*
-** TYPEDEFS
+** Following constant sets the size of the arrays to
+** be compressed/uncompressed.
*/
+#define ARRAY_SIZE 5000
+
+#define EXCLUDED 32000L /* Big positive value */
+
typedef struct {
unsigned char c; /* Byte value */
float freq; /* Frequency */
@@ -30,24 +40,17 @@ typedef struct {
int right; /* Right pointer = 1 */
} huff_node;
-/*
-** GLOBALS
-*/
static huff_node *hufftree; /* The huffman tree */
static long plaintextlen; /* Length of plaintext */
-/*
-** PROTOTYPES
-*/
static void create_text_line(char *dt,long nchars);
static void create_text_block(char *tb, unsigned long tblen,
unsigned short maxlinlen);
static clock_t DoHuffIteration(char *plaintext, char *comparray, char *decomparray,
- unsigned long arraysize, unsigned long nloops, huff_node *hufftree);
+ unsigned long nloops, huff_node *hufftree);
static void SetCompBit(uint8_t *comparray, uint32_t bitoffset, char bitchar);
static int GetCompBit(uint8_t *comparray, uint32_t bitoffset);
-
/**************
** DoHuffman **
***************
@@ -68,6 +71,7 @@ DoHuffman(void)
clock_t total_time = 0;
int iterations = 0;
static int is_adjusted = FALSE;
+ static int loops = 100;
/*
** Allocate memory for the plaintext and the compressed text.
@@ -80,20 +84,20 @@ DoHuffman(void)
** than 512 bytes. This is actually a super-conservative
** estimate...but, who cares?)
*/
- plaintext = malloc(lochuffstruct->arraysize);
+ plaintext = malloc(ARRAY_SIZE * sizeof(char));
if (!plaintext) {
fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context);
exit(1);
}
- comparray = malloc(lochuffstruct->arraysize);
+ comparray = malloc(ARRAY_SIZE * sizeof(char));
if (!comparray) {
fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context);
free(plaintext);
exit(1); /* FIXME: do I need exits here? */
}
- decomparray = malloc(lochuffstruct->arraysize);
+ decomparray = malloc(ARRAY_SIZE * sizeof(char));
if (!decomparray) {
fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context);
free(plaintext);
@@ -120,9 +124,9 @@ DoHuffman(void)
** added by Uwe F. Mayer
*/
randnum((int32_t)13);
- create_text_block(plaintext,lochuffstruct->arraysize-1,(unsigned short)500);
- plaintext[lochuffstruct->arraysize-1L]='\0';
- plaintextlen=lochuffstruct->arraysize;
+ create_text_block(plaintext,ARRAY_SIZE-1,(unsigned short)500);
+ plaintext[ARRAY_SIZE-1L]='\0';
+ plaintextlen=ARRAY_SIZE;
/*
** See if we need to perform self adjustment loop.
@@ -134,16 +138,16 @@ DoHuffman(void)
** # of loops and increasing the loop count until we
** get a number of loops that we can use.
*/
- for (lochuffstruct->loops = 100; lochuffstruct->loops < MAXHUFFLOOPS; lochuffstruct->loops += 10) {
- if (DoHuffIteration(plaintext, comparray, decomparray, lochuffstruct->arraysize, lochuffstruct->loops, hufftree) > MINIMUM_TICKS) {
+ for (; loops < LOOP_MAX; loops += 10) {
+ if (DoHuffIteration(plaintext, comparray, decomparray, loops, hufftree) > MINIMUM_TICKS) {
break;
}
}
}
do {
- total_time += DoHuffIteration(plaintext, comparray, decomparray, lochuffstruct->arraysize, lochuffstruct->loops, hufftree);
- iterations += lochuffstruct->loops;
+ total_time += DoHuffIteration(plaintext, comparray, decomparray, loops, hufftree);
+ iterations += loops;
} while (total_time < MINIMUM_SECONDS * CLOCKS_PER_SEC);
free(plaintext);
@@ -267,7 +271,7 @@ bytessofar+=linelen;
** (c) Decompresses the text and verifies correct decompression
*/
static clock_t
-DoHuffIteration(char *plaintext, char *comparray, char *decomparray, unsigned long arraysize, unsigned long nloops, huff_node *hufftree)
+DoHuffIteration(char *plaintext, char *comparray, char *decomparray, unsigned long nloops, huff_node *hufftree)
{
clock_t start, stop;
int i; /* Index */
@@ -299,12 +303,12 @@ for(i=0;i<256;i++)
hufftree[i].c=(unsigned char)i;
}
-for(j=0;j<arraysize;j++)
+for(j=0;j<ARRAY_SIZE;j++)
hufftree[(int)plaintext[j]].freq+=(float)1.0;
for(i=0;i<256;i++)
if(hufftree[i].freq != (float)0.0)
- hufftree[i].freq/=(float)arraysize;
+ hufftree[i].freq/=(float)ARRAY_SIZE;
/* Reset the second half of the tree. Otherwise the loop below that
** compares the frequencies up to index 512 makes no sense. Some
@@ -388,7 +392,7 @@ while(1)
** Huffman tree built...compress the plaintext
*/
bitoffset=0L; /* Initialize bit offset */
-for(i=0;i<arraysize;i++)
+for(i=0;i<ARRAY_SIZE;i++)
{
c=(int)plaintext[i]; /* Fetch character */
/*
diff --git a/nmglobal.h b/nmglobal.h
index cd49cad..85f030b 100644
--- a/nmglobal.h
+++ b/nmglobal.h
@@ -62,40 +62,8 @@ typedef struct {
double results; /* Results */
} IDEAStruct;
-
-/************************
-** HUFFMAN COMPRESSION **
-************************/
-
-/*
-** DEFINES
-*/
-/*
-** MAXHUFFLOOPS
-**
-** This constant specifies the maximum number of Huffman
-** compression loops the system will try for. This keeps
-** the test from going off into the weeds. This is not
-** a critical constant, and can be increased if your
-** system is a real barn-burner.
-*/
-/*#define MAXHUFFLOOPS 50000L*/
-#define MAXHUFFLOOPS 500000L
-
-/*
-** Following constant sets the size of the arrays to
-** be compressed/uncompressed.
-*/
-#define HUFFARRAYSIZE 5000L
-
-/*
-** TYPEDEFS
-*/
-
typedef struct {
double results; /* Results */
- unsigned long arraysize; /* Size of array */
- unsigned long loops; /* # of times to compress/decompress */
} HuffStruct;
/********************************