Friday, July 9, 2010

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.



11 comments:

Jer said...

Thanks, Jeff.

I'm a little confused (or being numb today) as to which build setting to add the conditional to: Architectures, Valid Architectures? Even if I highlight one of those and click the gear it doesn't seem to come up with the same text as what you mention.

Jeff LaMarche said...

Jer:

The option you want to look for is called "Compile for Thumb"

Jeff

Shaun Inman said...

Should the checkbox beside "Compile for Thumb" remain checked? Or will unchecking it cause thumb to be disabled for both architectures?

文郭ss華賴ss添卉顏俊s任 said...

先為別人的快樂著想,是超人;先為自己的快樂著想,是凡人;使別人不快樂,自己也不快樂的,是笨人。..................................................

Tyler said...

usually one uses thumb to increase code density -- we dont really have to worry about that for the iphone/A4, there is plenty of RAM to play with. I am not so sure about the speedup in the instruction pipeline though..

margh said...

yeah!!! its very useful to us. my blog also like that iphone related information with PR:2. If you like our blog we will exchange our links..Thanks..

margh said...

thanks for your infomation

韋志韋志 said...

A contented mind is a perpetual feast...................................................................

SEO Services Consultants said...

Nice information, many thanks to the author. It is incomprehensible to me now, but in general, the usefulness and significance is overwhelming. Thanks again and good luck! Web Design Company

Peppe said...

Thank you Jeff, When i search Google for an Iphone OpenGL ES question often i find the best answer on this site.

I have question, tough, switching off Thumb on Iphone 3GS( should be ArmV7) i noticed a little improvement in my app performance?

Reading you article it should be the opposite behaviour.
Am I miss something else?

Thank's a lot

James said...

As I know you can always buy generic xanax online and no rx required!