为什么快速整数类型比其他整数类型快? [英] Why are the fast integer types faster than the other integer types?

查看:94
本文介绍了为什么快速整数类型比其他整数类型快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ISO / IEC 9899:2018(C18)中,在7.20.1.3中进行了规定:

In ISO/IEC 9899:2018 (C18), it is stated under 7.20.1.3:


7.20.1.3最快的最小值-宽度整数类型

7.20.1.3 Fastest minimum-width integer types

1下列每个类型都指定一个整数类型,该整数类型通常最快( 268)用于所有具有以下类型的整数类型

1 Each of the following types designates an integer type that is usually fastest268) to operate with among all integer types that have at least the specified width.

2 typedef名称 int_fastN_t 指定宽度为at的最快带符号整数类型typedef名称 uint_fastN_t 指定最快的无符号整数类型,其宽度至少为N。

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.

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_t, int_fast16_t, int_fast32_t, int_fast64_t, uint_fast8_t, uint_fast16_t, uint_fast32_t, uint_fast64_t

此表格的所有其他类型都是可选的。

All other types of this form are optional.

268)不能保证指定的类型在所有情况下都是最快的。如果实现没有明确的理由选择一种类型而不是另一种类型,则只需选择一些满足符号性和宽度要求的整数类型。

268) The designated type is not guaranteed to be fastest for all purposes; 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.


  • 为什么这些快速整数类型比其他整数类型快吗?



我用C ++标记了这个问题,因为快速整数类型是也可以在C ++ 17的 cstdint 头文件中找到。不幸的是,在ISO / IEC 14882:2017(C ++ 17)中,没有关于其解释的章节;

I tagged the question with C++, because the fast integer types are also available in C++17 in the header file of cstdint. Unfortunately, in ISO/IEC 14882:2017 (C++17) there is no such section about their explanation; I had implemented that section otherwise in the question´s body.

信息:在C语言中,它们是在 stdint.h 的头文件中声明。

Information: In C, they are declared in the header file of stdint.h.

推荐答案

想象一个仅执行64位算术运算的CPU。现在想象一下如何在这种CPU上实现无符号8位加法。为了获得正确的结果,它必然涉及多个操作。在这种CPU上,64位运算要比其他整数宽度上的运算更快。在这种情况下,所有 Xint_fastY_t 可能都是64位类型的别名。

Imagine a CPU that performs only 64 bit arithmetic operations. Now imagine how you would implement an unsigned 8 bit addition on such CPU. 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. In this situation, all of Xint_fastY_t might presumably be an alias of the 64 bit type.

如果CPU支持窄整数类型的快速运算,因此较宽的类型不会比较窄的类型快,那么 Xint_fastY_t 不会(不应)成为比必需的宽类型的别名表示所有Y位。

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.

出于好奇,我检查了某些体系结构上特定实现(GNU,Linux)的大小。在同一体系结构上的所有实现中,这些都不相同:

Out of curiosity, I checked the sizes on a particular implementation (GNU, Linux) on some architectures. 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.

表格截图,适用于Android用户:

Screenshot of table, for Android users:

(Android中没有框绘字符单色字体-参考

这篇关于为什么快速整数类型比其他整数类型快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