Saturday, September 28, 2013

Thumb

I thought I had done a post on this at some point, but after Googling around, I guess I never did. There were a couple of Twitter discussions about the subject in the past few days, so I thought it was worth mentioning. You can get more in-depth detail about this subject by watching the two OpenGL ES videos from the 2009 Tech Talk World Tour Videos (iTunes link, requires logging in with iPhone SDK account).


The ARM architecture has something called thumb mode (or just thumb). Now, I'm not a hardware engineer so the following may not be 100% technically accurate, but my understanding is that basically thumb mode uses a subset of the processor's available operations and passes two 16-bit operations in the space of a single 32-bit operation, allowing commands to be sent to the CPU twice as fast. For most applications, this is great, and leads to an improvement in overall performance.

However, with the ARMv6-based chips in the original iPhone, the iPhone 3g, and the first generation iPod touch¹, thumb mode didn't have access to the vector processors, so floating point operations forced the processor to convert the two 16-bit operations back into two 32-bit operations, perform the floating point math, and then convert back to the thumb operations, meaning you not only didn't see a performance increase, you often saw a dramatic decrease in performance with thumb on when writing heavy floating-point code, such as you would for an OpenGL ES application.

The version of thumb in ARMv7 which is used by the chip in the iPhone 3Gs and which is also used by Apple's A4 chip and therefore available on the iPad and iPhone 4, does have full access to the vector processors, so you can get the benefit of thumb while doing large amounts of floating point operations, so you want thumb on for these processors.

Therefore, if you're writing an OpenGL ES application, or anything else that does a lot of floating point operations, you want to use conditional build settings in Xcode to turn "Compile for Thumb" ON for ARMv7 and OFF for ARMv6.

You can add conditional build settings by selecting the build setting in Xcode and using the little gear button in the lower left corner of the Build Settings window. If you click it, it will popup a menu, and one of the options will be "Add Conditional Build Setting", which will add a new subrow to that setting. Select "ARMv6" on the left column and use the right column to turn it off. Once you do that, your application will build as a fat binary with an ARMv6 version that doesn't use thumb, and an ARMv7 version of the binary that does. Generally, the increase in application size is relatively minor compared to application's image and sound resources, and the performance gains can be substantial.

No comments:

Post a Comment