所以我有两个函数,一个是从double
投射到int64_t
,另一个是std::round
:
std::int64_t my_cast(double d)
{
auto t = static_cast<std::int64_t>(d);
return t;
}
std::int64_t my_round(double d)
{
auto t = std::round(d);
return t;
}
他们正常工作:cast(3.64)
= 3
和round(3.64)
= 4
。但是,当我看到集会时,他们似乎在做同样的事情。所以我想知道他们如何得到不同的结果?
$ g++ -std=c++1y -c -O3 ./round.cpp -o ./round.o
$ objdump -dS ./round.o
./round.o: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <_Z7my_castd>:
0: f2 48 0f 2c c0 cvttsd2si %xmm0,%rax
5: c3 retq
6: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
d: 00 00 00
0000000000000010 <_Z8my_roundd>:
10: 48 83 ec 08 sub $0x8,%rsp
14: e8 00 00 00 00 callq 19 <_Z7my_castd+0x19> <========!!!
19: 48 83 c4 08 add $0x8,%rsp
1d: f2 48 0f 2c c0 cvttsd2si %xmm0,%rax
22: c3 retq
Disassembly of section .text.startup:
0000000000000030 <_GLOBAL__sub_I__Z7my_castd>:
30: 48 83 ec 08 sub $0x8,%rsp
34: bf 00 00 00 00 mov $0x0,%edi
39: e8 00 00 00 00 callq 3e <_GLOBAL__sub_I__Z7my_castd+0xe>
3e: ba 00 00 00 00 mov $0x0,%edx
43: be 00 00 00 00 mov $0x0,%esi
48: bf 00 00 00 00 mov $0x0,%edi
4d: 48 83 c4 08 add $0x8,%rsp
51: e9 00 00 00 00 jmpq 56 <_Z8my_roundd+0x46>
我不确定callq
上14
的目的是什么,但即便如此,my_cast
和my_round
似乎只是在做cvttsd2si
我认为是截断转换。
然而,这两个函数,如我前面提到的,在同一输入上产生不同的(正确的)值(比如说3.64
)
发生了什么事?
答案 0 :(得分:18)
程序集输出更有用(g++ ... -S && cat round.s
):
...
_Z7my_castd:
.LFB225:
.cfi_startproc
cvttsd2siq %xmm0, %rax
ret
.cfi_endproc
...
_Z8my_roundd:
.LFB226:
.cfi_startproc
subq $8, %rsp
.cfi_def_cfa_offset 16
call round <<< This is what callq 19 means
addq $8, %rsp
.cfi_def_cfa_offset 8
cvttsd2siq %xmm0, %rax
ret
.cfi_endproc
如您所见,my_round
调用std::round
然后执行cvttsd2siq
指令。这是因为std::round(double)
会返回double
,因此其结果仍然必须转换为int64_t
。这就是cvttsd2siq
在你的两个职能中所做的事情。
答案 1 :(得分:18)
使用g ++,您可以使用-fdump-tree-optimized
开关更高级别地查看正在发生的事情:
$ g++ -std=c++1y -c -O3 -fdump-tree-optimized ./round.cpp
生成round.cpp.165t.optimized
文件:
;; Function int64_t my_cast(double) (_Z7my_castd, funcdef_no=224, decl_uid=4743$
int64_t my_cast(double) (double d)
{
long int t;
<bb 2>:
t_2 = (long int) d_1(D);
return t_2;
}
;; Function int64_t my_round(double) (_Z8my_roundd, funcdef_no=225, decl_uid=47$
int64_t my_round(double) (double d)
{
double t;
int64_t _3;
<bb 2>:
t_2 = round (d_1(D));
_3 = (int64_t) t_2;
return _3;
}
这里的差异非常明显(以及对round
函数的眩目的调用)。
答案 2 :(得分:12)
使用objdump -d
转储目标文件时,添加选项-r
非常重要,该选项命令该实用程序也转储重定位:
$ objdump -dr round.o
...
0000000000000010 <_Z8my_roundd>:
10: 48 83 ec 28 sub $0x28,%rsp
14: e8 00 00 00 00 callq 19 <_Z8my_roundd+0x9>
15: R_X86_64_PC32 _ZSt5roundd
19: 48 83 c4 28 add $0x28,%rsp
1d: f2 48 0f 2c c0 cvttsd2si %xmm0,%rax
现在,请注意出现的新行。这是一个体现在目标文件中的重定位指令。它指示链接器将_Z8my_roundd+0x9
和_ZSt5roundd
之间的距离添加到偏移量为15的值。
偏移量14处的e8
是相对呼叫的操作代码。以下4个字节必须包含被调用函数的IP相对偏移量(执行时指向下一条指令的IP)。因为编译器无法知道该距离,所以它会使其填充零并插入重定位,以便链接器可以在以后填充它。
在没有-r
选项的情况下进行反汇编时,重定位会被忽略,这会产生错误,即函数_Z8my_roundd
会调用它自己的中间位置。