summaryrefslogtreecommitdiff
path: root/cesar/lib/src/bitstream.c
blob: b7d2263e47f91618c7400a75a1dc507bea663621 (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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    lib/src/bitstream.c
 * \brief   Bit stream access using word access.
 * \ingroup lib
 */
#include "common/std.h"

#include "lib/bitstream.h"

/* Evaluates to a number composed of n LSB one bits, for n in 0-31. */
#define bit_mask(n) (~(0xffffffffu << (n)))

/* Evaluates to a number composed of n LSB one bits, for n in 32-63. */
#define bit_mask64u(n) (~((u64) (0xffffffffu << ((n) - 32)) << 32))

/** Union for quick access to words inside a double word. */
union dwu
{
    u64 dw;
    struct {
#if DEFS_BIG_ENDIAN
        u32 high, low;
#else
        u32 low, high;
#endif
    } w;
};

/**
 * Logical shift right of a 64 bit value of 1 to 32 bits.
 * \param  a  64 bit value
 * \param  s  shift (1-32)
 * \return  shifted value
 */
extern inline u64
lshrdu32 (u64 a, uint s)
{
    dbg_assert (s > 0 && s <= 32);
    union dwu i = { .dw = a };
    if (s == 32)
    {
        i.w.low = i.w.high;
        i.w.high = 0;
    }
    else
    {
        i.w.low = i.w.high << (32 - s) | i.w.low >> s;
        i.w.high = i.w.high >> s;
    }
    return i.dw;
}

/**
 * Logical shift left of a 32 bit value, or'ed with a 64 bit value.
 * \param  a  64 bit value to be or'ed with
 * \param  b  32 bit value to shift
 * \param  s  shift (0-31)
 * \return  or result
 *
 * Equivalent of:
 * \code
 * a |= (u64) b << s;
 * \endcode
 */
extern inline u64
lshlwo64 (u64 a, u32 b, uint s)
{
    dbg_assert (s < 32);
    union dwu i = { .dw = a };
    if (s == 0)
    {
        i.w.low |= b;
    }
    else
    {
        i.w.low |= b << s;
        i.w.high |= b >> (32 - s);
    }
    return i.dw;
}

void
bitstream_init (bitstream_t *ctx, void *data, uint nb_bytes,
                bitstream_direction_t direction)
{
    dbg_assert (ctx);
    dbg_assert (direction == BITSTREAM_READ || direction == BITSTREAM_WRITE);
    /* Initialise context. */
    ctx->direction = direction;
    ctx->buffer = 0;
    ctx->buffer_bits = 0;
    ctx->data = NULL;
    ctx->data_bits = 0;
    ctx->data_written_bits = 0;
    ctx->buffer_cb = NULL;
    /* Set first data buffer. */
    bitstream_set_buffer (ctx, data, nb_bytes);
}

void
bitstream_init_buffer_cb (bitstream_t *ctx, bitstream_buffer_cb_t cb,
                          void *user_data)
{
    dbg_assert (ctx);
    ctx->buffer_cb = cb;
    ctx->buffer_cb_user_data = user_data;
}

void
bitstream_set_buffer (bitstream_t *ctx, void *data, uint nb_bytes)
{
    dbg_assert (ctx);
    dbg_assert (ctx->direction == BITSTREAM_READ
                || ctx->direction == BITSTREAM_WRITE);
    dbg_assert (ctx->data == NULL && ctx->data_bits == 0);
    dbg_assert_ptr (data);
    dbg_assert (nb_bytes != 0);
    uint nb_bits = nb_bytes * 8;
    /* No problem with aligned buffers. */
    if (((uint) data & 0x3) == 0)
    {
        ctx->data = data;
        ctx->data_bits = nb_bits;
    }
    else
    {
        u32 *adata = (void *) ((uint) data & ~0x3);
        uint una = ((uint) data - (uint) adata) * 8;
        if (ctx->direction == BITSTREAM_READ)
        {
            u32 w = *adata;
            /* Drop unused LSB. */
            w >>= una;
            /* Mask unused MSB. */
            if (nb_bits + una < 32)
                w &= bit_mask (nb_bits);
            /* Put in work buffer. */
            ctx->buffer = lshlwo64 (ctx->buffer, w, ctx->buffer_bits);
            ctx->data = adata + 1;
            if (nb_bits + una < 32)
            {
                ctx->buffer_bits += nb_bits;
                ctx->data_bits = 0;
            }
            else
            {
                ctx->buffer_bits += 32 - una;
                ctx->data_bits = nb_bits + una - 32;
            }
        }
        else
        {
            /* If there is enough room in the work buffer, stuff data
             * preceding the unaligned buffer into it. */
            if (ctx->buffer_bits <= 32) /* 40 will be OK too, who cares... */
            {
                u32 w = *adata;
                /* Mask preceding data. */
                w &= bit_mask (una);
                /* Stuff it into work buffer. */
                ctx->buffer = (ctx->buffer << una) | w;
                ctx->buffer_bits += una;
                ctx->data = adata;
                ctx->data_bits = nb_bits + una;
                ctx->data_written_bits -= una; /* Fix written data. */
            }
            else
            {
                /* No enough room, should write the first word now. */
                u32 w = *adata;
                /* Mask preceding data. */
                u32 mask;
                mask = bit_mask (una);
                if (nb_bits + una < 32)
                    mask |= ~bit_mask (nb_bits + una);
                w &= mask;
                /* Add and store bits from work buffer. */
                w |= ((u32) ctx->buffer << una) & ~mask;
                *adata = w;
                /* Update context. */
                uint written_bits;
                if (nb_bits + una < 32)
                    written_bits = nb_bits;
                else
                    written_bits = 32 - una;
                ctx->buffer = lshrdu32 (ctx->buffer, written_bits);
                ctx->buffer_bits -= written_bits;
                ctx->data_bits = nb_bits - written_bits;
                ctx->data_written_bits += written_bits;
                ctx->data = adata + 1;
            }
        }
    }
}

uint
bitstream_read (bitstream_t *ctx, uint nb_bits)
{
    dbg_assert (ctx);
    dbg_assert (ctx->direction == BITSTREAM_READ);
    dbg_assert (nb_bits > 0 && nb_bits <= 32);
    /* If input buffer exhausted, read the next word. */
    while (nb_bits > ctx->buffer_bits)
    {
        if (ctx->data_bits)
        {
            /* Load bits from data buffer. */
            u32 w = *ctx->data++;
            uint read_bits = ctx->data_bits;
            if (read_bits < 32)
                w &= bit_mask (read_bits);
            else
                read_bits = 32;
            ctx->buffer = lshlwo64 (ctx->buffer, w, ctx->buffer_bits);
            ctx->data_bits -= read_bits;
            ctx->buffer_bits += read_bits;
        }
        else
        {
            /* Data buffer exhausted. */
            dbg_assert (ctx->buffer_cb);
            ctx->data = NULL;
            ctx->buffer_cb (ctx, ctx->buffer_cb_user_data);
        }
    }
    dbg_assert (nb_bits <= ctx->buffer_bits);
    /* Read from input buffer. */
    u32 v = ctx->buffer;
    if (nb_bits < 32)
        v &= bit_mask (nb_bits);
    ctx->buffer = lshrdu32 (ctx->buffer, nb_bits);
    ctx->buffer_bits -= nb_bits;
    return v;
}

void
bitstream_write (bitstream_t *ctx, uint value, uint nb_bits)
{
    dbg_assert (ctx);
    dbg_assert (ctx->direction == BITSTREAM_WRITE);
    dbg_assert (nb_bits > 0 && nb_bits <= 32);
    dbg_assert (nb_bits == 32 || (value & ~bit_mask (nb_bits)) == 0);
    /* Write to output buffer. */
    ctx->buffer = lshlwo64 (ctx->buffer, value, ctx->buffer_bits);
    ctx->buffer_bits += nb_bits;
    /* If at least 32 bits, write it. */
    while (ctx->buffer_bits >= 32)
    {
        if (ctx->data_bits)
        {
            /* Write bits to data buffer. */
            uint written_bits = ctx->data_bits;
            /* Do no clobber data following the data buffer. */
            if (written_bits >= 32)
            {
                written_bits = 32;
                *ctx->data++ = (u32) ctx->buffer;
            }
            else
            {
                u32 mask = bit_mask (written_bits);
                *ctx->data = (*ctx->data & ~mask)
                    | ((u32) ctx->buffer & mask);
                /* Do not update ctx->data, it is empty anyway. */
            }
            ctx->data_bits -= written_bits;
            ctx->data_written_bits += written_bits;
            ctx->buffer_bits -= written_bits;
            ctx->buffer = lshrdu32 (ctx->buffer, written_bits);
        }
        else
        {
            /* Data buffer exhausted. */
            dbg_assert (ctx->buffer_cb);
            ctx->data = NULL;
            ctx->buffer_cb (ctx, ctx->buffer_cb_user_data);
        }
    }
}

uint
bitstream_write_finalise (bitstream_t *ctx)
{
    dbg_assert (ctx);
    dbg_assert (ctx->direction == BITSTREAM_WRITE);
    /* If at least 1 bit, write it. */
    dbg_assert (ctx->buffer_bits < 32);
    while (ctx->buffer_bits)
    {
        /* Warning, differences with code in bitstream_write is subtle. */
        if (ctx->data_bits)
        {
            /* Write bits to data buffer. */
            uint written_bits;
            /* Do no clobber data following the data buffer. */
            if (ctx->data_bits >= 32)
            {
                written_bits = MIN (32u, ctx->buffer_bits);
                *ctx->data++ = (u32) (ctx->buffer & 0xffffffff);
            }
            else
            {
                written_bits = MIN (ctx->data_bits, ctx->buffer_bits);
                u32 mask = bit_mask (ctx->data_bits);
                *ctx->data = (*ctx->data & ~mask)
                    | ((u32) ctx->buffer & mask);
                /* Do not update ctx->data, it is empty anyway. */
            }
            /* This is not futile if a new buffer is set: */
            ctx->data_bits -= written_bits;
            ctx->data_written_bits += written_bits;
            ctx->buffer_bits -= written_bits;
            ctx->buffer = lshrdu32 (ctx->buffer, written_bits);
        }
        else
        {
            /* Data buffer exhausted. */
            dbg_assert (ctx->buffer_cb);
            ctx->data = NULL;
            ctx->buffer_cb (ctx, ctx->buffer_cb_user_data);
        }
    }
    return ctx->data_written_bits;
}

u64
bitstream_read_large (bitstream_t *ctx, uint nb_bits)
{
    dbg_assert (ctx);
    dbg_assert (nb_bits > 32 && nb_bits <= 64);
    u32 low = bitstream_read (ctx, 32);
    u32 hi = bitstream_read (ctx, nb_bits - 32);
    return ((u64) hi << 32) | low;
}

void
bitstream_write_large (bitstream_t *ctx, u64 value, uint nb_bits)
{
    dbg_assert (ctx);
    dbg_assert (nb_bits > 32 && nb_bits <= 64);
    bitstream_write (ctx, value, 32);
    bitstream_write (ctx, value >> 32, nb_bits - 32);
}

void
bitstream_read_buf (bitstream_t *ctx, u8 *buf, uint size)
{
    dbg_assert (ctx);
    dbg_assert_ptr (buf);
    while (size--)
        *buf++ = bitstream_read (ctx, 8);
}

void
bitstream_write_buf (bitstream_t *ctx, const u8 *buf, uint size)
{
    dbg_assert (ctx);
    dbg_assert_ptr (buf);
    while (size--)
        bitstream_write (ctx, *buf++, 8);
}

uint
bitstream_read_str (bitstream_t *ctx, char *str, uint size)
{
    char c;
    uint left;
    dbg_assert (ctx);
    dbg_assert_ptr (str);
    dbg_assert (size);
    left = size;
    while (1)
    {
        c = bitstream_read (ctx, 8);
        /* If read character is null, do not decrement left. */
        if (c && --left)
        {
            *str++ = c;
        }
        else
        {
            /* Always terminate with a null character. */
            *str = 0;
            break;
        }
    }
    return size - left;
}

uint
bitstream_write_str (bitstream_t *ctx, const char *str, uint size)
{
    char c;
    uint left;
    dbg_assert (ctx);
    dbg_assert_ptr (str);
    dbg_assert (size);
    left = size;
    do
    {
        c = *str++;
        /* If read character is null, do not decrement left. */
        if (c && !--left)
            c = '\0';
        bitstream_write (ctx, c, 8);
    } while (c);
    return size - left;
}

uint
bitstream_available_bits (bitstream_t *ctx)
{
    dbg_assert (ctx);
    dbg_assert (ctx->direction == BITSTREAM_READ
                || ctx->direction == BITSTREAM_WRITE);
    if (ctx->direction == BITSTREAM_READ)
        return ctx->data_bits + ctx->buffer_bits;
    else
        return ctx->data_bits - ctx->buffer_bits;
}

uint
bitstream_written_bits (bitstream_t *ctx)
{
    dbg_assert (ctx);
    dbg_assert (ctx->direction == BITSTREAM_READ
                || ctx->direction == BITSTREAM_WRITE);
    if (ctx->direction == BITSTREAM_READ)
        return 0;
    else
        return ctx->data_written_bits + ctx->buffer_bits;
}

void
bitstream_access_8 (bitstream_t *ctx, u8 *value, uint nb_bits)
{
    dbg_assert (ctx);
    dbg_assert (ctx->direction == BITSTREAM_READ
                || ctx->direction == BITSTREAM_WRITE);
    dbg_assert (value);
    dbg_assert (nb_bits <= 8);
    if (ctx->direction == BITSTREAM_READ)
        *value = bitstream_read (ctx, nb_bits);
    else
        bitstream_write (ctx, *value, nb_bits);
}

void
bitstream_access_16 (bitstream_t *ctx, u16 *value, uint nb_bits)
{
    dbg_assert (ctx);
    dbg_assert (ctx->direction == BITSTREAM_READ
                || ctx->direction == BITSTREAM_WRITE);
    dbg_assert (value);
    dbg_assert (nb_bits <= 16);
    if (ctx->direction == BITSTREAM_READ)
        *value = bitstream_read (ctx, nb_bits);
    else
        bitstream_write (ctx, *value, nb_bits);
}

void
bitstream_access_32 (bitstream_t *ctx, u32 *value, uint nb_bits)
{
    dbg_assert (ctx);
    dbg_assert (ctx->direction == BITSTREAM_READ
                || ctx->direction == BITSTREAM_WRITE);
    dbg_assert (value);
    if (ctx->direction == BITSTREAM_READ)
        *value = bitstream_read (ctx, nb_bits);
    else
        bitstream_write (ctx, *value, nb_bits);
}

void
bitstream_access_64 (bitstream_t *ctx, u64 *value, uint nb_bits)
{
    dbg_assert (ctx);
    dbg_assert (ctx->direction == BITSTREAM_READ
                || ctx->direction == BITSTREAM_WRITE);
    dbg_assert (value);
    if (ctx->direction == BITSTREAM_READ)
        *value = bitstream_read_large (ctx, nb_bits);
    else
        bitstream_write_large (ctx, *value, nb_bits);
}

uint
bitstream_access_finalise (bitstream_t *ctx)
{
    dbg_assert (ctx);
    dbg_assert (ctx->direction == BITSTREAM_READ
                || ctx->direction == BITSTREAM_WRITE);
    if (ctx->direction == BITSTREAM_READ)
        return 0;
    else
        return bitstream_write_finalise (ctx);
}

void
bitstream_access_buf (bitstream_t *ctx, u8 *buf, uint size)
{
    dbg_assert (ctx);
    dbg_assert (ctx->direction == BITSTREAM_READ
                || ctx->direction == BITSTREAM_WRITE);
    if (ctx->direction == BITSTREAM_READ)
        bitstream_read_buf (ctx, buf, size);
    else
        bitstream_write_buf (ctx, buf, size);
}

uint
bitstream_access_str (bitstream_t *ctx, char *str, uint size)
{
    dbg_assert (ctx);
    dbg_assert (ctx->direction == BITSTREAM_READ
                || ctx->direction == BITSTREAM_WRITE);
    if (ctx->direction == BITSTREAM_READ)
        return bitstream_read_str (ctx, str, size);
    else
        return bitstream_write_str (ctx, str, size);
}

uint
bitstream_direct_read (const void *data, uint bit_offset, uint nb_bits)
{
    const u32 *p;
    uint una;
    uint r;
    dbg_assert_ptr (data);
    dbg_assert (nb_bits > 0 && nb_bits <= 32);
    /* Normalise bit offset. */
    p = (const u32 *) ((u32) data + bit_offset / 8);
    una = ((u32) p & 0x3) * 8 + bit_offset % 8;
    p = (const u32 *) ((u32) p & ~0x3);
    /* Read from first word. */
    r = p[0] >> una;
    /* Read from second word if needed. */
    if (una + nb_bits > 32)
        r |= p[1] << (32 - una);
    /* Mask if needed. */
    if (nb_bits < 32)
        r &= bit_mask (nb_bits);
    return r;
}

u64
bitstream_direct_read_large (const void *data, uint bit_offset, uint nb_bits)
{
    const u32 *p;
    uint una;
    u64 r;
    dbg_assert_ptr (data);
    dbg_assert (nb_bits > 32 && nb_bits <= 64);
    /* Normalise bit offset. */
    p = (const u32 *) ((u32) data + bit_offset / 8);
    una = ((u32) p & 0x3) * 8 + bit_offset % 8;
    p = (const u32 *) ((u32) p & ~0x3);
    /* Read from first and second word. */
    r = p[0] | (u64) p[1] << 32;
    if (una != 0)
        r = lshrdu32 (r, una);
    /* Read from third word if needed. */
    if (una + nb_bits > 64)
        r |= (u64) (p[2] << (32 - una)) << 32;
    /* Mask if needed. */
    if (nb_bits < 64)
        r &= bit_mask64u (nb_bits);
    return r;
}

void
bitstream_direct_write (void *data, uint bit_offset, uint value,
                        uint nb_bits)
{
    u32 *p;
    uint una;
    dbg_assert_ptr (data);
    dbg_assert (nb_bits > 0 && nb_bits <= 32);
    dbg_assert (nb_bits == 32 || (value & ~bit_mask (nb_bits)) == 0);
    /* Normalise bit offset. */
    p = (u32 *) ((u32) data + bit_offset / 8);
    una = ((u32) p & 0x3) * 8 + bit_offset % 8;
    p = (u32 *) ((u32) p & ~0x3);
    /* Is it aligned? */
    if (!una)
    {
        /* Yes, only handle data after, only one word. */
        if (nb_bits < 32)
            p[0] = (p[0] & ~bit_mask (nb_bits)) | value;
        else
            p[0] = value;
    }
    else
    {
        u32 mask;
        /* No, should mask data before, may use two words. */
        mask = bit_mask (una);
        if (una + nb_bits < 32)
            mask |= ~bit_mask (una + nb_bits);
        p[0] = (p[0] & mask) | value << una;
        if (una + nb_bits > 32)
            p[1] = (p[1] & ~bit_mask (una + nb_bits - 32))
                | value >> (32 - una);
    }
}

void
bitstream_direct_write_large (void *data, uint bit_offset, u64 value,
                              uint nb_bits)
{
    u32 *p;
    uint una;
    dbg_assert_ptr (data);
    dbg_assert (nb_bits > 32 && nb_bits <= 64);
    dbg_assert (nb_bits == 64 || (value & ~bit_mask64u (nb_bits)) == 0);
    /* Normalise bit offset. */
    p = (u32 *) ((u32) data + bit_offset / 8);
    una = ((u32) p & 0x3) * 8 + bit_offset % 8;
    p = (u32 *) ((u32) p & ~0x3);
    /* Is it aligned? */
    if (!una)
    {
        /* Yes, only handle data after, only two words. */
        p[0] = value;
        if (nb_bits < 64)
            p[1] = (p[1] & ~bit_mask (nb_bits - 32)) | (u32) (value >> 32);
        else
            p[1] = value >> 32;
    }
    else
    {
        /* No, should mask data before, may use three words. */
        p[0] = (p[0] & bit_mask (una)) | (u32) value << una;
        u32 vm = lshrdu32 (value, 32 - una);
        if (una + nb_bits < 64)
        {
            p[1] = (p[1] & ~bit_mask (una + nb_bits - 32)) | vm;
        }
        else
        {
            p[1] = vm;
            if (una + nb_bits > 64)
                p[2] = (p[2] & ~bit_mask (una + nb_bits - 64))
                    | (u32) (value >> 32) >> (32 - una);
        }
    }
}

void
bitstream_memcpy (void *dest, const void *src, uint size)
{
    const u32 *s;
    u32 *d;
    u32 sw, dw;
    uint sb, db;
    uint duna, suna;
    dbg_assert_ptr (dest);
    dbg_assert_ptr (src);
    /* Setup destination. */
    duna = (u32) dest & 0x3;
    if (duna)
    {
        d = (u32 *) ((u32) dest & ~0x3);
        dw = *d;
        dw &= bit_mask (duna * 8);
        db = duna;
    }
    else
    {
        d = dest;
        dw = 0;
        db = 0;
    }
    /* Setup source. */
    suna = (u32) src & 0x3;
    if (suna)
    {
        s = (u32 *) ((u32) src & ~0x3);
        sw = *s++;
        sw >>= suna * 8;
        sb = 4 - suna;
    }
    else
    {
        s = src;
        sw = 0; /* Compiler warning. */
        sb = 0;
    }
    /* Copy bytes. */
    while (size)
    {
        if (sb == 0)
        {
            sw = *s++;
            sb = 4;
        }
        dw |= (sw & 0xff) << (db * 8);
        db++;
        sw >>= 8;
        sb--;
        if (db == 4)
        {
            *d++ = dw;
            db = 0;
            dw = 0;
        }
        size--;
    }
    /* Last destination word. */
    if (db)
    {
        dw |= *d & ~bit_mask (db * 8);
        *d = dw;
    }
}

bool
bitstream_memcmp (const void *s1, const void *s2, uint size)
{
    const u32 *p1, *p2;
    u32 w1, w2;
    uint b1, b2;
    uint una1, una2;
    dbg_assert_ptr (s1);
    dbg_assert_ptr (s2);
    /* Setup s1. */
    una1 = (u32) s1 & 0x3;
    if (una1)
    {
        p1 = (u32 *) ((u32) s1 & ~0x3);
        w1 = *p1++;
        w1 >>= una1 * 8;
        b1 = 4 - una1;
    }
    else
    {
        p1 = s1;
        w1 = 0; /* Compiler warning. */
        b1 = 0;
    }
    /* Setup s2. */
    una2 = (u32) s2 & 0x3;
    if (una2)
    {
        p2 = (u32 *) ((u32) s2 & ~0x3);
        w2 = *p2++;
        w2 >>= una2 * 8;
        b2 = 4 - una2;
    }
    else
    {
        p2 = s2;
        w2 = 0; /* Compiler warning. */
        b2 = 0;
    }
    /* Compare bytes. */
    while (size)
    {
        if (b1 == 0)
        {
            w1 = *p1++;
            b1 = 4;
        }
        if (b2 == 0)
        {
            w2 = *p2++;
            b2 = 4;
        }
        if ((w1 & 0xff) != (w2 & 0xff))
            return false;
        w1 >>= 8;
        b1--;
        w2 >>= 8;
        b2--;
        size--;
    }
    return true;
}