2016-04-30 11:51 GMT+02:00 Ola Røer Thorsen <o...@silentwings.no>: > > 2016-04-30 10:46 GMT+02:00 Jean-Michaël Celerier > <jeanmichael.celer...@gmail.com>: >> >> https://godbolt.org/g/DqBlFG >> >> With gcc -O1 >> >> float f(float x) >> { >> return 2. * x; >> } >> >> becomes >> >> f(float): >> addss %xmm0, %xmm0 >> ret >> >> >> and >> >> float g (float x) >> { >> return 2.f * x; >> } >> >> becomes >> >> g(float): >> addss %xmm0, %xmm0 >> ret >> > > This is on x86. I was talking about ARM with single-precision FPUs and the > general advice not to use double precision constants if possible there.
Also, Jean-Michaël's example is a little unfortunate since the compiler is smart enough to convert the multiplication by 2 to an addss instruction. Changing the constant to 2.1 is more interesting (https://godbolt.org/g/Cgw2cR): f(float): cvtss2sd %xmm0, %xmm0 mulsd .LC0(%rip), %xmm0 cvtsd2ss %xmm0, %xmm0 ret g(float): mulss .LC1(%rip), %xmm0 ret .LC0: .long 3435973837 .long 1073794252 .LC1: .long 1074161254 So conversion to double-precision and then back to single-precision in the code for f. For reference, here's the result with ARM GCC 4.5.3: https://godbolt.org/g/r7Kf1H Elvis > > This is what GCC itself says in the documentation for -Wdouble-promotion: > > -Wdouble-promotion (C, C++, Objective-C and Objective-C++ only)Give a > warning when a value of type float is implicitly promoted to double. CPUs > with a 32-bit “single-precision” floating-point unit implement float in > hardware, but emulate double in software. On such a machine, doing > computations using double values is much more expensive because of the > overhead required for software emulation. > > It is easy to accidentally do computations with double because > floating-point literals are implicitly of type double. For example, in: > > float area(float radius) > { > return 3.14159 * radius * radius; > } > > the compiler performs the entire computation with double because the > floating-point literal is a double. > > > > _______________________________________________ > Interest mailing list > Interest@qt-project.org > http://lists.qt-project.org/mailman/listinfo/interest > _______________________________________________ Interest mailing list Interest@qt-project.org http://lists.qt-project.org/mailman/listinfo/interest