[英]Array initialization optimization
When compiling the following code snippet (clang x86-64 -O3
) 编译以下代码片段时(clang x86-64
-O3
)
std::array<int, 5> test()
{
std::array<int, 5> values {{0, 1, 2, 3, 4}};
return values;
}
It produced the typical assembly that I would expect 它产生了我期望的典型装配
test(): # @test()
mov rax, rdi
mov ecx, dword ptr [rip + .L__const.test().values+16]
mov dword ptr [rdi + 16], ecx
movups xmm0, xmmword ptr [rip + .L__const.test().values]
movups xmmword ptr [rdi], xmm0
ret
.L__const.test().values:
.long 0 # 0x0
.long 1 # 0x1
.long 2 # 0x2
.long 3 # 0x3
.long 4 # 0x4
However for small arrays, it seems to have figured out a trick? 然而对于小型阵列,似乎已经找到了一个技巧?
std::array<int, 3> test()
{
std::array<int, 3> values {{0, 1, 2}};
return values;
}
This was the corresponding assembly 这是相应的装配
test(): # @test()
movabs rax, 4294967296
mov edx, 2
ret
Where did that magic number ( 4294967296
) come from? 这个神奇的数字(
4294967296
)来自哪里? Is that essentially a value that can be reinterpret_cast
back into an array of int
somehow? 这本质上是一个值,可以以某种方式
reinterpret_cast
为一个int
数组吗?
A std::array<int, 3>
is 96 bits wide on your implementation. std::array<int, 3>
在您的实现上是96位宽。 As such the ABI declares that it should be returned in RAX + the low 32 bits of RDX (aka EDX). 因此,ABI声明它应该在RAX + RDX的低32位(也称为EDX)中返回。
4294967296 is 2 32 , in hex it is $1'0000'0000
. 4294967296是2 32 ,十六进制是
$1'0000'0000
。 So the movabs
stores 0 in the low order 32 bits of RAX, and 1 in the high order bits of RAX. 因此,
movabs
在RAX的低位32位存储0,在RAX的高位位存储1。 The mov
stores 2 in EDX (which is exactly what you wanted). mov
在EDX中存储了2个(这正是你想要的)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.