summaryrefslogtreecommitdiff
path: root/mac/design/test/sacki/src/test_sacki.c
blob: ed723ae3f7b46536399beeb94db291238a2f42e0 (plain)
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/* Maria project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    test_sacki.c
 * \brief   Test SACKI software compression/decompression.
 * \ingroup mac_design_test
 */
#include "common/std.h"

#include <stdio.h>

#define DEBUG_UNCOMPRESS 0
#define DEBUG_COMPRESS 0

#if DEBUG_COMPRESS || DEBUG_UNCOMPRESS
/**
 * Print a binary word.
 * \param  w  word to print.
 * \param  b  width, padded with zero.
 *
 * The word should fit in b bits.
 */
void
print_bin (u32 w, uint b)
{
    for (; b; b--, w >>= 1)
        putchar ((w & 1) ? '1' : '0');
    dbg_assert (!w);
}
#endif /* DEBUG_COMPRESS || DEBUG_UNCOMPRESS */

/** Context used to handle sack nok. */
struct handle_sack_t
{
    /** Current position. */
    uint pos;
    /** Original uncompressed data. */
    u32 *bmp;
    /** Original uncompressed length. */
    uint bmpl;
    /** Original uncompressed length which fit in compressed data. */
    uint bmpl_fit;
    /** Test passed? */
    bool pass;
};
typedef struct handle_sack_t handle_sack_t;

/**
 * Handle a nok PB.
 * \param  user  obscure context pointer.
 * \param  n  number of the bad PB.
 *
 * This version is the test version in case you were wondering.
 */
void
handle_sack_nok (void *user, uint n)
{
    handle_sack_t *ctx = user;
    uint i;
    for (i = ctx->pos; i < n + 1; i++)
    {
        if (i < ctx->bmpl_fit)
        {
            if (i >= ctx->bmpl)
            {
                putchar ('!');
                ctx->pass = false;
            }
            else
            {
                u32 b = ctx->bmp[i / 32] & (1u << (i % 32));
                if ((!b && (i == n)) || (b && (i != n)))
                {
                    putchar ('!');
                    ctx->pass = false;
                }
            }
        }
        else
        {
            if (i != n)
            {
                putchar ('!');
                ctx->pass = false;
            }
        }
        putchar (i == n ? '1' : '0');
    }
    ctx->pos = n + 1;
}

/**
 * Handle a nok PB range.
 * \param  user  obscure context pointer.
 * \param  begin  number of the first bad PB.
 * \param  end  number after the last bad PB.
 */
void
handle_sack_nok_range (void *user, uint begin, uint end)
{
    for (; begin < end; begin++)
        handle_sack_nok (user, begin);
}

/**
 * Handle a SACKI compressed bitmap.
 * \param  si0  first word.
 * \param  si1  second word.
 * \param  si2  third word.
 * \param  sil  number of bit left.
 * \param  pbl  number of PB left.
 * \param  user  user data passed to handle_sack_nok.
 *
 * \todo  Finish uncompression with slow table.
 * \todo  Count invalid PB? Not trivial because of duplicates.
 * \todo  Return updated SACKI data.
 */
void
handle_compressed_sacki (u32 si0, u32 si1, u32 si2, uint sil, uint pbl,
                         void *user)
{
#include "src/sacki_decomp.c"
    uint pbn = 0;
    /* Uncompress using the fast table. */
    while (pbn < pbl && sil)
    {
        uint index = si0 & 0xff;
        uint eaten = sacki_decomp[index].eaten;
        uint produced = sacki_decomp[index].produced;
        uint nok0 = sacki_decomp[index].nok0;
        uint nok1 = sacki_decomp[index].nok1;
        uint nok2 = sacki_decomp[index].nok2;
        if (sil < eaten)
            break;
#if DEBUG_UNCOMPRESS
        printf ("eaten = %d/%d, produced = %d/%d, nokx = %d-%d-%d, code = ",
                eaten, sil, produced, pbl - pbn, nok0, nok1, nok2);
        print_bin (si0 & ((1u << eaten) - 1), eaten); putchar ('\n');
#endif /* DEBUG_UNCOMPRESS */
        sil -= eaten;
        si0 = si0 >> eaten | si1 << (32 - eaten);
        si1 = si1 >> eaten | si2 << (32 - eaten);
        si2 = si2 >> eaten;
        if (nok0 != SACKI_DECOMP_UNDEF_NOK && pbn + nok0 < pbl)
        {
            handle_sack_nok (user, pbn + nok0);
            if (nok1 != SACKI_DECOMP_UNDEF_NOK && pbn + nok1 < pbl)
            {
                handle_sack_nok (user, pbn + nok1);
                if (nok2 != SACKI_DECOMP_UNDEF_NOK && pbn + nok2 < pbl)
                {
                    handle_sack_nok (user, pbn + nok2);
                }
            }
        }
        pbn += produced;
    }
    /* Should finish to decode with a slow table. */
    /* Handle missing bits. */
    handle_sack_nok_range (user, pbn, pbl);
}

