Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2000, 2004 Maciej W. Rozycki |
Stephen Hemminger | 3927f2e | 2007-03-25 19:54:23 -0700 | [diff] [blame] | 3 | * Copyright (C) 2003, 07 Ralf Baechle (ralf@linux-mips.org) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Stephen Hemminger | 3927f2e | 2007-03-25 19:54:23 -0700 | [diff] [blame] | 12 | #include <linux/types.h> |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #if (_MIPS_SZLONG == 32) |
| 15 | |
| 16 | #include <asm/compiler.h> |
| 17 | |
| 18 | /* |
| 19 | * No traps on overflows for any of these... |
| 20 | */ |
| 21 | |
| 22 | #define do_div64_32(res, high, low, base) ({ \ |
Atsushi Nemoto | 7e95a01 | 2007-07-11 00:24:14 +0900 | [diff] [blame] | 23 | unsigned long __quot32, __mod32; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | unsigned long __cf, __tmp, __tmp2, __i; \ |
| 25 | \ |
| 26 | __asm__(".set push\n\t" \ |
| 27 | ".set noat\n\t" \ |
| 28 | ".set noreorder\n\t" \ |
| 29 | "move %2, $0\n\t" \ |
| 30 | "move %3, $0\n\t" \ |
| 31 | "b 1f\n\t" \ |
| 32 | " li %4, 0x21\n" \ |
| 33 | "0:\n\t" \ |
| 34 | "sll $1, %0, 0x1\n\t" \ |
| 35 | "srl %3, %0, 0x1f\n\t" \ |
| 36 | "or %0, $1, %5\n\t" \ |
| 37 | "sll %1, %1, 0x1\n\t" \ |
| 38 | "sll %2, %2, 0x1\n" \ |
| 39 | "1:\n\t" \ |
| 40 | "bnez %3, 2f\n\t" \ |
| 41 | " sltu %5, %0, %z6\n\t" \ |
| 42 | "bnez %5, 3f\n" \ |
| 43 | "2:\n\t" \ |
| 44 | " addiu %4, %4, -1\n\t" \ |
| 45 | "subu %0, %0, %z6\n\t" \ |
| 46 | "addiu %2, %2, 1\n" \ |
| 47 | "3:\n\t" \ |
| 48 | "bnez %4, 0b\n\t" \ |
| 49 | " srl %5, %1, 0x1f\n\t" \ |
| 50 | ".set pop" \ |
Atsushi Nemoto | 7e95a01 | 2007-07-11 00:24:14 +0900 | [diff] [blame] | 51 | : "=&r" (__mod32), "=&r" (__tmp), \ |
| 52 | "=&r" (__quot32), "=&r" (__cf), \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | "=&r" (__i), "=&r" (__tmp2) \ |
| 54 | : "Jr" (base), "0" (high), "1" (low)); \ |
| 55 | \ |
Atsushi Nemoto | 7e95a01 | 2007-07-11 00:24:14 +0900 | [diff] [blame] | 56 | (res) = __quot32; \ |
| 57 | __mod32; }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | #define do_div(n, base) ({ \ |
| 60 | unsigned long long __quot; \ |
| 61 | unsigned long __mod; \ |
| 62 | unsigned long long __div; \ |
| 63 | unsigned long __upper, __low, __high, __base; \ |
| 64 | \ |
| 65 | __div = (n); \ |
| 66 | __base = (base); \ |
| 67 | \ |
| 68 | __high = __div >> 32; \ |
| 69 | __low = __div; \ |
| 70 | __upper = __high; \ |
| 71 | \ |
| 72 | if (__high) \ |
| 73 | __asm__("divu $0, %z2, %z3" \ |
| 74 | : "=h" (__upper), "=l" (__high) \ |
| 75 | : "Jr" (__high), "Jr" (__base) \ |
| 76 | : GCC_REG_ACCUM); \ |
| 77 | \ |
| 78 | __mod = do_div64_32(__low, __upper, __low, __base); \ |
| 79 | \ |
| 80 | __quot = __high; \ |
| 81 | __quot = __quot << 32 | __low; \ |
| 82 | (n) = __quot; \ |
| 83 | __mod; }) |
Stephen Hemminger | 3927f2e | 2007-03-25 19:54:23 -0700 | [diff] [blame] | 84 | |
| 85 | extern uint64_t div64_64(uint64_t dividend, uint64_t divisor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | #endif /* (_MIPS_SZLONG == 32) */ |
| 87 | |
| 88 | #if (_MIPS_SZLONG == 64) |
| 89 | |
| 90 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | * Hey, we're already 64-bit, no |
| 92 | * need to play games.. |
| 93 | */ |
| 94 | #define do_div(n, base) ({ \ |
| 95 | unsigned long __quot; \ |
| 96 | unsigned int __mod; \ |
| 97 | unsigned long __div; \ |
| 98 | unsigned int __base; \ |
| 99 | \ |
| 100 | __div = (n); \ |
| 101 | __base = (base); \ |
| 102 | \ |
| 103 | __mod = __div % __base; \ |
| 104 | __quot = __div / __base; \ |
| 105 | \ |
| 106 | (n) = __quot; \ |
| 107 | __mod; }) |
| 108 | |
Stephen Hemminger | 3927f2e | 2007-03-25 19:54:23 -0700 | [diff] [blame] | 109 | static inline uint64_t div64_64(uint64_t dividend, uint64_t divisor) |
| 110 | { |
| 111 | return dividend / divisor; |
| 112 | } |
| 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | #endif /* (_MIPS_SZLONG == 64) */ |
| 115 | |
| 116 | #endif /* _ASM_DIV64_H */ |