j3-fortran / fortran_proposals

Proposals for the Fortran Standard Committee
178 stars 15 forks source link

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

Open marshallward opened 9 months ago

marshallward commented 9 months ago

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.