more changes on original files
[linux-2.4.git] / include / asm-mips / div64.h
1 /*
2  * Copyright (C) 2000, 2004  Maciej W. Rozycki
3  *
4  * This file is subject to the terms and conditions of the GNU General Public
5  * License.  See the file "COPYING" in the main directory of this archive
6  * for more details.
7  */
8 #ifndef _ASM_DIV64_H
9 #define _ASM_DIV64_H
10
11 #include <asm/compiler.h>
12
13 /*
14  * No traps on overflows for any of these...
15  */
16
17 #define do_div64_32(res, high, low, base) ({ \
18         unsigned long __quot, __mod; \
19         unsigned long __cf, __tmp, __tmp2, __i; \
20         \
21         __asm__(".set   push\n\t" \
22                 ".set   noat\n\t" \
23                 ".set   noreorder\n\t" \
24                 "move   %2, $0\n\t" \
25                 "move   %3, $0\n\t" \
26                 "b      1f\n\t" \
27                 " li    %4, 0x21\n" \
28                 "0:\n\t" \
29                 "sll    $1, %0, 0x1\n\t" \
30                 "srl    %3, %0, 0x1f\n\t" \
31                 "or     %0, $1, %5\n\t" \
32                 "sll    %1, %1, 0x1\n\t" \
33                 "sll    %2, %2, 0x1\n" \
34                 "1:\n\t" \
35                 "bnez   %3, 2f\n\t" \
36                 " sltu  %5, %0, %z6\n\t" \
37                 "bnez   %5, 3f\n" \
38                 "2:\n\t" \
39                 " addiu %4, %4, -1\n\t" \
40                 "subu   %0, %0, %z6\n\t" \
41                 "addiu  %2, %2, 1\n" \
42                 "3:\n\t" \
43                 "bnez   %4, 0b\n\t" \
44                 " srl   %5, %1, 0x1f\n\t" \
45                 ".set   pop" \
46                 : "=&r" (__mod), "=&r" (__tmp), "=&r" (__quot), "=&r" (__cf), \
47                   "=&r" (__i), "=&r" (__tmp2) \
48                 : "Jr" (base), "0" (high), "1" (low)); \
49         \
50         (res) = __quot; \
51         __mod; })
52
53 #define do_div(n, base) ({ \
54         unsigned long long __quot; \
55         unsigned long __mod; \
56         unsigned long long __div; \
57         unsigned long __upper, __low, __high, __base; \
58         \
59         __div = (n); \
60         __base = (base); \
61         \
62         __high = __div >> 32; \
63         __low = __div; \
64         __upper = __high; \
65         \
66         if (__high) \
67                 __asm__("divu   $0, %z2, %z3" \
68                         : "=h" (__upper), "=l" (__high) \
69                         : "Jr" (__high), "Jr" (__base) \
70                         : GCC_REG_ACCUM); \
71         \
72         __mod = do_div64_32(__low, __upper, __low, __base); \
73         \
74         __quot = __high; \
75         __quot = __quot << 32 | __low; \
76         (n) = __quot; \
77         __mod; })
78
79 #endif /* _ASM_DIV64_H */