Should I use int_least8_t or int_fast8_t?

Asked

Viewed 125 times

3

I am studying the limits of types that c++ 11 brought and I noticed that there are officially several types of integers.

In addition to the Joint Declaration int now I can declare:

  • int8_t
  • int_fast8_t
  • int_least8_t

In this question has a good explanation of each type.

Considering a platform where the int has a size of 10 bits. The type int8_t could not have exactly 8 bits.

What’s best for me to choose, int_fast8_t or int_least8_t?

  • Is there any platform in use with 10-bit byte?

  • I cannot say. I know few architectures. The question is more theoretical than practical. I am curious to know the rules that are involved in the whole decision-making of the compiler.

2 answers

3


In fact, until you have a reason to choose something other than int, you must choose it.

If you want to use a char, use it. It always has 1 byte. And I doubt that I will find an active platform that the byte is different from 8. Certainly none is less than 7, or standard C cannot work, after all it needs to be able to represent at least 95 different characters. And it would be the only case where the int_fast8_t would not be a char, but in practice this does not exist.

The question says that int_least8_t is always a char. It can be used to give a different semantics and indicate more clearly that there is a char and yes an integer with at least 8 bits. In practice changes nothing.

Want to choose for what? Without a problem definition, any solution can be nice.

If you are going to develop for several platforms and need to ensure that in all have the best possible performance for the integer of at least 8 bits will almost always be a int. I would not "waste time" and wear something like this if it is not proven that it is absolutely necessary.

C and C++ require the int has at least 16 bits and cannot be larger than the long int. If it actually has 10 bits, the implementation is not standard, then anything can happen according to the will of the implementor, but in practice it is no longer the C or C language++.

In theory each language implementer can put whatever they want into these types for each platform that it manages code, provided it obeys the rule of specification, and is within the standard. When in doubt they will choose the int which is quite common but not required to be the word size of the processor.

Actually the question is more imaginary than theoretical.

  • I made a small edit on the question to change the type of char to int. I could edit your answer accordingly?

  • 1

    I edited, but maybe it got worse :)

  • No problem, I believe it got better, even more emphasizing that this problem is more theoretical than practical.

3

What’s best for me to choose, int_fast8_t or int_least8_t?

  • int_least8_t is the smallest whole type with at least 8 bits, you must choose it if you prioritize space (memory)

  • int_fast8_tis the faster type with at least 8 bits, must choose it if prioritizes time (performance)

These definitions are present to explain the intentions when choosing the type. A math library that requires high performance may prefer the int_fast8_t, and a library that needs to store a lot of data may prefer the int_least8_t. As both types guarantee 8 bits of accuracy, the programmer is sure that he can perform calculations in the range [-128, 127] with the int_fast8_t, and record values between [-128, 127] with the int_least8_t.

In most personal processors (where I usually program) both types map to the signed char, but assuming I develop a program and then want to port it to another platform (like compiling a game for some mobile phone or video game), the compiler (or programmer) can change the type int_fast8_t to some faster in the new processor, increasing the performance of mathematical functions without altering the functioning of the code or other parts of the program.


(This may sound obvious to those who speak English, but expanding the names of the types helps:)

  • int_least8_t : tipo_inteiro_pelo_menos_8

  • int_fast8_t : tipo_inteiro_rápido_8

  • You kind of answered the same thing as the Soen question. Even so I gave +1 to supplement the question and serve as reference if any other user has doubt.

  • 1

    @jlHertel pity it was very similar (it was not the idea), but this is the reason why the types were invented/defined.

  • 1

    If it is an unsigned int, wouldn’t the maximum be 127 instead of 255? (assuming complement of 2)

  • @Yes! Corrected by!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.