2

I am writing a function to affect the feel of a control and found that dividing by 15.9 was about right. My natural instinct is to change this to a divide by 16 because that can be optimised by the compiler to a shift operation. In this case however the value is a float. Are there optimal divisors in float division?

2 Answers 2

2

Why not multiply by 0.0628930818 instead? If dividing by 15.9 is "about right", then multiplying by the reciprocal although possibly not perfectly accurate to the last decimal will still be "about right". An optimizing compiler might possibly even do this automatically, if some "permissive math" flags are used (normally, this is strictly not allowable, since it may lose some precision).

It's more accurate than dividing by 16, anyway. And, it's not as much of a hack as tampering with the exponent bits.

4
  • I am not interested in accuracy for this job. I am adjusting a value as the user turns a control and this value is used in the acceleration calculation. A value of 15 or 17 works just as well. I don't imagine that in the processor there is much difference between a divide or a multiply by the reciprocal. I'm also not interested in tampering with the internals of a float, I'm just wondering if I choose a good divisor if I save machine cycles.
    – Ant
    Oct 9, 2012 at 13:28
  • 3
    Well, a multiply is roughly the same as addition or subtraction, usually around 3-4 cycles on mainstream desktop CPUs, whereas division is rather around 40 cycles. Insofar, if perfect accuracy is not necessary (it's 99.9% accurate anyway) but saving machine cycles is, I'd definitively multiply by the reciprocal. On the other hand, using a value of e.g. 16.0 does not have any benefits because it is none faster using floating point math, and anything else (converting to and from fixed-point and shifting, tampering with exponent bits, etc.) is much, much more expensive.
    – Damon
    Oct 9, 2012 at 14:23
  • In SSE, tampering with the exponents is fast if done unsafely
    – harold
    Oct 9, 2012 at 16:10
  • Actually, since neither 15.9 nor its reciprocal can be represented the discussion concerning accuracy boils down to if one can be represented more accurately than the other. In that context the 10x execution time of a division may be the deciding factor to use the reciprocal and multiply. Oct 9, 2012 at 18:36
0

Dividing a float by 16 is pretty easy too. You just have to decrement the exponent by 4. But I don't think the compiler does any optimization here, because then it would also have to check for underflow.

To be clear: I'm not proposing to directly manipulate the exponent bits. I just wanted to indicate that the potentially fastest divisors for float are also the powers of two.

2
  • Hi Henrik, I thought of that too but as it throws away precision I assume the compiler does not do that.
    – Ant
    Oct 9, 2012 at 13:24
  • 2
    As long as you are enough powers of two away from an underflow this method is fast and safe and does not lose precision (or "throws away precision" as you put it). The question of course is if the exponent - at its smallest - will always be large enough so that an underflow will never occur. Oct 9, 2012 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.