Or to reformulate the question: is there a performance penalty in using unsigned values?
And in general: what is the most performant type (16bit signed?, 32bit signed? etc.) on the IPhone ARM processor?
On most processors, there are instructions for both signed and unsigned arithmetic, so the difference between using signed and unsigned integers comes down to which one the compiler uses. If any of the two is faster, it's completely processor specific, and most likely the difference is miniscule, if it exists at all.
When no negative numbers are required, unsigned integers are well-suited for networking and systems with little memory, because unsigned integers can store more positive numbers without taking up extra memory. New programmers sometimes get signed and unsigned mixed up.
An int is signed by default, meaning it can represent both positive and negative values. An unsigned is an integer that can never be negative.
There is no difference. unsigned and unsigned int are both synonyms for the same type (the unsigned version of the int type).
It always depends:
For loops signed integers as counters and limits are a tad faster because in C the compiler is free to assume that overflow never happends.
Consider this: You have a loop with an unsigned loop counter like this:
void function (unsigned int first, unsigned int last)
{
unsigned int i;
for (i=first; i!=last; i++)
{
// do something here...
}
}
In this loop the compiler must make sure that the loop even terminates if first is greater than last because I will wrap from UINT_MAX to 0 on overflow (just to name one example - there are other cases as well). This removes the opportunity for some loop optimizations. With signed loop counters the compiler assumes that wrap-around does not occur and may generate better code.
For integer division otoh unsigned integers are a tad faster on the ARM. The ARM does not has a hardware divide unit, so division is done in software, and is always done on unsigned values. You'll save some cycles for the extra-code required to turn a signed division into an unsigned division.
For all other things like arithmetic, logic, load and write to memory the choice of sign-ness will not make any difference.
Regarding the data-size: As Rune pointed out they are more or less of equal speed with 32 bit types beeing the fastest. Bytes and words sometimes need to be adjusted after processing as they reside in a 32 bit register and the upper (unused) bits need to be sign or zero extended.
However, the ARM CPU's have a relative small data-cache and are often connected to relative slow memory. If you're able to utilize the cache more efficient by choosing smaller data-types the code may executes faster even if the theoretical cycle-count goes up.
You have to experiment here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With