Essam Gouda
Essam Gouda

Reputation: 145

What is a fast integer? What defines how fast an integer is?

In this topic the following was mentioned:

The fast type (int_fast#_t) gives you an integer that’s the fastest type with a width of at least # bits (where # = 8, 16, 32, or 64). For example, int_fast32_t will give you the fastest integer type that’s at least 32 bits.

What does he mean by the fastest integer type? What defines the speed?

I assume that not all integers are accessed the same way, some are easier to access than the others but what I need to know what could lead to that speed of access?

I have read in one question that:

On some processors, if a variable gets stored in a register which is longer, the compiler may have to add extra code to lop off any extra bits. For example, if uint16_t x; gets stored in a 32-bit register on the ARM7-TDMI, the code x++; may need to be evaluated as x=((x+1)<<16)>>16);. On compilers for that platform, uint_fast16_t would most likely be defined as synonymous with uint32_t to avoid that.

What makes that faster? As in either case 32 bits will be looped over in the register level.

Upvotes: 11

Views: 4350

Answers (2)

Javier Elices
Javier Elices

Reputation: 2154

I think the concept of "fast" goes down to how the compiler is built. A good compiler is aware of the CPU architechture that it is generating code for. If this is the case, the compiler knows the size of the registers and the units that will do the eventual computations or data movements. Taking all that into account, those C / C++ data types allow you to choose an integer that is at least a certain size (can hold an 8, 16, 32 or 64 bit integer) that is also "fast" in terms of the actual type used by the CPU.

I would bet that in most cases this "fast" type will not be faster than the same regular type that holds that number of bits, but the "fast" type gives you the ability to get all possible performance in those cases in which a larger register will be better than a smaller one, both being able to hold the required number of bits.

I would also bet that the tradeof of this "fast" type may be that it will be larger than the same regular type. I.e. you trade speed vs. size.

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249133

Some 64-bit machines use 64 bits for everything, so doing math in 32-bit space costs extra (e.g. unsigned overflow has to be emulated). On such a machine, int_fast32_t can be 64 bits. Similarly for some 32-bit machines--int_fast8_t might be 32 bits there.

x86-64 is not really affected here--it retains 8-, 16-, and 32-bit instructions so the compiler can just say "do 8 bit math on these registers" and it doesn't matter that the registers are wider. If you're programming on commodity desktops or servers, you probably don't need to care about the "fast" integer types.

Upvotes: 20

Related Questions