-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.c
376 lines (307 loc) · 9.25 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* Copyright (C) 2005-2015 Christoph Rupp (chris@crupp.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include "for.h"
typedef uint32_t (*for_unpackfunc_t) (uint32_t, const uint8_t *, uint32_t *);
typedef uint32_t (*for_packfunc_t) (uint32_t, const uint32_t *, uint8_t *);
typedef uint32_t (*for_unpackxfunc_t)(uint32_t, const uint8_t *, uint32_t *,
uint32_t);
typedef uint32_t (*for_packxfunc_t) (uint32_t, const uint32_t *, uint8_t *,
uint32_t);
extern for_packfunc_t for_pack32[33];
extern for_unpackfunc_t for_unpack32[33];
extern for_packfunc_t for_pack16[33];
extern for_unpackfunc_t for_unpack16[33];
extern for_packfunc_t for_pack8[33];
extern for_unpackfunc_t for_unpack8[33];
extern for_packxfunc_t for_packx[33];
extern for_unpackxfunc_t for_unpackx[33];
#define VERIFY(c) while (!(c)) { \
printf("%s:%d: expression failed\n", \
__FILE__, __LINE__); \
abort(); \
}
#define VERIFY_ARRAY(a1, a2, len) \
do { \
uint32_t __i; \
for (__i = 0; __i < len; __i++) { \
if (a1[__i] != a2[__i]) { \
printf("data mismatch at %u\n", __i); \
abort(); \
} \
} \
} while (0)
static uint32_t inbuf[1024];
static uint32_t *
generate_input(uint32_t base, uint32_t length, uint32_t bits)
{
uint32_t i;
uint32_t max = (1 << bits) - 1;
for (i = 0; i < length; i++) {
if (bits == 0)
inbuf[i] = base;
else if (bits == 32)
inbuf[i] = base + i;
else
inbuf[i] = base + (i % max);
}
return &inbuf[0];
}
static void
highlevel_sorted(uint32_t length)
{
uint32_t i, s1, s2, s3;
uint8_t out[1024 * 10];
uint32_t in[1024 * 10];
uint32_t tmp[1024 * 10];
printf("highlevel sorted %u ints\n", length);
for (i = 0; i < length; i++)
in[i] = 33 + i;
s3 = for_compressed_size_sorted(in, length);
tmp[s3] = 'x';
s1 = for_compress_sorted(in, out, length);
VERIFY(tmp[s3] == 'x');
s2 = for_uncompress(out, tmp, length);
VERIFY(s1 == s2);
VERIFY(s2 == s3);
VERIFY_ARRAY(in, tmp, length);
for (i = 0; i < length; i++)
VERIFY(in[i] == for_select(out, i));
for (i = 0; i < length; i++)
VERIFY(i == for_linear_search(out, length, in[i]));
for (i = 0; i < length; i++) {
uint32_t actual;
uint32_t index = for_lower_bound_search(out, length, in[i], &actual);
VERIFY(in[i] == in[index]);
VERIFY(actual == in[i]);
}
}
static uint32_t
rnd()
{
static uint32_t a = 3;
a = (((a * 214013L + 2531011L) >> 16) & 32767);
return (a);
}
static void
highlevel_unsorted(uint32_t length)
{
uint32_t i, s1, s2, s3;
uint8_t out[1024 * 10];
uint32_t in[1024 * 10];
uint32_t tmp[1024 * 10];
printf("highlevel unsorted %u ints\n", length);
for (i = 0; i < length; i++)
in[i] = 7 + (rnd() - 7);
s3 = for_compressed_size_unsorted(in, length);
tmp[s3] = 'x';
s1 = for_compress_unsorted(in, out, length);
s2 = for_uncompress(out, tmp, length);
VERIFY(s1 == s2);
VERIFY(s2 == s3);
VERIFY(tmp[s3] == 'x');
VERIFY_ARRAY(in, tmp, length);
for (i = 0; i < length; i++)
VERIFY(in[i] == for_select(out, i));
for (i = 0; i < length; i++) {
uint32_t index = for_linear_search(out, length, in[i]);
VERIFY(in[i] == in[index]);
}
}
static void
append_sorted()
{
#undef max
#define max 10000
int i;
uint32_t s1, s2;
uint8_t out1[max * 8] = {0};
uint8_t out2[max * 8] = {0};
uint32_t in[max];
printf("append sorted, %d ints\n", max);
for (i = 0; i < max; i++) {
in[i] = i;
/* insert with "append" */
s1 = for_append_sorted(&out1[0], i, in[i]);
/* compress the full sequence */
s2 = for_compress_sorted(in, out2, i + 1);
VERIFY(s1 == s2);
VERIFY_ARRAY(out1, out2, s1);
}
}
static void
append_unsorted()
{
#undef max
#define max 10000
int i;
uint32_t s1, s2;
uint8_t out1[max * 8] = {0};
uint8_t out2[max * 8] = {0};
uint32_t in[max];
printf("append unsorted, %d ints\n", max);
for (i = 0; i < max; i++) {
in[i] = 7 + (rnd() - 7);
/* insert with "append" */
s1 = for_append_unsorted(&out1[0], i, in[i]);
/* compress the full sequence */
s2 = for_compress_unsorted(in, out2, i + 1);
VERIFY(s1 == s2);
VERIFY_ARRAY(out1, out2, s1);
}
}
static void
append_sorted_bignum()
{
#undef max
#define max 10
int i;
uint32_t s1, s2;
uint8_t out1[max * 8] = {0};
uint8_t out2[max * 8] = {0};
uint32_t in[max];
printf("append sorted bignum, %d ints\n", max);
for (i = 0; i < max; i++) {
in[i] = 1 << (17 + i);
/* insert with "append" */
s1 = for_append_sorted(&out1[0], i, in[i]);
/* compress the full sequence */
s2 = for_compress_sorted(in, out2, i + 1);
VERIFY(s1 == s2);
VERIFY_ARRAY(out1, out2, s1);
}
}
static void
lowlevel_block_func(uint32_t bits, for_packfunc_t pack, for_unpackfunc_t unpack,
uint32_t *in, uint32_t base, uint32_t length)
{
uint32_t i;
uint8_t out[1024];
uint32_t tmp[1024];
uint32_t s1 = pack(base, in, out);
uint32_t s2 = unpack(base, out, tmp);
VERIFY(s1 == s2);
VERIFY_ARRAY(in, tmp, length);
for (i = 0; i < length; i++)
VERIFY(in[i] == for_select_bits(out, base, bits, i));
for (i = 0; i < length; i++) {
uint32_t index = for_linear_search_bits(out, length, base, bits, in[i]);
VERIFY(in[i] == in[index]);
}
}
static void
lowlevel_blockx_func(uint32_t bits, for_packxfunc_t pack,
for_unpackxfunc_t unpack, uint32_t *in, uint32_t base,
uint32_t length)
{
uint32_t i;
uint8_t out[1024];
uint32_t tmp[1024];
uint32_t s1 = pack(base, in, out, length);
uint32_t s2 = unpack(base, out, tmp, length);
VERIFY(s1 == s2);
VERIFY_ARRAY(in, tmp, length);
for (i = 0; i < length; i++)
VERIFY(in[i] == for_select_bits(out, base, bits, i));
for (i = 0; i < length; i++) {
uint32_t index = for_linear_search_bits(out, length, base, bits, in[i]);
VERIFY(in[i] == in[index]);
}
}
static void
lowlevel_block32(uint32_t bits)
{
uint32_t *in = generate_input(10, 32, bits);
printf("lowlevel pack/unpack 32 ints, %2d bits\n", bits);
lowlevel_block_func(bits, for_pack32[bits], for_unpack32[bits], in, 10, 32);
}
static void
lowlevel_block16(uint32_t bits)
{
uint32_t *in = generate_input(10, 16, bits);
printf("lowlevel pack/unpack 16 ints, %2d bits\n", bits);
lowlevel_block_func(bits, for_pack16[bits], for_unpack16[bits], in, 10, 16);
}
static void
lowlevel_block8(uint32_t bits)
{
uint32_t *in = generate_input(10, 8, bits);
printf("lowlevel pack/unpack 8 ints, %2d bits\n", bits);
lowlevel_block_func(bits, for_pack8[bits], for_unpack8[bits], in, 10, 8);
}
static void
lowlevel_blockx(int length, uint32_t bits)
{
uint32_t *in = generate_input(10, 8, bits);
printf("lowlevel pack/unpack %d ints, %2d bits\n", length, bits);
lowlevel_blockx_func(bits, for_packx[bits],
for_unpackx[bits], in, 10, length);
}
int
main()
{
int i, b;
for (i = 0; i <= 32; i++)
lowlevel_block32(i);
for (i = 0; i <= 32; i++)
lowlevel_block16(i);
for (i = 0; i <= 32; i++)
lowlevel_block8(i);
for (i = 0; i < 32; i++) {
for (b = 0; b < 8; b++)
lowlevel_blockx(b, i);
}
highlevel_sorted(0);
highlevel_sorted(1);
highlevel_sorted(2);
highlevel_sorted(3);
highlevel_sorted(16);
highlevel_sorted(17);
highlevel_sorted(32);
highlevel_sorted(33);
highlevel_sorted(64);
highlevel_sorted(65);
highlevel_sorted(128);
highlevel_sorted(129);
highlevel_sorted(256);
highlevel_sorted(257);
highlevel_sorted(1024);
highlevel_sorted(1025);
highlevel_sorted(1333);
highlevel_unsorted(0);
highlevel_unsorted(1);
highlevel_unsorted(2);
highlevel_unsorted(3);
highlevel_unsorted(16);
highlevel_unsorted(17);
highlevel_unsorted(32);
highlevel_unsorted(33);
highlevel_unsorted(64);
highlevel_unsorted(65);
highlevel_unsorted(128);
highlevel_unsorted(129);
highlevel_unsorted(256);
highlevel_unsorted(257);
highlevel_unsorted(1024);
highlevel_unsorted(1025);
highlevel_unsorted(1333);
append_sorted();
append_sorted_bignum();
append_unsorted();
printf("\nsuccess!\n");
return 0;
}