From cf882ef3b56643053b3a825ac446ff92e55fd945 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20R=C3=B8nningstad?= Date: Wed, 10 Jan 2024 10:54:45 +0100 Subject: [PATCH] zcbor: Make changes to zcbor code to satisfy mynewt compile options MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit bit-casting between uint and float. Signed-off-by: Øyvind Rønningstad --- boot/zcbor/src/zcbor_common.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/boot/zcbor/src/zcbor_common.c b/boot/zcbor/src/zcbor_common.c index af93e7ff..3697983a 100644 --- a/boot/zcbor/src/zcbor_common.c +++ b/boot/zcbor/src/zcbor_common.c @@ -355,14 +355,19 @@ 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; @@ -370,8 +375,10 @@ uint16_t zcbor_float32_to_16(float input) 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. */