git » fp-git.git » commit 1402e4e

optimized maskBit function

author ecalot
2005-03-13 21:58:14 UTC
committer ecalot
2005-03-13 21:58:14 UTC
parent 5627c7910b41f7991e208b1ea048a983cb6eaa3e

optimized maskBit function

PR/src/lib/compression/lzg_compress.c +9 -13
stuff/contest/lzg/lzg_compress.c +9 -13

diff --git a/PR/src/lib/compression/lzg_compress.c b/PR/src/lib/compression/lzg_compress.c
index 5c3e9f6..1249946 100644
--- a/PR/src/lib/compression/lzg_compress.c
+++ b/PR/src/lib/compression/lzg_compress.c
@@ -138,22 +138,22 @@ void search_best_pattern(unsigned char *input, int inputSize,
 
 /* Insert the specified bit in the current maskByte. If the maskByte is full,
  * start a new one. */
-void pushMaskBit(int b, unsigned char **maskByte, int *maskBit,
+void pushMaskBit(int b, unsigned char **maskByte, 
                  unsigned char *output, int *outputPos)
 {
-	if ( (!(*maskByte)) || (*maskBit == 8) )
+	static int maskBit=8;
+	if ( maskBit == 8 ) /* first time or maskBit is full */
 	{
 		/* start a new maskByte */
 		*maskByte = output + *outputPos;
 		(*outputPos)++;
 		**maskByte = 0;
-		*maskBit = 0;
+		maskBit = 0;
 printf("maskbyte i=%d\n", *outputPos - 1);
 	}
 
-	**maskByte >>= 1;
-	**maskByte += b << 7; /* TODO: |= */
-	(*maskBit)++;
+	**maskByte |= b<<maskBit;
+	maskBit++;
 }
 
 /* Insert the two bytes describing the pattern repetition to the output. */
@@ -174,8 +174,7 @@ void compressLzg(unsigned char* input, int inputSize,
                  unsigned char* output, int *outputSize)
 {
 	int inputPos = 0, outputPos = 0;
-	unsigned char *maskByte = NULL;
-	int maskBit = 0;
+	unsigned char *maskByte;
 	int i;
 
 	/* Create ghost window filled with zeros before input data: */
@@ -196,7 +195,7 @@ void compressLzg(unsigned char* input, int inputSize,
 		if (best_pattern_len < MIN_PATTERN_SIZE)
 		{
 			/* No suitable pattern found. Just copy the current byte. */
-			pushMaskBit(1, &maskByte, &maskBit, output, &outputPos);
+			pushMaskBit(1, &maskByte, output, &outputPos);
 			output[outputPos] = input[inputPos];
 printf("copy i=%d o=%d data=%02x\n", outputPos, inputPos, output[outputPos]);
 			inputPos++;
@@ -205,7 +204,7 @@ printf("copy i=%d o=%d data=%02x\n", outputPos, inputPos, output[outputPos]);
 		else
 		{
 			/* Can compress. Repeat the best pattern. */
-			pushMaskBit(0, &maskByte, &maskBit, output, &outputPos);
+			pushMaskBit(0, &maskByte, output, &outputPos);
 			addPattern(input, inputPos, output, outputPos,
 			           best_pattern, best_pattern_len);
 			inputPos += best_pattern_len;
@@ -213,9 +212,6 @@ printf("copy i=%d o=%d data=%02x\n", outputPos, inputPos, output[outputPos]);
 		}
 	}
 
-	/* finish last maskByte: */
-	*maskByte >>= (8 - maskBit); /* TODO: optimize with ~ */
-
 	*outputSize = outputPos;
 }
 
diff --git a/stuff/contest/lzg/lzg_compress.c b/stuff/contest/lzg/lzg_compress.c
index 5c3e9f6..1249946 100644
--- a/stuff/contest/lzg/lzg_compress.c
+++ b/stuff/contest/lzg/lzg_compress.c
@@ -138,22 +138,22 @@ void search_best_pattern(unsigned char *input, int inputSize,
 
 /* Insert the specified bit in the current maskByte. If the maskByte is full,
  * start a new one. */
-void pushMaskBit(int b, unsigned char **maskByte, int *maskBit,
+void pushMaskBit(int b, unsigned char **maskByte, 
                  unsigned char *output, int *outputPos)
 {
-	if ( (!(*maskByte)) || (*maskBit == 8) )
+	static int maskBit=8;
+	if ( maskBit == 8 ) /* first time or maskBit is full */
 	{
 		/* start a new maskByte */
 		*maskByte = output + *outputPos;
 		(*outputPos)++;
 		**maskByte = 0;
-		*maskBit = 0;
+		maskBit = 0;
 printf("maskbyte i=%d\n", *outputPos - 1);
 	}
 
-	**maskByte >>= 1;
-	**maskByte += b << 7; /* TODO: |= */
-	(*maskBit)++;
+	**maskByte |= b<<maskBit;
+	maskBit++;
 }
 
 /* Insert the two bytes describing the pattern repetition to the output. */
@@ -174,8 +174,7 @@ void compressLzg(unsigned char* input, int inputSize,
                  unsigned char* output, int *outputSize)
 {
 	int inputPos = 0, outputPos = 0;
-	unsigned char *maskByte = NULL;
-	int maskBit = 0;
+	unsigned char *maskByte;
 	int i;
 
 	/* Create ghost window filled with zeros before input data: */
@@ -196,7 +195,7 @@ void compressLzg(unsigned char* input, int inputSize,
 		if (best_pattern_len < MIN_PATTERN_SIZE)
 		{
 			/* No suitable pattern found. Just copy the current byte. */
-			pushMaskBit(1, &maskByte, &maskBit, output, &outputPos);
+			pushMaskBit(1, &maskByte, output, &outputPos);
 			output[outputPos] = input[inputPos];
 printf("copy i=%d o=%d data=%02x\n", outputPos, inputPos, output[outputPos]);
 			inputPos++;
@@ -205,7 +204,7 @@ printf("copy i=%d o=%d data=%02x\n", outputPos, inputPos, output[outputPos]);
 		else
 		{
 			/* Can compress. Repeat the best pattern. */
-			pushMaskBit(0, &maskByte, &maskBit, output, &outputPos);
+			pushMaskBit(0, &maskByte, output, &outputPos);
 			addPattern(input, inputPos, output, outputPos,
 			           best_pattern, best_pattern_len);
 			inputPos += best_pattern_len;
@@ -213,9 +212,6 @@ printf("copy i=%d o=%d data=%02x\n", outputPos, inputPos, output[outputPos]);
 		}
 	}
 
-	/* finish last maskByte: */
-	*maskByte >>= (8 - maskBit); /* TODO: optimize with ~ */
-
 	*outputSize = outputPos;
 }