88#include <stddef.h>
99#include <math.h>
1010
11- /* Throws a compile-time error if the assert fails */
12- #define STATIC_ASSERT (condition , message ) \
13- do { \
14- typedef char static_assertion_##__LINE__[(condition) ? 1 : -1]; \
15- } while (0)
16-
1711typedef struct {
1812 uint8_t * data ; // The bitstream data, stored as bytes
1913 size_t bytes ; // The number of bytes in the bitstream
@@ -36,31 +30,29 @@ void bitstream_init(bitstream_t *bitstream, uint8_t *data, size_t bytes);
3630 * @param num_bits The length of the data in bits.
3731 * @return Returns 0 if successful, -1 if there is insufficient space in the bitstream, and 1 if overflow occurs.
3832 */
39- #define bitstream_add (bitstream , value , num_bits ) \
40- (/* Check bitstream parameter type */ \
41- STATIC_ASSERT ( \
42- __builtin_types_compatible_p (typeof (bitstream ), \
43- bitstream_t * ), \
44- "First argument of bitstream_add() must be a pointer to bitstream_t." ), /* Check num_bits parameter type */ \
45- STATIC_ASSERT ( \
46- __builtin_types_compatible_p (typeof (num_bits ), size_t ), \
47- "Third argument of bitstream_add() must be size_t." ), /* Type dispatch */ \
48- _Generic((value ), \
49- uint8_t : bitstream_add_uint , \
50- uint16_t : bitstream_add_uint , \
51- uint32_t : bitstream_add_uint , \
52- int8_t : bitstream_add_int , \
53- int16_t : bitstream_add_int , \
54- int32_t : bitstream_add_int , \
55- default : bitstream_add_unsupported_type )(bitstream , value , \
56- num_bits ))
33+ #define STATIC_ASSERT (condition , message ) \
34+ do { \
35+ typedef char static_assertion_##__LINE__[(condition) ? 1 : -1]; \
36+ } while (0)
37+
38+ #define bitstream_add (bitstream , value , num_bits ) \
39+ (STATIC_ASSERT( \
40+ __builtin_types_compatible_p(typeof(bitstream), \
41+ bitstream_t *), \
42+ "First argument of bitstream_add() must be a pointer to bitstream_t."), \
43+ STATIC_ASSERT(__builtin_types_compatible_p(typeof(num_bits), size_t), \
44+ "Third argument of bitstream_add() must be size_t."), \
45+ _Generic((value), \
46+ uint8_t: bitstream_add_uint, \
47+ uint16_t: bitstream_add_uint, \
48+ uint32_t: bitstream_add_uint, \
49+ int8_t: bitstream_add_int, \
50+ int16_t: bitstream_add_int, \
51+ int32_t: bitstream_add_int, )(bitstream, value, num_bits))
5752
5853/* Internal functions (not meant to be called directly). */
5954/* Used by the bitstream_add() macro depending on the input data type. */
6055int bitstream_add_uint (bitstream_t * bitstream , uint32_t value , size_t num_bits );
6156int bitstream_add_int (bitstream_t * bitstream , int32_t value , size_t num_bits );
6257
63- /* This function will result in a compile-time error when called. */
64- int bitstream_add_unsupported_type (void );
65-
6658#endif // BITSTREAM_H
0 commit comments