Commit cf882ef3 authored by Øyvind Rønningstad's avatar Øyvind Rønningstad Committed by Jamie
Browse files

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

bit-casting between uint and float.

Signed-off-by: default avatarØyvind Rønningstad <oyvind.ronningstad@nordicsemi.no>
parent c8d213a9
Loading
Loading
Loading
Loading
+10 −3
Changes for boot/zcbor/src/zcbor_common.c: 10 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -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. */