PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

THUMB vs. ARM

Hey,

as far as i understand, the only reason for using the THUMB instructions in assembler is the higher code density. For speed, one should always go for ARM instructions.
Does anyone know if this is generally true or are there exception to this rule? Has someone measured performance vs. code size? The only real problem i see is that a too large rutine in ARM could be small enough if recoded in THUMB to use the cache better. And does a veneer mean a high preformance penalty? What does eVC do, it seems like it always generates ARM, or does it depend on the optimization settings, i.e. if i go for "smallest code" will it use THUMB instead of ARM instructions?

I guess THUMB is cool for the smaller devices and processors like cell phones and the like, i dont think with modern PDA's having at least 32MB the size of an executable is such a problem.

thanx in advance, i hope some guru can answer some of this  :)

cryo
The StrongARM used in the current crop of Pocket PC devices has an ARM V4 core (no Thumb support).

eVC's ARM compiler supports generation of ARM Thumb instructions via the /QRarch4T switch, however I doubt that this is invoked through the compiler optimizer's flags because it would never produce the proper code in a debug build where the optimizations typically are disabled.

Realize that although the Thumb instructions are half the size of the ARM instructions, it might take two of them to do what you could do in one of the ARM instructions.

Code size typically isn't a problem, it's usually data size that gets you in trouble on a handheld device - especially with games.  Porting code from the PC can sometimes be a challenge because the Windows programmers have basically no memory restrictions (or at least they seem to code that way).  As long as there is free disk space, malloc won't fail.

I recall some gaming device using Thumb instructions... Does the GameBoy Advance use Thumb instructions?

thanx Digby,

yeah, the GBA supports both THUMB and ARM instructions. I have read some tutorials on GBA coding and it seems to be possible to jump between ARM and THUMB code areas arbitrarily.
BTW: GBA sites are a good source of graphics-oriented ARM assembler source code. I have found http://www.gbadev.org to quite useful.

cryo
Also--THUMB is much faster for instruction lookup when your memory bus is 16-bit instead of 32-bit (true for many embedded devices.) In this case, each ARM instruction causes an extra 1-read stall, which can be significant on slow devices.
What Digby said is spot on.

The THUMB instruction set is essentially a sub-set of the standard ARM instructions. Though you can fit more instructions in the same memory, they're less powerful in a few ways:

sometimes you don't get the free barrel shift - there're LSL, LSR instructions in thumb IIRC

you only get to play with 8 registers

AFAICR there is no "penalty" for using THUMB - the processor simply translates the THUMB instruction into an ARM instruction and executes it, free of charge.

You can jump twixt ARM and THUMB arbitrarily, yes, using the BX instruction (branch and exchange).

The only real problem i see is that a too large rutine in ARM could be small enough if recoded in THUMB to use the cache better.


I think that the ARM caches are generally large enough to cover that sort of thing - remember there are distinct instruction and data caches in most implementations (the ones where there is a cache). Of course, the GBA has no on-chip cache - the internal memory is used instead. In my experience it's usually the data caching that's the problem, not the instruction caching.

For something like a looping routine, once the instructions hit the cache the routine runs at full tilt...
...If it's in THUMB it's faster to load it into the cache (less instructions).
...If it's in ARM it's probably a faster routine because the ARM instructions can do more per cycle (more powerful instructions).

So you decide which is "better" - a slower routine or a faster load time. For a short loop, THUMB likely wins; for a long loop, ARM will win because it's inner loop should be shorter execution-wise.

Processors like the StrongARMs have a 32-byte cache line fill, so even if the PPC ones supported THUMB you'd still be loading at least 32-bytes into the cache at once anyway.


As for metrics, from the <a href="http://www.devrs.com/gba/files/gbadevfaqs.php#ARMvsThumb">GBA Dev FAQs</a>:

Thumb code is able to provide up to 65% of the code size of ARM, and 160% of the performance of ARM when accessing 16 bit memory. (Source: ARM DDI 0029E) Since most code is usually run out of ROM (which is 16 bit) it makes sense to use a Thumb compiler. For the times when you need the extra performance of ARM, you can always code these subroutines in ARM assembly language and place them in internal WRAM.


The ARM DDI documents are available on the ARM site.

Cheers,

Refractor