Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 561
Essam Gouda Avatar asked Sep 27 '17 11:09

Essam Gouda


People also ask

Why int is faster than short?

The integer promotions mean that no operation (addition, bitwise, logical etc etc) in the language can occur on a smaller integer type than int. Thus, operations on char/short/int are generally equally fast, as the former ones are promoted to the latter.

Is int faster than long long?

There are too many variables to know for sure how well your program will perform using long by default instead of int. There are reasons why it could be faster and reasons why it could be slower. It could be a total wash. The typical thing to do is to just use int if you don't care about the size of the integer.

What is int_fast16_t?

int_fast16_t is most efficient type in speed with at least the range of a 16 bit int. Example: A given platform may have decided that int should be 32-bit for many reasons, not only speed. The same system may find a different type is fastest for 16-bit integers.


1 Answers

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.

like image 195
John Zwinck Avatar answered Oct 24 '22 00:10

John Zwinck