Difference between int32_t and int_fast32_t [duplicate]
Asked Answered
C

2

21

What is the difference between the two? I know that int32_t is exactly 32 bits regardless of the environment but, as its name suggests that it's fast, how much faster can int_fast32_t really be compared to int32_t? And if it's significantly faster, then why so?

Ceylon answered 23/4, 2013 at 8:0 Comment(1)
int_fast32_t is the fastest available signed integer type with at least 32 bits.Burnaby
S
30

C is specified in terms of an idealized, abstract machine. But real-world hardware has behavioural characteristics that are not captured by the language standard. The _fast types are type aliases that allow each platform to specify types which are "convenient" for the hardware.

For example, if you had an array of 8-bit integers and wanted to mutate each one individually, this would be rather inefficient on contemporary desktop machines, because their load operations usually want to fill an entire processor register, which is either 32 or 64 bit wide (a "machine word"). So lots of loaded data ends up wasted, and more importantly, you cannot parallelize the loading and storing of two adjacent array elements, because they live in the same machine word and thus need to be load-modify-stored sequentially.

The _fast types are usually as wide as a machine word, if that's feasible. That is, they may be wider than you need and thus consume more memory (and thus are harder to cache!), but your hardware may be able to access them faster. It all depends on the usage pattern, though. (E.g. an array of int_fast8_t would probably be an array of machine words, and a tight loop modifying such an array may well benefit significantly.)

The only way to find out whether it makes any difference is to compare!

Settle answered 23/4, 2013 at 8:9 Comment(0)
J
12

int32_t is an integer which is exactly 32bits. It is useful if you want for example to create a struct with an exact memory placement.

int_fast32_t is the "fastest" integer for your current processor that is at last bigger or equal to an int32_t. I don't know if there is really a gain for current processors (x86 or ARM)

But I can at last outline a real case : I used to work with a 32bits PowerPC processor. When accessing misaligned 16bits int16_t, it was inefficient for it has to first realign them in one of its 32bits registers. For non memory-mapped data, since we didn't have memory restrictions, it was more efficient to use int_fast16_t (which were in fact 32bits int).

Jarrod answered 23/4, 2013 at 8:11 Comment(3)
I don't think an answer that puts the one point of interest in Dr. Evil quotes is very useful... what does "fastest" mean?Settle
@KerrekSB: the Standard just says "fastest", with a footnote "The designated type is not guaranteed to be fastest for all purposes; if the implementation has no clear grounds for choosing one type over another, it will simply pick some integer type satisfying the signedness and width requirements." So I think the quotes are quite appropriate, since each implementer is free to decide for themselves how to measure "speed". int_fast32_t could easily be 32 bits on a 64 bit implementation.Pillion
Sorry, a meeting interrupted the writing of my answer. It is now complete with a - hopefully - enlightening example.Jarrod

© 2022 - 2024 — McMap. All rights reserved.