C++ 什么是 uint_fast32_t,为什么要使用它而不是常规的 int 和 uint32_t?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8500677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:41:15  来源:igfitidea点击:

What is uint_fast32_t and why should it be used instead of the regular int and uint32_t?

c++ctypes

提问by Amumu

So the reason for typedef:ed primitive data types is to abstract the low-level representation and make it easier to comprehend (uint64_tinstead of long longtype, which is 8 bytes).

所以typedef:ed 原始数据类型的原因是抽象低级表示并使其更容易理解(uint64_t而不是long long类型,它是 8 个字节)。

However, there is uint_fast32_twhich has the same typedefas uint32_t. Will using the "fast" version make the program faster?

但是,有uint_fast32_t与 相同typedefuint32_t。使用“快速”版本会使程序更快吗?

回答by ybungalobill

  • intmay be as small as 16 bits on some platforms. It may not be sufficient for your application.
  • uint32_tis not guaranteed to exist. It's an optional typedefthat the implementation must provide iff it has an unsigned integer type of exactly 32-bits. Some have a 9-bit bytes for example, so they don't have a uint32_t.
  • uint_fast32_tstates your intent clearly: it's a type of at least32 bits which is the best from a performance point-of-view. uint_fast32_tmay be in fact 64 bits long. It's up to the implementation.
  • int在某些平台上可能小到 16 位。它可能不足以满足您的应用程序。
  • uint32_t不保证存在。typedef如果它具有恰好 32 位的无符号整数类型,则实现必须提供它是可选的。例如,有些具有 9 位字节,因此它们没有uint32_t.
  • uint_fast32_t清楚地说明您的意图:它是一种至少32 位的类型,从性能的角度来看是最好的。uint_fast32_t实际上可能是 64 位长。这取决于实施。

... there is uint_fast32_twhich has the same typedef as uint32_t...

... 有uint_fast32_tuint32_t...相同的 typedef

What you are looking at is not the standard. It's a particular implementation (BlackBerry). So you can't deduce from there that uint_fast32_tis always the same as uint32_t.

你看的不是标准。这是一个特定的实现(黑莓)。所以你不能从那里推断出uint_fast32_t总是与uint32_t.

See also:

也可以看看:

回答by Nawaz

The difference lies in their exact-ness and availability.

不同之处在于它们的准确性和可用性。

The dochere says:

这里的文档说:

unsigned integer type with width of exactly8, 16, 32 and 64 bits respectively (provided only if the implementation directly supports the type):

uint8_t
uint16_t
uint32_t
uint64_t

宽度分别为8、16、32和 64 位的无符号整数类型(仅当实现直接支持该类型时才提供):

uint8_t
uint16_t
uint32_t
uint64_t

And

fastest unsigned unsigned integer type with width of at least8, 16, 32 and 64 bits respectively

uint_fast8_t
uint_fast16_t
uint_fast32_t
uint_fast64_t    

最快的无符号无符号整数类型,宽度分别至少为8、16、32 和 64 位

uint_fast8_t
uint_fast16_t
uint_fast32_t
uint_fast64_t    

So the difference is pretty much clear that uint32_tis a type which has exactly32bits, and an implementation should provide it only ifit has type with exactly32 bits, and then it can typedef that type as uint32_t. This means, uint32_tmay or may not be available.

因此,区别非常明显,uint32_t即具有精确32位的类型,并且只有当它具有精确为32 位的类型时,实现才应提供它,然后它可以将该类型定义为uint32_t. 这意味着,uint32_t可能可用,也可能不可

On the other hand, uint_fast32_tis a type which has at least32 bits, which also means, if an implementation may typedef uint32_tas uint_fast32_tifit provides uint32_t. If it doesn't provide uint32_t, then uint_fast32_tcould be a typedef of any type which has at least 32bits.

另一方面,uint_fast32_t是一种至少有32 位的类型,这也意味着,如果一个实现可以 typedefuint32_tuint_fast32_t好像它提供uint32_t. 如果它不提供uint32_t,则uint_fast32_t可能是任何类型的 typedef,它至少具有32位。

回答by Harley Sugarman

When you #include inttypes.hin your program, you get access to a bunch of different ways for representing integers.

当你#include inttypes.h在你的程序中时,你可以使用一系列不同的方式来表示整数。

The uint_fast*_t type simply defines the fastest type for representing a given number of bits.

uint_fast*_t 类型只是定义了表示给定位数的最快类型。

Think about it this way: you define a variable of type shortand use it several times in the program, which is totally valid. However, the system you're working on might work more quickly with values of type int. By defining a variable as type uint_fast*t, the computer simply chooses the most efficient representation that it can work with.

可以这样想:你定义了一个类型的变量,short并在程序中多次使用它,这是完全有效的。但是,您正在使用的系统可能会更快地使用 type 值int。通过将变量定义为 type uint_fast*t,计算机只需选择它可以使用的最有效的表示。

If there is no difference between these representations, then the system chooses whichever one it wants, and uses it consistently throughout.

如果这些表示之间没有区别,则系统会选择它想要的任何一种,并始终一致地使用它。

回答by Gil Colgate

Note that the fast version could be larger than 32 bits. While the fast int will fit nicely in a register and be aligned and the like: but, it will use more memory. If you have large arrays of these your program will be slower due to more memory cache hits and bandwidth.

请注意,快速版本可能大于 32 位。虽然快速 int 将很好地适合寄存器并对齐等:但是,它将使用更多内存。如果你有大量的这些数组,你的程序会因为更多的内存缓存命中和带宽而变慢。

I don't think modern CPUS will benefit from fast_int32, since generally the sign extending of 32 to 64 bit can happen during the load instruction and the idea that there is a 'native' integer format that is faster is old fashioned.

我认为现代 CPU 不会从 fast_int32 中受益,因为通常在加载指令期间会发生 32 位到 64 位的符号扩展,并且存在更快的“本机”整数格式的想法是老式的。