A few things IOS developers ought to know about the ARM architecture

Source: Internet
Author: User
Tags integer division

Http://wanderingcoder.net/2010/07/19/ought-arm/

A few things IOS developers ought to know about the arm ubunturefiled under: programming-Pierre lebeaupin @ 4:49 AM

When I wrote my introduction to neon iPhone, I considered some knowledge about the iOS devices 'processors as assumed to be known by the reader. However, from some discussions online I have
Realized some of this knowledge was not universal; my bad. furthermore these are things I think are useful for iPhone programming in general (not just if you're interested in neon), even if you program in High-level objective-C. you coshould live without them,
But knowing them will make you a better iPhone programmer.

The basics

All iOS devices released so far are powered by processors based on the ARM architecture; as you'll see, this architecture is a bit unlike what you may be
Used to on the desktop with x86 or even PowerPC. however, it is not a "special" or "niche" architecture: nearly all mobile phones (not just smartphones) are based on ARM; practically all ipods were based on ARM, as well as nearly all MP3 players; PDAs and
Pocket PCs were generally ARM-based; Nintendo portable tables les are based on ARM since the GBA; it is now invading graphic calculators with some Ti and HP models using it; and if you want pedigree, know the Newton was based on ARM as well (in fact, Apple was
An early investor in arm). And that's only mentioning gadgets; countless arm processors have shipped in unassuming embedded roles.

Arm processors are renowned for their small size on the silicon die, low power usage, and of course their performance (within their power class ). the ARM architecture (at least as used in the iOS platform) is little-Endian, just like x86. it is a 32-bit
RISC architecture, like MIPS, PowerPC, etc. notice the simulator does not execute arm code, when building for the simulator your app is compiled for x86 and executes natively, so none of the following applies when running on the simulator, you need to be running
On target.

Armv7, arm11, Cortex A8 and A4, oh my!

The ARM architecture comes in a few different versions developed over time; each one added some new instructions and other improvements, while being backwards compatible with the previous versions. the first iPhone had a processor that implements armv6 (short
For arm version 6), while the latest devices have processors that can support armv7. so when you compile code, you specify the architecture version you're re targeting, and the compiler will restrict the instructions it generates to those available in that architecture
Version; the same goes for the specified er, which will check that the instruied used in the Code are present in the specified architecture version. in the end, you have object code that targets a specific architecture variant, armv6 or armv7 (or armv5 or
Armv4, but given that armv6 is the baseline in iOS development, you're very unlikely to target these); the object and executable files are in fact marked with the architecture they target, runotool -vh foo.oOn one of your object or executable
Files sometime.

However, it does not make sense to say that the original iPhone had "the armv6 processor": armv6 does not designate a particle processor, but the set of instructions a processor can run. the processor core implementation used in the original iPhone was
The arm11 (it was the ARM1176JZF-S, to be really accurate, but it matters very little, just remember it was a member of the arm11 family); As mentioned earlier, this processor implements armv6. subsequent devices used arm11 as well, up until the iPhone 3gs
Which started using the cortex A8 processor core, used in all IOS devices released since then at the time of this writing (this is not yet certain, but strongly suspected, in the case of the iPhone 4 ). this core implements the armv7 instruction set, or in
Short, supports armv7.

Now having said that, do not go around and write code that detects which device your code is executing on and tries to figure out which architecture it supports using the known information on currently released devices. besides being the most unreliable
Code you cocould write, this kind of code will break when run on a device released after your application. so please don't do it, otherwise I swear I will come to your house and maim you. this information is just so that you have a rough idea of the installed
Base of devices that can support armv7 and the ones that can only run armv6; I'll get to detection in a minute.

But you may be wondering: "I thought the iPad and iPhone 4 had an A4 processor, not a cortex A8 ?!" The A4 is in fact the whole applications system on a chip, which provided des not only a cortex A8 core, but also graphics hardware, as well as video and audio
Codec accelerators and other digital blocks. The SOC and the processor core on it are very different things; the processor core does not even take the majority of the space on the silicon die.

Armv7 support on the latest devices wocould be pretty useless if you couldn't take advantage of it, so you can do so, but always doing so wowould prevent your code from running on earlier devices, which may not be what you want. so how do you detect which architecture
Version A device supports so that you can take advantage of armv7 features if and only if they are present? The thing is, you don't. Instead, your code is compiled twice, once targeting armv6, And once targeting armv7; the two executables are then put together
In a fat binary, and at runtime the device will itself choose the best half it can support. yes, Mach-o fat binaries are not just for grouping completely different CPU ubuntures (e.g. powerPC and Intel, making a universal binary), or 32 and 64 bit versions
Of an architecture, but also two variants (CPU subtypes, in Mach-O parlance) of the same architecture. the outcome is that from the viewpoint of the programmer, everything gets decided at compile time: the Code Compiled targeting armv6 will only ever run on
Armv6 devices, and the Code Compiled targeting armv7 will only ever run on armv7 (or better ).

If you 've read my neon Post, you may remember that in that post I also suggested a way to do the detection and selection at runtime. if you check now, you'll notice I have actually removed that part, and now recommend you do not use that method any more.1 this
Is because while it does work, it was impossible (or at the very least too tricky to implement to do so without any error) to ensure that the Code wocould keep working the day it is run on a future armv8 processor. the fact the specified ented status of that API
Is unclear doesn't help, either (its man page isn't in the IOS man pages ). you shoshould exclusively use compile-time demo-and fat executables if you want to run on armv6 and take advantage of armv7.

One last note on the subject: In the context of iOS devices, the ARM architecture versions do not strictly mean what they mean for ARM processors in general. for instance, IOS code that requires armv6 actually requires support for floating-point instructions
As well (vfpv2, to be accurate), which is an optional part of armv6, but has been present since the original iPhone, so when armv6 is mentioned in iOS development (e.g. as a compiler-arch setting or as the CPU subtype of an executable) Hardware floating-point
Support is implied. the same goes for armv7 and neon: neon is actually an optional part of the ARMv7-A profile, But neon has been present in all IOS devices supporting armv7, so when developing for iOS neon is considered part of armv7.

Conditional execution

A nifty feature of the ARM architecture is that most instructions can be conditionally executed: If a condition is false, the instruction will have no effect. this allows short if blocks to be implemented more efficiently: while the usual method is to jump
To after the block if the condition is false, instead the instructions in the block are conditionalized, saving one branch.

Now I wouldn't mention this if it was just a feature the compiler uses to make the code more efficient; while it is that, i'm mentioning it because it can be surprising when debugging. indeed, you can sometimes see the debugger go inside if blocks whose
Conditions you know are false (e.g. Early error returns), or go in both sides of an IF-else! This is because the processor actually goes through that code, but some parts of it aren't actually executed, because they are conditionalized. Moreover, if you put
A breakpoint inside such an if block, it may be hit even if the condition is false!

That being said, it seems (in my limited testing) that the compiler avoids generating conditionally executed instructions in the debug configuration, so it shoshould only occur when debugging optimized code; unfortunately sometimes you have no choice but
Do so.

Thumb

The thumb instruction set is a subset of the arm instruction set, compressed so that instructions take only 16 bits (all arm instructions are 32 bits in size; it is still a 32-bit architecture, just the instructions take less space ). it is not a different
Architecture, rather is shoshould be seen as a shorthand for the most common arm instructions and functionality. the advantage, of course, is that it allows an important ction in code size, saving memory, cache, and code bandwidth; while this is more useful
In microcontroller type applications where it allows hardware savings in the memory used, it is still useful on iOS devices, and as such it is enabled by default in xcode IOS projects. the code size ction is nice, but never actually reaches 50% as sometimes
Two thumb instructions are required for one equivalent arm instruction. arm and thumb instructions cannot be freely intermixed, the processor needs to switch mode when going from one to the other; this can only occur when calling or returning from a function.

When targeting armv6, compiling for thumb is a big tradeoff. armv6 thumb code has access to fewer registers, does not have conditional instructions, and in particle cannot use the floating-point hardware. this means for every single floating-point addition,
Subtraction, multiplication, Etc ., floating-point thumb code must call a system function to do it. yes, this is as slow as it sounds. for this reason, I recommend disabling thumb mode when targeting armv6. if you do leave it on, make sure you profile your
Code, and if some parts are slow you shoshould first try disabling thumb at least for that part (easy with file-specific compiler flags in xcode, use-mno-thumb). Remember that floating-point calculations are pretty common on iOS since quartz and
Core animation use a floating-point coordinate system.

When targeting armv7, however, all these drawbacks disappear: armv7 contains thumb-2, an extension of the thumb instruction set which adds support for conditional execution and 32-bit thumb instructions that allow access to all arm registers as well as hardware
Floating-point and neon. it's pretty much a free ction in code size, so it shoshould be left on (or reenabled if you disabled it ); use conditional build settings in xcode to have it enabled for armv7 but disabled for armv6.

In discussions on the internet you may find mentions that code needs to be "interworking" to use thumb; unless you write your own assembly code, you don't have to worry about it as all code is interworking In the iOS platform. when displaying assembly, shark
May have trouble figuring out whether the function is arm or thumb, if you see invalid or nonsensical instructions, you may need to switch from one to the other.

Alignment

In the iOS platform unaligned accesses are supported; however, they are slower than aligned accesses, so try and avoid them. in some particle cases (those involving load/store multiple instructions, if you're interested), unaligned accesses can be a hundred
Times slower than aligned accesses, because the processor cannot handle these and has to ask the OS for balance (read this article, it's the same phenomenon that on powerpc causes unaligned
Doubles to be so much slower). So be careful, alignment still matters.

Division

This one always surprises everyone. Open the ARM architecture Manual (if you don't already have it, see Introduction to neon on iPhone, section Architecture Overview) and try and find an integer
Division instruction. Go ahead, I'll wait. Can't find it? It's normal. There is none. Yes, the ARM architecture has no hardware support for integer division, it must be written med in software. If you compile the following code:

int ThousandDividedBy(int divisor){    return 1000/divisor;}

To assembly, you'll see the compiler has inserted a call to a function called "___ divsi3 "; it's a system function that implements the division in software (notice the divisor shocould be non-constant, as otherwise the division is likely to be turned
Into a multiplication). This means that on arm, integer division is actually an operating system benchmark!

"!" You may say, having finally returned various from the arm manual "you're wrong! There is an arm division instruction, even two! Here, sdiv and udiv !" Sorry to rain on your parade, but these instructions are only available in the ARMv7-R and ARMv7-M
Profiles (real-time and embedded, respectively-think motor microcontrollers and wristwatches), not in ARMv7-A which is the profile that the iOS devices that have armv7 do support. Sorry!

Gcc

It's not a secret that the arm code generated by GCC is considered to be crap. On other ARM-based platforms professional developers use the toolchain provided by arm itself, rvds,
But this is not an option on the iOS platform as rvds doesn't support the Mach-O runtime used by OSX, only the elf runtime. but there is at least an alternative to GCC, as now llvm can be used for iOS development. while I 've not tested it much, I have at least
Seen nice improvements on 64-bit integer code (a special weak point of GCC on arm) when using llvm. Hopefully, llvm will prove to be an improvement over GCC in all domains.

~

There, you're now a better IOS developer!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.