Floating-Point Determinism Between Apple A5 and Apple A6 Processors

I am developing a Box2D physics multiplayer game for iOS. Multiplayer uses the lock method as usual. The game updates the physical world in a timely manner. On iOS devices with the same processor, there is no desynchronization.

However, when testing on new iOS devices with the Apple A6 chip, desync occurred. Looking at my log file gives me the impression that desync is pretty fast, and probably due to some kind of floating point operation that I still could not recognize.

I can guarantee that only Box2D is the only module that needs to be synchronized in the design of the game, and all mutliplayer commands and inputs are not disabled in accordance with my journal.

I tried to change all the transcendental functions: sinf, cosf, pow, sqrtf, atan2f for the dual version, but with no luck.

Is there a way to get the Apple A6 to handle floating point numbers just like the Apple A5, like some compiler options?

I will really appreciate any response.

+8
floating-point iphone box2d deterministic
source share
3 answers

The mathematical functions of the library use different algorithms on A5 and A6. If they differ by more than ulp or two, you may have encountered an error; report it. Otherwise, the variation is likely to be within the expected tolerances of a quality mathematical library. To take a look at the reasons why this is so, the best link is Jan Olman’s email on the mac-games-dev mailing list a few years ago, “the math library is not a security tool,” which addressed this exact issue in the context of Mac OS X. ( The tl; dr version is that the goal of delivering bit-identical results across architectures that some game developers want, fundamentally contradicts the most efficient provision of highly accurate answers on all architectures that everyone wants to develop tchiki [and users, as it benefits the response and battery life), something has to give, and the general purpose always has the last priority) for the system library. The Apple Developer Forum will be another good place to find information.

+5
source share

This is actually Nguyen Truong Chung.

Thanks to everyone for the answers. I really appreciate your answers, which enlightened me as a way to continue debugging! At the moment, I somehow figured out the reason for desync, but without specific solutions. I want to give you the information I received, and I hope I can get some more photos.

1. Conclusion:

I have this function that uses cos. I printed the log as follows:

void rotateZ (angle of the float)

{

if( angle ) { const float sinTheta = sin( angle ); const float cosTheta = cos( angle ); // I logged here myLog( "Vector3D::SelfRotateZ(%x) %x, %x", *(unsigned int*)&angle, *(unsigned int*)&cosTheta, *(unsigned int*)&sinTheta ); .... } 

}

Desync happened as follows:

On iPad4: Vector3D :: SelfRotateZ (404800d2) bf7ff708, 3c8782bc On iPhone4: Vector3D :: SelfRotateZ (404800d2) bf7ff709, 3c8782bc

2. Retesting:

And the story does not stop here, because:

  • I tried this line of code at the beginning of the game:

{unsigned int zz = 0x404800d2;

 float yy = 0; memcpy( &yy, &zz, 4 ); const float temp1 = cos( yy ); printf( "%x\n", *(unsigned int*)&temp1; 

}

  • I ran the code above on the same iPhone4 and guessed what? I got this: bf7ff708

  • I put this code in the game update cycle, and the result was still bf7ff708 in each cycle.

  • What else? The value 0x404800d2 is the initialized value of the game, so every time the game starts, the two lines of esync above are always present there.

3: Poll:

So, I decided to forget what happened above, and temporarily replaced sin, cos function with simple Taylor implementations that I found on dreamcode.net. Desync is no longer happening.

It seems that the cos function is not deterministic even on one iPhone 4 (OS version 5).

My question is: do we have an explanation why the cos function returns a different result for the same input on the same phone? Here I have the input 0x404800d2 and two different outputs: bf7ff708 and bf7ff709. However, I cannot reproduce the result of bf7ff709 by simple coding.

It seems to me that I need the source code of the mathematical functions of the OS (floating point version) in order to clearly understand this. Is the above problem that I found enough as an error report?

+3
source share

Actually Nguyen Truong Chung again. :)

Many thanks for your help.

I just want to report that desync is fixed after I rewrote all the transcendental functions like cos, sin, sqrt, pow, atan2, atan, asin, acos, etc. (as much as possible).

+1
source share

All Articles