Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2000, 2004 Maciej W. Rozycki |
| 3 | * Copyright (C) 2003 Ralf Baechle |
| 4 | * |
| 5 | * This file is subject to the terms and conditions of the GNU General Public |
| 6 | * License. See the file "COPYING" in the main directory of this archive |
| 7 | * for more details. |
| 8 | */ |
| 9 | #ifndef _ASM_DIV64_H |
| 10 | #define _ASM_DIV64_H |
| 11 | |
| 12 | #if (_MIPS_SZLONG == 32) |
| 13 | |
| 14 | #include <asm/compiler.h> |
| 15 | |
| 16 | /* |
| 17 | * No traps on overflows for any of these... |
| 18 | */ |
| 19 | |
| 20 | #define do_div64_32(res, high, low, base) ({ \ |
| 21 | unsigned long __quot, __mod; \ |
| 22 | unsigned long __cf, __tmp, __tmp2, __i; \ |
| 23 | \ |
| 24 | __asm__(".set push\n\t" \ |
| 25 | ".set noat\n\t" \ |
| 26 | ".set noreorder\n\t" \ |
| 27 | "move %2, $0\n\t" \ |
| 28 | "move %3, $0\n\t" \ |
| 29 | "b 1f\n\t" \ |
| 30 | " li %4, 0x21\n" \ |
| 31 | "0:\n\t" \ |
| 32 | "sll $1, %0, 0x1\n\t" \ |
| 33 | "srl %3, %0, 0x1f\n\t" \ |
| 34 | "or %0, $1, %5\n\t" \ |
| 35 | "sll %1, %1, 0x1\n\t" \ |
| 36 | "sll %2, %2, 0x1\n" \ |
| 37 | "1:\n\t" \ |
| 38 | "bnez %3, 2f\n\t" \ |
| 39 | " sltu %5, %0, %z6\n\t" \ |
| 40 | "bnez %5, 3f\n" \ |
| 41 | "2:\n\t" \ |
| 42 | " addiu %4, %4, -1\n\t" \ |
| 43 | "subu %0, %0, %z6\n\t" \ |
| 44 | "addiu %2, %2, 1\n" \ |
| 45 | "3:\n\t" \ |
| 46 | "bnez %4, 0b\n\t" \ |
| 47 | " srl %5, %1, 0x1f\n\t" \ |
| 48 | ".set pop" \ |
| 49 | : "=&r" (__mod), "=&r" (__tmp), "=&r" (__quot), "=&r" (__cf), \ |
| 50 | "=&r" (__i), "=&r" (__tmp2) \ |
| 51 | : "Jr" (base), "0" (high), "1" (low)); \ |
| 52 | \ |
| 53 | (res) = __quot; \ |
| 54 | __mod; }) |
| 55 | |
| 56 | #define do_div(n, base) ({ \ |
| 57 | unsigned long long __quot; \ |
| 58 | unsigned long __mod; \ |
| 59 | unsigned long long __div; \ |
| 60 | unsigned long __upper, __low, __high, __base; \ |
| 61 | \ |
| 62 | __div = (n); \ |
| 63 | __base = (base); \ |
| 64 | \ |
| 65 | __high = __div >> 32; \ |
| 66 | __low = __div; \ |
| 67 | __upper = __high; \ |
| 68 | \ |
| 69 | if (__high) \ |
| 70 | __asm__("divu $0, %z2, %z3" \ |
| 71 | : "=h" (__upper), "=l" (__high) \ |
| 72 | : "Jr" (__high), "Jr" (__base) \ |
| 73 | : GCC_REG_ACCUM); \ |
| 74 | \ |
| 75 | __mod = do_div64_32(__low, __upper, __low, __base); \ |
| 76 | \ |
| 77 | __quot = __high; \ |
| 78 | __quot = __quot << 32 | __low; \ |
| 79 | (n) = __quot; \ |
| 80 | __mod; }) |
| 81 | #endif /* (_MIPS_SZLONG == 32) */ |
| 82 | |
| 83 | #if (_MIPS_SZLONG == 64) |
| 84 | |
| 85 | /* |
| 86 | * Don't use this one in new code |
| 87 | */ |
| 88 | #define do_div64_32(res, high, low, base) ({ \ |
| 89 | unsigned int __quot, __mod; \ |
| 90 | unsigned long __div; \ |
| 91 | unsigned int __low, __high, __base; \ |
| 92 | \ |
| 93 | __high = (high); \ |
| 94 | __low = (low); \ |
| 95 | __div = __high; \ |
| 96 | __div = __div << 32 | __low; \ |
| 97 | __base = (base); \ |
| 98 | \ |
| 99 | __mod = __div % __base; \ |
| 100 | __div = __div / __base; \ |
| 101 | \ |
| 102 | __quot = __div; \ |
| 103 | (res) = __quot; \ |
| 104 | __mod; }) |
| 105 | |
| 106 | /* |
| 107 | * Hey, we're already 64-bit, no |
| 108 | * need to play games.. |
| 109 | */ |
| 110 | #define do_div(n, base) ({ \ |
| 111 | unsigned long __quot; \ |
| 112 | unsigned int __mod; \ |
| 113 | unsigned long __div; \ |
| 114 | unsigned int __base; \ |
| 115 | \ |
| 116 | __div = (n); \ |
| 117 | __base = (base); \ |
| 118 | \ |
| 119 | __mod = __div % __base; \ |
| 120 | __quot = __div / __base; \ |
| 121 | \ |
| 122 | (n) = __quot; \ |
| 123 | __mod; }) |
| 124 | |
| 125 | #endif /* (_MIPS_SZLONG == 64) */ |
| 126 | |
| 127 | #endif /* _ASM_DIV64_H */ |