Fixed point implementation issues

I have some questions that have come up while I've been porting Tobias's fixed point code.

1. Why does the main code use sqrtf(x) instead of using the c++ sqrt(x). The nice thing about sqrt is that it is associated with the type of data, while sqrtf forces conversion to float. Same with atanf2.

2. Why is there a b2Abs? wouldn't the c++ abs(x) work just fine?

3. There are four places in the code where 100.0 * EPSILON is computed and used as a threshold. Since EPSILON cannot be such a small value in fixed point, I found that at least one of these thresholds was way too high and circles were going through polygon. I removed the 100.0 and things worked (this was in b2Distance, the line with pointCount==3). Any general advise about how I might set these would be nice.

4. In b2Island.cpp, in solve, the angularVelocity is squared and then compared to a threshold. Squaring the angular velocity causes overflow so I had to change this code, but after looking at the code I don't understand why this squared value is computed at all. I changed the line to:
b->m_angularVelocity = b2Clamp(b->m_angularVelocity,
-b2_maxAngularVelocity, b2_maxAngularVelocity);
and things work and seem clearer. What the squaring some sort of optimization?

4. It would be nice if this or some similar fixed point implementation could be added to the SVN code (right now I have it working against version 80). I am willing to work to make this happen. The code I have now changes float32 to be Fixed so it has very little impact on the existing code base and can be compiled with the option -DTARGET_FLOAT32_IS_FIXED to generate fixed code instead of the normal float code.

Robert
kavaler wrote:
1. Why does the main code use sqrtf(x) instead of using the c++ sqrt(x). The nice thing about sqrt is that it is associated with the type of data, while sqrtf forces conversion to float. Same with atanf2.

I've seen cases where this leads to poor performance on some platforms. On the PS2 a call to atan2 would cause a conversion to double and the double version of atan2 was about 50 times slower than the atan2f version. I try not assume people have good C++ compilers.

Quote:
2. Why is there a b2Abs? wouldn't the c++ abs(x) work just fine?

In general I avoid using the STL if I can. This is discussed in some other threads.

Quote:
3. There are four places in the code where 100.0 * EPSILON is computed and used as a threshold. Since EPSILON cannot be such a small value in fixed point, I found that at least one of these thresholds was way too high and circles were going through polygon. I removed the 100.0 and things worked (this was in b2Distance, the line with pointCount==3). Any general advise about how I might set these would be nice.


It sounds like you are porting the SVN version? b2Distance isn't used in 1.4.3. It is really hard to advise you on this. The tolerance in b2Distance comes from a book where the author did a lot of numerical experiments. So in a sense you need to tune this to make sure things work well.

Quote:
4. In b2Island.cpp, in solve, the angularVelocity is squared and then compared to a threshold. Squaring the angular velocity causes overflow so I had to change this code, but after looking at the code I don't understand why this squared value is computed at all. I changed the line to:
b->m_angularVelocity = b2Clamp(b->m_angularVelocity,
-b2_maxAngularVelocity, b2_maxAngularVelocity);
and things work and seem clearer. What the squaring some sort of optimization?


Yeah, multiplication is faster than evaluating conditionals on floating point numbers. In your case the conditionals should be faster.

Quote:
4. It would be nice if this or some similar fixed point implementation could be added to the SVN code (right now I have it working against version 80). I am willing to work to make this happen. The code I have now changes float32 to be Fixed so it has very little impact on the existing code base and can be compiled with the option -DTARGET_FLOAT32_IS_FIXED to generate fixed code instead of the normal float code.


After 2.0 comes out, I will accept a patch for this. However, I don't have the time to maintain a fixed point version. Sorry!

Original thread: https://box2d.org/forum/viewtopic.php?t=266