Divide and Get Remainder at the same time?

Ben picture Ben · Oct 9, 2010 · Viewed 23.4k times · Source

Apparently, x86 (and probably a lot of other instruction sets) put both the quotient and the remainder of a divide operation in separate registers.

Now, we can probably trust compilers to optimize a code such as this to use only one call to divide:

( x / 6 )
( x % 6 )

And they probably do. Still, do any languages (or libraries, but mainly looking for languages) support giving both the divide and modulo results at the same time? If so, what are they, and What does the syntax look like?

Answer

bta picture bta · Oct 9, 2010

C has div and ldiv. Whether these generate separate instructions for the quotient and remainder will depend on your particular standard library implementation and compiler and optimization settings. Starting with C99, you also have lldiv for larger numbers.