zcbor: Make changes to zcbor code to satisfy mynewt compile options

bit-casting between uint and float.

Signed-off-by: Øyvind Rønningstad <oyvind.ronningstad@nordicsemi.no>
This commit is contained in:
Øyvind Rønningstad 2024-01-10 10:54:45 +01:00 committed by Jamie
parent c8d213a9a1
commit cf882ef3b5
1 changed files with 10 additions and 3 deletions

View File

@ -355,14 +355,19 @@ float zcbor_float16_to_32(uint16_t input)
: (expo + (F32_BIAS - F16_BIAS)); : (expo + (F32_BIAS - F16_BIAS));
uint32_t value32 = (sign << F32_SIGN_OFFS) | (new_expo << F32_EXPO_OFFS) uint32_t value32 = (sign << F32_SIGN_OFFS) | (new_expo << F32_EXPO_OFFS)
| (mantissa << (F32_EXPO_OFFS - F16_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) 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 sign = value32 >> F32_SIGN_OFFS;
uint32_t expo = (value32 >> F32_EXPO_OFFS) & F32_EXPO_MSK; uint32_t expo = (value32 >> F32_EXPO_OFFS) & F32_EXPO_MSK;
@ -370,8 +375,10 @@ uint16_t zcbor_float32_to_16(float input)
uint16_t value16 = (uint16_t)(sign << F16_SIGN_OFFS); uint16_t value16 = (uint16_t)(sign << F16_SIGN_OFFS);
uint32_t abs_value32 = value32 & ~(1 << F32_SIGN_OFFS);
float abs_input; 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)) { if (abs_input <= (F16_MIN / 2)) {
/* 0 or too small for float16. Round down to 0. value16 is already correct. */ /* 0 or too small for float16. Round down to 0. value16 is already correct. */