4

I've read that using short vs int is actually creating an inefficiency for the compiler in that it needs to use the int datatype regardless because of C integer promotion. Is this true for 16-bit microprocessors?

Another question: If I have an array of 1s and 0s, is it most efficient to use uint8_t or the unsigned char in this 16-bit microprocessor? Or is there still an issue with it being converted back to int..

Please help me clear up this muddy issue in my mind. Thanks!

6
  • 1
    You haven't told us what particular 16-bit microprocessor you're talking about. There are at least many dozens of potential candidates, why not just tell us who makes it and what its model number is.
    – jer
    Feb 14, 2011 at 20:04
  • 2
    The exact size of an intergral type is implementation defined - the compiler can decide depending on the target platform. I doubt it would be stupid enough to use a size for int that the processor doesn't support natively - especially since int is inofficially supposed to be the target's native word size. Check sizeof int on the compiler in question on the target in question.
    – user395760
    Feb 14, 2011 at 20:06
  • @jer: I'm working on a Analog Device Blackfin if that helps.
    – O_O
    Feb 14, 2011 at 20:08
  • From sizeof(int), I get 4. With sizeof(short), I get 2. I kinda expected the sizeof(int) to be 2 as well... Should I still be using int? Still don't make total sense to me. Thanks.
    – O_O
    Feb 14, 2011 at 20:30
  • 1
    Don't confuse C Implementation with microprocessor. The former can make its types virtually any size, even if the CPU could not natively handle it.
    – user611775
    Feb 15, 2011 at 1:27

4 Answers 4

5
  1. Is it really an issue? On most 16 bit systems I've heard of, int and short end up being the same size (16 bits), so there shouldn't really be a difference in practice.

  2. If uint8_t exists on a system, it's going to be synonymous with unsigned char. unsigned char will be the smallest unsigned type avaliable on the system. If it's any more than 8 bits, there will be no uint8_t. If it's less than 8 bits, then it's violating the standard. There will be no efficiency difference since one has to be defined in terms of the other.

Lastly, do you really need to worry about these kind of microscopic differences? If you do you'll need to peek at the assembly output or (more likely) profile and see which one is faster.

2

On a Blackfin it is probably not a simple answer whether 32 or 16 bit types will generate higher performance generally since it supports 16, 32 and 64-bit instructions, and has two 16 bit MACs. It will depend on the operations, but I suggest that you trust your compiler optimiser to make such decisions, it knows more about the processor's instruction timing and scheduling than you probably care to.

That said it may be that in your compiler int and short are the same size in any case. Consult the documentation, ot test with sizeof , or look in the limits.h header for numeric ranges that will infer the widths or the various types.

If you truly want to restrict data type size use the stdint.h types such as int16_t.

stdint.h also defines fastest minimum-width integer types such as int_fast16_t, this will guarantee a minimum width, but will use a larger type if it will be faster on your target. This is the probably the most portable way of solving your problem, but it relies on the implementer to have made good decisions about the appropriate types to use. On most architectures it makes little or no difference, but on RISC and DSP architectures that may not be the case. It may also not be the case that a particular size is fastest in all circumstances, and that is probably especially true in the case of Blackfin.

In some cases (where large amounts of data are transferred to an from external memory), the fastest size is likely to be one that matches the data bus width.

2

On a 16-bit or larger processor, if you don't care how much storage things will take, use 'int' instead of 'short' or 'signed char'. If you don't care about storage requirements or wrapping behavior, use 'unsigned int' instead of 'unsigned short' or 'unsigned char'. On an 8-bit processor, 'char' types may be faster than 'int', but on 16-bit and larger processors where 16-bit math is faster than 32-bit math, 'int' is likely to be 16 bits so there's no need to use 'short' or 'char' for speed.

BTW, on some processors, 'unsigned short' is much slower than 'unsigned int', because the C standard requires that operations on unsigned types 'wrap'. If unsigned short variable "foo" is stored in a register, a typical ARM compiler generating code for "foo+=1;" would generate one instruction to do the increment, and two instructions to truncate the value to 65535 [BTW, an optimizing compiler that noticed that 'foo' could never exceed 65536 could shave an instruction, but I don't know if any real compilers would]. The signed 'short' would not have to be slower than 'signed int', since no truncation is mandated by the standard; I'm not sure whether any compilers would skip the truncation for signed types, though.

6
  • 2
    IMO your discussion about unsigned doesn't make sense. You can just do you can just doing your addition, subtraction, multiplication using the full register and only do the masking to 65535 when you need to store the result (that's a nice property of modulo arithmetic). Actually short and char are just "storage" types because they're automatically converted to int in an expression... for example unsigned char x=255; int y = x+1;' will assign 256 to y` even if a char is 8 bit and an int is 16, as no wrapping will happen during the addition.
    – 6502
    Feb 14, 2011 at 20:40
  • 2
    A bit confused with wrapping and truncation; they are not the same. I just examined the output of an ARM compiler (ARM's own armcc) using ARM mode (as opposed to Thumb) on an ARM7TDMI target, and your assertions do not hold up.
    – Clifford
    Feb 14, 2011 at 20:55
  • 2
    @6502: If an unsigned short variable is held in a register, then compliance with the spec will require that the register be truncated to an unsigned short. Saying "intval = ushortval + 1;" would require that the value not be truncated, but "ushortval +=1; intval = ushortval;" would require truncation, as would "if (!++ushortval) ...".
    – supercat
    Feb 14, 2011 at 21:36
  • 2
    @Clifford: What did you try and what was the result? Note that intermediate computations in registers don't have to be truncated to ushort (indeed they can't be in any case that would matter) but assignments to variables which happen to have been optimized to registers must be truncated if unsigned. Signed registers don't have to be truncated, but I wouldn't be surprised if some compilers do it anyway.
    – supercat
    Feb 14, 2011 at 21:38
  • 1
    @supercat: What I tried and the result are not easy to report in a comment block. I declared a volatile short integer variable, then did foo+=1 as you suggested, and then assigned that to a second static volatile short. I used signed shorts since you did not specify and the original post specified signed in any case. They were volatile to prevent the compiler making assumptions about stored values. The static declaration was to force memory storage rather than a register. I probably don't care enough to investigate further.
    – Clifford
    Feb 15, 2011 at 16:33
0

I make a point of having a block that looks like this in projects that rely on byte sizes:

typedef uint8 char
typedef uint16 short
typedef uint32 long

For whatever datatypes are appropriate.

Any conversion issues should be figured out at compile time. Those will be cpu and compiler dependent.

Of course adding 2 32-bit numbers on a 16-bit CPU will entail some finangling by the compiler. There can also be amusing things when you dink with loading from memory, depending on memory word width and whether you can load from any address, or if bytes have to be loaded from a given boundary

In short, YMMV, and optimize after profiling.

3
  • 1
    Instead of rolling your own datatypes, simply #include <stdint.h> to get uint8_t, uint32_t, etc. That way, you won't have to write your own typedefs for every platform, as those data types are defined (by the C99 standard) to be the specified length on all conforming implementations.
    – bta
    Feb 14, 2011 at 20:16
  • @bta: Sure, if you can rely on using C99-conformant compilers! But yeah, that's a 'best practice' if you can. Feb 14, 2011 at 20:25
  • 1
    <stdint.h> is merely a header file implementation, you do not need a C99 compiler; you can create your own that is C99 compatible for any compiler that does not provide it. That will save you having to create these types for all compilers you might ever use.
    – Clifford
    Feb 14, 2011 at 20:59

Your Answer

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

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