Skip to content

Commit

Permalink
zcbor: Make changes to zcbor code to satisfy mynewt compile options
Browse files Browse the repository at this point in the history
printf format type and bit-casting between uint and float.

Signed-off-by: Øyvind Rønningstad <[email protected]>
  • Loading branch information
oyvindronningstad authored and nordicjm committed Jan 25, 2024
1 parent 63ddb71 commit f09e205
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
20 changes: 10 additions & 10 deletions boot/zcbor/include/zcbor_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,41 +73,41 @@ do { \
#endif

__attribute__((used))
static void zcbor_print_compare_lines(const uint8_t *str1, const uint8_t *str2, uint32_t size)
static void zcbor_print_compare_lines(const uint8_t *str1, const uint8_t *str2, size_t size)
{
for (uint32_t j = 0; j < size; j++) {
for (size_t j = 0; j < size; j++) {
zcbor_do_print("%x ", str1[j]);
}
zcbor_do_print("\r\n");
for (uint32_t j = 0; j < size; j++) {
for (size_t j = 0; j < size; j++) {
zcbor_do_print("%x ", str2[j]);
}
zcbor_do_print("\r\n");
for (uint32_t j = 0; j < size; j++) {
for (size_t j = 0; j < size; j++) {
zcbor_do_print("%x ", str1[j] != str2[j]);
}
zcbor_do_print("\r\n");
zcbor_do_print("\r\n");
}

__attribute__((used))
static void zcbor_print_compare_strings(const uint8_t *str1, const uint8_t *str2, uint32_t size)
static void zcbor_print_compare_strings(const uint8_t *str1, const uint8_t *str2, size_t size)
{
for (uint32_t i = 0; i <= size / 16; i++) {
zcbor_do_print("line %d (char %d)\r\n", i, i*16);
for (size_t i = 0; i <= size / 16; i++) {
zcbor_do_print("line %zu (char %zu)\r\n", i, i*16);
zcbor_print_compare_lines(&str1[i*16], &str2[i*16],
MIN(16, (size - i*16)));
}
zcbor_do_print("\r\n");
}

__attribute__((used))
static void zcbor_print_compare_strings_diff(const uint8_t *str1, const uint8_t *str2, uint32_t size)
static void zcbor_print_compare_strings_diff(const uint8_t *str1, const uint8_t *str2, size_t size)
{
bool printed = false;
for (uint32_t i = 0; i <= size / 16; i++) {
for (size_t i = 0; i <= size / 16; i++) {
if (memcmp(&str1[i*16], &str2[i*16], MIN(16, (size - i*16))) != 0) {
zcbor_do_print("line %d (char %d)\r\n", i, i*16);
zcbor_do_print("line %zu (char %zu)\r\n", i, i*16);
zcbor_print_compare_lines(&str1[i*16], &str2[i*16],
MIN(16, (size - i*16)));
printed = true;
Expand Down
13 changes: 10 additions & 3 deletions boot/zcbor/src/zcbor_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,23 +355,30 @@ float zcbor_float16_to_32(uint16_t input)
: (expo + (F32_BIAS - F16_BIAS));
uint32_t value32 = (sign << F32_SIGN_OFFS) | (new_expo << F32_EXPO_OFFS)
| (mantissa << (F32_EXPO_OFFS - F16_EXPO_OFFS));
return *(float *)&value32;
float result;

memcpy(&result, &value32, sizeof(result));
return result;
}
}


uint16_t zcbor_float32_to_16(float input)
{
uint32_t value32 = *(uint32_t *)&input;
uint32_t value32;

memcpy(&value32, &input, sizeof(value32));

uint32_t sign = value32 >> F32_SIGN_OFFS;
uint32_t expo = (value32 >> F32_EXPO_OFFS) & F32_EXPO_MSK;
uint32_t mantissa = value32 & F32_MANTISSA_MSK;

uint16_t value16 = (uint16_t)(sign << F16_SIGN_OFFS);

uint32_t abs_value32 = value32 & ~(1 << F32_SIGN_OFFS);
float abs_input;
*(uint32_t *)&abs_input = value32 & ~(1 << F32_SIGN_OFFS);

memcpy(&abs_input, &abs_value32, sizeof(abs_input));

if (abs_input <= (F16_MIN / 2)) {
/* 0 or too small for float16. Round down to 0. value16 is already correct. */
Expand Down

0 comments on commit f09e205

Please sign in to comment.