Eric Biederman | f3ed1cf | 2004-10-16 08:38:58 +0000 | [diff] [blame] | 1 | #ifndef __I386_DIV64 |
| 2 | #define __I386_DIV64 |
| 3 | |
| 4 | /* |
| 5 | * do_div() is NOT a C function. It wants to return |
| 6 | * two values (the quotient and the remainder), but |
| 7 | * since that doesn't work very well in C, what it |
| 8 | * does is: |
| 9 | * |
| 10 | * - modifies the 64-bit dividend _in_place_ |
| 11 | * - returns the 32-bit remainder |
| 12 | * |
| 13 | * This ends up being the most efficient "calling |
| 14 | * convention" on x86. |
| 15 | */ |
| 16 | #define do_div(n,base) ({ \ |
| 17 | unsigned long __upper, __low, __high, __mod, __base; \ |
| 18 | __base = (base); \ |
| 19 | asm("":"=a" (__low), "=d" (__high):"A" (n)); \ |
| 20 | __upper = __high; \ |
| 21 | if (__high) { \ |
| 22 | __upper = __high % (__base); \ |
| 23 | __high = __high / (__base); \ |
| 24 | } \ |
| 25 | asm("divl %2":"=a" (__low), "=d" (__mod):"rm" (__base), "0" (__low), "1" (__upper)); \ |
| 26 | asm("":"=A" (n):"a" (__low),"d" (__high)); \ |
| 27 | __mod; \ |
| 28 | }) |
| 29 | |
Eric Biederman | f3ed1cf | 2004-10-16 08:38:58 +0000 | [diff] [blame] | 30 | #endif |