#if 0
/**
 * Handle a SACKI compressed bitmap.  Slower version.
 * \param  si0  first word.
 * \param  si1  second word.
 * \param  si2  third word.
 * \param  sil  number of bit left.
 * \param  pbl  number of PB left.
 */
void
handle_compressed_sacki (u32 si0, u32 si1, u32 si2, uint sil, uint pbl)
{
    uint pbn = 0;
    while (pbn < pbl && sil)
    {
        if (!(si0 & 1))
        {
            /* Handle 0 as a special case to reduce decomp_action size. */
            pbn += 4;
            sil--;
            si0 = si0 >> 1 | si1 << 31;
            si1 = si1 >> 1 | si2 << 31;
            si2 = si2 >> 1;
        }
        else
        {
            static const u8 decomp_action[32] =
            {
#if 0
#!/usr/bin/perl
%decomp = (
    '001' => 0, # 010
    '101' => 1, # 100
    '011' => 2, # 001
    '0111' => 3, # 1000
    '001111' => 4, # 110
    '101111' => 5, # 101
    '11111' => 6, # 11
);
for my $i (0 .. 31) {
    my @a;
    for (keys %decomp) {
        my $n = length ($_) - 1;
        my $b = oct ("0b$_") >> 1;
        push @a, $decomp{$_} if ($i & (1 << $n) - 1) == $b;
    }
    die $i unless scalar @a == 1;
    print "$a[0], ";
}
print "\n";
#endif
                0, 2, 1, 3, 0, 2, 1, 4, 0, 2, 1, 3, 0, 2, 1, 6,
                0, 2, 1, 3, 0, 2, 1, 5, 0, 2, 1, 3, 0, 2, 1, 6,
            };
            static const uint decomp_action_codel[7] = { 3, 3, 3, 4, 6, 6, 5 };
            uint action = decomp_action[(si0 >> 1) & 31];
            uint codel = decomp_action_codel[action];
            if (codel > sil)
            {
                sil = 0;
            }
            else
            {
                sil -= codel;
                si0 = si0 >> codel | si1 << (32 - codel);
                si1 = si1 >> codel | si2 << (32 - codel);
                si2 = si2 >> codel;
                switch (action)
                {
                case 0:
                    handle_sack_nok (pbn + 1);
                    pbn += 3;
                    break;
                case 1:
                    handle_sack_nok (pbn + 2);
                    pbn += 3;
                    break;
                case 2:
                    handle_sack_nok (pbn + 0);
                    pbn += 3;
                    break;
                case 3:
                    handle_sack_nok (pbn + 3);
                    pbn += 4;
                    break;
                case 4:
                    handle_sack_nok (pbn + 1);
                    handle_sack_nok (pbn + 2);
                    pbn += 3;
                    break;
                case 5:
                    handle_sack_nok (pbn + 0);
                    handle_sack_nok (pbn + 2);
                    pbn += 3;
                    break;
                case 6:
                    handle_sack_nok (pbn + 0);
                    handle_sack_nok (pbn + 1);
                    pbn += 2;
                    break;
                }
            }
        }
    }
}
#endif

/**
 * Compress a sacki bitmap.
 * \param  bmp  bitmap to compress, each word will be read only once.
 * \param  bmpl  number of bits in bmp.
 * \param  si  where to store sacki.
 * \param  sis  start offset in bits in si.
 * \param  sil  number of bits available in si.
 * \return  next free bit in si.
 */
