thanks for the answer. I looked at the hacker enthusiasm. If you look at the divdi3 function in HD format, then the DIVS call, the 64 / 32-> 32 command, when the divider is 32-bit, and the result, as you know, does not overflow. The machine I work on does not have general instructions 64 / 32-> 32, it has a special case mentioned above. The above function for 64 / 32-> 32 is my implementation for DIVU in the unsigned case, so I am trying to work out something similar for DIVS.
I could just forget about the DIVS path, but that in the vast majority of cases, and I want to hit it as much as possible, it's just a complicated implementation.
Sorry for the incomprehensible pseudo-code, but everything is unsigned and mostly 32-bit.
DIVU(uint64 a, uint32 b) { // assume: a / b guaranteed not to overflow // a = 64bit dividend, ah & al are hi & lo 32bits respectively // b = 32bit divisor uint32 q1 = udive(ah, b) // (ah << 32) / b uint32 r1 = -(q1 * b) // remainder of the above, shortcut for (ah << 32) - (q1 * b) since (ah << 32) & 0xffffffff == 0 uint32 q2 = al / b // al / b using regular unsigned division uint32 r2 = al - (q2 * b) // remainder of the above uint32 q = q1 + q2 uint32 r = r1 + r2 // r < r2, r overflowed and is >32bits, implies r > b since b is 32bits // r >= b, quotient too small by 1, adjust if (r < r2) or (r >= b) q = q + 1 return q }
Joon
source share