Here’s what I could dig up myself, for x86-64 doing FP math with SSE2 (not legacy x87 where changing the rounding mode for C++’s truncation semantics was expensive):
-
When I take a look at the generated assembly from clang and gcc, it looks like the cast
int
todouble
, it boils down to one instruction:cvttsd2si
.From
double
toint
it’scvtsi2sd
. (cvtsi2sdl
AT&T syntax forcvtsi2sd
with 32-bit operand-size.)With auto-vectorization, we get
cvtdq2pd
.So I suppose the question becomes: what is the cost of those?
-
These instructions each cost approximately the same as an FP
addsd
plus amovq xmm, r64
(fp <- integer) ormovq r64, xmm
(integer <- fp), because they decode to 2 uops which on the same ports, on mainstream (Sandybridge/Haswell/Sklake) Intel CPUs.The Intel® 64 and IA-32 Architectures Optimization Reference Manual says that cost of the
cvttsd2si
instruction is 5 latency (see Appendix C-16).cvtsi2sd
, depending on your architecture, has latency varying from 1 on Silvermont to more like 7-16 on several other architectures.Agner Fog’s instruction tables have more accurate/sensible numbers, like 5-cycle latency for
cvtsi2sd
on Silvermont (with 1 per 2 clock throughput), or 4c latency on Haswell, with one per clock throughput (if you avoid the dependency on the destination register from merging with the old upper half, like gcc usually does withpxor xmm0,xmm0
).SIMD packed-
float
to packed-int
is great; single uop. But converting todouble
requires a shuffle to change element size. SIMD float/double<->int64_t doesn’t exist until AVX512, but can be done manually with limited range.Intel’s manual defines latency as: “The number of clock cycles that are required for the execution core to complete the execution of all of the μops that form an instruction.” But a more useful definition is the number of clocks from an input being ready until the output becomes ready. Throughput is more important than latency if there’s enough parallelism for out-of-order execution to do its job: What considerations go into predicting latency for operations on modern superscalar processors and how can I calculate them by hand?.
-
The same Intel manual says that an integer
add
instruction costs 1 latency and an integerimul
costs 3 (Appendix C-27). FPaddsd
andmulsd
run at 2 per clock throughput, with 4 cycle latency, on Skylake. Same for the SIMD versions, and for FMA, with 128 or 256-bit vectors.On Haswell,
addsd
/addpd
is only 1 per clock throughput, but 3 cycle latency thanks to a dedicated FP-add unit.
So, the answer boils down to:
1) It’s hardware optimized, and the compiler leverages the hardware machinery.
2) It costs only a bit more than a multiply does in terms of the # of cycles in one direction, and a highly variable amount in the other (depending on your architecture). Its cost is neither free nor absurd, but probably warrants more attention given how easy it is write code that incurs the cost in a non-obvious way.