Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strict-aliasing warnings #17

Open
BallscrewBob opened this issue Aug 23, 2017 · 7 comments
Open

strict-aliasing warnings #17

BallscrewBob opened this issue Aug 23, 2017 · 7 comments
Labels
type: imperfection Perceived defect in any part of project

Comments

@BallscrewBob
Copy link

BallscrewBob commented Aug 23, 2017

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;
^
@facchinm facchinm added the type: imperfection Perceived defect in any part of project label Sep 4, 2017
@ErikBob
Copy link

ErikBob commented Aug 29, 2019

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.

@thekunalsaini
Copy link

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

@ErikBob
Copy link

ErikBob commented Aug 18, 2020

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

@larsinka
Copy link

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.

@per1234 per1234 changed the title Warnings and failure strict-aliasing warnings Jan 26, 2022
@PaulStoffregen
Copy link
Contributor

In what file?

float Madgwick::invSqrt(float x) {

@larsinka
Copy link

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

@ErikBob
Copy link

ErikBob commented Jan 28, 2022

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); }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: imperfection Perceived defect in any part of project
Projects
None yet
Development

No branches or pull requests

6 participants