3

If I am trying to multiply a float by a whole number is it faster to multiply it by a whole number being represented by an integer

int x;
...
float y = 0.5784f * x; //Where x contains a dynamically chosen whole number

or by another float (provided there is no loss in accuracy)

float x;
...
float y = 0.5784f * x; //Where x contains a dynamically chosen and floating point representable whole number

or does it vary greatly between hardware? Is there a common circuit (found in most floating point units) that handles float and integer multiplication or is the general practice for the hardware to first convert the integer into a float and then use a circuit that performs float * float? What if the whole number being represented is extremely small such as a value of 0 or 1 determined dynamically and used to determine whether or not the float is added to a sum without branching?

int x;
...
float y = 0.5784f + 0.3412f * x; //Where x contains either 0 or 1 (determined dynamically).

Thanks for the help in advance.

Ryoku
  • 397
  • 2
  • 16
  • The language requires that the integer is converted to float before multiplication. Though the language also has the "As If" rule: if it can do it better another way (and get the same result) it is allowed to do that. – Martin York Dec 21 '20 at 06:05
  • @MartinYork Very interesting and good to know. By any chance do you know where I can specifications such as that about the language. Not asking you to waste your time sifting through it to find that exact line, just a general location to where language specifications like that exist. – Ryoku Dec 21 '20 at 06:26
  • You can find links to the standard here: https://stackoverflow.com/a/4653479/14065 "AS IF RULE" Section 4.1 [intro.abstract] **Conforming implementations are required to emulate (only) the observable behavior of the abstract machine as explained below**. "ARITHMETIC CONVERSIONS" Section 7.4 [expr.arith.conv] Paragraph 1 Point 4: **Otherwise, if either operand is float, the other shall be converted to float.** – Martin York Dec 21 '20 at 18:50
  • Basically the standard describes how it wants the language to behave. It does not restrict the implementation in any way as long as the observable behavior conforms to the standard. This allows for highly efficient optimizations if the compiler can prove to itself that the observable behavior is unchanged by the optimization. – Martin York Dec 21 '20 at 18:55
  • @MartinYork Thanks for the link, I'm gonna be pouring over this the next time I get stuck, probably get the chance to answer many of my own questions! – Ryoku Dec 21 '20 at 23:55

1 Answers1

3

Is it faster to multiply a float by an integer or another float

In general, float * float is faster, yet I suspect little or no difference. The speed of a program is a result of the entire code, not just this line. Faster here may cost one more in other places.

Trust your compile or get a better compiler to emit code that performs 0.5784f * some_int well.

In the 0.5784f * some_int case, the language obliges some_int to act as if it is converted to float first*1 before the multiplication. But a sharp compiler may known of implementation specific tricks to perform the multiplications better/faster directly without a separate explicit conversion - as long as it gets an allowable result..


In the float y = 0.5784f + 0.3412f * x; //Where x contains either 0 or 1 (determined dynamically). a compile might see that too and take advantage to emit efficient code.


Only in select cases and with experience will you out-guess the compiler. Code for clarity first.

You could always profile different codes/compiler options and compare.


Tip: In my experience, I find more performance gains with a larger view of code than the posted concern - which verges on micro-optimization.


*1 See FLT_EVAL_METHOD for other possibilities.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • Also good to advise they can compile with varying levels of optimization dumping to assembly and just check `:)` – David C. Rankin Dec 21 '20 at 06:08
  • Thanks for the response, and for the follow up. Yeah I know you shouldn't try to beat the compiler, and I'm not really trying to, this is more about advancing my theoretical understanding than practical and increasing my ability to reason through compiler optimized assembly (why certain decisions are made), which is why I thank you again for including what the language obliges. I you could also add a reference to where such rules for the language exist (not necessarily that rule in particular) it would be greatly appreciated, and probably solve many more questions I'll eventually have. – Ryoku Dec 21 '20 at 06:49
  • @Ryoku "add a reference to where such rules for the language exist" is really all in the [C spec.](https://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents). For `*` see § 6.5.5 1 "usual arithmetic conversions are performed on the operands". – chux - Reinstate Monica Dec 21 '20 at 09:05
  • @chux-ReinstateMonica thank you so much for the reference, I wasn't quite sure what to look for – Ryoku Dec 21 '20 at 09:16