Which is better? int8_t vs int32_t in 32 bits MCU

2.6k views Asked by At

Assume to have

function f(int8_t a, int8_t b) //  a b only need 8 bits

Another option is:

function f(int32_t a, int32_t b) // a b only need 8 bits

It runs in 32bits MCU, like ARM Cortex_M. Which one is better option with respect to required code size, data size and executing efficiency?

If in 8 bits MCU such as 8051, the int8_t should be better, right?

4

There are 4 answers

0
Serge Ballesta On

C provides you in stdint.h some more types for which you can assume that the question is answered at the compiler implementation level. Extract from C99 Draft :

7.20.1.3 Fastest minimum-width integer types

1 Each of the following types designates an integer type that is usually fastest255) to operate with among all integer types that have at least the specified width.

2 The typedef name int_fastN_t designates the fastest signed integer type with a width of at least N. The typedef name uint_fastN_t designates the fastest unsigned integer type with a width of at least N.

3 The following types are required: int_fast8_t int_fast16_t int_fast32_t int_fast64_t uint_fast8_t uint_fast16_t uint_fast32_t uint_fast64_t

So provided they are defined in your architecture just use int_fast8_t (or uint_fast8_t)

1
paulsm4 On

Serge Ballesta's response above about using int_fast8_t/uint_fast8_t if at all possible is an excellent suggestion.

Here is a good article on the subject:

32‐Bit Microcontroller Code Size Analysis, Joseph Yiu, Andrew Frame:

Overview

Microcontroller application program code size can directly affect the cost and power consumption of products therefore it is almost always viewed as an important factor in the selection of a microcontroller for embedded projects. Since the release and availability of 32‐bit processors such as the ARM Cortex‐M3, more and more microcontroller users have discovered the benefits of switching to 32‐bit products – lower power, greater energy efficiency, smaller code size and much better performance. Whilst most of the benefits of using 32‐bit microcontrollers are widely known, the code size advantage of 32‐bit microcontrollers is less obvious.

In this article we will explain why 32‐bit microcontrollers can reduce application code size whilst still achieving high system performance and ease of use.

One very interesting point in the article is the role of 16-bit "int" with your 32-bit MCU. In certain situtations, Keith Thompson "Why not use int?" can be good advice.

Also informative:

Efficient C Tips #1 – Choosing the correct integer size

... I wrestled with these problems for many years before finally realizing that the C99 standards committee has solved this problem for us. Quite a few people now know that the C99 standard standardized the naming conventions for specific integer types (int8_t, uint8_t, int16_t etc). What isn’t so well known is that they also defined data types which are “minimum width” and also “fastest width”.

To see if your compiler is C99 compliant, open up stdint.h. If it is compliant, as well as the uint8_t etc data types, you’ll also see at least two other sections – minimum width types and fastest minimum width types.

Fixed width unsigned 8 bit integer: uint8_t

Minimum width unsigned 8 bit integer: uint_least8_t

Fastest minimum width unsigned 8 bit integer: uint_fast8_t

0
Grady Player On

if you use an 8 bit value, you may actually incur a penalty of masking that value back to 8 bits after operations... if your registers are 32 bits and there is no harm in using 32 bit values, then just use them...

0
kkrambo On

Spend your effort and energy making the code easier to understand for future developers. Don't worry about making things easy for the compiler or CPU. Use the type that makes the intention and/or abstraction most clear for future developers.

If the bit width of the function parameter doesn't matter then use int (or unsigned int). Examples where bit width doesn't matter might include local variables and loop counters. If the parameter value must be a certain bit width then use uint8_t, uint16_t, or uint32_t. Specific bit widths are often important when the variable represents a fixed size field in a peripheral register, file, or protocol message. If execution speed is important then use uint_fast8_t, etc. This might be in a piece of code that executes often or in an interrupt.

All of these types provide an additional clue to developers reading your code about what you thought was important and what your intention was. Your energy is better spent making the code clear for developers rather than trying to out-think the compiler with micro-optimizations for the CPU.