Stoatman
Stoatman

Reputation: 758

What causes int8_t and int_fast8_t to be different?

I did some searching and understood the difference between these data types. However, what confuses me is that, what causes the difference. When I go to stdin.h library, I see that int8_t and int_fast8_t is typedef as signed char. So, it is the same, right? But then, why int_fast8_t is always faster or equal to int8_t in term of speed?

Upvotes: 2

Views: 628

Answers (3)

Baum mit Augen
Baum mit Augen

Reputation: 50063

So, it is the same, right?

Yes, on this particular implementation, it is. But in general, this need not be the case.

But then, why int_fast8_t is always faster or equal to int8_t in term of speed?

If both types are the same, the equal part applies. If the implementation has something faster it can use, the types will not be the same and the int_fast8_t will perform better (in some sense). (The types may also differ, but yield the same performance, but that is still covered by the "faster or equal" part of the quote.)

Bottom of the line, there does not actually have to be a difference between int8_t and int_fast8_t, and it is not uncommon for them to be the same type.

Upvotes: 0

Zbynek Vyskovsky - kvr000
Zbynek Vyskovsky - kvr000

Reputation: 18825

The fast version of intNs is guaranteed to be at least N bits size but rather being something which processor can handle natively.

On x86 it doesn't probably matter that match but for example old Alpha processors didn't support 8-bit or 16-bit types directly (at least the older versions). So this int_fast8_t was defined as 32-bit integer.

Upvotes: 0

Pete Becker
Pete Becker

Reputation: 76315

Clearly, with those definitions, for whatever platform you're on, int8_t and int_fast8_t are equally fast. On some platforms a size larger than 8 bits could be faster, and in that case it would be appropriate to typedef int_fast8_t to that larger type. And on platforms that don't have an 8-bit type (some DSPs define char as 16 or 32 bits, and some old systems used 9-bit bytes), int8_t won't exist, but int_fast8_t will.

Upvotes: 1

Related Questions