简体   繁体   English

为什么要将两个32位整数组合成64位整数?

[英]Why would you combine two 32 bit integers into a 64 bit integer?

Recently I saw how a compiler combined two 32 bit integers which were values of properties of a class and stored them as a 64 bit integer. 最近我看到编译器如何组合两个32位整数,它们是类属性的值并将它们存储为64位整数。 My question now is, why is this done? 我现在的问题是,为什么要这样做? What advantages are there when combining integers? 结合整数有什么好处?

for example if we had the following properties of a class 例如,如果我们有类的以下属性

class FooBar {
 int x = 1;
 int y = 100;
}

so instead of 而不是

i32 = 00000001
i32 = 01100100

We get: 我们得到:

i64 = 0000000101100100

Why would you combine them? 你为什么要把它们合并?

The existing (as I write this) answer and comments, while partially correct, miss the point of this optimization. 现有的(正如我写的那样)答案和评论虽然部分正确,却忽略了这种优化的重点。 It is to replace two instructions (working with 32-bit data) with one instruction (working with 64-bit data). 它是用一条指令替换两条指令(使用32位数据)(使用64位数据)。 This results in a very slight reduction in code size and probably execution time. 这导致代码大小和执行时间的轻微减少。

The compiler is initializing both variables with one 64-bit instruction (since they share consecutive memory addresses). 编译器使用一条64位指令初始化两个变量(因为它们共享连续的内存地址)。 The variables are separate, and will be accessed separately. 变量是分开的,将单独访问。 No shifting or masking is needed. 不需要移位或掩蔽。

This is frequently seen in constructors when many members are initialized. 当许多成员被初始化时,这在构造函数中经常出现。 A frequent case is with zero initialization, where the compiler will zero out a register then use that one value to initialize multiple members, combining writes to consecutive memory addresses with a larger single write (for example, by writing a 16-bit short zero value instead of two 8-bit ones). 一个常见的情况是零初始化,其中编译器将寄存器归零,然后使用该值初始化多个成员,将写入与连续存储器地址组合,具有更大的单个写入(例如,通过写入16位短零值而不是两个8位的)。

I believe you're observing an optimization. 我相信你正在观察优化。 Intel instructions such as PADDSW assume multiple packed operands. 英特尔指令(如PADDSW假定多个打包操作数。

https://en.wikipedia.org/wiki/X86_instruction_listings https://en.wikipedia.org/wiki/X86_instruction_listings

There are also benefits in only using 1 entry in a 64bit architecture cache. 在64位架构缓存中仅使用1个条目也有好处。

There is a cost to unpack if you only want one of the values, but I suspect whatever code optimizer is running estimates there are better savings pack the values. 如果您只需要其中一个值,则需要解压缩成本,但我怀疑无论代码优化器运行的是什么,估计值都会有更好的节省。

It used to be normal to align all members of a C struct onto a word boundary. 以前将C结构的所有成员对齐到单词边界是正常的。 That is a single char and an int would not be packed, but aligned to the word size of the machine. 这是一个单独的charint不会被打包,而是与机器的单词大小对齐。 So, struct { char, int} would have a sizeof(..) of 8 bytes. 因此, struct { char, int}sizeof(..)为8个字节。 I'm guessing that situation flipped? 我猜这种情况有所改变?

Very interesting. 很有意思。

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

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