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

Fix simulation consistency and memory leaks #35

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

pkdawson
Copy link

@pkdawson pkdawson commented Sep 1, 2016

I was getting wildly different simulation results with Visual C++, using the same seed/parameters that I was using on Linux or OS X. Most of that was caused by the fact that sizeof(long) is 4, as opposed to 8 on Clang, GCC, etc.

float kc = (seed*seed) % 256;

On this line in particular, the signed 32-bit integer would often overflow into the negatives. Making it explicitly 64-bit keeps behavior consistent across platforms.

That mostly fixes it, but I was still seeing subtle inconsistencies. This is harder to explain. For some reason, sinf yields different results when MSVC (2013 or 2015) produces a 64-bit binary. For example, with floats expressed as their raw bytes:

sinf(5.516943f) = 0xbf3184cd on GCC, Clang, and MSVC x86
sinf(5.516943f) = 0xbf3184ce on MSVC x86-64

Tried adjusting some flags, but it didn't help. Using the double version of sin and then casting back to float gets the correct results. This solution is a little ugly, but it's the only thing that worked.

@psi29a
Copy link
Member

psi29a commented Sep 2, 2016

Thanks for PR, can you have a look at linthub and see about addressing those issues? As for the rest.. it seems we lost python 2.6 support.

@pkdawson
Copy link
Author

pkdawson commented Sep 4, 2016

I changed the C-style casts. The other linthub complaints are about lines of code which I didn't touch.

@psi29a
Copy link
Member

psi29a commented Sep 5, 2016

Great, thank you. :)

@pkdawson
Copy link
Author

Found a very similar precision issue with sin and cos on Apple's version of Clang. I could make the code a little nicer by creating wrapper functions instead of macros, but I'm pretty sure that doing the calculation at double precision is the best way to fix this problem after all.

@@ -44,6 +44,9 @@ uint32_t Segments::area()
void Segments::reset()
{
memset(segment, -1, sizeof(uint32_t) * _area);
for (int i = 0; i<seg_data.size(); i++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linthub is suggesting to put spaces around the < operator

@pkdawson pkdawson changed the title Fix simulation consistency on MSVC Fix simulation consistency and memory leaks Sep 30, 2016
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

Successfully merging this pull request may close these issues.

3 participants