简体   繁体   English

来自stdint.h的快速类型的溢出行为

[英]Overflow behaviour of fast types from stdint.h

C99 and C++11 (and before them POSIX) introduced least and fast types in the stdint header, eg int_fast16_t or uint_fast8_t . C99和C ++ 11(以及POSIX之前的POSIX)在stdint标头中引入了leastfast类型,例如int_fast16_tuint_fast8_t

I wonder what guarantees of the overflow behaviour of these types are given. 我想知道如何保证这些类型的溢出行为。 If these are the same as for "normal" integer types (so that unsigned types wrap around on overflow), I wonder how uint_fast8_t can actually be mapped to any different type than the fixed-witdh uint8_t type, and thus be faster. 如果这些与“普通”整数类型相同(以便无符号类型在溢出时回绕),我想知道uint_fast8_t实际上如何可以映射到与fixed-witdh uint8_t类型不同的任何类型,从而更快。

The overflow rules are the same as for any signed/unsigned integer variable. 溢出规则与任何有符号/无符号整数变量相同。

uint_fast16_t is mapped to the fastest size that is at least 16 bits. uint_fast16_t映射到至少16位的最快大小。 If the fastest size happens to be 32 bits on a platform then the behaviour would be different. 如果最快的大小恰好是平台上的32位,则行为将有所不同。 Consider: 考虑:

uint_fast16_t k = 1 << 16;

if (k == 0) {
    printf("k is 16 bits\n");
} else {
    printf("k is larger than 16 bits\n");
}

If these are the same as for "normal" integer types (so that unsigned types wrap around on overflow) 如果这些与“普通”整数类型相同(以便无符号类型在溢出时环绕)

The guarantees are exactly the same. 保证是完全一样的。 Signed integers will overflow, and unsigned will wrap around. 有符号整数将溢出,而无符号整数将环绕。 The maximum value that can be represented depends on which integer type the the type is an alias of. 可以表示的最大值取决于该类型是别名的整数类型。

I wonder how uint_fast8_t can actually be mapped to any different type than the fixed-witdh uint8_t type 我想知道如何将uint_fast8_t实际映射到与fixed-witdh uint8_t类型不同的任何类型

It can be an alias of any unsigned integer type that is at least 8 bits wide. 它可以是任何至少8位宽的无符号整数类型的别名。

C11 n1570 says that the C11 n1570表示

The typedef name int_fastN_t designates the fastest signed integer type with a width of at least N . typedef名称int_fastN_t指定宽度至少为N的最快带符号整数类型。 The typedef name uint_fastN_t designates the fastest unsigned integer type with a width of at least N . 类型定义名称uint_fastN_t指定最快的无符号整数类型,其宽度至少为N。


Thus no behaviour is guaranteed as such; 因此,不能保证任何行为。 what these say is that int_fastN_t must not have signed overflow in the range 2^(n-1) - 1 ... 2^(n-1) - 1 ; 这些意思是int_fastN_t一定不能在2^(n-1) - 1 ... 2^(n-1) - 1范围内签名溢出; uint_fastN_t must not have a wraparound for values less than 2^n - 1 . 对于小于2^n - 1值, uint_fastN_t不得包含环绕。 If you need a more exact wraparound behaviour, then do not use the fast types, and instead use the exact width types (aka intN_t and uintN_t ). 如果您需要更精确的环绕行为,请不要使用快速类型,而应使用确切的宽度类型(即intN_tuintN_t )。

I wonder how uint_fast8_t can actually be mapped to any different type than the fixed-width uint8_t type, and thus be faster. 我想知道如何将uint_fast8_t实际映射到与固定宽度uint8_t类型不同的任何类型,从而更快。

Firstly, there doesn't have to be a uint8_t type. 首先,不必 uint8_t类型。 On a 36-bit word addressed machine (they have existed), a char would probably be 9 bits wide. 在36位字寻址的机器(它们已经存在)上,一个char可能为9位宽。 ( Word addressed means that the natural way to access memory is in words (of some size). Addressing sub-sections of a word requires shifting and masking, and pointers which address such sub-sections need additional bits to refer to the sub-unit within the word.) 字寻址意味着自然的访问内存的方式是用 (一定大小)。对一个字的子节进行寻址需要移位和屏蔽,而寻址这些子节的指针需要附加的位来引用该子单元。字内。)

On such a machine, the compiler author would have the interesting decision as whether to make uint_fast8_t be the same as unsigned char (9 bits) or the 36-bit unsigned int type. 在这样的机器上,编译器作者会做出一个有趣的决定,即使uint_fast8_tunsigned char (9位)还是36位unsigned int类型相同。

As others have said, there will be an implementation defined maximum of these types, and if you exceed that limit the unsigned types will wrap and the signed types will cause undefined behaviour. 正如其他人所说的,将有一个实现定义的这些类型的最大值,如果超出该限制,则未签名的类型将自动换行,并且已签名的类型将导致未定义的行为。

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

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