Skip to content

Commit cf882ef

Browse files
oyvindronningstadnordicjm
authored andcommitted
zcbor: Make changes to zcbor code to satisfy mynewt compile options
bit-casting between uint and float. Signed-off-by: Øyvind Rønningstad <[email protected]>
1 parent c8d213a commit cf882ef

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

boot/zcbor/src/zcbor_common.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,23 +355,30 @@ float zcbor_float16_to_32(uint16_t input)
355355
: (expo + (F32_BIAS - F16_BIAS));
356356
uint32_t value32 = (sign << F32_SIGN_OFFS) | (new_expo << F32_EXPO_OFFS)
357357
| (mantissa << (F32_EXPO_OFFS - F16_EXPO_OFFS));
358-
return *(float *)&value32;
358+
float result;
359+
360+
memcpy(&result, &value32, sizeof(result));
361+
return result;
359362
}
360363
}
361364

362365

363366
uint16_t zcbor_float32_to_16(float input)
364367
{
365-
uint32_t value32 = *(uint32_t *)&input;
368+
uint32_t value32;
369+
370+
memcpy(&value32, &input, sizeof(value32));
366371

367372
uint32_t sign = value32 >> F32_SIGN_OFFS;
368373
uint32_t expo = (value32 >> F32_EXPO_OFFS) & F32_EXPO_MSK;
369374
uint32_t mantissa = value32 & F32_MANTISSA_MSK;
370375

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

378+
uint32_t abs_value32 = value32 & ~(1 << F32_SIGN_OFFS);
373379
float abs_input;
374-
*(uint32_t *)&abs_input = value32 & ~(1 << F32_SIGN_OFFS);
380+
381+
memcpy(&abs_input, &abs_value32, sizeof(abs_input));
375382

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

0 commit comments

Comments
 (0)