-
-
Notifications
You must be signed in to change notification settings - Fork 393
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
Fix -Wstrict-aliasing
#2528
Fix -Wstrict-aliasing
#2528
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #2528 +/- ##
=======================================
Coverage 84.37% 84.37%
=======================================
Files 375 376 +1
Lines 61595 61633 +38
Branches 12052 12060 +8
=======================================
+ Hits 51970 52004 +34
- Misses 9625 9629 +4
Continue to review full report in Codecov by Sentry.
|
* Casting a `uint64_t*` to `double*` invokes undefined behavior, since it violates the strict aliasing rules of ISO C. Instead of casting pointers, let's read through a union which is supported by C and yields the same performant assembly code. Closes: https://bugs.gentoo.org/924864
I'll note that recent versions of MSVC generate different code for this version on x64 than the plan pointer cast. There's one extra instruction, and as far as I can tell it's writing the value to the main memory and reading it back instead of using only registers. https://godbolt.org/z/WEWPsc3cK A similar difference persists with the actual igraph use case as well. @SoapGentoo Can you comment on the difference between the two versions? Is there any reason why the current version wouldn't work? |
The current code invokes undefined behavior, and just because it hasn't failed catastrophically yet, doesn't mean you should continue using it. Correct, MSVC seems to be storing it to the stack and loading it back from the stack, mainly because its codegen is terrible. I strongly doubt this will have an impact on runtime performance, since modern CPUs partially virtualize the stack anyways. If you want a simple example how violating strict aliasing breaks code, just look at https://godbolt.org/z/rE4sW5K53. |
Also mingw GCC and mingw clang both exist, and both don't have this problem... |
uint64_t*
todouble*
invokes undefined behavior, since it violates the strict aliasing rules of ISO C. Instead of casting pointers, let's read through a union which is supported by C and yields the same performant assembly code.Closes: https://bugs.gentoo.org/924864