简体   繁体   English

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

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

So the reason for typedef :ed primitive data types is to abstract the low-level representation and make it easier to comprehend ( uint64_t instead of long long type, which is 8 bytes).所以typedef :ed 原始数据类型的原因是抽象了低级表示并使其更容易理解( uint64_t而不是long long类型,它是 8 个字节)。

However, there is uint_fast32_t which has the same typedef as uint32_t .但是,有uint_fast32_tuint32_t具有相同的typedef Will using the "fast" version make the program faster?使用“快速”版本会使程序更快吗?

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

... there is uint_fast32_t which 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_t is always the same as uint32_t .所以你不能从那里推断出uint_fast32_t总是与uint32_t相同。

See also:也可以看看:

The difference lies in their exact-ness and availability.不同之处在于它们的准确性和可用性。

The doc here says:这里的文档说:

unsigned integer type with width of exactly 8, 16, 32 and 64 bits respectively ( provided only if the implementation directly supports the type ):宽度分别为8、16、32和 64 位的无符号整数类型(仅当实现直接支持该类型时才提供):

 uint8_t uint16_t uint32_t uint64_t

And

fastest unsigned unsigned integer type with width of at least 8, 16, 32 and 64 bits respectively最快的无符号无符号整数类型,宽度分别至少为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_t is a type which has exactly 32 bits, and an implementation should provide it only if it has type with exactly 32 bits, and then it can typedef that type as uint32_t .因此,区别非常明显,即uint32_t是一种恰好具有32位的类型,并且只有当它具有恰好具有32 位的类型时,实现才应提供它,然后它可以将该类型定义为uint32_t This means, uint32_t may or may not be available .这意味着, uint32_t可能可用也可能不可

On the other hand, uint_fast32_t is a type which has at least 32 bits, which also means, if an implementation may typedef uint32_t as uint_fast32_t if it provides uint32_t .另一方面, uint_fast32_t是一种至少有32 位的类型,这也意味着,如果实现可以将uint32_tuint_fast32_t如果它提供uint32_t If it doesn't provide uint32_t , then uint_fast32_t could be a typedef of any type which has at least 32 bits.如果它不提供uint32_t ,则uint_fast32_t可以是至少具有32位的任何类型的 typedef。

When you #include inttypes.h in 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 short and use it several times in the program, which is totally valid.可以这样想:你定义了一个short类型的变量,并在程序中多次使用它,这是完全有效的。 However, the system you're working on might work more quickly with values of type int .但是,您正在使用的系统可能会更快地使用int类型的值。 By defining a variable as type uint_fast*t , the computer simply chooses the most efficient representation that it can work with.通过将变量定义为uint_fast*t类型,计算机只需选择它可以使用的最有效的表示。

If there is no difference between these representations, then the system chooses whichever one it wants, and uses it consistently throughout.如果这些表示之间没有区别,则系统会选择它想要的任何一种,并始终一致地使用它。

Note that the fast version could be larger than 32 bits.请注意,快速版本可能大于 32 位。 While the fast int will fit nicely in a register and be aligned and the like: but, it will use more memory.虽然快速 int 将很好地适合寄存器并对齐等:但是,它将使用更多内存。 If you have large arrays of these your program will be slower due to more memory cache hits and bandwidth.如果你有大量的这些数组,你的程序会因为更多的内存缓存命中和带宽而变慢。

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 位的符号扩展,并且存在更快的“本机”整数格式的想法是老式的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM