Skip to content

Commit 79e60b4

Browse files
author
lsleonard
committed
v2.1.7 updates
1. In main.c, changed benchmarking to have max of 2000 external loops under timer with remaining loops run inside timer. 2. In td64.c, decodeAdaptiveTextMode, made dtbmThisVal internal to function rather than static global. Added a read-ahead byte so that peaking at bits does not require an extra read. The read-ahead means main loop must stop three values early to avoid reading beyond end of input values, and those three values must be processed without read-ahead. 3. In td64.c, decodeStringMode, made dsmThisVal internal to function rather than static global.
1 parent a9774fc commit 79e60b4

File tree

7 files changed

+241
-109
lines changed

7 files changed

+241
-109
lines changed

main.c

Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <string.h>
3131

3232
#define BENCHMARK_LOOP_COUNT // special loop count for benchmarking
33+
#define EXTERNAL_LOOP_COUNT_MAX 2000
3334
//#define TEST_TD512 // invokes test_td512_1to512
3435

3536
#ifdef TD512_TEST_MODE
@@ -94,7 +95,7 @@ int main(int argc, char* argv[])
9495
int loopCnt; // argv[4] option: default is 1
9596
uint32_t blockSize=512; // block size to use when iterating through file
9697

97-
printf("tiny data compression td512 %s block size: %d\n", TD512_VERSION, blockSize);
98+
printf("tiny data compression td512 %s\n", TD512_VERSION);
9899
#ifdef TEST_TD512
99100
int32_t retVal;
100101
if ((retVal=test_td512_1to512()) != 0) // do check of 1 to 512 values
@@ -132,7 +133,7 @@ int main(int argc, char* argv[])
132133
if (argc >= 3)
133134
{
134135
sscanf(argv[2], "%d", &loopCnt);
135-
if (loopCnt < 1 || loopCnt > 1000)
136+
if (loopCnt < 1)
136137
loopCnt = 1;
137138
}
138139
else
@@ -141,41 +142,60 @@ int main(int argc, char* argv[])
141142
}
142143
#ifdef BENCHMARK_LOOP_COUNT // special loop count for benchmarking
143144
loopCnt = 100000000 / len;
144-
loopCnt = (loopCnt < 20) ? 10 : loopCnt;
145-
loopCnt = (loopCnt > 2000) ? 2000 : loopCnt;
145+
loopCnt = (loopCnt < 20) ? 20 : loopCnt;
146146
#endif
147147
loopNum = 0;
148-
printf(" loop count=%d\n", loopCnt);
149-
148+
uint32_t internalLoop;
149+
internalLoop = 1;
150+
uint32_t savedInternalLoopCnt = 1;
151+
if (loopCnt > EXTERNAL_LOOP_COUNT_MAX)
152+
{
153+
savedInternalLoopCnt = loopCnt/EXTERNAL_LOOP_COUNT_MAX;
154+
loopCnt = EXTERNAL_LOOP_COUNT_MAX;
155+
}
156+
printf(" block size= %d loop count= %d*%d\n", blockSize, loopCnt, savedInternalLoopCnt);
157+
150158
COMPRESS_LOOP:
159+
internalLoop = savedInternalLoopCnt;
151160
nBytesRemaining=(int32_t)len;
152161
totalCompressedBytes=0;
153162
srcBlockOffset=0;
154163
dstBlockOffset=0;
155164

