如何防止 C 语言操作中的位大小提升?

How to Prevent Bit-Size Promotion in C-Language Ops?

我的理解per reading C99 [PDF] is that it declares types (e.g. unsigned int) in terms of rank, range, etc. in such a way that does not mandate a maximum true bit size, although a minimum size is implied. I've read on Whosebug that while unsigned int compiles as uint32_t on 32-bit platforms, it may be promoted to 64-bit on X86-64 platforms. Further, Wikipedia says the Unix/Linux family of operating systems uses 64-bit for long by default. Posts indicate that the main concern is unexpected behavior; performance issues of promoting or not promoting appear to be secondary.

为避免潜在的意外行为,将程序从一个平台移植到另一个平台时(例如将 Windows 应用程序移植到 Linux),是否可以强制编译器(例如 gcc) 不仅要遵守特定类型的某个最大位宽(例如,强制 long 在当前代码的上下文中保证为 32 位,与一般平台大小相反),而且还要强制执行对涉及该限制值的所有数学运算/数学运算中的中间结果有这样的限制

There's been discussion of how to restrict long 按值限制为特定大小,但这似乎仅限于限制值本身,而不是操作。

如果在 X86-64 CPU 上编译的代码中自动提升过程中的 post 关于 int32_t 乘法和意外溢出的提升仍然可能发生 - 按值限制不能确保预期结果/一致性。

我们如何不仅可以限制值,还可以限制操作的中间值?

在与特定类型相关的所有操作中强制执行最大位宽的能力(假设这是可能的)是否因编译器而异,或者它是否以某种方式标准化?

即如果 long 本身是 64 位的,我们可以通过某些预处理器指令等将其强制为 32 位吗?

您在 stdint.h

中有特殊类型
uint8_t, uint16_t, uint32_t & uint64_t - for unsigned
int8_t, int16_t, int32_t & int64_t - for signed

那些类型保证对象的宽度;