Debian Codegraph @

src/common/div64.h

raw symbols SCIP
1 #ifndef _ASM_GENERIC_DIV64_H
2 #define _ASM_GENERIC_DIV64_H
3 /*
4 * Copyright (C) 2003 Bernardo Innocenti <[email protected]>
5 * Based on former asm-ppc/div64.h and asm-m68knommu/div64.h
6 *
7 * The semantics of do_div() are:
8 *
9 * u32 do_div(UINT64 *n, u32 base)
10 * {
11 * u32 remainder = *n % base;
12 * *n = *n / base;
13 * return remainder;
14 * }
15 *
16 * NOTE: macro parameter n is evaluated multiple times,
17 * beware of side effects!
18 */
19
20 //#include <linux/types.h>
21 #include <comdef.h>
22
23 static unsigned int __div64_32(UINT64 *n, unsigned int base)
24 {
25 UINT64 rem = *n;
26 UINT64 b = base;
27 UINT64 res, d = 1;
28 unsigned int high = rem >> 32;
29
30 /* Reduce the thing a bit first */
31 res = 0;
32 if (high >= base) {
33 high /= base;
34 res = (UINT64) high << 32;
35 rem -= (UINT64) (high*base) << 32;
36 }
37
38 while ((s64)b > 0 && b < rem) {
39 b = b+b;
40 d = d+d;
41 }
42
43 do {
44 if (rem >= b) {
45 rem -= b;
46 res += d;
47 }
48 b >>= 1;
49 d >>= 1;
50 } while (d);
51
52 *n = res;
53 return rem;
54 }
55
56 /* The unnecessary pointer compare is there
57 * to check for type safety (n must be 64bit)
58 */
59 # define do_div(n,base) ({ \
60 u32 __base = (base); \
61 u32 __rem; \
62 (void)(((typeof((n)) *)0) == ((UINT64 *)0)); \
63 if (((n) >> 32) == 0) { \
64 __rem = (u32)(n) % __base; \
65 (n) = (u32)(n) / __base; \
66 } else \
67 __rem = __div64_32(&(n), __base); \
68 __rem; \
69 })
70
71 /* Wrapper for do_div(). Doesn't modify dividend and returns
72 * the result, not reminder.
73 */
74 static inline UINT64 lldiv(UINT64 dividend, u32 divisor)
75 {
76 UINT64 __res = dividend;
77 do_div(__res, divisor);
78 return(__res);
79 }
80
81 static inline UINT64 div_u64_rem(UINT64 dividend, u32 divisor, u32 *remainder)
82 {
83 *remainder = dividend % divisor;
84 return dividend / divisor;
85 }
86
87 #endif /* _ASM_GENERIC_DIV64_H */