summaryrefslogtreecommitdiff
path: root/cesar/mac/pbproc/src/sacki_enc.c
blob: fceffd9f74ed29e0755815028b204e8547887730 (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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    mac/pbproc/src/sacki_enc.c
 * \brief   SACKI compression.
 * \ingroup mac_pbproc
 */
#include "common/std.h"

#include "inc/sacki_enc.h"

#include "pbproc_sacki_enc_tables.h"

#ifdef PBPROC_SACKI_ENC_DEBUG
# undef PBPROC_SACKI_ENC_DEBUG
void
pbproc_sacki_enc_debug (u32 code, uint codel, uint eat);
# define PBPROC_SACKI_ENC_DEBUG(code, codel, eat) \
    pbproc_sacki_enc_debug ((code), (codel), (eat))
#else
# define PBPROC_SACKI_ENC_DEBUG(code, codel, eat)
#endif

static const struct sack_enc_finish_t * const sack_enc_f[] = {
    sack_enc_f1,
    sack_enc_f2,
    sack_enc_f3,
};

void
pbproc_sacki_enc_init (pbproc_sacki_enc_t *ctx, uint sil)
{
    ctx->si[0] = 0;
    ctx->si[1] = 0;
    ctx->si[2] = 0;
    ctx->sis = 0;
    ctx->sil = sil;
    ctx->bmps = 0;
}

void
pbproc_sacki_enc_process (pbproc_sacki_enc_t *ctx, const volatile u32 *bmp,
                          uint bmpl, bool final)
{
    u32 bmp0, bmp1; /* Extended bmp cache. */
    uint bmp01l; /* Number of bits remaining in {bmp1, bmp0}. */
    uint bmps; /* Start offset in bmp. */
    u32 si0; /* SACKI write cache. */
    uint sisw; /* SACKI start offset in words. */
    uint sisb; /* SACKI start offset in bits. */
    uint sil; /* SACKI bits left. */
    /* Load bmp cache. */
    bmps = ctx->bmps;
    bmp += bmps / 32;
    bmpl -= bmps;
    bmp01l = MIN (bmpl, 32 - bmps % 32);
    bmpl -= bmp01l;
    bmp0 = *bmp++;
    bmp0 >>= bmps % 32;
    bmp1 = 0;
    /* Load si cache. */
    sisw = ctx->sis / 32;
    sisb = ctx->sis % 32;
    si0 = ctx->si[sisw];
    sil = ctx->sil;
    /* Fast compression. */
    while (1)
    {
        /* Load words? */
        if (bmp01l < 8 && bmpl)
        {
            bmp1 = *bmp++;
            bmp0 |= bmp1 << bmp01l;
            bmp1 = bmp01l == 0 ? 0 : bmp1 >> (32 - bmp01l);
            if (bmpl >= 32)
            {
                bmp01l += 32;
                bmpl -= 32;
            }
            else
            {
                bmp01l += bmpl;
                bmpl = 0;
            }
        }
        /* Can continue? */
        if (bmp01l < 8 || sil == 0)
            break;
        /* Look up table. */
        u32 code = sack_enc_8[bmp0 & 0xff].code;
        uint codel = sack_enc_8[bmp0 & 0xff].codel;
        uint bmp_eat = sack_enc_8[bmp0 & 0xff].eat;
        PBPROC_SACKI_ENC_DEBUG (code, codel, bmp_eat);
        /* Output bits. */
        codel = MIN (codel, sil);
        si0 |= code << sisb;
        if (sisb + codel >= 32)
        {
            /* si0 overflow, store it. */
            ctx->si[sisw] = si0;
            si0 = code >> (32 - sisb);
            sisw++;
            sisb = sisb + codel - 32;
        }
        else
        {
            sisb += codel;
        }
        sil -= codel;
        /* Consume input. */
        dbg_assert (bmp_eat <= bmp01l);
        bmp0 >>= bmp_eat;
        bmp0 |= bmp1 << (32 - bmp_eat);
        bmp1 >>= bmp_eat;
        bmp01l -= bmp_eat;
        bmps += bmp_eat;
    }
    dbg_assert ((bmp01l < 8 && bmpl == 0) || sil == 0);
    if (final)
    {
        while (bmp01l >= 4 && sil)
        {
            u32 code = sack_enc_4[bmp0 & 0xf].code;
            uint codel = sack_enc_4[bmp0 & 0xf].codel;
            uint bmp_eat = sack_enc_4[bmp0 & 0xf].eat_m1 + 1;
            PBPROC_SACKI_ENC_DEBUG (code, codel, bmp_eat);
            /* Output bits. */
            codel = MIN (codel, sil);
            si0 |= code << sisb;
            if (sisb + codel >= 32)
            {
                /* si0 overflow, store it. */
                ctx->si[sisw] = si0;
                si0 = code >> (32 - sisb);
                sisw++;
                sisb = sisb + codel - 32;
            }
            else
            {
                sisb += codel;
            }
            sil -= codel;
            /* Consume input. */
            dbg_assert (bmp_eat <= bmp01l);
            bmp0 >>= bmp_eat;
            bmp01l -= bmp_eat;
            bmps += bmp_eat;
        }
        if (bmp01l && sil)
        {
            const struct sack_enc_finish_t *t = sack_enc_f[bmp01l - 1];
            u32 bmp0m = bmp0 & ((1 << bmp01l) - 1);
            u32 code = t[bmp0m].code;
            u32 codel = t[bmp0m].codel;
            PBPROC_SACKI_ENC_DEBUG (code, codel, bmp01l);
            /* Output bits. */
            codel = MIN (codel, sil);
            si0 |= code << sisb;
            if (sisb + codel >= 32)
            {
                /* si0 overflow, store it. */
                ctx->si[sisw] = si0;
                si0 = code >> (32 - sisb);
                sisw++;
                sisb = sisb + codel - 32;
            }
            else
            {
                sisb += codel;
            }
            sil -= codel;
        }
        bmps += bmp01l;
        bmp01l = 0;
    }
    dbg_assert (bmp01l <= 8 || sil == 0);
    ctx->bmps = bmps;
    ctx->sil = sil;
    ctx->sis = sisw * 32 + sisb;
    if (sisb)
    {
        si0 &= (1u << sisb) - 1;
        ctx->si[sisw] = si0;
    }
}