Page 1 of 1

bug in cpSpaceSegmentQueryFirst

Posted: Fri Dec 25, 2009 3:36 pm
by viblo
I think Ive found a bug in cpSpaceSegmentQueryFirst, it hangs sometimes depending on the start/end value.
For example, I made two calls to it in an empty space:
with start = (13,1) and end = (131.01,2) it works
with start = (0,0) and end = (131.01,0) hangs in what feels like an infinite loop

(I did the testing from python, but I don't think that should affect this problem)

Re: bug in cpSpaceSegmentQueryFirst

Posted: Sun Dec 27, 2009 12:06 pm
by slembcke
I wasn't able to reproduce the issue. Did you change the spatial hash settings at all?

Re: bug in cpSpaceSegmentQueryFirst

Posted: Sun Dec 27, 2009 8:24 pm
by viblo
Oh, I should have created a little c program to test it directly... But you know how it is, I like coding in python much more than in c ;)

Anyway, after I read your post I created a little test program in c and at the first I couldn't reproduce it either, but after a lot of printf-debugging in the chipmunk code I think Ive found both the problem and why it didn't show up for me first. (Anyone know of a better way than binary-search-with-printfs to debug inside a compiled chipmunk.dll loaded from python?)

The problem is that in the cpSpaceHashSegmentQuery function in cpSpaceHash.c the next_v variable can be NaN. When you compile with -ffast-math the if-statement in the while loop select one exec path, and when you compile without the other path. That is, next_v < next_h is true with -ffast-math, and false without -ffast-math.

Re: bug in cpSpaceSegmentQueryFirst

Posted: Sun Dec 27, 2009 9:05 pm
by slembcke
That's weird. I've never actually ran into an issue where -ffast-math didn't produce the correct results anyway on the PPC and Intel OS X and Linux machines that I've run on.

As for debugging, I'd normally say to just use good old GDB, but I have no idea if that would work on Windows with DLLs and all.

Re: bug in cpSpaceSegmentQueryFirst

Posted: Tue Dec 29, 2009 10:06 am
by viblo
This little code-snippet prints 1 with -ffast-math but 0 without:

Code: Select all

double b = 0;
b = b/b;
printf("%i", b == b);
Same result on both my laptop with intel cpu, win vista and mingw/gcc v.3.4.5, and on a small server I have with an amd cpu running ubuntu and gcc 4.4.1.

Re: bug in cpSpaceSegmentQueryFirst

Posted: Tue Dec 29, 2009 11:47 am
by slembcke
Bah. Crap. You are right. I guess there is a fair amount of fiddling with infinity, but never NaN except for there. Thus the code attempting to fix them never actually worked when compiled as release. My debug target does not include -ffast-math I think, so I probably just never noticed.

Code: Select all

	// fix NANs in horizontal directions
	next_h = (next_h == next_h ? next_h : dt_dx);
	next_v = (next_v == next_v ? next_v : dt_dy);

Re: bug in cpSpaceSegmentQueryFirst

Posted: Tue Dec 29, 2009 12:02 pm
by slembcke
Ok. I added a fix to detect the 0*infinity to prevent ever getting a NaN. Now it works using -ffast-math and without on processors that at least handle infinite math nicely without it.