uint
compress_sacki (u32 *bmp, uint bmpl, u32 *si, uint sis, uint sil)
{
    u32 bmp0, bmp1; /* bmp read cache */
    uint bmp0l; /* bits left in {bmp1,bmp0} */
    u32 si0; /* si write cache */
    uint sisw; /* si start offset in words */
    uint sisb; /* si0 start offset in bits */
    /* Load bmp cache. */
    bmp0 = *bmp++;
    bmp1 = 0;
    bmp0l = MIN (32u, bmpl);
    bmpl -= bmp0l;
    /* Address the right sacki word. */
    sisw = sis / 32;
    sisb = sis % 32;
    si0 = si[sisw]; /* Do not clobber si. */
    while (bmp0l && sil)
    {
        /* Assumes extra bits are zeros. */
        struct compress_action_do_t
        {
            u32 code:6;
            u32 codel:3;
            u32 bmp_eat:3;
        };
        static const struct compress_action_do_t compress_action_do[8] =
        {
            { 0x1f, 5, 2 }, /* 0: 11 -> 11111 */
            { 0x2f, 6, 3 }, /* 1: 101 -> 101111 */
            { 0x03, 3, 3 }, /* 2: 001 -> 011 */
            { 0x0f, 6, 3 }, /* 3: 110 -> 001111 */
            { 0x01, 3, 3 }, /* 4: 010 -> 001 */
            { 0x05, 3, 3 }, /* 5: 100 -> 101 */
            { 0x07, 4, 4 }, /* 6: 1000 -> 0111 */
            { 0x00, 1, 4 }, /* 7: 0000 -> 0 */
        };
        static const u8 compress_action[16] =
        {
            7, 2, 4, 0, 5, 1, 3, 0, 6, 2, 4, 0, 5, 1, 3, 0
        };
        /* Match a bit pattern. */
        uint action = compress_action[bmp0 & 0xf];
        u32 code = compress_action_do[action].code;
        uint codel = compress_action_do[action].codel;
        uint bmp_eat = compress_action_do[action].bmp_eat;
#if DEBUG_COMPRESS
        printf ("action = %d, bmp0 = ", action);
        print_bin (bmp0, bmp0l); printf (", code = ");
        print_bin (code, codel); putchar ('\n');
#endif /* DEBUG_COMPRESS */
        /* Output bits. */
        codel = MIN (codel, sil);
        code &= (1u << codel) - 1;
        si0 |= code << sisb;
        if (sisb + codel >= 32)
        {
            /* si0 overflow, store it and proceed with next one. */
            si[sisw] = si0;
            si0 = code >> (32 - sisb);
            sisw++;
            sisb = sisb + codel - 32;
        }
        else
        {
            sisb += codel;
        }
        sil -= codel;
        /* Load words? */
        dbg_assert (bmp_eat <= bmp0l || bmpl == 0);
        bmp_eat = MIN (bmp_eat, bmp0l);
        bmp0 >>= bmp_eat;
        bmp0 |= bmp1 << (32 - bmp_eat);
        bmp1 >>= bmp_eat;
        bmp0l -= bmp_eat;
        if (bmp0l < 4 && bmpl)
        {
            bmp1 = *bmp++;
            bmp0 |= bmp1 << bmp0l;
            bmp1 >>= (32 - bmp0l);
            if (bmpl >= 32)
            {
                bmp0l += 32;
                bmpl -= 32;
            }
            else
            {
                bmp0l += bmpl;
                bmpl = 0;
            }
        }
    }
    if (sisb)
        si[sisw] = si0;
    return sisw * 32 + sisb;
}

/**
 * Test the compression and decompression algorithms.
 * \param  in  Uncompressed data, as text, see below.
 * \param  out  Expected compressed data, as text, see below.
 * \param  offset  Used (random) bits in SACKI.
 * \return  true for success.
 *
 * The text is a LSB first sequence of '0' and '1'.  This sequence is then
 * encoded at runtime in binary tables.  Space are ignored during this
 * encoding.  For \a in, two consecutive spaces separates bits which can be
 * encoded (there is enough room in SACKI) from extra bits.
 */
