Some code that rounds up the division to demonstrate (C-syntax):
#define SINT64 long long int
#define SINT32 long int
SINT64 divRound(SINT64 dividend, SINT6
Here's my really naive solution. Your mileage may vary.
Keep a sign bit, which is sign(dividend) ^ sign(divisor)
. (Or *
, or /
, if you're storing your sign as 1 and -1, as opposed to false and true. Basically, negative if either one is negative, positive if none or both are negative.)
Then, call the unsigned division function on the absolute values of both. Then tack the sign back onto the result.
P.S. That is actually how __divdi3
is implemented in libgcc2.c
(from GCC 4.2.3, the version that's installed on my Ubuntu system). I just checked. :-)