News for hackers. Stuff that matters.
Re:Why is division so much more complex than other arithmeti... by Iulioh on Friday August 11, 2023 @03:22PM (#37092773)
Can you explain what do you mean with:

>It's worth noting that you can generally divide much faster if you know the divisor ahead of time

Division is just multiplication with 1/x so i don't understand

Re:Why is division so much more complex than other arithmeti... by kstrauser on Friday August 11, 2023 @03:24PM (#37092814)
Exactly. If you know you’re going to later be dividing by 23.7, you can precompute 1/23.7 and store that as a constant. Then you can later multiply by it.
Re:Why is division so much more complex than other arithmeti... by MaxMatti on Friday August 11, 2023 @03:24PM (#37092816)
Yes but 1/x is just another division, so it only helps to speed up the final division of you're able to calculate 1/x beforehand.
Re:Why is division so much more complex than other arithmeti... by aidenn0 on Friday August 11, 2023 @03:27PM (#37092851)
For integer division, you may find that rounding is not identical after multiplying by the reciprocal.
Re:Why is division so much more complex than other arithmeti... by Findecanor on Friday August 11, 2023 @04:19PM (#37093521)
Doesn't the risk of lack of precision apply to both floating point and fixed-point? I'd think the reciprocal would need to be exactly represented in the type, or to have more bits than the original type to produce a correct result in the last bit (or two bits?).

Also, with integers, a signed right shift is rounding down (towards negative infinity), whereas the division operator/instruction in many languages/hardware is rounding towards 0.

To adjust the rounding, you'd add the sign-bit to the first fractional bit before shifting the last step. Let's say that 'x' is a signed long, and a signed long has 64 bits, then:

result = ((x >> amount-1) + ((unsigned long)x >> 63)) >> 1;

Re:Why is division so much more complex than other arithmeti... by aidenn0 on Friday August 11, 2023 @05:38PM (#37094360)
> Doesn't the risk of lack of precision apply to both floating point and fixed-point? I'd think the reciprocal would need to be exactly represented in the type, or to have more bits than the original type to produce a correct result in the last bit (or two bits?).

Yes, but this was historically okay on an x87 FPU which had more precise representation than the common external formats.

Re:Why is division so much more complex than other arithmeti... by shmerl on Friday August 11, 2023 @03:25PM (#37092825)
How do you calculate 1/x without using division?
Re:Why is division so much more complex than other arithmeti... by codefreakxff on Friday August 11, 2023 @03:39PM (#37093021)
Not being snarky, at the simplest level by knowing x ahead of time you can change your code from y = n/2 into y = n * 0.5. Not wildly important by itself, but in a loop - maybe… I suspect compilers optimize for that already.
Re:Why is division so much more complex than other arithmeti... by shmerl on Friday August 11, 2023 @03:48PM (#37093131)
I still don't get it. How do you know that 1/x = 0.abc... without performing the division? I mean in general case, not in something matching binary tricks. Such as 1/3 for example. Unless you mean you somehow know the value of 1/x ahead of time. But where does it come from?
Re:Why is division so much more complex than other arithmeti... by pdonis on Friday August 11, 2023 @04:01PM (#37093289)
If you know the divisor x ahead of time you can pre-compute 1/x at compile time, so that your actual compiled code never does the division--only your compiler does. Your actual compiled code just does the multiplication by a pre-computed constant (and the compiled code doesn't have to know that that constant was pre-computed as the reciprocal of x).
Re:Why is division so much more complex than other arithmeti... by shmerl on Friday August 11, 2023 @04:19PM (#37093510)
Ah, thanks for clarifying.
Re:Why is division so much more complex than other arithmeti... by a_e_k on Friday August 11, 2023 @05:03PM (#37093999)
Often, you can amortize a division or reciprocal by calculating it once and then reusing it. Frequently the divisor is dynamic, but reused locally.

For example, if you want to normalize a 3D vector you could do:

    mag = sqrt(x*x + y*y + z*z)
    x /= mag
    y /= mag
    z /= mag
That's three divisions with the same divisor. But you could instead do:

    invMag = 1.0 / sqrt(x*x + y*y + z*z)
    x *= invMag
    y *= invMag
    z *= invMag
There's still a single division (or reciprocal) done here. But you've eliminated at least the other two. (And it's even better if you have an rsqrt function.)
Re:Why is division so much more complex than other arithmeti... by fuzzylightbulb on Friday August 11, 2023 @04:09PM (#37093376)
so basically just precompute all the answers you will need and then just look up the values later?
Re:Why is division so much more complex than other arithmeti... by circuit10 on Friday August 11, 2023 @04:21PM (#37093553)
There’s no lookup needed if the value is constant
Re:Why is division so much more complex than other arithmeti... by IshKebab on Friday August 11, 2023 @03:48PM (#37093133)
He is talking about the fact that compilers can turn division by a constant into multiplications and bit shifts which are much faster.

Here's an example: https://godbolt.org/z/8vG637fb5

It compiles x/6 to some mad multiplication, bitshift and add.

Re:Why is division so much more complex than other arithmeti... by ridiculous_fish on Friday August 11, 2023 @04:02PM (#37093298)
For flooring integer division with a bounded dividend and known constant, the idea is to multiply by a scaled reciprocal. For example with 32 bit division by 3, you might precompute 2^32 / 3, multiply by that, and then flooring-divide by 2*32 (right shift).

It gets tricky because 2^32/3 is not an integer, so you must round it. This introduces error; the idea is to show that the error is wiped out by the flooring divide. I did the monster math here:

https://ridiculousfish.com/blog/posts/labor-of-division-epis...