MadgwickAHRS icon indicating copy to clipboard operation
MadgwickAHRS copied to clipboard

`strict-aliasing` warnings

Open BallscrewBob opened this issue 7 years ago • 7 comments

In the desktop IDE I get a couple of "warnings" but in the ONLINE editor CREATE I get a compilation failure with the following output.

/home/admin/builder/arduino-builder/latest/Madgwick-1.2.0/src/MadgwickAHRS.cpp: In static member function ‘static float Madgwick::invSqrt(float)’:
/home/admin/builder/arduino-builder/latest/Madgwick-1.2.0/src/MadgwickAHRS.cpp:234:20: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
long i = *(long*)&y;
^
/home/admin/builder/arduino-builder/latest/Madgwick-1.2.0/src/MadgwickAHRS.cpp:236:16: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
y = *(float*)&i;
^

BallscrewBob avatar Aug 23 '17 13:08 BallscrewBob

C++ doesn't like Type Punning. You would have to deactive checking for strict-aliasing in your compiler, which I wouldn't recommend. Simply replace fastInvSquare() by math.h 1/sqrt(). The speedloss is insignificant.

ErikBob avatar Aug 29 '19 22:08 ErikBob

@facchinm can you explain how to rectify this error(approach)

thekunalsaini avatar Mar 20 '20 08:03 thekunalsaini

Simply replace fastInvSquare() by math.h 1/sqrt()

ErikBob avatar Aug 18 '20 21:08 ErikBob

I am with thekunalsaini as I get the same error and don't understand the fix. Do I have to search and replace fastInvSquare() by math.h 1/sqrt()? In what file? Sorry if its a stupid question but I just don't understand it.

larsinka avatar Jan 26 '22 14:01 larsinka

In what file?

https://github.com/arduino-libraries/MadgwickAHRS/blob/0a909d25d0878f0c24525aeb4553cdd90473f50b/src/MadgwickAHRS.cpp#L231

PaulStoffregen avatar Jan 26 '22 15:01 PaulStoffregen

Sorry but if I replace float Madgwick::invSqrt(float x) { with float math.h 1/sqrt(float x) { it (understandably) throws an error.

larsinka avatar Jan 26 '22 15:01 larsinka

math.h is the header containing sqrt() you can replace float Madgwick::invSqrt(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)); y = y * (1.5f - (halfx * y * y)); return y; }

by

float Madgwick::invSqrt(float x) { return 1/sqrt(x); }

ErikBob avatar Jan 28 '22 09:01 ErikBob