bool
test_compress (const char *in, const char *out, uint offset)
{
    /* Convert string to bits. */
    u32 bmp[8];
    uint bmpp = 0;
    uint bmpp_fit = 256;
    const char *s;
    uint i;
    bool pass = true;
    for (i = 0; i < COUNT (bmp); i++)
        bmp[i] = 0;
    for (s = in; *s; s++)
    {
        if (s[0] == ' ' && s[1] == ' ')
            bmpp_fit = bmpp;
        while (*s == ' ') s++;
        dbg_assert (*s == '1' || *s == '0');
        if (*s == '1')
            bmp[bmpp / 32] |= 1u << (bmpp % 32);
        bmpp++;
    }
    /* Fill sil. */
    uint sil;
    u32 si[3];
    dbg_assert (offset < 72);
    for (i = 0; i < COUNT (si); i++)
    {
        if (offset <= i * 32)
            si[i] = 0;
        else
        {
            si[i] = 0xa55aa55a;
            if (offset < (i + 1) * 32)
            {
                si[i] &= (1u << (offset - i * 32)) - 1;
            }
        }
    }
    /* Compress. */
    sil = compress_sacki (bmp, bmpp, si, offset, 72 - offset);
    if (sil > 72)
    {
        pass = false;
        printf ("sacki overflow\n");
    }
    else
    {
        /* Display. */
        printf ("%s (offset = %d) =>\n  ", in, offset);
        for (i = offset, s = out; i < sil; i++)
        {
            while (*s == ' ') s++;
            dbg_assert (*s == '1' || *s == '0' || *s == '\0');
            u32 b = si[i / 32] & (1u << (i % 32));
            if (!*s || (*s == '1' && !b) || (*s == '0' && b))
            {
                pass = false;
                putchar ('!');
            }
            if (*s) s++;
            putchar (b ? '1' : '0');
        }
        if (*s)
        {
            pass = false;
            printf (" (!%d)", sil);
        }
        /* Uncompress. */
        if (!offset)
        {
            printf (" ->\n");
            handle_sack_t ctx = { 0, bmp, bmpp, bmpp_fit, pass };
            handle_compressed_sacki (si[0], si[1], si[2], sil, bmpp, &ctx);
            pass = ctx.pass;
            for (; ctx.pos < bmpp
                 && !(bmp[ctx.pos / 32] & (1u << (ctx.pos % 32)));
                 ctx.pos++)
                ;
            if (ctx.pos != bmpp)
            {
                pass = false;
                printf (" (!%d)", ctx.pos);
            }
        }
        putchar ('\n');
    }
    if (!pass)
    {
        printf ("FAIL\n");
    }
    putchar ('\n');
    return pass;
}

int
main (void)
{
    bool pass = true;
    pass = test_compress (
        "111011000110100010001000011101100011010001000100001",
        "11111111101110111100100101111001111111110111011110010010111100110",
        0) && pass;
    pass = test_compress (
        "111011000110100010001000011101100011010001000100001",
        "11111111101110111100100101111001111111110111011110010010111100110",
        5) && pass;
    pass = test_compress (
        "111011000110100010001000011101100011010001000100001",
        "11111111101110111100100101111001111111110111011110010010111100",
        10) && pass;
    pass = test_compress (
        "0000 0000 001 0001 0000 101 0000 0000 0000 0000 0000 11 0000 11 0000"
        " 0000 0000 0000 0000 001 0000 0000 0000 0000 11 11 100 0000 0000 010"
        " 0000 0000 0000 0000 0000 0000 0",
        "0 0 101 1110 0 111101 0 0 0 0 0 11111 0 11111 0 0 0 0 0 101 0 0 0 0"
        " 11111 11111 110 0 0 100 0 0 0 0 0 0 0", 0) && pass;
    pass = test_compress (
        "0000 0000 001 0001 0000 101 0000 0000 0000 0000 0000 11 0000 11 0000"
        " 0000 0000 0000 0000 001 0000 0000 0000 0000 11 11 100 0000 0000 010"
        " 0000 0000 0000 0000 0000 0000 0000  011 11 0001",
        "0 0 101 1110 0 111101 0 0 0 0 0 11111 0 11111 0 0 0 0 0 101 0 0 0 0"
        " 11111 11111 110 0 0 100 0 0 0 0 0 0 0 111", 0) && pass;
    if (pass)
    {
        printf ("PASS\n");
    }
    return 0;
}