ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitCommon/OptIO/src/fpc.c
Revision: 1.1
Committed: Thu Feb 26 14:35:15 2009 UTC (16 years, 2 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_032, Mit_031, Mit_025c_branch2, Mit_025c_branch1, Mit_030, Mit_029c, Mit_030_pre1, Mit_029a, Mit_029, Mit_029_pre1, Mit_028a, Mit_025c_branch0, Mit_028, Mit_027a, Mit_027, Mit_026, Mit_025e, Mit_025d, Mit_025c, Mit_025b, Mit_025a, Mit_025, Mit_025pre2, Mit_024b, Mit_025pre1, Mit_024a, Mit_024, Mit_023, Mit_022a, Mit_022, Mit_020d, TMit_020d, Mit_020c, Mit_021, Mit_021pre2, Mit_021pre1, Mit_020b, Mit_020a, Mit_020, Mit_020pre1, Mit_018, Mit_017, Mit_017pre3, Mit_017pre2, Mit_017pre1, V07-05-00, Mit_016, Mit_015b, Mit_015a, Mit_015, Mit_014e, Mit_014d, Mit_014c, Mit_014b, ConvRejection-10-06-09, Mit_014a, Mit_014, Mit_014pre3, Mit_014pre2, Mit_014pre1, Mit_013d, Mit_013c, Mit_013b, Mit_013a, Mit_013, Mit_013pre1, Mit_012i, Mit_012g, Mit_012f, Mit_012e, Mit_012d, Mit_012c, Mit_012b, Mit_012a, Mit_012, Mit_011a, Mit_011, Mit_010a, Mit_010, Mit_009c, Mit_009b, Mit_009a, Mit_009, Mit_008, Mit_008pre2, Mit_008pre1, HEAD
Branch point for: Mit_025c_branch
Log Message:
Added FLT compression (does not help at this point).

File Contents

# Content
1 // $Id:$
2
3 const long long mask[8] =
4 {0x0000000000000000LL,
5 0x00000000000000ffLL,
6 0x000000000000ffffLL,
7 0x0000000000ffffffLL,
8 0x000000ffffffffffLL,
9 0x0000ffffffffffffLL,
10 0x00ffffffffffffffLL,
11 0xffffffffffffffffLL};
12
13 void Compress(long predsizem1,
14 unsigned long long *inbuf, unsigned int inlen,
15 unsigned char *outbuf, unsigned int *outlen)
16 {
17 register long i, out, intot, hash, dhash, code, bcode, ioc;
18 register long long val, lastval, stride, pred1, pred2, xor1, xor2;
19 register long long *fcm, *dfcm;
20
21 predsizem1 = (1L << predsizem1) - 1;
22
23 hash = 0;
24 dhash = 0;
25 lastval = 0;
26 pred1 = 0;
27 pred2 = 0;
28 fcm = (long long *)calloc(predsizem1 + 1, 8);
29 dfcm = (long long *)calloc(predsizem1 + 1, 8);
30 if (!fcm || !dfcm) {
31 return;
32 }
33
34 intot = inlen;
35 if (intot>0) {
36 val = inbuf[0];
37 out = 0 + ((intot + 1) >> 1);
38 *((long long *)&outbuf[(out >> 3) << 3]) = 0;
39 for (i = 0; i < intot; i += 2) {
40 xor1 = val ^ pred1;
41 fcm[hash] = val;
42 hash = ((hash << 6) ^ ((unsigned long long)val >> 48)) & predsizem1;
43 pred1 = fcm[hash];
44
45 stride = val - lastval;
46 xor2 = val ^ (lastval + pred2);
47 lastval = val;
48 val = inbuf[i + 1];
49 dfcm[dhash] = stride;
50 dhash = ((dhash << 2) ^ ((unsigned long long)stride >> 40)) & predsizem1;
51 pred2 = dfcm[dhash];
52
53 code = 0;
54 if ((unsigned long long)xor1 > (unsigned long long)xor2) {
55 code = 0x80;
56 xor1 = xor2;
57 }
58 bcode = 7; // 8 bytes
59 if (0 == (xor1 >> 56))
60 bcode = 6; // 7 bytes
61 if (0 == (xor1 >> 48))
62 bcode = 5; // 6 bytes
63 if (0 == (xor1 >> 40))
64 bcode = 4; // 5 bytes
65 if (0 == (xor1 >> 24))
66 bcode = 3; // 3 bytes
67 if (0 == (xor1 >> 16))
68 bcode = 2; // 2 bytes
69 if (0 == (xor1 >> 8))
70 bcode = 1; // 1 byte
71 if (0 == xor1)
72 bcode = 0; // 0 bytes
73
74 *((long long *)&outbuf[(out >> 3) << 3]) |= xor1 << ((out & 0x7) << 3);
75 if (0 == (out & 0x7))
76 xor1 = 0;
77 *((long long *)&outbuf[((out >> 3) << 3) + 8]) =
78 (unsigned long long)xor1 >> (64 - ((out & 0x7) << 3));
79
80 out += bcode + (bcode >> 2);
81 code |= bcode << 4;
82
83 xor1 = val ^ pred1;
84 fcm[hash] = val;
85 hash = ((hash << 6) ^ ((unsigned long long)val >> 48)) & predsizem1;
86 pred1 = fcm[hash];
87
88 stride = val - lastval;
89 xor2 = val ^ (lastval + pred2);
90 lastval = val;
91 val = inbuf[i + 2];
92 dfcm[dhash] = stride;
93 dhash = ((dhash << 2) ^ ((unsigned long long)stride >> 40)) & predsizem1;
94 pred2 = dfcm[dhash];
95
96 bcode = code | 0x8;
97 if ((unsigned long long)xor1 > (unsigned long long)xor2) {
98 code = bcode;
99 xor1 = xor2;
100 }
101 bcode = 7; // 8 bytes
102 if (0 == (xor1 >> 56))
103 bcode = 6; // 7 bytes
104 if (0 == (xor1 >> 48))
105 bcode = 5; // 6 bytes
106 if (0 == (xor1 >> 40))
107 bcode = 4; // 5 bytes
108 if (0 == (xor1 >> 24))
109 bcode = 3; // 3 bytes
110 if (0 == (xor1 >> 16))
111 bcode = 2; // 2 bytes
112 if (0 == (xor1 >> 8))
113 bcode = 1; // 1 byte
114 if (0 == xor1)
115 bcode = 0; // 0 bytes
116
117 *((long long *)&outbuf[(out >> 3) << 3]) |= xor1 << ((out & 0x7) << 3);
118 if (0 == (out & 0x7))
119 xor1 = 0;
120 *((long long *)&outbuf[((out >> 3) << 3) + 8]) =
121 (unsigned long long)xor1 >> (64 - ((out & 0x7) << 3));
122
123 out += bcode + (bcode >> 2);
124 outbuf[0 + (i >> 1)] = code | bcode;
125 }
126 if (0 != (intot & 1)) {
127 out -= bcode + (bcode >> 2);
128 }
129 }
130 *outlen=out;
131 free(fcm);
132 free(dfcm);
133 }
134
135 void Decompress(long predsizem1,
136 unsigned char *inbuf, unsigned int inlen,
137 unsigned long long *outbuf, unsigned int *outlen)
138 {
139 register long i, in, intot, hash, dhash, code, bcode, end, tmp, ioc;
140 register long long val, lastval, stride, pred1, pred2, next;
141 register long long *fcm, *dfcm;
142
143 predsizem1 = (1L << predsizem1) - 1;
144
145 hash = 0;
146 dhash = 0;
147 lastval = 0;
148 pred1 = 0;
149 pred2 = 0;
150 fcm = (long long *)calloc(predsizem1 + 1, 8);
151 dfcm = (long long *)calloc(predsizem1 + 1, 8);
152 if (!fcm || !dfcm) {
153 return;
154 }
155
156 intot = *outlen;
157 in = inlen;
158
159 if (intot>0) {
160 in = (intot + 1) >> 1;
161 for (i = 0; i < intot; i += 2) {
162 code = inbuf[i >> 1];
163
164 val = *((long long *)&inbuf[(in >> 3) << 3]);
165 next = *((long long *)&inbuf[((in >> 3) << 3) + 8]);
166 tmp = (in & 0x7) << 3;
167 val = (unsigned long long)val >> tmp;
168 next <<= 64 - tmp;
169 if (0 == tmp)
170 next = 0;
171 val |= next;
172
173 bcode = (code >> 4) & 0x7;
174 val &= mask[bcode];
175 in += bcode + (bcode >> 2);
176
177 if (0 != (code & 0x80))
178 pred1 = pred2;
179 val ^= pred1;
180
181 fcm[hash] = val;
182 hash = ((hash << 6) ^ ((unsigned long long)val >> 48)) & predsizem1;
183 pred1 = fcm[hash];
184
185 stride = val - lastval;
186 dfcm[dhash] = stride;
187 dhash = ((dhash << 2) ^ ((unsigned long long)stride >> 40)) & predsizem1;
188 pred2 = val + dfcm[dhash];
189 lastval = val;
190
191 outbuf[i] = val;
192
193 val = *((long long *)&inbuf[(in >> 3) << 3]);
194 next = *((long long *)&inbuf[((in >> 3) << 3) + 8]);
195 tmp = (in & 0x7) << 3;
196 val = (unsigned long long)val >> tmp;
197 next <<= 64 - tmp;
198 if (0 == tmp)
199 next = 0;
200 val |= next;
201
202 bcode = code & 0x7;
203 val &= mask[bcode];
204 in += bcode + (bcode >> 2);
205
206 if (0 != (code & 0x8))
207 pred1 = pred2;
208 val ^= pred1;
209
210 fcm[hash] = val;
211 hash = ((hash << 6) ^ ((unsigned long long)val >> 48)) & predsizem1;
212 pred1 = fcm[hash];
213
214 stride = val - lastval;
215 dfcm[dhash] = stride;
216 dhash = ((dhash << 2) ^ ((unsigned long long)stride >> 40)) & predsizem1;
217 pred2 = val + dfcm[dhash];
218 lastval = val;
219
220 outbuf[i + 1] = val;
221 }
222 }
223 free(fcm);
224 free(dfcm);
225 }
226
227 void FLTCompressBS(long predsizem1,
228 char *inbuf, unsigned int inlen,
229 unsigned char *outbuf, unsigned int *outlen)
230 {
231 // test double
232 int memsize1 = inlen / sizeof(double) + inlen % sizeof(double);
233 char *memblock1 = (char*)calloc(memsize1*sizeof(double),1);
234 memcpy(memblock1, inbuf, inlen);
235 unsigned int outlen1 = *outlen*2+1024;
236 char *outbuf1 = (char*)calloc(outlen1,1);
237 Compress(predsizem1, (unsigned long long*)memblock1, memsize1, outbuf1, &outlen1);
238
239 if (0)
240 printf("DecompressionD %d from %i to %i, yields %.2f%%\n",
241 predsizem1, inlen, outlen1, 100.*outlen1/inlen);
242
243 if (outlen1<inlen) {
244 *outlen=outlen1;
245 memcpy(outbuf,memblock1,outlen1);
246 } else {
247 *outlen=inlen;
248 memcpy(outbuf,inbuf,inlen);
249 }
250
251 free(memblock1);
252 free(outbuf1);
253
254 #if 0
255 // test float
256 int memsize2 = inlen / sizeof(float) + inlen % sizeof(float);
257 double *memblock2 = calloc(memsize2,sizeof(double));
258 int i;
259 char *iptr=(char*)memblock1;
260 for(i=0;i<memsize2;++i) {
261 float *fptr=(float*)iptr;
262 double val=*fptr;
263 memblock2[i]=val;
264 iptr+=4;
265 }
266 unsigned int outlen2 = *outlen*2+1024;
267 char *outbuf2 = (char*)calloc(outlen2,1);
268 Compress(predsizem1, (unsigned long long*)memblock2, memsize2, outbuf2, &outlen2);
269
270 printf("DecompressionF %d from %i to %i, yields %.2f%%\n",
271 predsizem1, inlen, outlen2, 100.*outlen2/inlen);
272
273 free(memblock2);
274 free(outbuf2);
275 #endif
276 }