I am aware of the errors that can occur when doing conversions between floating point numbers and integers, but what about performance (please disregard the accuracy issues)?
Does performance, in general, suffer if I do n-ary operations on operands of differing arithmetic types, that is, on differing floating point types (e.g. float and double) and floating point/integer type combinations (e.g. float and int)? Do there exist rules of thumb, such as, to keep all operands the same type?
P.S.: I am asking because I’m writing an expression template library and would like to know whether to allow binary operations on vectors containing values of differing arithmetic types.
I suspect the answer to this question is going to vary by target architecture, because the conversions can (but might not) to occur in hardware. For example, consider the following code, which causes some interconversions between int and float:
When I tried to compile this with g++ (I’m on Ubuntu, x86) with default settings, and used gdb to disassemble:
Note the instructions with a cvt-prefixed mnemonic. These are conversion instructions. So in this case, the conversion is happening in hardware in a handful of instructions. So, depending on how many cycles these instructions cost, it could be reasonably fast. But again, a different architecture (or different compiler) could change the story.
Edit: On a fun side note, there’s an extra conversion in there due to me accidentally specifying 0.5 instead of 0.5f. That’s why the cvtpd2ps op is in there.
Edit: x86 has had FP support for a long time (since the 80s), so C++ compilers targeting x86 will generally make use of the hardware (unless the compiler is seriously behind the times). Thanks Hot Licks for pointing this out.