libdivide icon indicating copy to clipboard operation
libdivide copied to clipboard

Modulo support

Open sesse opened this issue 9 years ago • 8 comments

Hi,

Is there a chance to get support for the modulo operation in libdivide, without having to do x - (x / d) * d manually? Ideally, of course, in the same operation as div if it's possible to get it cheap.

On a quick test, it looks like GCC can generate code with only one mul for div+mod of the same divisor, so it should. be possible. (I tested with the rather arbitrary value of 17; div-only was six instructions including a mul, and div+mod was twelve in total, with no additional mul.)

sesse avatar Feb 26 '16 14:02 sesse

Oh doh, of course this is because 17 can be done as a LEA instruction instead of a mul. So perhaps this was too optimistic?

sesse avatar Feb 26 '16 14:02 sesse

Yeah, to my knowledge, there is no way to compute mod faster than dividing, multiplying, and subtracting. Maybe someone will find a way some day.

ridiculousfish avatar Feb 26 '16 21:02 ridiculousfish

Perhaps at least a convenience function that works like std::div() could be provided, so we don't all have to roll our own. I know it's not a lot of code, but it might help.

pcbeard avatar Sep 11 '17 17:09 pcbeard

For 32-bit mod on x64 there is a new mod method that involves two multiplications https://arxiv.org/abs/1902.01961 and is benchmarked faster than the current divide, multiply, and subtract implementation in libdivide. The author's implementation is here: https://github.com/lemire/fastmod

bmkessler avatar Feb 11 '19 23:02 bmkessler

+1 I came here after finding the same project, specifically this post. I hope it's useful.

KingDuckZ avatar Feb 18 '19 22:02 KingDuckZ

Maybe adding the post's solution to libdivide would be the right thing to do? What do you think @ridiculousfish ?

DonaldTsang avatar Mar 26 '19 14:03 DonaldTsang

To ridiculousfish: you may or may not recall that a few years ago we had an email exchange on faster integer division by constant divisors.

"Maybe someone will find a way some day." - Others have already mentioned the recent Lemire-Kaser-Kurz paper, which I only discovered yesterday. What they haven't said is that libdivide is mentioned in their paper, and that in their paper one of their benchmarks for their direct computation of remainders - compared with first calculating the quotient, and secondly the remainder with r = n - q*d - is with libdivide. I'd say that counts as a citation!

colinb2r avatar Nov 13 '19 06:11 colinb2r

Yep, I think we should implement their remainder algorithm in libdivide too!

ridiculousfish avatar Nov 13 '19 21:11 ridiculousfish