简体   繁体   English

为什么快速整数类型比其他整数类型快?

[英]Why are the fast integer types faster than the other integer types?

In ISO/IEC 9899:2018 (C18), it is stated under 7.20.1.3:在 ISO/IEC 9899:2018 (C18) 中,它在 7.20.1.3 下说明:

7.20.1.3 Fastest minimum-width integer types 7.20.1.3 最快的最小宽度整数类型

1 Each of the following types designates an integer type that is usually fastest 268) to operate with among all integer types that have at least the specified width. 1 以下每种类型都指定了一个整数类型,在所有至少具有指定宽度的整数类型中,该类型通常是最快的268)

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. 2 typedef 名称int_fastN_t指定宽度至少为 N 的最快有符号整数类型。 typedef 名称uint_fastN_t指定宽度至少为 N 的最快无符号整数类型。

3 The following types are required: 3 需要以下类型:

int_fast8_t , int_fast16_t , int_fast32_t , int_fast64_t , uint_fast8_t , uint_fast16_t , uint_fast32_t , uint_fast64_t int_fast8_tint_fast16_tint_fast32_tint_fast64_tuint_fast8_tuint_fast16_tuint_fast32_tuint_fast64_t

All other types of this form are optional.此表格的所有其他类型都是可选的。


268) The designated type is not guaranteed to be fastest for all purposes; 268)不保证指定的类型在所有用途中都是最快的; if the implementation has no clear grounds for choosing one type over another, it will simply pick some integer type satisfying the signedness and width requirements.如果实现没有明确的理由选择一种类型而不是另一种,它只会选择一些满足符号和宽度要求的整数类型。


But it is not stated why these "fast" integer types are faster.但是没有说明为什么这些“快速”整数类型更快。

  • Why are these fast integer types faster than the other integer types?为什么这些快速整数类型比其他整数类型快?

I tagged the question with C++, because the fast integer types are also available in C++17 in the header file of cstdint .我用 C++ 标记了这个问题,因为 C++17 的cstdint头文件中也提供了快速整数类型。 Unfortunately, in ISO/IEC 14882:2017 (C++17) there is no such section about their explanation;不幸的是,在 ISO/IEC 14882:2017 (C++17) 中没有关于它们的解释的这样的部分; I had implemented that section otherwise in the question´s body.我已经在问题正文中以其他方式实施了该部分。


Information: In C, they are declared in the header file of stdint.h .信息:在 C 中,它们在stdint.h的头文件中声明。

Imagine a CPU that performs only 64 bit arithmetic operations.想象一个只执行 64 位算术运算的 CPU。 Now imagine how you would implement an unsigned 8 bit addition on such CPU.现在想象一下如何在这样的 CPU 上实现无符号 8 位加法。 It would necessarily involve more than one operation to get the right result.要获得正确的结果,必然会涉及多个操作。 On such CPU, 64 bit operations are faster than operations on other integer widths.在这样的 CPU 上,64 位操作比其他整数宽度上的操作快。 In this situation, all of Xint_fastY_t might presumably be an alias of the 64 bit type.在这种情况下,所有Xint_fastY_t可能都是 64 位类型的别名。

If a CPU supports fast operations for narrow integer types and thus a wider type is not faster than a narrower one, then Xint_fastY_t will not (should not) be an alias of the wider type than is necessary to represent all Y bits.如果 CPU 支持窄整数类型的快速操作,因此较宽的类型并不比较窄的类型快,那么Xint_fastY_t将不会(不应该)是比表示所有 Y 位所需的更宽类型的别名。

Out of curiosity, I checked the sizes on a particular implementation (GNU, Linux) on some architectures.出于好奇,我检查了某些架构上特定实现(GNU、Linux)的大小。 These are not same across all implementations on same architecture:这些在同一架构上的所有实现中并不相同:

