Così ho due funzioni, si getta solo double
-int64_t
, le altre chiamate std::round
:doppia intesa per int64_t conversione
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;
}
Essi funzionano correttamente: cast(3.64)
= 3
e round(3.64)
= 4
. Ma quando guardo l'assemblea, sembrano fare la stessa cosa. Quindi mi chiedo come ottengono risultati diversi?
$ 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>
io non sono sicuro di quello che la finalità di tale callq
on line 14
è per, ma, anche con quello, my_cast
e my_round
sembrano essere solo facendo un cvttsd2si
che, credo è la conversione con troncamento.
Tuttavia, le due funzioni, come ho già detto in precedenza, producono diversi valori (corrette) sullo stesso ingresso (diciamo 3.64
)
Che cosa sta accadendo?
Si noti che round (x) = trunc (x + 0,5). Sospetto che tu non abbia identificato correttamente tutto il codice della macchina qui. –
GCC 5.3.0 produce una chiamata a 'round' https://gcc.godbolt.org/ @ Cheersandhth.-Alf è vero solo per valori non negativi –
' callq 19' è un riferimento non ancora risolto, ma 'std: : round'. Sarà compilato quando l'oggetto è collegato. –