From 8844114a1dd0157b6765647d4542ea2327243e63 Mon Sep 17 00:00:00 2001 From: Vicente Romero Date: Tue, 1 Feb 2022 11:30:41 +0000 Subject: [PATCH 01/10] rename BSWriter > bswriter --- bitstream.h | 12 ++++++------ encode_generic.h | 2 +- tests/unit/bitstream_test.c | 14 +++++++------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/bitstream.h b/bitstream.h index f2b84cf..b0c6670 100644 --- a/bitstream.h +++ b/bitstream.h @@ -16,7 +16,7 @@ #define BIT_STREAM_MAX_WRITE 57 #define BIT_STREAM_MAX_READ BIT_STREAM_MAX_WRITE -struct BSWriter { +struct bswriter { uint64_t bit_container; unsigned int bit_pos; uint8_t *start_ptr; @@ -30,7 +30,7 @@ static inline size_t bswriter_align_buffer_size(size_t orig_size) return orig_size + 8 - (orig_size % 8); } -static inline VtencErrorCode bswriter_init(struct BSWriter *writer, +static inline VtencErrorCode bswriter_init(struct bswriter *writer, uint8_t *out_buf, size_t out_capacity) { writer->bit_container = 0; @@ -43,7 +43,7 @@ static inline VtencErrorCode bswriter_init(struct BSWriter *writer, return VtencErrorNoError; } -static inline void bswriter_append(struct BSWriter *writer, +static inline void bswriter_append(struct bswriter *writer, uint64_t value, unsigned int n_bits) { assert(n_bits <= BIT_STREAM_MAX_WRITE); @@ -53,7 +53,7 @@ static inline void bswriter_append(struct BSWriter *writer, writer->bit_pos += n_bits; } -static inline VtencErrorCode bswriter_flush(struct BSWriter *writer) +static inline VtencErrorCode bswriter_flush(struct bswriter *writer) { size_t const n_bytes = writer->bit_pos >> 3; @@ -67,7 +67,7 @@ static inline VtencErrorCode bswriter_flush(struct BSWriter *writer) return VtencErrorNoError; } -static inline VtencErrorCode bswriter_write(struct BSWriter *writer, +static inline VtencErrorCode bswriter_write(struct bswriter *writer, uint64_t value, unsigned int n_bits) { assert(n_bits <= BIT_STREAM_MAX_WRITE); @@ -82,7 +82,7 @@ static inline VtencErrorCode bswriter_write(struct BSWriter *writer, return VtencErrorNoError; } -static inline size_t bswriter_close(struct BSWriter *writer) +static inline size_t bswriter_close(struct bswriter *writer) { if (writer->ptr <= writer->end_ptr) bswriter_flush(writer); diff --git a/encode_generic.h b/encode_generic.h index 4b322b6..6733b5b 100644 --- a/encode_generic.h +++ b/encode_generic.h @@ -48,7 +48,7 @@ struct EncodeCtx(WIDTH) { int skip_full_subtrees; size_t min_cluster_length; struct enc_stack stack; - struct BSWriter bits_writer; + struct bswriter bits_writer; }; static VtencErrorCode encctx_init(WIDTH)(struct EncodeCtx(WIDTH) *ctx, diff --git a/tests/unit/bitstream_test.c b/tests/unit/bitstream_test.c index b20fa12..4d1cafe 100644 --- a/tests/unit/bitstream_test.c +++ b/tests/unit/bitstream_test.c @@ -26,7 +26,7 @@ int test_bswriter_align_buffer_size(void) int test_bswriter_init_1(void) { - struct BSWriter writer; + struct bswriter writer; const size_t buf_cap = 4; uint8_t buf[buf_cap]; @@ -37,7 +37,7 @@ int test_bswriter_init_1(void) int test_bswriter_init_2(void) { - struct BSWriter writer; + struct bswriter writer; const size_t buf_cap = 8; uint8_t buf[buf_cap]; @@ -48,7 +48,7 @@ int test_bswriter_init_2(void) int test_bswriter_write_1(void) { - struct BSWriter writer; + struct bswriter writer; const size_t buf_cap = 16; uint8_t buf[buf_cap]; @@ -73,7 +73,7 @@ int test_bswriter_write_1(void) int test_bswriter_write_2(void) { - struct BSWriter writer; + struct bswriter writer; const size_t buf_cap = 8; uint8_t buf[buf_cap]; @@ -99,7 +99,7 @@ int test_bswriter_write_2(void) int test_bswriter_write_3(void) { - struct BSWriter writer; + struct bswriter writer; const size_t buf_cap = 24; uint8_t buf[buf_cap]; @@ -123,7 +123,7 @@ int test_bswriter_write_3(void) int test_bswriter_close_1(void) { - struct BSWriter writer; + struct bswriter writer; const size_t buf_cap = 8; uint8_t buf[buf_cap]; @@ -135,7 +135,7 @@ int test_bswriter_close_1(void) int test_bswriter_close_2(void) { - struct BSWriter writer; + struct bswriter writer; const size_t buf_cap = 8; uint8_t buf[buf_cap]; From afeb94aa87255dcfdd2e99e6ec6575bfade3e338 Mon Sep 17 00:00:00 2001 From: Vicente Romero Date: Tue, 1 Feb 2022 11:34:28 +0000 Subject: [PATCH 02/10] rename BSReader > bsreader --- bitstream.h | 10 +++++----- decode_generic.h | 2 +- tests/unit/bitstream_test.c | 16 ++++++++-------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/bitstream.h b/bitstream.h index b0c6670..1225a84 100644 --- a/bitstream.h +++ b/bitstream.h @@ -90,7 +90,7 @@ static inline size_t bswriter_close(struct bswriter *writer) return (writer->ptr - writer->start_ptr) + (writer->bit_pos > 0); } -struct BSReader { +struct bsreader { uint64_t bit_container; unsigned int bits_loaded; unsigned int bits_consumed; @@ -99,7 +99,7 @@ struct BSReader { const uint8_t *end_ptr; }; -static inline void bsreader_init(struct BSReader *reader, +static inline void bsreader_init(struct bsreader *reader, const uint8_t *buf, size_t buf_len) { reader->bit_container = 0; @@ -110,7 +110,7 @@ static inline void bsreader_init(struct BSReader *reader, reader->end_ptr = reader->start_ptr + buf_len; } -static inline VtencErrorCode bsreader_load(struct BSReader *reader) +static inline VtencErrorCode bsreader_load(struct bsreader *reader) { size_t n_bytes; @@ -145,7 +145,7 @@ static inline VtencErrorCode bsreader_load(struct BSReader *reader) return VtencErrorNoError; } -static inline VtencErrorCode bsreader_read(struct BSReader *reader, +static inline VtencErrorCode bsreader_read(struct bsreader *reader, unsigned int n_bits, uint64_t *read_value) { assert(n_bits <= BIT_STREAM_MAX_READ); @@ -163,7 +163,7 @@ static inline VtencErrorCode bsreader_read(struct BSReader *reader, return VtencErrorNoError; } -static inline size_t bsreader_size(struct BSReader *reader) +static inline size_t bsreader_size(struct bsreader *reader) { return (reader->ptr - reader->start_ptr) + (reader->bits_consumed >> 3) + ((reader->bits_consumed & 7) > 0); } diff --git a/decode_generic.h b/decode_generic.h index 1101036..9c5ef4d 100644 --- a/decode_generic.h +++ b/decode_generic.h @@ -44,7 +44,7 @@ struct DecodeCtx(WIDTH) { int reconstruct_full_subtrees; size_t min_cluster_length; struct dec_stack stack; - struct BSReader bits_reader; + struct bsreader bits_reader; }; static VtencErrorCode decctx_init(WIDTH)(struct DecodeCtx(WIDTH) *ctx, diff --git a/tests/unit/bitstream_test.c b/tests/unit/bitstream_test.c index 4d1cafe..1ae0251 100644 --- a/tests/unit/bitstream_test.c +++ b/tests/unit/bitstream_test.c @@ -153,7 +153,7 @@ int test_bswriter_close_2(void) int test_bsreader_read_1(void) { - struct BSReader reader; + struct bsreader reader; const uint8_t buf[] = {}; const size_t buf_len = sizeof(buf); uint64_t val=0; @@ -167,7 +167,7 @@ int test_bsreader_read_1(void) int test_bsreader_read_2(void) { - struct BSReader reader; + struct bsreader reader; const uint8_t buf[] = {0xff}; const size_t buf_len = sizeof(buf); uint64_t val=0; @@ -182,7 +182,7 @@ int test_bsreader_read_2(void) int test_bsreader_read_3(void) { - struct BSReader reader; + struct bsreader reader; const uint8_t buf[] = {0xff, 0x66}; const size_t buf_len = sizeof(buf); uint64_t val=0; @@ -198,7 +198,7 @@ int test_bsreader_read_3(void) int test_bsreader_read_4(void) { - struct BSReader reader; + struct bsreader reader; const uint8_t buf[] = {0xba}; const size_t buf_len = sizeof(buf); uint64_t val=0; @@ -217,7 +217,7 @@ int test_bsreader_read_4(void) int test_bsreader_read_5(void) { - struct BSReader reader; + struct bsreader reader; const uint8_t buf[] = { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33 @@ -237,7 +237,7 @@ int test_bsreader_read_5(void) int test_bsreader_read_6(void) { - struct BSReader reader; + struct bsreader reader; const uint8_t buf[] = { 0xff, 0xab, 0x11, 0xcd, 0x55, 0x55, 0x55, 0x55, 0x66, 0x66, 0x66, 0x66 }; @@ -264,7 +264,7 @@ int test_bsreader_read_6(void) int test_bsreader_read_7(void) { - struct BSReader reader; + struct bsreader reader; const uint8_t buf[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 @@ -283,7 +283,7 @@ int test_bsreader_read_7(void) int test_bsreader_size(void) { - struct BSReader reader; + struct bsreader reader; const uint8_t buf[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; From ae9b05db2ee0ca98461e40c32eabb17bb985b565 Mon Sep 17 00:00:00 2001 From: Vicente Romero Date: Tue, 1 Feb 2022 11:46:40 +0000 Subject: [PATCH 03/10] rename DecodeCtx > decctx --- decode_generic.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/decode_generic.h b/decode_generic.h index 9c5ef4d..b541645 100644 --- a/decode_generic.h +++ b/decode_generic.h @@ -12,7 +12,7 @@ #include "error.h" #include "internals.h" -#define DecodeCtx(_width_) PASTE2(DecodeCtx, _width_) +#define decctx(_width_) PASTE2(decctx, _width_) #define decctx_init(_width_) WIDTH_SUFFIX(decctx_init, _width_) #define decode_lower_bits_step(_width_) WIDTH_SUFFIX(decode_lower_bits_step, _width_) #define decode_lower_bits(_width_) WIDTH_SUFFIX(decode_lower_bits, _width_) @@ -38,7 +38,7 @@ do { \ } \ } while (0) -struct DecodeCtx(WIDTH) { +struct decctx(WIDTH) { TYPE *values; size_t values_len; int reconstruct_full_subtrees; @@ -47,7 +47,7 @@ struct DecodeCtx(WIDTH) { struct bsreader bits_reader; }; -static VtencErrorCode decctx_init(WIDTH)(struct DecodeCtx(WIDTH) *ctx, +static VtencErrorCode decctx_init(WIDTH)(struct decctx(WIDTH) *ctx, const VtencDecoder *dec, const uint8_t *in, size_t in_len, TYPE *out, size_t out_len) { @@ -69,7 +69,7 @@ static VtencErrorCode decctx_init(WIDTH)(struct DecodeCtx(WIDTH) *ctx, return VtencErrorNoError; } -static inline VtencErrorCode decode_lower_bits_step(WIDTH)(struct DecodeCtx(WIDTH) *ctx, +static inline VtencErrorCode decode_lower_bits_step(WIDTH)(struct decctx(WIDTH) *ctx, TYPE *value, unsigned int n_bits) { #if WIDTH > BIT_STREAM_MAX_READ @@ -97,7 +97,7 @@ static inline VtencErrorCode decode_lower_bits_step(WIDTH)(struct DecodeCtx(WIDT #endif } -static inline VtencErrorCode decode_lower_bits(WIDTH)(struct DecodeCtx(WIDTH) *ctx, +static inline VtencErrorCode decode_lower_bits(WIDTH)(struct decctx(WIDTH) *ctx, TYPE *values, size_t values_len, unsigned int n_bits, TYPE higher_bits) { size_t i; @@ -119,7 +119,7 @@ static inline void decode_full_subtree(WIDTH)(TYPE *values, size_t values_len, T } } -static inline void bcltree_add(WIDTH)(struct DecodeCtx(WIDTH) *ctx, +static inline void bcltree_add(WIDTH)(struct decctx(WIDTH) *ctx, const struct dec_bit_cluster *cluster) { if (cluster->length == 0) @@ -128,17 +128,17 @@ static inline void bcltree_add(WIDTH)(struct DecodeCtx(WIDTH) *ctx, dec_stack_push(&ctx->stack, cluster); } -static inline int bcltree_has_more(WIDTH)(struct DecodeCtx(WIDTH) *ctx) +static inline int bcltree_has_more(WIDTH)(struct decctx(WIDTH) *ctx) { return !dec_stack_empty(&ctx->stack); } -static inline struct dec_bit_cluster *bcltree_next(WIDTH)(struct DecodeCtx(WIDTH) *ctx) +static inline struct dec_bit_cluster *bcltree_next(WIDTH)(struct decctx(WIDTH) *ctx) { return dec_stack_pop(&ctx->stack); } -static VtencErrorCode decode_bit_cluster_tree(WIDTH)(struct DecodeCtx(WIDTH) *ctx) +static VtencErrorCode decode_bit_cluster_tree(WIDTH)(struct decctx(WIDTH) *ctx) { bcltree_add(WIDTH)(ctx, &(struct dec_bit_cluster){0, ctx->values_len, WIDTH, 0}); @@ -193,7 +193,7 @@ static VtencErrorCode decode_bit_cluster_tree(WIDTH)(struct DecodeCtx(WIDTH) *ct void vtenc_decode(WIDTH)(VtencDecoder *dec, const uint8_t *in, size_t in_len, TYPE *out, size_t out_len) { - struct DecodeCtx(WIDTH) ctx; + struct decctx(WIDTH) ctx; uint64_t max_values = dec->allow_repeated_values ? LIST_MAX_VALUES : SET_MAX_VALUES; dec->last_error_code = VtencErrorNoError; From 8131bdb988f21da63cc48aea1c0128ac1ca1def7 Mon Sep 17 00:00:00 2001 From: Vicente Romero Date: Tue, 1 Feb 2022 13:28:30 +0000 Subject: [PATCH 04/10] rename EncodeCtx > encctx --- encode_generic.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/encode_generic.h b/encode_generic.h index 6733b5b..bab9b72 100644 --- a/encode_generic.h +++ b/encode_generic.h @@ -13,7 +13,7 @@ #include "error.h" #include "internals.h" -#define EncodeCtx(_width_) PASTE2(EncodeCtx, _width_) +#define encctx(_width_) PASTE2(encctx, _width_) #define encctx_init(_width_) WIDTH_SUFFIX(encctx_init, _width_) #define encctx_close(_width_) WIDTH_SUFFIX(encctx_close, _width_) #define count_zeros_at_bit_pos(_width_) WIDTH_SUFFIX(count_zeros_at_bit_pos, _width_) @@ -42,7 +42,7 @@ do { \ } \ } while(0) -struct EncodeCtx(WIDTH) { +struct encctx(WIDTH) { const TYPE *values; size_t values_len; int skip_full_subtrees; @@ -51,7 +51,7 @@ struct EncodeCtx(WIDTH) { struct bswriter bits_writer; }; -static VtencErrorCode encctx_init(WIDTH)(struct EncodeCtx(WIDTH) *ctx, +static VtencErrorCode encctx_init(WIDTH)(struct encctx(WIDTH) *ctx, const VtencEncoder *enc, const TYPE *in, size_t in_len, uint8_t *out, size_t out_cap) { @@ -71,12 +71,12 @@ static VtencErrorCode encctx_init(WIDTH)(struct EncodeCtx(WIDTH) *ctx, return bswriter_init(&(ctx->bits_writer), out, out_cap); } -static inline size_t encctx_close(WIDTH)(struct EncodeCtx(WIDTH) *ctx) +static inline size_t encctx_close(WIDTH)(struct encctx(WIDTH) *ctx) { return bswriter_close(&(ctx->bits_writer)); } -static inline VtencErrorCode encode_lower_bits_step(WIDTH)(struct EncodeCtx(WIDTH) *ctx, +static inline VtencErrorCode encode_lower_bits_step(WIDTH)(struct encctx(WIDTH) *ctx, uint64_t value, unsigned int n_bits) { #if WIDTH > BIT_STREAM_MAX_WRITE @@ -95,7 +95,7 @@ static inline VtencErrorCode encode_lower_bits_step(WIDTH)(struct EncodeCtx(WIDT return bswriter_write(&(ctx->bits_writer), value & BITS_SIZE_MASK[n_bits], n_bits); } -static inline VtencErrorCode encode_lower_bits(WIDTH)(struct EncodeCtx(WIDTH) *ctx, +static inline VtencErrorCode encode_lower_bits(WIDTH)(struct encctx(WIDTH) *ctx, const TYPE *values, size_t values_len, unsigned int n_bits) { size_t i; @@ -107,7 +107,7 @@ static inline VtencErrorCode encode_lower_bits(WIDTH)(struct EncodeCtx(WIDTH) *c return VtencErrorNoError; } -static inline void bcltree_add(WIDTH)(struct EncodeCtx(WIDTH) *ctx, +static inline void bcltree_add(WIDTH)(struct encctx(WIDTH) *ctx, const struct enc_bit_cluster *cluster) { if (cluster->bit_pos == 0) @@ -119,17 +119,17 @@ static inline void bcltree_add(WIDTH)(struct EncodeCtx(WIDTH) *ctx, enc_stack_push(&ctx->stack, cluster); } -static inline int bcltree_has_more(WIDTH)(struct EncodeCtx(WIDTH) *ctx) +static inline int bcltree_has_more(WIDTH)(struct encctx(WIDTH) *ctx) { return !enc_stack_empty(&ctx->stack); } -static inline struct enc_bit_cluster *bcltree_next(WIDTH)(struct EncodeCtx(WIDTH) *ctx) +static inline struct enc_bit_cluster *bcltree_next(WIDTH)(struct encctx(WIDTH) *ctx) { return enc_stack_pop(&ctx->stack); } -static VtencErrorCode encode_bit_cluster_tree(WIDTH)(struct EncodeCtx(WIDTH) *ctx) +static VtencErrorCode encode_bit_cluster_tree(WIDTH)(struct encctx(WIDTH) *ctx) { bcltree_add(WIDTH)(ctx, &(struct enc_bit_cluster){0, ctx->values_len, WIDTH}); @@ -167,7 +167,7 @@ static VtencErrorCode encode_bit_cluster_tree(WIDTH)(struct EncodeCtx(WIDTH) *ct size_t vtenc_encode(WIDTH)(VtencEncoder *enc, const TYPE *in, size_t in_len, uint8_t *out, size_t out_cap) { - struct EncodeCtx(WIDTH) ctx; + struct encctx(WIDTH) ctx; uint64_t max_values = enc->allow_repeated_values ? LIST_MAX_VALUES : SET_MAX_VALUES; enc->last_error_code = VtencErrorNoError; From 4570592026b48d3ae7696b525e8d7dadd1573a94 Mon Sep 17 00:00:00 2001 From: Vicente Romero Date: Tue, 1 Feb 2022 13:58:27 +0000 Subject: [PATCH 05/10] add macros to improve readability of encode_generic.h code --- encode_generic.h | 85 +++++++++++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 37 deletions(-) diff --git a/encode_generic.h b/encode_generic.h index bab9b72..fff2c2b 100644 --- a/encode_generic.h +++ b/encode_generic.h @@ -13,24 +13,35 @@ #include "error.h" #include "internals.h" -#define encctx(_width_) PASTE2(encctx, _width_) -#define encctx_init(_width_) WIDTH_SUFFIX(encctx_init, _width_) -#define encctx_close(_width_) WIDTH_SUFFIX(encctx_close, _width_) -#define count_zeros_at_bit_pos(_width_) WIDTH_SUFFIX(count_zeros_at_bit_pos, _width_) -#define encode_lower_bits_step(_width_) WIDTH_SUFFIX(encode_lower_bits_step, _width_) -#define encode_lower_bits(_width_) WIDTH_SUFFIX(encode_lower_bits, _width_) -#define bcltree_add(_width_) WIDTH_SUFFIX(bcltree_add, _width_) -#define bcltree_visit(_width_) WIDTH_SUFFIX(bcltree_visit, _width_) -#define bcltree_has_more(_width_) WIDTH_SUFFIX(bcltree_has_more, _width_) -#define bcltree_next(_width_) WIDTH_SUFFIX(bcltree_next, _width_) -#define encode_bit_cluster_tree(_width_) WIDTH_SUFFIX(encode_bit_cluster_tree, _width_) -#define vtenc_encode(_width_) WIDTH_SUFFIX(vtenc_encode, _width_) -#define vtenc_max_encoded_size(_width_) WIDTH_SUFFIX(vtenc_max_encoded_size, _width_) +#define encctx_(_width_) PASTE2(encctx, _width_) +#define encctx encctx_(WIDTH) +#define encctx_init_(_width_) WIDTH_SUFFIX(encctx_init, _width_) +#define encctx_init encctx_init_(WIDTH) +#define encctx_close_(_width_) WIDTH_SUFFIX(encctx_close, _width_) +#define encctx_close encctx_close_(WIDTH) +#define count_zeros_at_bit_pos_(_width_) WIDTH_SUFFIX(count_zeros_at_bit_pos, _width_) +#define count_zeros_at_bit_pos count_zeros_at_bit_pos_(WIDTH) +#define encode_lower_bits_step_(_width_) WIDTH_SUFFIX(encode_lower_bits_step, _width_) +#define encode_lower_bits_step encode_lower_bits_step_(WIDTH) +#define encode_lower_bits_(_width_) WIDTH_SUFFIX(encode_lower_bits, _width_) +#define encode_lower_bits encode_lower_bits_(WIDTH) +#define bcltree_add_(_width_) WIDTH_SUFFIX(bcltree_add, _width_) +#define bcltree_add bcltree_add_(WIDTH) +#define bcltree_has_more_(_width_) WIDTH_SUFFIX(bcltree_has_more, _width_) +#define bcltree_has_more bcltree_has_more_(WIDTH) +#define bcltree_next_(_width_) WIDTH_SUFFIX(bcltree_next, _width_) +#define bcltree_next bcltree_next_(WIDTH) +#define encode_bit_cluster_tree_(_width_) WIDTH_SUFFIX(encode_bit_cluster_tree, _width_) +#define encode_bit_cluster_tree encode_bit_cluster_tree_(WIDTH) +#define vtenc_encode_(_width_) WIDTH_SUFFIX(vtenc_encode, _width_) +#define vtenc_encode vtenc_encode_(WIDTH) +#define vtenc_max_encoded_size_(_width_) WIDTH_SUFFIX(vtenc_max_encoded_size, _width_) +#define vtenc_max_encoded_size vtenc_max_encoded_size_(WIDTH) #define ENC_RETURN_WITH_CODE(ctx, enc, code) \ do { \ (enc)->last_error_code = code; \ - encctx_close(WIDTH)((ctx)); \ + encctx_close((ctx)); \ return 0; \ } while (0) @@ -42,7 +53,7 @@ do { \ } \ } while(0) -struct encctx(WIDTH) { +struct encctx { const TYPE *values; size_t values_len; int skip_full_subtrees; @@ -51,7 +62,7 @@ struct encctx(WIDTH) { struct bswriter bits_writer; }; -static VtencErrorCode encctx_init(WIDTH)(struct encctx(WIDTH) *ctx, +static VtencErrorCode encctx_init(struct encctx *ctx, const VtencEncoder *enc, const TYPE *in, size_t in_len, uint8_t *out, size_t out_cap) { @@ -71,12 +82,12 @@ static VtencErrorCode encctx_init(WIDTH)(struct encctx(WIDTH) *ctx, return bswriter_init(&(ctx->bits_writer), out, out_cap); } -static inline size_t encctx_close(WIDTH)(struct encctx(WIDTH) *ctx) +static inline size_t encctx_close(struct encctx *ctx) { return bswriter_close(&(ctx->bits_writer)); } -static inline VtencErrorCode encode_lower_bits_step(WIDTH)(struct encctx(WIDTH) *ctx, +static inline VtencErrorCode encode_lower_bits_step(struct encctx *ctx, uint64_t value, unsigned int n_bits) { #if WIDTH > BIT_STREAM_MAX_WRITE @@ -95,19 +106,19 @@ static inline VtencErrorCode encode_lower_bits_step(WIDTH)(struct encctx(WIDTH) return bswriter_write(&(ctx->bits_writer), value & BITS_SIZE_MASK[n_bits], n_bits); } -static inline VtencErrorCode encode_lower_bits(WIDTH)(struct encctx(WIDTH) *ctx, +static inline VtencErrorCode encode_lower_bits(struct encctx *ctx, const TYPE *values, size_t values_len, unsigned int n_bits) { size_t i; for (i = 0; i < values_len; ++i) { - RETURN_IF_ERROR(encode_lower_bits_step(WIDTH)(ctx, values[i], n_bits)); + RETURN_IF_ERROR(encode_lower_bits_step(ctx, values[i], n_bits)); } return VtencErrorNoError; } -static inline void bcltree_add(WIDTH)(struct encctx(WIDTH) *ctx, +static inline void bcltree_add(struct encctx *ctx, const struct enc_bit_cluster *cluster) { if (cluster->bit_pos == 0) @@ -119,22 +130,22 @@ static inline void bcltree_add(WIDTH)(struct encctx(WIDTH) *ctx, enc_stack_push(&ctx->stack, cluster); } -static inline int bcltree_has_more(WIDTH)(struct encctx(WIDTH) *ctx) +static inline int bcltree_has_more(struct encctx *ctx) { return !enc_stack_empty(&ctx->stack); } -static inline struct enc_bit_cluster *bcltree_next(WIDTH)(struct encctx(WIDTH) *ctx) +static inline struct enc_bit_cluster *bcltree_next(struct encctx *ctx) { return enc_stack_pop(&ctx->stack); } -static VtencErrorCode encode_bit_cluster_tree(WIDTH)(struct encctx(WIDTH) *ctx) +static VtencErrorCode encode_bit_cluster_tree(struct encctx *ctx) { - bcltree_add(WIDTH)(ctx, &(struct enc_bit_cluster){0, ctx->values_len, WIDTH}); + bcltree_add(ctx, &(struct enc_bit_cluster){0, ctx->values_len, WIDTH}); - while (bcltree_has_more(WIDTH)(ctx)) { - struct enc_bit_cluster *cluster = bcltree_next(WIDTH)(ctx); + while (bcltree_has_more(ctx)) { + struct enc_bit_cluster *cluster = bcltree_next(ctx); size_t cl_from = cluster->from; size_t cl_len = cluster->length; unsigned int cl_bit_pos = cluster->bit_pos; @@ -144,11 +155,11 @@ static VtencErrorCode encode_bit_cluster_tree(WIDTH)(struct encctx(WIDTH) *ctx) continue; if (cl_len <= ctx->min_cluster_length) { - RETURN_IF_ERROR(encode_lower_bits(WIDTH)(ctx, ctx->values + cl_from, cl_len, cl_bit_pos)); + RETURN_IF_ERROR(encode_lower_bits(ctx, ctx->values + cl_from, cl_len, cl_bit_pos)); continue; } - size_t n_zeros = count_zeros_at_bit_pos(WIDTH)(ctx->values + cl_from, cl_len, cur_bit_pos); + size_t n_zeros = count_zeros_at_bit_pos(ctx->values + cl_from, cl_len, cur_bit_pos); unsigned int enc_len = bits_len_u64(cl_len); RETURN_IF_ERROR(bswriter_write(&(ctx->bits_writer), n_zeros, enc_len)); @@ -156,36 +167,36 @@ static VtencErrorCode encode_bit_cluster_tree(WIDTH)(struct encctx(WIDTH) *ctx) struct enc_bit_cluster zeros_cluster = {cl_from, n_zeros, cur_bit_pos}; struct enc_bit_cluster ones_cluster = {cl_from + n_zeros, cl_len - n_zeros, cur_bit_pos}; - bcltree_add(WIDTH)(ctx, &ones_cluster); - bcltree_add(WIDTH)(ctx, &zeros_cluster); + bcltree_add(ctx, &ones_cluster); + bcltree_add(ctx, &zeros_cluster); } } return VtencErrorNoError; } -size_t vtenc_encode(WIDTH)(VtencEncoder *enc, const TYPE *in, size_t in_len, +size_t vtenc_encode(VtencEncoder *enc, const TYPE *in, size_t in_len, uint8_t *out, size_t out_cap) { - struct encctx(WIDTH) ctx; + struct encctx ctx; uint64_t max_values = enc->allow_repeated_values ? LIST_MAX_VALUES : SET_MAX_VALUES; enc->last_error_code = VtencErrorNoError; ENC_RETURN_ON_ERROR(&ctx, enc, - encctx_init(WIDTH)(&ctx, enc, in, in_len, out, out_cap) + encctx_init(&ctx, enc, in, in_len, out, out_cap) ); if ((uint64_t)in_len > max_values) { ENC_RETURN_WITH_CODE(&ctx, enc, VtencErrorInputTooBig); } - ENC_RETURN_ON_ERROR(&ctx, enc, encode_bit_cluster_tree(WIDTH)(&ctx)); + ENC_RETURN_ON_ERROR(&ctx, enc, encode_bit_cluster_tree(&ctx)); - return encctx_close(WIDTH)(&ctx); + return encctx_close(&ctx); } -size_t vtenc_max_encoded_size(WIDTH)(size_t in_len) +size_t vtenc_max_encoded_size(size_t in_len) { return bswriter_align_buffer_size((WIDTH / 8) * (in_len + 1)); } From b4f997ee791988830894210378626af0c65d2a00 Mon Sep 17 00:00:00 2001 From: Vicente Romero Date: Tue, 1 Feb 2022 14:11:09 +0000 Subject: [PATCH 06/10] add macros to improve readability of decode_generic.h code --- decode_generic.h | 73 +++++++++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 32 deletions(-) diff --git a/decode_generic.h b/decode_generic.h index b541645..b0188f4 100644 --- a/decode_generic.h +++ b/decode_generic.h @@ -12,17 +12,26 @@ #include "error.h" #include "internals.h" -#define decctx(_width_) PASTE2(decctx, _width_) -#define decctx_init(_width_) WIDTH_SUFFIX(decctx_init, _width_) -#define decode_lower_bits_step(_width_) WIDTH_SUFFIX(decode_lower_bits_step, _width_) -#define decode_lower_bits(_width_) WIDTH_SUFFIX(decode_lower_bits, _width_) -#define decode_full_subtree(_width_) WIDTH_SUFFIX(decode_full_subtree, _width_) -#define bcltree_add(_width_) WIDTH_SUFFIX(bcltree_add, _width_) -#define bcltree_visit(_width_) WIDTH_SUFFIX(bcltree_visit, _width_) -#define bcltree_has_more(_width_) WIDTH_SUFFIX(bcltree_has_more, _width_) -#define bcltree_next(_width_) WIDTH_SUFFIX(bcltree_next, _width_) -#define decode_bit_cluster_tree(_width_) WIDTH_SUFFIX(decode_bit_cluster_tree, _width_) -#define vtenc_decode(_width_) WIDTH_SUFFIX(vtenc_decode, _width_) +#define decctx_(_width_) PASTE2(decctx, _width_) +#define decctx decctx_(WIDTH) +#define decctx_init_(_width_) WIDTH_SUFFIX(decctx_init, _width_) +#define decctx_init decctx_init_(WIDTH) +#define decode_lower_bits_step_(_width_) WIDTH_SUFFIX(decode_lower_bits_step, _width_) +#define decode_lower_bits_step decode_lower_bits_step_(WIDTH) +#define decode_lower_bits_(_width_) WIDTH_SUFFIX(decode_lower_bits, _width_) +#define decode_lower_bits decode_lower_bits_(WIDTH) +#define decode_full_subtree_(_width_) WIDTH_SUFFIX(decode_full_subtree, _width_) +#define decode_full_subtree decode_full_subtree_(WIDTH) +#define bcltree_add_(_width_) WIDTH_SUFFIX(bcltree_add, _width_) +#define bcltree_add bcltree_add_(WIDTH) +#define bcltree_has_more_(_width_) WIDTH_SUFFIX(bcltree_has_more, _width_) +#define bcltree_has_more bcltree_has_more_(WIDTH) +#define bcltree_next_(_width_) WIDTH_SUFFIX(bcltree_next, _width_) +#define bcltree_next bcltree_next_(WIDTH) +#define decode_bit_cluster_tree_(_width_) WIDTH_SUFFIX(decode_bit_cluster_tree, _width_) +#define decode_bit_cluster_tree decode_bit_cluster_tree_(WIDTH) +#define vtenc_decode_(_width_) WIDTH_SUFFIX(vtenc_decode, _width_) +#define vtenc_decode vtenc_decode_(WIDTH) #define DEC_RETURN_WITH_CODE(dec, code) \ do { \ @@ -38,7 +47,7 @@ do { \ } \ } while (0) -struct decctx(WIDTH) { +struct decctx { TYPE *values; size_t values_len; int reconstruct_full_subtrees; @@ -47,7 +56,7 @@ struct decctx(WIDTH) { struct bsreader bits_reader; }; -static VtencErrorCode decctx_init(WIDTH)(struct decctx(WIDTH) *ctx, +static VtencErrorCode decctx_init(struct decctx *ctx, const VtencDecoder *dec, const uint8_t *in, size_t in_len, TYPE *out, size_t out_len) { @@ -69,7 +78,7 @@ static VtencErrorCode decctx_init(WIDTH)(struct decctx(WIDTH) *ctx, return VtencErrorNoError; } -static inline VtencErrorCode decode_lower_bits_step(WIDTH)(struct decctx(WIDTH) *ctx, +static inline VtencErrorCode decode_lower_bits_step(struct decctx *ctx, TYPE *value, unsigned int n_bits) { #if WIDTH > BIT_STREAM_MAX_READ @@ -97,20 +106,20 @@ static inline VtencErrorCode decode_lower_bits_step(WIDTH)(struct decctx(WIDTH) #endif } -static inline VtencErrorCode decode_lower_bits(WIDTH)(struct decctx(WIDTH) *ctx, +static inline VtencErrorCode decode_lower_bits(struct decctx *ctx, TYPE *values, size_t values_len, unsigned int n_bits, TYPE higher_bits) { size_t i; for (i = 0; i < values_len; ++i) { values[i] = higher_bits; - RETURN_IF_ERROR(decode_lower_bits_step(WIDTH)(ctx, &values[i], n_bits)); + RETURN_IF_ERROR(decode_lower_bits_step(ctx, &values[i], n_bits)); } return VtencErrorNoError; } -static inline void decode_full_subtree(WIDTH)(TYPE *values, size_t values_len, TYPE higher_bits) +static inline void decode_full_subtree(TYPE *values, size_t values_len, TYPE higher_bits) { size_t i; @@ -119,7 +128,7 @@ static inline void decode_full_subtree(WIDTH)(TYPE *values, size_t values_len, T } } -static inline void bcltree_add(WIDTH)(struct decctx(WIDTH) *ctx, +static inline void bcltree_add(struct decctx *ctx, const struct dec_bit_cluster *cluster) { if (cluster->length == 0) @@ -128,22 +137,22 @@ static inline void bcltree_add(WIDTH)(struct decctx(WIDTH) *ctx, dec_stack_push(&ctx->stack, cluster); } -static inline int bcltree_has_more(WIDTH)(struct decctx(WIDTH) *ctx) +static inline int bcltree_has_more(struct decctx *ctx) { return !dec_stack_empty(&ctx->stack); } -static inline struct dec_bit_cluster *bcltree_next(WIDTH)(struct decctx(WIDTH) *ctx) +static inline struct dec_bit_cluster *bcltree_next(struct decctx *ctx) { return dec_stack_pop(&ctx->stack); } -static VtencErrorCode decode_bit_cluster_tree(WIDTH)(struct decctx(WIDTH) *ctx) +static VtencErrorCode decode_bit_cluster_tree(struct decctx *ctx) { - bcltree_add(WIDTH)(ctx, &(struct dec_bit_cluster){0, ctx->values_len, WIDTH, 0}); + bcltree_add(ctx, &(struct dec_bit_cluster){0, ctx->values_len, WIDTH, 0}); - while (bcltree_has_more(WIDTH)(ctx)) { - struct dec_bit_cluster *cluster = bcltree_next(WIDTH)(ctx); + while (bcltree_has_more(ctx)) { + struct dec_bit_cluster *cluster = bcltree_next(ctx); size_t cl_from = cluster->from; size_t cl_len = cluster->length; unsigned int cl_bit_pos = cluster->bit_pos; @@ -162,12 +171,12 @@ static VtencErrorCode decode_bit_cluster_tree(WIDTH)(struct decctx(WIDTH) *ctx) } if (ctx->reconstruct_full_subtrees && is_full_subtree(cl_len, cl_bit_pos)) { - decode_full_subtree(WIDTH)(ctx->values + cl_from, cl_len, cl_higher_bits); + decode_full_subtree(ctx->values + cl_from, cl_len, cl_higher_bits); continue; } if (cl_len <= ctx->min_cluster_length) { - RETURN_IF_ERROR(decode_lower_bits(WIDTH)(ctx, ctx->values + cl_from, cl_len, cl_bit_pos, cl_higher_bits)); + RETURN_IF_ERROR(decode_lower_bits(ctx, ctx->values + cl_from, cl_len, cl_bit_pos, cl_higher_bits)); continue; } @@ -182,24 +191,24 @@ static VtencErrorCode decode_bit_cluster_tree(WIDTH)(struct decctx(WIDTH) *ctx) struct dec_bit_cluster zeros_cluster = {cl_from, n_zeros, next_bit_pos, cl_higher_bits}; struct dec_bit_cluster ones_cluster = {cl_from + n_zeros, cl_len - n_zeros, next_bit_pos, cl_higher_bits | (1LL << (next_bit_pos))}; - bcltree_add(WIDTH)(ctx, &ones_cluster); - bcltree_add(WIDTH)(ctx, &zeros_cluster); + bcltree_add(ctx, &ones_cluster); + bcltree_add(ctx, &zeros_cluster); } } return VtencErrorNoError; } -void vtenc_decode(WIDTH)(VtencDecoder *dec, const uint8_t *in, size_t in_len, +void vtenc_decode(VtencDecoder *dec, const uint8_t *in, size_t in_len, TYPE *out, size_t out_len) { - struct decctx(WIDTH) ctx; + struct decctx ctx; uint64_t max_values = dec->allow_repeated_values ? LIST_MAX_VALUES : SET_MAX_VALUES; dec->last_error_code = VtencErrorNoError; DEC_RETURN_ON_ERROR(dec, - decctx_init(WIDTH)(&ctx, dec, in, in_len, out, out_len) + decctx_init(&ctx, dec, in, in_len, out, out_len) ); if ((uint64_t)out_len > max_values) { @@ -208,5 +217,5 @@ void vtenc_decode(WIDTH)(VtencDecoder *dec, const uint8_t *in, size_t in_len, memset(out, 0, out_len * sizeof(*out)); - DEC_RETURN_ON_ERROR(dec, decode_bit_cluster_tree(WIDTH)(&ctx)); + DEC_RETURN_ON_ERROR(dec, decode_bit_cluster_tree(&ctx)); } From 64651a1ba76ebf4373d13978e730d2b99da7642a Mon Sep 17 00:00:00 2001 From: Vicente Romero Date: Tue, 1 Feb 2022 14:50:39 +0000 Subject: [PATCH 07/10] rename macros resembling function in lower case --- bitstream.h | 4 ++-- decode_generic.h | 24 ++++++++++++------------ encode_generic.h | 8 ++++---- error.h | 21 +++------------------ 4 files changed, 21 insertions(+), 36 deletions(-) diff --git a/bitstream.h b/bitstream.h index 1225a84..4f03fc1 100644 --- a/bitstream.h +++ b/bitstream.h @@ -75,7 +75,7 @@ static inline VtencErrorCode bswriter_write(struct bswriter *writer, if (writer->ptr > writer->end_ptr) return VtencErrorEndOfStream; if (n_bits + writer->bit_pos >= 64) - RETURN_IF_ERROR(bswriter_flush(writer)); + return_if_error(bswriter_flush(writer)); bswriter_append(writer, value, n_bits); @@ -151,7 +151,7 @@ static inline VtencErrorCode bsreader_read(struct bsreader *reader, assert(n_bits <= BIT_STREAM_MAX_READ); if (n_bits + reader->bits_consumed > reader->bits_loaded) { - RETURN_IF_ERROR(bsreader_load(reader)); + return_if_error(bsreader_load(reader)); if (n_bits + reader->bits_consumed > reader->bits_loaded) return VtencErrorNotEnoughBits; diff --git a/decode_generic.h b/decode_generic.h index b0188f4..301fc3d 100644 --- a/decode_generic.h +++ b/decode_generic.h @@ -33,17 +33,17 @@ #define vtenc_decode_(_width_) WIDTH_SUFFIX(vtenc_decode, _width_) #define vtenc_decode vtenc_decode_(WIDTH) -#define DEC_RETURN_WITH_CODE(dec, code) \ +#define dec_return_with_code(dec, code) \ do { \ (dec)->last_error_code = code; \ return; \ } while (0) -#define DEC_RETURN_ON_ERROR(dec, exp) \ +#define dec_return_on_error(dec, exp) \ do { \ const VtencErrorCode code = (exp); \ if (code != VtencErrorNoError) { \ - DEC_RETURN_WITH_CODE(dec, code); \ + dec_return_with_code(dec, code); \ } \ } while (0) @@ -86,20 +86,20 @@ static inline VtencErrorCode decode_lower_bits_step(struct decctx *ctx, unsigned int shift = 0; if (n_bits > BIT_STREAM_MAX_READ) { - RETURN_IF_ERROR(bsreader_read(&(ctx->bits_reader), BIT_STREAM_MAX_READ, &lower)); + return_if_error(bsreader_read(&(ctx->bits_reader), BIT_STREAM_MAX_READ, &lower)); *value |= lower; shift = BIT_STREAM_MAX_READ; n_bits -= BIT_STREAM_MAX_READ; } - RETURN_IF_ERROR(bsreader_read(&(ctx->bits_reader), n_bits, &lower)); + return_if_error(bsreader_read(&(ctx->bits_reader), n_bits, &lower)); *value |= lower << shift; return VtencErrorNoError; #else uint64_t lower; - RETURN_IF_ERROR(bsreader_read(&(ctx->bits_reader), n_bits, &lower)); + return_if_error(bsreader_read(&(ctx->bits_reader), n_bits, &lower)); *value |= (TYPE)lower; return VtencErrorNoError; @@ -113,7 +113,7 @@ static inline VtencErrorCode decode_lower_bits(struct decctx *ctx, for (i = 0; i < values_len; ++i) { values[i] = higher_bits; - RETURN_IF_ERROR(decode_lower_bits_step(ctx, &values[i], n_bits)); + return_if_error(decode_lower_bits_step(ctx, &values[i], n_bits)); } return VtencErrorNoError; @@ -176,13 +176,13 @@ static VtencErrorCode decode_bit_cluster_tree(struct decctx *ctx) } if (cl_len <= ctx->min_cluster_length) { - RETURN_IF_ERROR(decode_lower_bits(ctx, ctx->values + cl_from, cl_len, cl_bit_pos, cl_higher_bits)); + return_if_error(decode_lower_bits(ctx, ctx->values + cl_from, cl_len, cl_bit_pos, cl_higher_bits)); continue; } enc_len = bits_len_u64(cl_len); - RETURN_IF_ERROR(bsreader_read(&(ctx->bits_reader), enc_len, &n_zeros)); + return_if_error(bsreader_read(&(ctx->bits_reader), enc_len, &n_zeros)); if (n_zeros > (uint64_t)cl_len) return VtencErrorWrongFormat; @@ -207,15 +207,15 @@ void vtenc_decode(VtencDecoder *dec, const uint8_t *in, size_t in_len, dec->last_error_code = VtencErrorNoError; - DEC_RETURN_ON_ERROR(dec, + dec_return_on_error(dec, decctx_init(&ctx, dec, in, in_len, out, out_len) ); if ((uint64_t)out_len > max_values) { - DEC_RETURN_WITH_CODE(dec, VtencErrorOutputTooBig); + dec_return_with_code(dec, VtencErrorOutputTooBig); } memset(out, 0, out_len * sizeof(*out)); - DEC_RETURN_ON_ERROR(dec, decode_bit_cluster_tree(&ctx)); + dec_return_on_error(dec, decode_bit_cluster_tree(&ctx)); } diff --git a/encode_generic.h b/encode_generic.h index fff2c2b..9957ac9 100644 --- a/encode_generic.h +++ b/encode_generic.h @@ -92,7 +92,7 @@ static inline VtencErrorCode encode_lower_bits_step(struct encctx *ctx, { #if WIDTH > BIT_STREAM_MAX_WRITE if (n_bits > BIT_STREAM_MAX_WRITE) { - RETURN_IF_ERROR(bswriter_write( + return_if_error(bswriter_write( &(ctx->bits_writer), value & BITS_SIZE_MASK[BIT_STREAM_MAX_WRITE], BIT_STREAM_MAX_WRITE @@ -112,7 +112,7 @@ static inline VtencErrorCode encode_lower_bits(struct encctx *ctx, size_t i; for (i = 0; i < values_len; ++i) { - RETURN_IF_ERROR(encode_lower_bits_step(ctx, values[i], n_bits)); + return_if_error(encode_lower_bits_step(ctx, values[i], n_bits)); } return VtencErrorNoError; @@ -155,13 +155,13 @@ static VtencErrorCode encode_bit_cluster_tree(struct encctx *ctx) continue; if (cl_len <= ctx->min_cluster_length) { - RETURN_IF_ERROR(encode_lower_bits(ctx, ctx->values + cl_from, cl_len, cl_bit_pos)); + return_if_error(encode_lower_bits(ctx, ctx->values + cl_from, cl_len, cl_bit_pos)); continue; } size_t n_zeros = count_zeros_at_bit_pos(ctx->values + cl_from, cl_len, cur_bit_pos); unsigned int enc_len = bits_len_u64(cl_len); - RETURN_IF_ERROR(bswriter_write(&(ctx->bits_writer), n_zeros, enc_len)); + return_if_error(bswriter_write(&(ctx->bits_writer), n_zeros, enc_len)); { struct enc_bit_cluster zeros_cluster = {cl_from, n_zeros, cur_bit_pos}; diff --git a/error.h b/error.h index 4214064..e0a21d1 100644 --- a/error.h +++ b/error.h @@ -8,27 +8,12 @@ #include "vtenc.h" -#define RETURN_IF_ERROR(exp) \ +#define return_if_error(exp) \ do { \ const VtencErrorCode code = (exp); \ - if (code != VtencErrorNoError) \ + if (code != VtencErrorNoError) { \ return code; \ -} while(0) - -#define RETURN_IF_ERROR_WITH(exp, cleanup) \ -do { \ - const VtencErrorCode code = (exp); \ - if (code != VtencErrorNoError) { \ - (cleanup); \ - return code; \ - } \ -} while(0) - -#define RETURN_ZERO_IF_ERROR(exp) \ -do { \ - const VtencErrorCode code = (exp); \ - if (code != VtencErrorNoError) \ - return 0; \ + } \ } while(0) #endif /* VTENC_ERROR_H_ */ From 2c28dc8dc7f926b33904b4f0fe90d9993d85d10f Mon Sep 17 00:00:00 2001 From: Vicente Romero Date: Tue, 1 Feb 2022 15:01:45 +0000 Subject: [PATCH 08/10] rename WIDTH > BITWIDTH --- decode.c | 16 +++++++------- decode_generic.h | 44 +++++++++++++++++++-------------------- encode.c | 16 +++++++------- encode_generic.h | 54 ++++++++++++++++++++++++------------------------ internals.h | 2 +- 5 files changed, 66 insertions(+), 66 deletions(-) diff --git a/decode.c b/decode.c index 0a4a782..6758e78 100644 --- a/decode.c +++ b/decode.c @@ -30,35 +30,35 @@ CREATE_STACK(dec_stack, struct dec_bit_cluster, DECODE_STACK_MAX_SIZE) #define LIST_MAX_VALUES VTENC_LIST_MAX_VALUES #define TYPE uint8_t -#define WIDTH 8 +#define BITWIDTH 8 #define SET_MAX_VALUES VTENC_SET_MAX_VALUES8 #include "decode_generic.h" #undef TYPE -#undef WIDTH +#undef BITWIDTH #undef SET_MAX_VALUES #define TYPE uint16_t -#define WIDTH 16 +#define BITWIDTH 16 #define SET_MAX_VALUES VTENC_SET_MAX_VALUES16 #include "decode_generic.h" #undef TYPE -#undef WIDTH +#undef BITWIDTH #undef SET_MAX_VALUES #define TYPE uint32_t -#define WIDTH 32 +#define BITWIDTH 32 #define SET_MAX_VALUES VTENC_SET_MAX_VALUES32 #include "decode_generic.h" #undef TYPE -#undef WIDTH +#undef BITWIDTH #undef SET_MAX_VALUES #define TYPE uint64_t -#define WIDTH 64 +#define BITWIDTH 64 #define SET_MAX_VALUES VTENC_SET_MAX_VALUES64 #include "decode_generic.h" #undef TYPE -#undef WIDTH +#undef BITWIDTH #undef SET_MAX_VALUES #undef LIST_MAX_VALUES diff --git a/decode_generic.h b/decode_generic.h index 301fc3d..48628df 100644 --- a/decode_generic.h +++ b/decode_generic.h @@ -12,26 +12,26 @@ #include "error.h" #include "internals.h" -#define decctx_(_width_) PASTE2(decctx, _width_) -#define decctx decctx_(WIDTH) -#define decctx_init_(_width_) WIDTH_SUFFIX(decctx_init, _width_) -#define decctx_init decctx_init_(WIDTH) -#define decode_lower_bits_step_(_width_) WIDTH_SUFFIX(decode_lower_bits_step, _width_) -#define decode_lower_bits_step decode_lower_bits_step_(WIDTH) -#define decode_lower_bits_(_width_) WIDTH_SUFFIX(decode_lower_bits, _width_) -#define decode_lower_bits decode_lower_bits_(WIDTH) -#define decode_full_subtree_(_width_) WIDTH_SUFFIX(decode_full_subtree, _width_) -#define decode_full_subtree decode_full_subtree_(WIDTH) -#define bcltree_add_(_width_) WIDTH_SUFFIX(bcltree_add, _width_) -#define bcltree_add bcltree_add_(WIDTH) -#define bcltree_has_more_(_width_) WIDTH_SUFFIX(bcltree_has_more, _width_) -#define bcltree_has_more bcltree_has_more_(WIDTH) -#define bcltree_next_(_width_) WIDTH_SUFFIX(bcltree_next, _width_) -#define bcltree_next bcltree_next_(WIDTH) -#define decode_bit_cluster_tree_(_width_) WIDTH_SUFFIX(decode_bit_cluster_tree, _width_) -#define decode_bit_cluster_tree decode_bit_cluster_tree_(WIDTH) -#define vtenc_decode_(_width_) WIDTH_SUFFIX(vtenc_decode, _width_) -#define vtenc_decode vtenc_decode_(WIDTH) +#define decctx_(_width_) BITWIDTH_SUFFIX(decctx, _width_) +#define decctx decctx_(BITWIDTH) +#define decctx_init_(_width_) BITWIDTH_SUFFIX(decctx_init, _width_) +#define decctx_init decctx_init_(BITWIDTH) +#define decode_lower_bits_step_(_width_) BITWIDTH_SUFFIX(decode_lower_bits_step, _width_) +#define decode_lower_bits_step decode_lower_bits_step_(BITWIDTH) +#define decode_lower_bits_(_width_) BITWIDTH_SUFFIX(decode_lower_bits, _width_) +#define decode_lower_bits decode_lower_bits_(BITWIDTH) +#define decode_full_subtree_(_width_) BITWIDTH_SUFFIX(decode_full_subtree, _width_) +#define decode_full_subtree decode_full_subtree_(BITWIDTH) +#define bcltree_add_(_width_) BITWIDTH_SUFFIX(bcltree_add, _width_) +#define bcltree_add bcltree_add_(BITWIDTH) +#define bcltree_has_more_(_width_) BITWIDTH_SUFFIX(bcltree_has_more, _width_) +#define bcltree_has_more bcltree_has_more_(BITWIDTH) +#define bcltree_next_(_width_) BITWIDTH_SUFFIX(bcltree_next, _width_) +#define bcltree_next bcltree_next_(BITWIDTH) +#define decode_bit_cluster_tree_(_width_) BITWIDTH_SUFFIX(decode_bit_cluster_tree, _width_) +#define decode_bit_cluster_tree decode_bit_cluster_tree_(BITWIDTH) +#define vtenc_decode_(_width_) BITWIDTH_SUFFIX(vtenc_decode, _width_) +#define vtenc_decode vtenc_decode_(BITWIDTH) #define dec_return_with_code(dec, code) \ do { \ @@ -81,7 +81,7 @@ static VtencErrorCode decctx_init(struct decctx *ctx, static inline VtencErrorCode decode_lower_bits_step(struct decctx *ctx, TYPE *value, unsigned int n_bits) { -#if WIDTH > BIT_STREAM_MAX_READ +#if BITWIDTH > BIT_STREAM_MAX_READ uint64_t lower; unsigned int shift = 0; @@ -149,7 +149,7 @@ static inline struct dec_bit_cluster *bcltree_next(struct decctx *ctx) static VtencErrorCode decode_bit_cluster_tree(struct decctx *ctx) { - bcltree_add(ctx, &(struct dec_bit_cluster){0, ctx->values_len, WIDTH, 0}); + bcltree_add(ctx, &(struct dec_bit_cluster){0, ctx->values_len, BITWIDTH, 0}); while (bcltree_has_more(ctx)) { struct dec_bit_cluster *cluster = bcltree_next(ctx); diff --git a/encode.c b/encode.c index 4a4bcba..34a2686 100644 --- a/encode.c +++ b/encode.c @@ -29,35 +29,35 @@ CREATE_STACK(enc_stack, struct enc_bit_cluster, ENCODE_STACK_MAX_SIZE) #define LIST_MAX_VALUES VTENC_LIST_MAX_VALUES #define TYPE uint8_t -#define WIDTH 8 +#define BITWIDTH 8 #define SET_MAX_VALUES VTENC_SET_MAX_VALUES8 #include "encode_generic.h" #undef TYPE -#undef WIDTH +#undef BITWIDTH #undef SET_MAX_VALUES #define TYPE uint16_t -#define WIDTH 16 +#define BITWIDTH 16 #define SET_MAX_VALUES VTENC_SET_MAX_VALUES16 #include "encode_generic.h" #undef TYPE -#undef WIDTH +#undef BITWIDTH #undef SET_MAX_VALUES #define TYPE uint32_t -#define WIDTH 32 +#define BITWIDTH 32 #define SET_MAX_VALUES VTENC_SET_MAX_VALUES32 #include "encode_generic.h" #undef TYPE -#undef WIDTH +#undef BITWIDTH #undef SET_MAX_VALUES #define TYPE uint64_t -#define WIDTH 64 +#define BITWIDTH 64 #define SET_MAX_VALUES VTENC_SET_MAX_VALUES64 #include "encode_generic.h" #undef TYPE -#undef WIDTH +#undef BITWIDTH #undef SET_MAX_VALUES #undef LIST_MAX_VALUES diff --git a/encode_generic.h b/encode_generic.h index 9957ac9..b4fc78b 100644 --- a/encode_generic.h +++ b/encode_generic.h @@ -13,30 +13,30 @@ #include "error.h" #include "internals.h" -#define encctx_(_width_) PASTE2(encctx, _width_) -#define encctx encctx_(WIDTH) -#define encctx_init_(_width_) WIDTH_SUFFIX(encctx_init, _width_) -#define encctx_init encctx_init_(WIDTH) -#define encctx_close_(_width_) WIDTH_SUFFIX(encctx_close, _width_) -#define encctx_close encctx_close_(WIDTH) -#define count_zeros_at_bit_pos_(_width_) WIDTH_SUFFIX(count_zeros_at_bit_pos, _width_) -#define count_zeros_at_bit_pos count_zeros_at_bit_pos_(WIDTH) -#define encode_lower_bits_step_(_width_) WIDTH_SUFFIX(encode_lower_bits_step, _width_) -#define encode_lower_bits_step encode_lower_bits_step_(WIDTH) -#define encode_lower_bits_(_width_) WIDTH_SUFFIX(encode_lower_bits, _width_) -#define encode_lower_bits encode_lower_bits_(WIDTH) -#define bcltree_add_(_width_) WIDTH_SUFFIX(bcltree_add, _width_) -#define bcltree_add bcltree_add_(WIDTH) -#define bcltree_has_more_(_width_) WIDTH_SUFFIX(bcltree_has_more, _width_) -#define bcltree_has_more bcltree_has_more_(WIDTH) -#define bcltree_next_(_width_) WIDTH_SUFFIX(bcltree_next, _width_) -#define bcltree_next bcltree_next_(WIDTH) -#define encode_bit_cluster_tree_(_width_) WIDTH_SUFFIX(encode_bit_cluster_tree, _width_) -#define encode_bit_cluster_tree encode_bit_cluster_tree_(WIDTH) -#define vtenc_encode_(_width_) WIDTH_SUFFIX(vtenc_encode, _width_) -#define vtenc_encode vtenc_encode_(WIDTH) -#define vtenc_max_encoded_size_(_width_) WIDTH_SUFFIX(vtenc_max_encoded_size, _width_) -#define vtenc_max_encoded_size vtenc_max_encoded_size_(WIDTH) +#define encctx_(_width_) BITWIDTH_SUFFIX(encctx, _width_) +#define encctx encctx_(BITWIDTH) +#define encctx_init_(_width_) BITWIDTH_SUFFIX(encctx_init, _width_) +#define encctx_init encctx_init_(BITWIDTH) +#define encctx_close_(_width_) BITWIDTH_SUFFIX(encctx_close, _width_) +#define encctx_close encctx_close_(BITWIDTH) +#define count_zeros_at_bit_pos_(_width_) BITWIDTH_SUFFIX(count_zeros_at_bit_pos, _width_) +#define count_zeros_at_bit_pos count_zeros_at_bit_pos_(BITWIDTH) +#define encode_lower_bits_step_(_width_) BITWIDTH_SUFFIX(encode_lower_bits_step, _width_) +#define encode_lower_bits_step encode_lower_bits_step_(BITWIDTH) +#define encode_lower_bits_(_width_) BITWIDTH_SUFFIX(encode_lower_bits, _width_) +#define encode_lower_bits encode_lower_bits_(BITWIDTH) +#define bcltree_add_(_width_) BITWIDTH_SUFFIX(bcltree_add, _width_) +#define bcltree_add bcltree_add_(BITWIDTH) +#define bcltree_has_more_(_width_) BITWIDTH_SUFFIX(bcltree_has_more, _width_) +#define bcltree_has_more bcltree_has_more_(BITWIDTH) +#define bcltree_next_(_width_) BITWIDTH_SUFFIX(bcltree_next, _width_) +#define bcltree_next bcltree_next_(BITWIDTH) +#define encode_bit_cluster_tree_(_width_) BITWIDTH_SUFFIX(encode_bit_cluster_tree, _width_) +#define encode_bit_cluster_tree encode_bit_cluster_tree_(BITWIDTH) +#define vtenc_encode_(_width_) BITWIDTH_SUFFIX(vtenc_encode, _width_) +#define vtenc_encode vtenc_encode_(BITWIDTH) +#define vtenc_max_encoded_size_(_width_) BITWIDTH_SUFFIX(vtenc_max_encoded_size, _width_) +#define vtenc_max_encoded_size vtenc_max_encoded_size_(BITWIDTH) #define ENC_RETURN_WITH_CODE(ctx, enc, code) \ do { \ @@ -90,7 +90,7 @@ static inline size_t encctx_close(struct encctx *ctx) static inline VtencErrorCode encode_lower_bits_step(struct encctx *ctx, uint64_t value, unsigned int n_bits) { -#if WIDTH > BIT_STREAM_MAX_WRITE +#if BITWIDTH > BIT_STREAM_MAX_WRITE if (n_bits > BIT_STREAM_MAX_WRITE) { return_if_error(bswriter_write( &(ctx->bits_writer), @@ -142,7 +142,7 @@ static inline struct enc_bit_cluster *bcltree_next(struct encctx *ctx) static VtencErrorCode encode_bit_cluster_tree(struct encctx *ctx) { - bcltree_add(ctx, &(struct enc_bit_cluster){0, ctx->values_len, WIDTH}); + bcltree_add(ctx, &(struct enc_bit_cluster){0, ctx->values_len, BITWIDTH}); while (bcltree_has_more(ctx)) { struct enc_bit_cluster *cluster = bcltree_next(ctx); @@ -198,5 +198,5 @@ size_t vtenc_encode(VtencEncoder *enc, const TYPE *in, size_t in_len, size_t vtenc_max_encoded_size(size_t in_len) { - return bswriter_align_buffer_size((WIDTH / 8) * (in_len + 1)); + return bswriter_align_buffer_size((BITWIDTH / 8) * (in_len + 1)); } diff --git a/internals.h b/internals.h index c1a8e53..d79d95b 100644 --- a/internals.h +++ b/internals.h @@ -15,7 +15,7 @@ #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define PASTE2(x, y) x ## y #define PASTE3(x, y, z) x ## y ## z -#define WIDTH_SUFFIX(_name_, _width_) PASTE2(_name_, _width_) +#define BITWIDTH_SUFFIX(_name_, _width_) PASTE2(_name_, _width_) /* VTEnc constants */ From 66e3e5d39970692c1115ff9d5a7ec6565052227d Mon Sep 17 00:00:00 2001 From: Vicente Romero Date: Thu, 3 Feb 2022 16:36:19 +0000 Subject: [PATCH 09/10] add CODING_STYLE.md --- CODING_STYLE.md | 107 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 CODING_STYLE.md diff --git a/CODING_STYLE.md b/CODING_STYLE.md new file mode 100644 index 0000000..ac71650 --- /dev/null +++ b/CODING_STYLE.md @@ -0,0 +1,107 @@ +# Coding style + +The purpose of this document is to define a set of rules that must be followed to keep a consistent coding style, which will make the source code easier to read and maintain. + +It is strongly influenced by the [Linux kernel coding style](https://www.kernel.org/doc/html/v4.10/process/coding-style.html#linux-kernel-coding-style), but it has some differences. + +The rules don't follow any particular order and don't intend to cover every single aspect of creating and maintaining code. Instead, you should take it as a guide that will remove some of the burden of making decisions, specially those with regards to coding aesthetics. + +## Rules + +1. Use 2-spaced indentation. + +2. This is the preferred style for statement braces (`if`, `for`, `switch`, etc.): + ```c + if (condition) { + do_x(); + } else { + do_y(); + } + ``` + +3. Braces can be skipped when there's only one statement: + ```c + if (condition) + do_x(); + ``` + +4. This is also acceptable when line is not too long: + ```c + if (condition) return -1; + ``` + +5. Functions are a special case, with opening brace at the beginning of a new line: + ```c + int function(int x) + { + /* Body goes here */ + } + ``` + +6. Put a space after these keywords: `if, switch, case, for, do, while` + +7. When declaring pointers, the preferred use of * is adjacent to the data name or function name and not adjacent to the type name: + ```c + char *str; + char *copy_str(const char *str); + ``` + +8. Avoid using typedefs as much as possible. Read rationale here: https://www.kernel.org/doc/html/v4.10/process/coding-style.html#typedefs + +9. Function names are `snake_case`: + ```c + int process_request(const struct *request); + ``` + +10. Variable names are `snake_case`: + ```c + size_t arr_len; + char *tmp, *filter_name; + ``` + +11. Struct names are `snake_case`: + ```c + struct decoding_ctx { + size_t processed; + size_t remaining; + }; + ``` + +12. Enum names are `snake_case`: + ```c + enum response_error { + BAD_REQUEST_ERROR, + UNREACHABLE_ERROR + }; + ``` + +13. Names of macros defining constants are `CAPITALISED`: + ```c + #define MAGIC_NUMBER 42 + ``` + +14. Labels in enums are `CAPITALISED`: + ```c + enum { + PERSIST, + CONTINUE, + ABORT + }; + ``` + +15. Macros resembling functions may be named in lower case: + ```c + #define list_add(list, element) + ``` + +16. The preferred style for multi-line comments is: + ```c + /* + * This is a multi-line comment that doesn't fit in a + * single line. + * + * Leave a blank line between paragraphs within the block + * comment. + */ + ``` + From e4138fe574309bef4367d67017fb2fe85827c455 Mon Sep 17 00:00:00 2001 From: Vicente Romero Date: Mon, 7 Feb 2022 17:03:54 +0000 Subject: [PATCH 10/10] change macros resembling functions to lower case on encode_generic.h --- encode_generic.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/encode_generic.h b/encode_generic.h index b4fc78b..792475f 100644 --- a/encode_generic.h +++ b/encode_generic.h @@ -38,18 +38,18 @@ #define vtenc_max_encoded_size_(_width_) BITWIDTH_SUFFIX(vtenc_max_encoded_size, _width_) #define vtenc_max_encoded_size vtenc_max_encoded_size_(BITWIDTH) -#define ENC_RETURN_WITH_CODE(ctx, enc, code) \ +#define enc_return_with_code(ctx, enc, code) \ do { \ (enc)->last_error_code = code; \ encctx_close((ctx)); \ return 0; \ } while (0) -#define ENC_RETURN_ON_ERROR(ctx, enc, exp) \ +#define enc_return_on_error(ctx, enc, exp) \ do { \ const VtencErrorCode code = (exp); \ if (code != VtencErrorNoError) { \ - ENC_RETURN_WITH_CODE(ctx, enc, code); \ + enc_return_with_code(ctx, enc, code); \ } \ } while(0) @@ -183,15 +183,15 @@ size_t vtenc_encode(VtencEncoder *enc, const TYPE *in, size_t in_len, enc->last_error_code = VtencErrorNoError; - ENC_RETURN_ON_ERROR(&ctx, enc, + enc_return_on_error(&ctx, enc, encctx_init(&ctx, enc, in, in_len, out, out_cap) ); if ((uint64_t)in_len > max_values) { - ENC_RETURN_WITH_CODE(&ctx, enc, VtencErrorInputTooBig); + enc_return_with_code(&ctx, enc, VtencErrorInputTooBig); } - ENC_RETURN_ON_ERROR(&ctx, enc, encode_bit_cluster_tree(&ctx)); + enc_return_on_error(&ctx, enc, encode_bit_cluster_tree(&ctx)); return encctx_close(&ctx); }