2

When APP_ABI includes armeabi-v7a, Android's build system will include -mthumb as a compile option.

ARMv7-a is a 32-bit processor family. Why does Android use -mthumb rather than -marm?

Must native components compiled with the NDK use -mthumb? Or is it OK to use -marm?

If -marm is OK, do any interworking options need to be present?

1 Answer 1

8

Android uses -mthumb because it produces more compact code. The bitness of the processor has nothing to do with it.

In the normal ARM instruction set, each instruction is 32 bit, and each instruction is quite expressive (there's different instructions to do a lot of different things). Unfortunatey this also means that the code can be a bit large, since every single instruction takes 4 bytes.

The thumb instruction set was introduced to work around this issue - here each instruction is only 16 bits long, thus you can fit twice as many instructions in the same space, but since the instructions are shorter, there's not as many different instructions. Each instruction still operates on 32 bit registers and otherwise behaves exactly like the ARM instructions, so it's just a different, more compact way to write (mostly) a subset of the ARM instructions. Due to the limited number of instructions, most things tend to need a bit more instructions than in ARM mode. So depending on what the code actually does, the actual reduction in code size isn't exactly 50%. Also, since there may be more instructions than before, it may actually run slower than before. (But since it's smaller, it uses less instruction cache, which might on the other hand benefit performance.)

In practice, Thumb code tended to be slower than ARM code, but more compact, so for code where size mattered more than speed, you'd use Thumb.

Since Thumb2 (which is available in ARMv7), there's a bit more expressiveness in the Thumb instruction set, so the performance difference is smaller - this is why it's enabled by default in Android - in almost all cases it reduces the code size without significantly affecting the performance. If you've got some really performance critical code, you might want to benchmark to see if it hurts or helps for you. (For building with ndk-build and Android.mk, check the LOCAL_ARM_MODE variable.)

If you build code manually, you can choose whether you add -mthumb or -marm, and AFAIK you don't need any extra interworking options regardless of which one of them you choose.

3
  • To sum up the results of the last couple of questions: Android API does not usually matter. However, there are some caveats. (1) the API must provide the ABI support. For example, armeabi and arm-eabi-v7a needs API 3; and x86 and mips needs API 9. (2) The native code is general purpose; and does not depend on specialized platform features link sensors. Obviously, API level would matter if I were doing platform specific stuff like programming sensors. Is that correct?
    – jww
    Sep 8, 2014 at 15:53
  • Yes, that sounds correct. I haven't used sensors myself so I don't know the details of that API, but in general, yes, the API level only matters if you're using actual APIs from the system. If you do a pure computational library, which does all input/output via e.g. memory buffers, you can build it for the minimum API level.
    – mstorsjo
    Sep 8, 2014 at 16:21
  • Also, you can mix the API level used for building different architectures. If you've got target=android-3 in project.properties, ndk-build will actually use android-9 for x86 and mips (if those architectures are enabled). The resulting APK will run on all devices from API level 3 and up. Since there are no official android devices with x86 or mips running anything less than API level 9, it doesn't hurt that those are built with a higher API level. And for all ARM devices, it will be built with API level 3. Thus it works on all devices with API level >= 3.
    – mstorsjo
    Sep 8, 2014 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.