156165
// compress and write result
157166
begin = clock();
158-
while (nBytesRemaining > 0)
167+
while (internalLoop > 0)
159168
{
160-
uint32_t nBlockBytes=(uint32_t)nBytesRemaining>=blockSize ? blockSize : (uint32_t)nBytesRemaining;
161-
nCompressedBytes = td512(src+srcBlockOffset, dst+dstBlockOffset, nBlockBytes);
162-
if (nCompressedBytes < 0)
163-
exit(nCompressedBytes); // error occurred
164-
nBytesRemaining -= nBlockBytes;
165-
totalCompressedBytes += (uint32_t)nCompressedBytes;
166-
srcBlockOffset += nBlockBytes;
167-
dstBlockOffset += (uint32_t)nCompressedBytes;
169+
while (nBytesRemaining > 0)
170+
{
171+
uint32_t nBlockBytes=(uint32_t)nBytesRemaining>=blockSize ? blockSize : (uint32_t)nBytesRemaining;
172+
nCompressedBytes = td512(src+srcBlockOffset, dst+dstBlockOffset, nBlockBytes);
173+
if (nCompressedBytes < 0)
174+
exit(nCompressedBytes); // error occurred
175+
nBytesRemaining -= nBlockBytes;
176+
totalCompressedBytes += (uint32_t)nCompressedBytes;
177+
srcBlockOffset += nBlockBytes;
178+
dstBlockOffset += (uint32_t)nCompressedBytes;
179+
}
180+
if (--internalLoop > 0)
181+
{
182+
// perform some loops within the timing structure
183+
totalCompressedBytes = 0;
184+
nBytesRemaining=(int32_t)len;
185+
srcBlockOffset=0;
186+
dstBlockOffset=0;
187+
}
168188
}
169189
end = clock();
170190
timeSpent = (double)(end - begin) / (double)CLOCKS_PER_SEC;
171-
if (timeSpent < minTimeSpent)
191+
if (timeSpent < minTimeSpent && timeSpent > 1.e-10)
172192
minTimeSpent = timeSpent;
173193
if (++loopNum < loopCnt)
174194
{
175195
usleep(10); // sleep 10 us
176196
goto COMPRESS_LOOP;
177197
}
178-
timeSpent = minTimeSpent;
198+
timeSpent = minTimeSpent / savedInternalLoopCnt;
179199
printf("compression=%.02f%% %.00f bytes per second inbytes=%lu outbytes=%u\n", (float)100*(1.0-((float)totalCompressedBytes/(float)len)), (float)len/(float)timeSpent, len, totalCompressedBytes);
180200
#ifdef TD512_TEST_MODE
181201
double totalBlocks=gExtendedTextCnt+gExtendedStringCnt+gtd64Cnt;
@@ -209,34 +229,45 @@ int main(int argc, char* argv[])
209229
loopNum = 0;
210230
DECOMPRESS_LOOP:
211231
// decompress and write result
232+
internalLoop = savedInternalLoopCnt;
212233
totalOutBytes = 0;
213234
nBytesRemaining = (int32_t)len3;
214235
srcBlockOffset = 0;
215236
dstBlockOffset = 0;
216237
begin = clock();
217-
while (nBytesRemaining > 0)
238+
while (internalLoop > 0)
218239
{
219-
int32_t nRetBytes;
220-
nRetBytes = td512d(src+srcBlockOffset, dst+dstBlockOffset, &bytesProcessed);
221-
if (nRetBytes < 0)
222-
return nRetBytes;
223-
assert(nBytesRemaining>=blockSize?nRetBytes==blockSize:1);
224-
nBytesRemaining -= bytesProcessed;
225-
totalOutBytes += (uint32_t)nRetBytes;
226-
srcBlockOffset += bytesProcessed;
227-
dstBlockOffset += (uint32_t)nRetBytes;
240+
while (nBytesRemaining > 0)
241+
{
242+
int32_t nRetBytes;
243+
nRetBytes = td512d(src+srcBlockOffset, dst+dstBlockOffset, &bytesProcessed);
244+
if (nRetBytes < 0)
245+
return nRetBytes;
246+
assert(nBytesRemaining>=blockSize?nRetBytes==blockSize:1);
247+
nBytesRemaining -= bytesProcessed;
248+
totalOutBytes += (uint32_t)nRetBytes;
249+
srcBlockOffset += bytesProcessed;
250+
dstBlockOffset += (uint32_t)nRetBytes;
251+
}
252+
if (--internalLoop > 0)
253+
{
254+
totalOutBytes = 0;
255+
nBytesRemaining = (int32_t)len3;
256+
srcBlockOffset = 0;
257+
dstBlockOffset = 0;
258+
}
228259
}
229260
end = clock();
230261
timeSpent = (double)(end - begin) / (double)CLOCKS_PER_SEC;
231-
if (timeSpent < minTimeSpent)
262+
if (timeSpent < minTimeSpent && timeSpent > 1.e-10)
232263
minTimeSpent = timeSpent;
233264
if (++loopNum < loopCnt)
234265
{
235266
usleep(10); // sleep 10 us
236267
goto DECOMPRESS_LOOP;
237268
}
238-
timeSpent = minTimeSpent;
239-
printf("decompression=%.00f bytes per second inbytes=%lu outbytes=%lu\n", (float)len/(float)timeSpent, len3, len);
269+
timeSpent = minTimeSpent / savedInternalLoopCnt;
270+
printf("decompression=%.00f bytes per second inbytes=%lu outbytes=%u\n", (float)len/(float)timeSpent, len3, totalOutBytes);
240271
fwrite(dst, len, 1, ofile);
241272
fclose(ofile);
242273
free(src);
@@ -256,7 +287,7 @@ int main(int argc, char* argv[])
256287
printf("td512 error: decompressed file differs from original input file\n");
257288
free(src);
258289
free(dst);
259-
return 11;
290+
return 31;
260291
}
261292
free(src);
262293
free(dst);

