]> git.sesse.net Git - fjl/blob - bitsource.h
Try to reduce the register pressure a bit in the unstuff code.
[fjl] / bitsource.h
1 #ifndef _BITSOURCE_H
2 #define _BITSOURCE_H 1
3
4 #include <assert.h>
5 #include <stdbool.h>
6 #include <stddef.h>
7 #include <stdint.h>
8 #include <sys/types.h>
9 #include <arpa/inet.h>
10
11 #include "input.h"
12
13 // Optimize for 64 bits. We might want to replace this for 32-bit machines
14 // (benchmark later).
15 typedef uint64_t bitreservoir_t;
16 typedef uint32_t bitreservoir_fill_t;
17
18 static inline bitreservoir_fill_t read_bitreservoir_fill(uint8_t* source)
19 {
20         return ntohl(*(bitreservoir_fill_t*)(source));
21 }
22
23 static const unsigned BITRESERVOIR_SIZE = 8 * sizeof(bitreservoir_t);
24 static const unsigned BITRESERVOIR_FILL_SIZE = 8 * sizeof(bitreservoir_fill_t);
25 static const unsigned BYTERESERVOIR_SIZE = 4096;
26
27 // A data source for efficient reading of bit-level data.
28 struct bit_source {
29         // Short-term bit reservoir; holds up to 64 bits. When it's empty,
30         // it needs to get refilled from the medium-term bit reservoir.
31         bitreservoir_t bits;
32         unsigned bits_available;
33         
34         // Medium-term bit reservoir; holds a few kilobytes of spare data.
35         // When this is empty, it needs to be refilled from the input
36         // stream.
37         uint8_t* bytes;
38         uint8_t* byte_read_ptr;
39         unsigned bytes_available;
40
41         // Some clients will purposedly read a bit ahead of the stream, causing
42         // problems at EOF. Thus, the client is allowed to request that we pad
43         // the end stream with a few bytes after the source reports EOF.
44         int padding_bytes_available;
45
46         // Data source.
47         input_func_t* input_func;
48         void* userdata;
49         bool source_eof;
50 };
51
52 void init_bit_source(struct bit_source* source, input_func_t* input_func,
53                      unsigned padding_bytes, void* userdata);
54
55 // Internal function. Do not use.
56 void possibly_refill_slow_path(struct bit_source* source, unsigned num_bits);
57
58 // Make sure there's at least NUM_BITS available in the short-term bit reservoir.
59 // You usually want to call this before read_bits(). The reason it's separate
60 // is that if you want two reads and you know the size of both, it's faster to
61 // refill A+B, read A, read B than refill A, read A, refill B, read B.
62 static inline void possibly_refill(struct bit_source* source, unsigned num_bits)
63 {
64         assert(num_bits <= BITRESERVOIR_FILL_SIZE + 1);
65
66         if (source->bits_available >= num_bits) {
67                 // Fast path (~90% of invocations?)
68                 return;
69         }
70
71         // Slower path (~99% of remaining invocations?)
72         assert(source->bits_available + BITRESERVOIR_FILL_SIZE < BITRESERVOIR_SIZE);
73         if (source->bytes_available >= sizeof(bitreservoir_fill_t)) {
74                 bitreservoir_fill_t fill = read_bitreservoir_fill(source->byte_read_ptr);
75                 source->byte_read_ptr += sizeof(bitreservoir_fill_t);
76                 source->bytes_available -= sizeof(bitreservoir_fill_t);
77                 source->bits |= (bitreservoir_t)fill << (BITRESERVOIR_SIZE - BITRESERVOIR_FILL_SIZE - source->bits_available);
78                 source->bits_available += BITRESERVOIR_FILL_SIZE;
79                 return;
80         }
81
82         // Slow path: Refill from data source.
83         // Should not be inlined, so split into a separate function.
84         possibly_refill_slow_path(source, num_bits);
85 }
86
87 static inline unsigned read_bits(struct bit_source* source, unsigned num_bits)
88 {
89         assert(source->bits_available >= num_bits);
90         unsigned ret = (source->bits >> (BITRESERVOIR_SIZE - num_bits));
91         source->bits <<= num_bits;
92         source->bits_available -= num_bits;
93         return ret;
94 }
95
96 static inline unsigned peek_bits(struct bit_source* source, unsigned num_bits)
97 {
98         assert(source->bits_available >= num_bits);
99         return (source->bits >> (BITRESERVOIR_SIZE - num_bits));
100 }
101
102 #endif /* !defined(_BITSOURCE_H) */