\Arduino\libraries\M5StickC\src\utility\MahonyAHRS.cpp: In function 'float invSqrt(float)':
\Arduino\libraries\M5StickC\src\utility\MahonyAHRS.cpp:244:20: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
long i = *(long*)&y;
^
\Arduino\libraries\M5StickC\src\utility\MahonyAHRS.cpp:246:16: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
y = *(float*)&i;
^
Test Code
#include <M5StickC.h>
float invSqrtOld(float x) {
float halfx = 0.5f * x;
float y = x;
long i = *(long*)&y;
i = 0x5f3759df - (i>>1);
y = *(float*)&i;
y = y * (1.5f - (halfx * y * y));
return y;
}
float invSqrtNew(float x) {
typedef union {
float f;
long l;
} union_t;
float halfx = 0.5f * x;
float y = x;
long i = ((union_t*)&y)->l;
i = 0x5f3759df - (i >> 1);
y = ((union_t*)&i)->f;
y = y * (1.5f - (halfx * y * y));
return y;
}
void setup() {
M5.begin();
for ( float i = 0 ; i < 5 ; i += 0.5 ) {
Serial.printf( "i = %f, invSqrt() = %f, invSqrtOld() = %f, invSqrtNew() = %f\n", i, invSqrt(i), invSqrtOld(i), invSqrtNew(i) );
}
}
void loop() {
}
target warning
Test Code
output