td512.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ uint32_t checktd64(const unsigned char *inVals, unsigned char *tempOutVals)
5454
// return 0 to select extended string mode
5555
// 1 to select td64
5656
// 2 to select td64 after processing first 64 as random
57-
unsigned char val256[256]={0};
58-
uint32_t count[MAX_TD64_BYTES]={0};
57+
uint8_t val256[256]={0};
58+
uint8_t count[MAX_TD64_BYTES]={0};
5959
uint32_t highBitCheck=0;
6060
uint32_t i=0;
6161

@@ -91,8 +91,8 @@ uint32_t checktd64(const unsigned char *inVals, unsigned char *tempOutVals)
9191
retBits = encodeExtendedStringMode(inVals, tempOutVals, 64, &nValuesRead);
9292
if (retBits <= 0)
9393
return 1; // process this block with td64
94-
if (retBits > (retBitstd64=td64(inVals, tempOutVals, 64)))
95-
return retBitstd64; // pick td64 if it compresses better and return compressed values
94+
if (retBits+16 > (retBitstd64=td64(inVals, tempOutVals, 64)))
95+
return retBitstd64; // pick td64 if string mode is less than 3% better and return compressed values
9696
}
9797
return 0;
9898
} // end checktd64
@@ -103,6 +103,7 @@ uint32_t checkTextMode(const unsigned char *inVals, uint32_t nValues, uint32_t *
103103
// return 0 to skip text mode
104104
// 1 to use text mode
105105
// 2 to use string mode
106+
106107
if (nValues < 96)
107108
return 0; // expecting at least 96 values
108109
uint32_t charCount=0;

td512.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,20 @@
5555
2. In td512.c, modified checktd64 (name change from checkSingleValueMode) to return a code for expected random data to be processed as a failing block of 64 bytes in the same way that td64 would handle this block.
5656
3. In td64.c, modified decodeAdaptiveTextMode to read one byte ahead to improve speed of processing dtbmPeekBits. This change can require one byte read beyond length of input array.
5757
*/
58+
// Notes for version 2.1.7:
59+
/*
60+
1. In main.c, changed benchmarking to have max of 2000 external loops under timer with remaining loops run inside timer.
61+
2. In td64.c, decodeAdaptiveTextMode, made dtbmThisVal internal to function rather than static global. Added a read-ahead byte so that peaking at bits does not require an extra read. The read-ahead means main loop must stop three values early to avoid reading beyond end of input values, and those three values must be processed without read-ahead.
62+
3. In td64.c, decodeStringMode, made dsmThisVal internal to function rather than static global.
63+
*/
5864
#ifndef td512_h
5965
#define td512_h
6066

6167
#include "td64.h"
6268
#include "tdString.h"
6369
#include <unistd.h>
6470

65-
#define TD512_VERSION "v2.1.6"
71+
#define TD512_VERSION "v2.1.7"
6672
#define MIN_VALUES_EXTENDED_MODE 128
6773
#define MIN_UNIQUES_SINGLE_VALUE_MODE_CHECK 14
6874
#define MIN_VALUES_TO_COMPRESS 16

0 commit comments

Comments
 (0)