Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modulo support #9

Open
sesse opened this issue Feb 26, 2016 · 8 comments
Open

Modulo support #9

sesse opened this issue Feb 26, 2016 · 8 comments

Comments

@sesse
Copy link

sesse commented Feb 26, 2016

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
Copy link
Author

sesse commented Feb 26, 2016

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?

@ridiculousfish
Copy link
Owner

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.

@pcbeard
Copy link

pcbeard commented Sep 11, 2017

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.

@bmkessler
Copy link

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

@KingDuckZ
Copy link

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

@DonaldTsang
Copy link

DonaldTsang commented Mar 26, 2019

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

@colinb2r
Copy link

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!

@ridiculousfish
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants