Commit c2988baf authored by Randolph Chung's avatar Randolph Chung Committed by Linus Torvalds

[PATCH] fix __div64_32 to do division properly

This fixes the generic __div64_32() to correctly handle divisions by
large 32-bit values (as used by nanosleep() and friends, for example).

It's a simple bit-at-a-time implementation with a reduction of the high
32-bits handled manually.  Architectures that can do 64/32-bit divisions
in hardware should implement their own more efficient versions.
parent d33648ef
...@@ -25,25 +25,34 @@ ...@@ -25,25 +25,34 @@
uint32_t __div64_32(uint64_t *n, uint32_t base) uint32_t __div64_32(uint64_t *n, uint32_t base)
{ {
uint32_t low, low2, high, rem; uint64_t rem = *n;
uint64_t b = base;
low = *n & 0xffffffff; uint64_t res, d = 1;
high = *n >> 32; uint32_t high = rem >> 32;
rem = high % (uint32_t)base;
high = high / (uint32_t)base; /* Reduce the thing a bit first */
low2 = low >> 16; res = 0;
low2 += rem << 16; if (high >= base) {
rem = low2 % (uint32_t)base; high /= base;
low2 = low2 / (uint32_t)base; res = (uint64_t) high << 32;
low = low & 0xffff; rem -= (uint64_t) (high*base) << 32;
low += rem << 16; }
rem = low % (uint32_t)base;
low = low / (uint32_t)base; while ((int64_t)b > 0 && b < rem) {
b = b+b;
*n = low + d = d+d;
((uint64_t)low2 << 16) + }
((uint64_t)high << 32);
do {
if (rem >= b) {
rem -= b;
res += d;
}
b >>= 1;
d >>= 1;
} while (d);
*n = res;
return rem; return rem;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment