-1

I looked inside the header file <stdint.h> on my implementation. I see the following:

typedef long int int_fast16_t;
typedef long int int_fast32_t;
typedef long int int_fast64_t;

I have a 64-bit system, so a long int occupies 64 bits. Why are all three data types typedefed as being long ints? I understand the case of the int_fast64_t, which is 64 bits. But why are the 16 and 32-bit data types having 64 bits? Is this some kind of error? I created a small program to check if this is the case:

sizeof(int_fast8_t) : 1
sizeof(int_fast16_t) : 8
sizeof(int_fast32_t) : 8
sizeof(int_fast64_t) : 8

Are the sizes of these data types implementation defined? What features or characteristics defines a data type as being "fast"? Is it the speed that chunks of data get loaded from the RAM to the CPU? If int_fast16_t and int_fast32_t are 8 bytes wide, what are the benefits in performance? Is it really faster to access a 64-bit data type on a 64-bit system?

Galaxy
  • 2,363
  • 2
  • 25
  • 59
  • Because that’s the fastest at least 16but datatype on that platform. It fits in a register so it can be handled efficiently. – Sami Kuhmonen Jul 04 '18 at 17:07
  • 3
    Haven't you answered your own question? the _`fast` thing is telling the compiler to pick the most "convenient" type for the architecture. Yours is 64-bit. So it is piking 64 bit type. – Eugene Sh. Jul 04 '18 at 17:07
  • 2
    "Fixed" bit widths have nothing to do with this. The sizes are *guaranteed* to be *at least* that size by the official specifications. However, there are many, many CPUs that do not natively have (for example) 8-bit sized registers or memory cells. In such cases, the compiler has to provide code to make `unsigned char c=255; c++;` appear as if it *is* just 8 bits wide. – Jongware Jul 04 '18 at 17:13
  • 2
    `intN_t ` types have fixed size if they're available. Fast and least types are always available and implementation defined. – Petr Skocik Jul 04 '18 at 17:14
  • @SamiKuhmonen: It's not. This is just a bug in the gcc/glibc definitions where somebody did not think through the consequences. 16-bit types are the same speed for basically everything, and **a lot faster** for division or use in large arrays (where larger type will blow away the cache). `int_fastN_t` should never be defined as anything but `int_leastN_t` except on archs that completely lack N-bit store/load operations. – R.. GitHub STOP HELPING ICE Jul 04 '18 at 19:35
  • Is a 64-bit data type the fastest integer data type on a 64-bit system? Is a 16-bit data type really slower? What do you want by the term that a data type is "fast"? Is it faster to load a [u]int_fastN_t data type from the main memory into the CPU than a regular data type? – Galaxy Jul 04 '18 at 19:43
  • The question is good except for the **last sentence**. Could we remove the **last sentence** and ask why is `int_fast32_t` 8 bytes when `int` is 4 bytes on GCC Linux x86-64? Because as far as I know it really isn't fastest! – Antti Haapala -- Слава Україні Jul 04 '18 at 20:14
  • @AnttiHaapala: I assume that `int` is 4 bytes on GCC Linux x86-64 because of tradition. Experienced programmers were used to `int` being 4 bytes, and they wanted to keep it that way to prevent code from potentially breaking. Because then each array takes up twice as much space. Backwards compatibility was probably chosen over speed of accessing the data type. A 64-bit int may also cause issues in structure padding. https://www.youtube.com/watch?v=TtAsN0ptKcw&index=37&list=PL0oekSefhQVJdk0hSRu6sZ2teWM740NtL – Galaxy Jul 04 '18 at 20:21

1 Answers1

5

These types are not a fixed size. They are the type that is at least the given size that will tend to be fastest.

These types are defined in section 7.20.1.3 of the C standard:

1 Each of the following types designates an integer type that is usually fastest to operate with among all integer types that have at least the specified width.

2 The typedef name int_fastN_t designates the fastest signed integer type with a width of at least N. The typedef name uint_fastN_t designates the fastest unsigned integer type with a width of at least N.

dbush
  • 205,898
  • 23
  • 218
  • 273