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
bit-casting between uint and float.

Signed-off-by: Øyvind Rønningstad <[email protected]>
  • Loading branch information
oyvindronningstad committed Jan 26, 2024
1 parent e71453d commit 1accfb7
Showing 1 changed file with 10 additions and 3 deletions.
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 1accfb7

Please sign in to comment.