From b71c491f006d04613b6b61534e18144fa31b9257 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Mon, 29 Jun 2020 22:40:25 +0800 Subject: [PATCH] libc/math: Fix warning: dereferencing type-punned pointer will break strict-aliasing rules Signed-off-by: Xiang Xiao Change-Id: I15aa5d664c20ea7bb95ba60e056134d2ff933db9 --- libs/libc/math/lib_libsqrtapprox.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/libs/libc/math/lib_libsqrtapprox.c b/libs/libc/math/lib_libsqrtapprox.c index c0cd0af18e..69c4bfda69 100644 --- a/libs/libc/math/lib_libsqrtapprox.c +++ b/libs/libc/math/lib_libsqrtapprox.c @@ -38,13 +38,16 @@ float lib_sqrtapprox(float x) { - int32_t i; + union + { + int32_t i; + float x; + } u; /* Floats + bit manipulation = +inf fun! */ - i = *((int32_t *) & x); - i = 0x1fc00000 + (i >> 1); - x = *((float *)&i); + u.x = x; + u.i = 0x1fc00000 + (u.i >> 1); - return x; + return u.x; }