┌────╥───────────────────────────────────────────────────────────┐
│ Y  ║   sizeof(Xint_fastY_t) * CHAR_BIT                         │
│    ╟────────┬─────┬───────┬─────┬────────┬──────┬────────┬─────┤
│    ║ x86-64 │ x86 │ ARM64 │ ARM │ MIPS64 │ MIPS │ MSP430 │ AVR │
╞════╬════════╪═════╪═══════╪═════╪════════╪══════╪════════╪═════╡
│ 8  ║ 8      │ 8   │ 8     │ 32  │ 8      │ 8    │ 16     │ 8   │
│ 16 ║ 64     │ 32  │ 64    │ 32  │ 64     │ 32   │ 16     │ 16  │
│ 32 ║ 64     │ 32  │ 64    │ 32  │ 64     │ 32   │ 32     │ 32  │
│ 64 ║ 64     │ 64  │ 64    │ 64  │ 64     │ 64   │ 64     │ 64  │
└────╨────────┴─────┴───────┴─────┴────────┴──────┴────────┴─────┘

Note that although operations on the larger types may be faster, such types also take more space in cache, and thus using them doesn't necessarily yield better performance.请注意,虽然对较大类型的操作可能更快,但此类类型也会占用更多缓存空间,因此使用它们不一定会产生更好的性能。 Furthermore, one cannot always trust that the implementation has made the right choice in the first place.此外,人们不能总是相信实现首先做出了正确的选择。 As always, measuring is required for optimal results.与往常一样,为了获得最佳结果,需要进行测量。


Screenshot of table, for Android users:表格截图,适用于安卓用户:

上表截图

(Android doesn't have box-drawing characters in the mono font - ref ) (Android 没有单色字体中的框绘图字符 - ref

They aren't, at least not reliably.它们不是,至少不可靠。

The fast types are simply typedefs for regular types, however it is up to the implementation how to define them.快速类型只是常规类型的 typedef,但是如何定义它们取决于实现。 They must be at least the size requested, but they can be larger.它们必须至少达到要求的尺寸,但也可以更大。

It is true that on some architectures some integer types have better performance than others.确实,在某些体系结构上,某些整数类型比其他整数类型具有更好的性能。 For example, early ARM implementations had memory access instructions for 32-bit words and for unsigned bytes, but they did not have instructions for half-words or signed bytes.例如,早期的ARM实现具有针对 32 位字和无符号字节的内存访问指令,但它们没有针对半字或有符号字节的指令。 The half-word and signed-byte instructions were added later, but they still have less flexible addressing options, because they had to be shoehorned into the spare encoding space.后来添加了半字和有符号字节指令,但它们的寻址选项仍然不太灵活,因为它们必须硬塞到备用编码空间中。 Furthermore all the actual data processing instructions on ARM work on words, so in some cases it may be necessary to mask off smaller values after calculation to give correct results.此外,ARM 上的所有实际数据处理指令都对字进行处理,因此在某些情况下,可能需要在计算后屏蔽掉较小的值才能给出正确的结果。

However, there is also the competing concern of cache pressure, even if it takes more instructions to load/store/process a smaller value.然而,缓存压力也存在竞争问题,即使加载/存储/处理较小的值需要更多的指令。 The smaller value may still perform better if it reduces the number of cache misses.如果它减少缓存未命中的数量,较小的值可能仍然表现得更好。

The definitions of the types on many common platforms do not seem to have been thought through.许多常见平台上的类型定义似乎没有经过深思熟虑。 In particular, modern 64-bit platforms tend to have good support for 32-bit integers, yet the "fast" types are often unnecessarily 64-bit on these platforms.特别是,现代 64 位平台往往对 32 位整数有很好的支持,但“快速”类型在这些平台上通常是不必要的 64 位。

Furthermore, types in C become part of the platform's ABI.此外,C 中的类型成为平台 ABI 的一部分。 So even if a platform vendor discovers they made dumb choices, it is difficult to change those dumb choices later.因此,即使平台供应商发现他们做出了愚蠢的选择,以后也很难改变这些愚蠢的选择。

Ignore the "fast" types.忽略“快速”类型。 If you are really concerned about integer performance, benchmark your code with all the available sizes.如果您真的关心整数性能,请使用所有可用大小对您的代码进行基准测试。

The fast types are not faster than all other integer types -- they are in fact identical to some "normal" integer type (they're just an alias for that type) -- whichever type happens to be the fastest for holding a value of at least that many bits.快速类型并不比所有其他整数类型快——它们实际上某些“普通”整数类型相同(它们只是该类型的别名)——无论哪种类型恰好是持有值的最快的至少有那么多位。

It just platform-dependent which integer type each fast type is an alias for.它只是平台相关的每个快速类型是哪个整数类型的别名。

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

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