Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizes int types for in memory storage #460

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions include/zcbor_decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ do { \
* fit in the result variable.
* Use zcbor_peek_error() to see the error code.
*/
bool zcbor_int8_decode(zcbor_state_t *state, int8_t *result);
bool zcbor_int16_decode(zcbor_state_t *state, int16_t *result);
bool zcbor_int32_decode(zcbor_state_t *state, int32_t *result); /* pint/nint */
bool zcbor_int64_decode(zcbor_state_t *state, int64_t *result); /* pint/nint */
bool zcbor_uint8_decode(zcbor_state_t *state, uint8_t *result);
bool zcbor_uint16_decode(zcbor_state_t *state, uint16_t *result);
bool zcbor_uint32_decode(zcbor_state_t *state, uint32_t *result); /* pint */
bool zcbor_uint64_decode(zcbor_state_t *state, uint64_t *result); /* pint */
bool zcbor_size_decode(zcbor_state_t *state, size_t *result); /* pint */
Expand Down Expand Up @@ -92,8 +96,12 @@ bool zcbor_float_decode(zcbor_state_t *state, double *result); /* IEEE754 float1
* expected value.
* Use zcbor_peek_error() to see the error code.
*/
bool zcbor_int8_expect(zcbor_state_t *state, int8_t expected);
bool zcbor_int16_expect(zcbor_state_t *state, int16_t expected);
bool zcbor_int32_expect(zcbor_state_t *state, int32_t expected); /* pint/nint */
bool zcbor_int64_expect(zcbor_state_t *state, int64_t expected); /* pint/nint */
bool zcbor_uint8_expect(zcbor_state_t *state, uint8_t expected);
bool zcbor_uint16_expect(zcbor_state_t *state, uint16_t expected);
bool zcbor_uint32_expect(zcbor_state_t *state, uint32_t expected); /* pint */
bool zcbor_uint64_expect(zcbor_state_t *state, uint64_t expected); /* pint */
bool zcbor_size_expect(zcbor_state_t *state, size_t expected); /* pint */
Expand All @@ -113,6 +121,8 @@ bool zcbor_float_expect(zcbor_state_t *state, double expected); /* IEEE754 float

/** Like the _expect() functions but the value is passed through a pointer.
* (for use as a zcbor_decoder_t function) */
bool zcbor_int8_pexpect(zcbor_state_t *state, int8_t *expected);
bool zcbor_int16_pexpect(zcbor_state_t *state, int16_t *expected);
bool zcbor_int32_pexpect(zcbor_state_t *state, int32_t *expected); /* pint/nint */
bool zcbor_int64_pexpect(zcbor_state_t *state, int64_t *expected); /* pint/nint */
bool zcbor_uint32_pexpect(zcbor_state_t *state, uint32_t *expected); /* pint */
Expand All @@ -132,8 +142,12 @@ bool zcbor_float_pexpect(zcbor_state_t *state, double *expected); /* IEEE754 flo
*
* Calls @ref zcbor_union_elem_code then @ref zcbor_[u]int[32|64]_expect.
*/
bool zcbor_int8_expect_union(zcbor_state_t *state, int8_t expected);
bool zcbor_int16_expect_union(zcbor_state_t *state, int16_t expected);
bool zcbor_int32_expect_union(zcbor_state_t *state, int32_t expected);
bool zcbor_int64_expect_union(zcbor_state_t *state, int64_t expected);
bool zcbor_uint8_expect_union(zcbor_state_t *state, uint8_t expected);
bool zcbor_uint16_expect_union(zcbor_state_t *state, uint16_t expected);
bool zcbor_uint32_expect_union(zcbor_state_t *state, uint32_t expected);
bool zcbor_uint64_expect_union(zcbor_state_t *state, uint64_t expected);

Expand Down
8 changes: 8 additions & 0 deletions include/zcbor_encode.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ do { \
* @retval false If the payload is exhausted. Or an unexpected error happened.
* Use zcbor_peek_error() to see the error code.
*/
bool zcbor_int8_put(zcbor_state_t *state, int8_t input);
bool zcbor_int16_put(zcbor_state_t *state, int16_t input);
bool zcbor_int32_put(zcbor_state_t *state, int32_t input); /* pint/nint */
bool zcbor_int64_put(zcbor_state_t *state, int64_t input); /* pint/nint */
bool zcbor_uint8_put(zcbor_state_t *state, uint8_t input);
bool zcbor_uint16_put(zcbor_state_t *state, uint16_t input);
bool zcbor_uint32_put(zcbor_state_t *state, uint32_t input); /* pint */
bool zcbor_uint64_put(zcbor_state_t *state, uint64_t input); /* pint */
bool zcbor_size_put(zcbor_state_t *state, size_t input); /* pint */
Expand All @@ -73,8 +77,12 @@ bool zcbor_float16_bytes_put(zcbor_state_t *state, uint16_t input); /* IEEE754 f
bool zcbor_float32_put(zcbor_state_t *state, float input); /* IEEE754 float32 */
bool zcbor_float64_put(zcbor_state_t *state, double input); /* IEEE754 float64 */

bool zcbor_int8_encode(zcbor_state_t *state, const int8_t *input);
bool zcbor_int16_encode(zcbor_state_t *state, const int16_t *input);
bool zcbor_int32_encode(zcbor_state_t *state, const int32_t *input); /* pint/nint */
bool zcbor_int64_encode(zcbor_state_t *state, const int64_t *input); /* pint/nint */
bool zcbor_uint8_encode(zcbor_state_t *state, const uint8_t *input);
bool zcbor_uint16_encode(zcbor_state_t *state, const uint16_t *input);
bool zcbor_uint32_encode(zcbor_state_t *state, const uint32_t *input); /* pint */
bool zcbor_uint64_encode(zcbor_state_t *state, const uint64_t *input); /* pint */
bool zcbor_size_encode(zcbor_state_t *state, const size_t *input); /* pint */
Expand Down
105 changes: 100 additions & 5 deletions src/zcbor_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,20 @@ bool zcbor_int_decode(zcbor_state_t *state, void *result, size_t result_size)
}


bool zcbor_int8_decode(zcbor_state_t *state, int8_t *result)
{
PRINT_FUNC();
return zcbor_int_decode(state, result, sizeof(*result));
}


bool zcbor_int16_decode(zcbor_state_t *state, int16_t *result)
{
PRINT_FUNC();
return zcbor_int_decode(state, result, sizeof(*result));
}


bool zcbor_int32_decode(zcbor_state_t *state, int32_t *result)
{
PRINT_FUNC();
Expand Down Expand Up @@ -266,13 +280,54 @@ bool zcbor_uint_decode(zcbor_state_t *state, void *result, size_t result_size)
}


bool zcbor_uint8_decode(zcbor_state_t *state, uint8_t *result)
{
PRINT_FUNC();
return zcbor_uint_decode(state, result, sizeof(*result));
}


bool zcbor_uint16_decode(zcbor_state_t *state, uint16_t *result)
{
PRINT_FUNC();
return zcbor_uint_decode(state, result, sizeof(*result));
}


bool zcbor_uint32_decode(zcbor_state_t *state, uint32_t *result)
{
PRINT_FUNC();
return zcbor_uint_decode(state, result, sizeof(*result));
}


bool zcbor_uint64_decode(zcbor_state_t *state, uint64_t *result)
{
PRINT_FUNC();
return zcbor_uint_decode(state, result, sizeof(*result));
}


bool zcbor_int8_expect_union(zcbor_state_t *state, int8_t result)
{
PRINT_FUNC();
if (!zcbor_union_elem_code(state)) {
ZCBOR_FAIL();
}
return zcbor_int8_expect(state, result);
}


bool zcbor_int16_expect_union(zcbor_state_t *state, int16_t result)
{
PRINT_FUNC();
if (!zcbor_union_elem_code(state)) {
ZCBOR_FAIL();
}
return zcbor_int16_expect(state, result);
}


bool zcbor_int32_expect_union(zcbor_state_t *state, int32_t result)
{
PRINT_FUNC();
Expand All @@ -293,6 +348,26 @@ bool zcbor_int64_expect_union(zcbor_state_t *state, int64_t result)
}


bool zcbor_uint8_expect_union(zcbor_state_t *state, uint8_t result)
{
PRINT_FUNC();
if (!zcbor_union_elem_code(state)) {
ZCBOR_FAIL();
}
return zcbor_uint8_expect(state, result);
}


bool zcbor_uint16_expect_union(zcbor_state_t *state, uint16_t result)
{
PRINT_FUNC();
if (!zcbor_union_elem_code(state)) {
ZCBOR_FAIL();
}
return zcbor_uint16_expect(state, result);
}


bool zcbor_uint32_expect_union(zcbor_state_t *state, uint32_t result)
{
PRINT_FUNC();
Expand All @@ -313,6 +388,20 @@ bool zcbor_uint64_expect_union(zcbor_state_t *state, uint64_t result)
}


bool zcbor_int8_expect(zcbor_state_t *state, int8_t expected)
{
PRINT_FUNC();
return zcbor_int64_expect(state, expected);
}


bool zcbor_int16_expect(zcbor_state_t *state, int16_t expected)
{
PRINT_FUNC();
return zcbor_int64_expect(state, expected);
}


bool zcbor_int32_expect(zcbor_state_t *state, int32_t expected)
{
PRINT_FUNC();
Expand Down Expand Up @@ -351,20 +440,26 @@ bool zcbor_int64_pexpect(zcbor_state_t *state, int64_t *expected)
}


bool zcbor_uint64_decode(zcbor_state_t *state, uint64_t *result)
#ifdef ZCBOR_SUPPORTS_SIZE_T
bool zcbor_size_decode(zcbor_state_t *state, size_t *result)
{
PRINT_FUNC();
return zcbor_uint_decode(state, result, sizeof(*result));
}
#endif


#ifdef ZCBOR_SUPPORTS_SIZE_T
bool zcbor_size_decode(zcbor_state_t *state, size_t *result)
bool zcbor_uint8_expect(zcbor_state_t *state, uint8_t expected)
{
PRINT_FUNC();
return zcbor_uint_decode(state, result, sizeof(*result));
return zcbor_uint64_expect(state, expected);
}

bool zcbor_uint16_expect(zcbor_state_t *state, uint16_t expected)
{
PRINT_FUNC();
return zcbor_uint64_expect(state, expected);
}
#endif


bool zcbor_uint32_expect(zcbor_state_t *state, uint32_t expected)
Expand Down
41 changes: 36 additions & 5 deletions src/zcbor_encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,55 +146,86 @@ bool zcbor_uint_encode(zcbor_state_t *state, const void *input_uint, size_t uint
return true;
}

bool zcbor_int8_encode(zcbor_state_t *state, const int8_t *input)
{
return zcbor_int_encode(state, input, sizeof(*input));
}

bool zcbor_int32_encode(zcbor_state_t *state, const int32_t *input)
bool zcbor_int16_encode(zcbor_state_t *state, const int16_t *input)
{
return zcbor_int_encode(state, input, sizeof(*input));
}

bool zcbor_int32_encode(zcbor_state_t *state, const int32_t *input)
{
return zcbor_int_encode(state, input, sizeof(*input));
}

bool zcbor_int64_encode(zcbor_state_t *state, const int64_t *input)
{
return zcbor_int_encode(state, input, sizeof(*input));
}

bool zcbor_uint8_encode(zcbor_state_t *state, const uint8_t *input)
{
return zcbor_uint_encode(state, input, sizeof(*input));
}

bool zcbor_uint32_encode(zcbor_state_t *state, const uint32_t *input)
bool zcbor_uint16_encode(zcbor_state_t *state, const uint16_t *input)
{
return zcbor_uint_encode(state, input, sizeof(*input));
}

bool zcbor_uint32_encode(zcbor_state_t *state, const uint32_t *input)
{
return zcbor_uint_encode(state, input, sizeof(*input));
}

bool zcbor_uint64_encode(zcbor_state_t *state, const uint64_t *input)
{
return zcbor_uint_encode(state, input, sizeof(*input));
}

bool zcbor_int8_put(zcbor_state_t *state, int8_t input)
{
return zcbor_int_encode(state, &input, sizeof(input));
}

bool zcbor_int32_put(zcbor_state_t *state, int32_t input)
bool zcbor_int16_put(zcbor_state_t *state, int16_t input)
{
return zcbor_int_encode(state, &input, sizeof(input));
}

bool zcbor_int32_put(zcbor_state_t *state, int32_t input)
{
return zcbor_int_encode(state, &input, sizeof(input));
}

bool zcbor_int64_put(zcbor_state_t *state, int64_t input)
{
return zcbor_int_encode(state, &input, sizeof(input));
}

bool zcbor_uint8_put(zcbor_state_t *state, uint8_t input)
{
return zcbor_uint_encode(state, &input, sizeof(input));
}

bool zcbor_uint16_put(zcbor_state_t *state, uint16_t input)
{
return zcbor_uint_encode(state, &input, sizeof(input));
}

bool zcbor_uint32_put(zcbor_state_t *state, uint32_t input)
{
return zcbor_uint_encode(state, &input, sizeof(input));
}


bool zcbor_uint64_put(zcbor_state_t *state, uint64_t input)
{
return zcbor_uint_encode(state, &input, sizeof(input));
}


#ifdef ZCBOR_SUPPORTS_SIZE_T
bool zcbor_size_put(zcbor_state_t *state, size_t input)
{
Expand Down
14 changes: 12 additions & 2 deletions zcbor/zcbor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1955,11 +1955,12 @@ def bit_size(self):
"""The bit width of the integers as represented in code."""
bit_size = None
if self.type in ["UINT", "INT", "NINT"]:
assert self.default_bit_size in [32, 64], "The default_bit_size must be 32 or 64."
assert self.default_bit_size in [8, 16, 32, 64], \
"The default_bit_size must be 8, 16, 32 or 64."
if self.default_bit_size == 64:
bit_size = 64
else:
bit_size = 32
bit_size = self.default_bit_size

for v in [self.value or 0, self.max_value or 0, self.min_value or 0]:
if (type(v) is str):
Expand All @@ -1968,9 +1969,18 @@ def bit_size(self):
elif self.type == "UINT":
if (v > UINT32_MAX):
bit_size = 64
elif (v > UINT16_MAX):
bit_size = 32
elif (v > UINT8_MAX):
bit_size = 16
else:
if (v > INT32_MAX) or (v < INT32_MIN):
bit_size = 64
elif (v > INT16_MAX) or (v < INT16_MIN):
bit_size = 32
elif (v > INT8_MAX) or (v < INT8_MIN):
bit_size = 16

return bit_size

def float_type(self):
Expand Down
Loading