Advantages and disadvantages between int vs int_fast8_t

While I was studying data types in c. I came across int_fast8_t and int_least8_t data types. I did not know this, so I did it. I found several answers here.

Difference between int8_t, int_least8_t and int_fast8_t?

The answers of int_fast8_t are the fastest, but I am surprised that everything speeds up. What technologies does the compiler use to speed things up? and also when there are int types datatype and short , long modifiers for resizing int. What is the need for this ( int_fast8_t ) data type?

If int_fast8_t faster, we can just ignore int and always go for int_fast8_t , since everyone needs speed.

Are there any restrictions for int_fast8_t ? What are the advantages or disadvantages between int_fast8_t and int . Any help is appreciated. Thanks

+4
source share
3 answers

int_fast8_t is the fastest integer type with a width of at least 8 bits. There is no reason to think that it will be faster than int (which is usually the fastest integer type).

In addition, the two types may have different widths, that is, they often cannot be used interchangeably.

+3
source

The short answer is compatibility if you have ever moved your code from one architecture to another.

int_fast8_t is likely to be the same as int , since in most (many? some?) architectures, the int type is defined as the size of the native word (32 bits for a 32-bit architecture, 64 bits in a 64-bit architecture). Basically, int_fast8_t says that if I interpret correctly, the base type will be the fastest, which can contain at least 8 bits of information.

If you move your code to a different architecture, you can be sure that you will still have at least 8 bits if you use int_fast8_t . You cannot be sure of this if you just use int .

+4
source

For example, on a 32-bit platform, int is 32-bit. The CPU may have instructions for working with 32-bit pieces of data. It may not have such instructions for 8-bit data. In this case, the processor should probably use some bitwise operations to process data of 8-bit types.

On such a platform, int_fast8_t can be implemented as a 32-bit int . But you cannot use it as a 32-bit int , it can only be used as an 8-bit type without undefined behavior.

+2
source

Source: https://habr.com/ru/post/1496318/


All Articles