Actually, the code you wrote won't generate any division instructions since the compiler can figure out the results at compile time. I wrote a little test program and set the compiler (VC++ 10SP1) to generate an assembly code listing.
#include <iostream>
using namespace std;
struct result {
long quotient, remainder;
};
result divide(long num, long den) {
result d = { num / den, num % den };
return d;
}
int main() {
result d = divide(3, 2);
d = divide(10, 3);
cout << d.quotient << " : " << d.remainder << endl;
return 0;
}
I had to write it this way and explicitly tell the compiler to not inline any functions.
Otherwise the compiler would have happily optimized away most of the code. Here is the resulting assembly code for the divide function.
; 8 : result divide(long num, long den) {
00000 55 push ebp
00001 8b ec mov ebp, esp
; 9 : result d = { num / den, num % den };
00003 99 cdq
00004 f7 7d 08 idiv DWORD PTR _den$[ebp]
; 10 : return d;
; 11 : }
00007 5d pop ebp
00008 c3 ret 0
It's smart enough to generate a single IDIV instruction and use the quotient and remainder generated by it. Modern C and C++ compilers have gotten really good at this sort of optimization. Unless you have a performance problem and have profiled your code to determine where the bottleneck is, don't try to second guess the compiler.