Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Floating point determinism between Apple A5 and Apple A6 CPUs

I am developing a multiplayer game with Box2D physics for iOS. The multiplayer is using lock-step method as usual. The game updates physics world fixed-timely. There is no desync among iOS devices with same CPU.

However, when testing with new iOS devices with Apple A6 chip, desync happened. Viewing my log file gives me the impression that desync happens quite fast and it was probably because of some floating point operation that I could not find out which yet.

I can guarantee that only Box2D is the only module needed to be synchronized in the design of the game, and all mutliplayer commands and inputs are not out-of-sync according to my log.

I have tried changing all transcendental functions: sinf, cosf, pow, sqrtf, atan2f to double version, but without any luck.

Is there any way to force Apple A6 treating floating point numbers as same as Apple A5 like some compiler options?

I will really appreciate any answer.

like image 475
Nguyễn Trường Chung Avatar asked Dec 15 '12 02:12

Nguyễn Trường Chung


2 Answers

A number of math library functions use different algorithms on the A5 and A6. If they differ by more than an ulp or two, you may have found a bug; please report it. Otherwise, the variation is likely within the expected tolerances of good-quality math library. For a glimpse into the reasons of why this is so, the best reference is Ian Ollmann's email to the mac-games-dev mailing list several 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, which some game developers want, is fundamentally in conflict with delivering high-accuracy answers as efficiently as possible on all architectures, which all developers [and users, since it benefits responsiveness and battery life] want; something has to give, and for a general-purpose system library the latter necessarily takes priority). The Apple developer forums would be another good place to look for information.

like image 192
Stephen Canon Avatar answered Nov 13 '22 20:11

Stephen Canon


It's actually Nguyen Truong Chung again. :)

Thank you very much for your help so far.

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

like image 27
Tín Tả Tơi Avatar answered Nov 13 '22 19:11

Tín Tả Tơi