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

idiv(a, b): Floor division ("round to negative infinity") #328

Open
marshallward opened this issue Jan 30, 2024 · 0 comments
Open

idiv(a, b): Floor division ("round to negative infinity") #328

marshallward opened this issue Jan 30, 2024 · 0 comments

Comments

@marshallward
Copy link
Contributor

marshallward commented Jan 30, 2024

It is often useful to decompose an integer by factorization:

a = b * (a/b) + a % b

This works fine for a > 0 but gets messy when a is negative, since the math operators in C-like languages tend to either ignore the sign or truncate any fractional bits ("round to zero").

Fortran addresses the modulo issue by including both mod() for C-like modulus, and modulo() for the more integer-friendly handling.

mod(-7,5) = -|mod(7,5)| = -2
modulo(-7,5) = 3

But it is not so easy to compute floor division. To the best of my knowledge, this is the most efficient option.

e = (a - modulo(a,b)) / b

This is also common, though it unnecessarily brings floating point into the calculation.

e = floor(a / real(b))

I suppose these are fine, but they feel unintuitive to me.

I think an intrinsic could be useful here, in the same way that the mod/modulo distinction has been useful. And, perhaps, somewhere there is a CPU which does floor division, and the intrinsic could map directly onto this instruction? Ok, maybe not...

Anyway, it would be very little work to implement and would help readability, if only ever so slightly. Or is this just more intrinsic bloat?

I proposed the name idiv here but maybe not such a great suggestion since x86 idiv chomps the fraction rounds to zero. But it feels like the best option.

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

1 participant