First, here is what I understand and think what is true for the question.
Use fast data types for single variables like counters or
for
loop indexes. For example:#define LOOP_COUNT (100U) uint_fast8_t index; for(index = 0; index < LOOP_COUNT; index++){ /* Do something */ }
I suppose the most suitable type in here is
uint_fast8_t
sinceindex
can never exceed 255 and this will be the fastest implementation for all platforms. If I usedunsigned int
instead, it will be fastest in >=16 bits platforms but will be slower in <16 bits platforms asint
is 16 bits minimum by standard. Also, if I useduint8_t
it will be slower on >8 bits platforms as the compiler adds anAND 0xFF
instruction to check overflow for each increment (my ARM7 compiler does that even on full speed optimization).size_t
is also not an option since it can be bigger than the native integer size.Bad side (?) of this if an overflow is expected for 8 bits, it is not gonna happen. Programmer should check for overflow manually (as he/she should IMHO), which may result in buggy code if forgotten. Also, compiler (and even PC-Lint to my surprise) will not give any warning/issue if LOOP_COUNT "accidentally" set to a value bigger than 255 on >8 bits platforms, but the warning will be generated on an 8 bits platform, which will reduce portability and introduce bugs, but this can be avoided with
#if
checks.Use least data types as much as possible if memory usage is of concern like in arrays or structures. For example:
uint_least8_t array[100];
It is the most portable and efficient way to declare arrays if memory usage is of concern. This type will give a byte array if byte access is possible on the platform, and give the smallest accessible width integer array otherwise. Also, least types can be used in structures if we have arrays of the structure.
Least type can also suffer the problems fast types do, as width of variables can be changed on different platforms for both cases.
Avoid fixed width data types as much as possible as they may not even exist on some platforms, except hardware register access, communication protocol mapping, etc. where we need to know the exact bits of the variable. For example:
typedef struct { uint8_t flags; uint8_t length; uint8_t data[100]; uint16_t crc; } __attribute__((packed)) package_t;
Usually
__attribute__((packed))
(or something similar) should be used to ensure no padding will be inserted for these cases, as this can be a problem by itself.
Now, if my understanding is true, I think least data types are more likely to be used in arrays or structures, fast data types are more likely to be used for single variables and fixed data types are unlikely to be used in order to achieve maximum portability and efficiency. But typing "fast" and "least" every time is not encouraging. So, I think of a type set as follows:
typedef [u]intN_t os_[u|s]exactN_t;
typedef [u]int_fastN_t os_[u|s]N_t;
/* I couldn't come up with a better name */
typedef [u]int_leastN_t os_[u|s]minN_t;
/* These may change */
typedef uint_least8_t os_byte_t;
typedef uint_least16_t os_word_t;
/* ... */
- First and the important question is, is my understanding true?
- What would be the most portable AND efficient way to use C99 standard types and how would you declare them if it is not true?
- Does my type set makes sense or is it likely to produce buggy code?
Also, I would be pleased to know how and where you use C99 standard types.
uint32_t
actually being 32-bits.