Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantages and disadvantages between int vs int_fast8_t

Tags:

c

While I was learning datatypes in c. I came across int_fast8_t and int_least8_t data types. I did not know these, So I have googled it. I have found some answers here:

The difference of int8_t, int_least8_t and int_fast8_t?

The answers are int_fast8_t is fastest but Iam surprised what things makes it fast. What techniques does compiler use to make it fast ? and also, When there exists int datatype and short,long modifiers to modify int size. What is the need of this (int_fast8_t) datatypes ?

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

Is there any limitations for int_fast8_t? What are the advantages or disadvantages between int_fast8_t and int.

like image 701
niko Avatar asked Aug 10 '13 14:08

niko


3 Answers

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

int_fast8_t will probably be the same as int since in most (many? some?) architectures the int type is defined to be the size of the native word (32 bits for a 32 bit architecture, 64 bits in 64 bit architecture). Basically, int_fast8_t is saying that, if I'm interpreting correctly, that the underlying type will be the fastest that can hold at least 8 bits of information.

If you move your code to different architecture you can be sure you'll still have at least 8 bits to work with if you use int_fast8_t. You can not be sure of that if you simply use int.

like image 119
GSP Avatar answered Sep 24 '22 02:09

GSP


int_fast8_t is the fastest integer type that's at least 8 bits wide. There is no reason to think that it'll be faster than int (which is typically the fastest integer type there is.)

Besides, the two types could have different width, meaning they often can't be used interchangeably.

like image 43
NPE Avatar answered Sep 23 '22 02:09

NPE


For example, on a 32-bit platform, int is 32-bit, The CPU might have instructions to operate on 32-bit chunks of data. It might not have such instructions for 8-bit data. In this case, the CPU probably has to use some bitwise operations to process 8-bit types data.

On such platform, int_fast8_t may be implemented as 32-bit int. But you can't use it as a 32-bit int, you can only use it like a 8-bit type without undefined behavior.

like image 37
Yu Hao Avatar answered Sep 25 '22 02:09

Yu Hao