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

Assert failure with compile optimizations #1

Closed
mathisloge opened this issue Mar 8, 2019 · 5 comments
Closed

Assert failure with compile optimizations #1

mathisloge opened this issue Mar 8, 2019 · 5 comments

Comments

@mathisloge
Copy link

mathisloge commented Mar 8, 2019

If you compile the tests with gcc -g -Wall -Wextra -pedantic-errors tests/tests.c -o bin/tests -O3
you get the error message

===========================================================
ENCODE DECODE MOTOROLA 32 bits double negative value
===========================================================

step 9.2

value to be encoded: -50.6164129

encoded frame: 00 00 00 00 00 00 00 00

decoded value:       0.0000000
tests: tests/tests.c:736: main: Assertion `physical_value == decode_double(frame, startbit, length, MOTOROLA, factor, offset)' failed.
Aborted (core dumped)

The error occurs at optimization levels 1-3 and s (size optimization).

@nisennenmondai
Copy link
Owner

Hello,
Thank you for your input, I'll investigate this issue when I have a bit of time

nisen

@nisennenmondai nisennenmondai pinned this issue Mar 8, 2019
@nisennenmondai
Copy link
Owner

nisennenmondai commented Mar 11, 2019

Hello,
Bug is fixed, I don't know why but GCC is doing some weird optimization when the value past in encode functions is negative, resulting in a 0 being written in the frame. Physical value should be declared as volatile to force compiler not to optimize this variable, let me know if this works for you so I can close this issue.

3f2104a

nisen

@mathisloge
Copy link
Author

Hey sorry,
the volatile fix works. Thanks

@nisennenmondai
Copy link
Owner

nisennenmondai commented Apr 2, 2019

Hey sorry,
the volatile fix works. Thanks

I believe I found the real problem and it is quite interesting see this link bellow if you want more information

https://www.embeddeduse.com/2013/08/25/casting-a-negative-float-to-an-unsigned-int/

So basically I think the compiler is doing an implicit double cast float -> int -> unsigned int. But with optimization activated it is doing float -> unsigned int directly thus skipping the int cast resulting in an 0 being inserted when value is inferior to -1 as stated in the article. I am not 100% sure but this is my best guess. So volatile keyword is now removed and instead I put a cast to int before uint64_t.

@mathisloge
Copy link
Author

Thanks for sharing the article. It is really interesting, Sometimes casting can be a real thieve of time.

At the moment I have no time to look at the problem in the code, but it sounds like a possible cause.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants