Wolfgang Denk | 7b64fef | 2006-10-24 14:21:16 +0200 | [diff] [blame] | 1 | #ifndef _ASM_GENERIC_DIV64_H |
| 2 | #define _ASM_GENERIC_DIV64_H |
| 3 | /* |
| 4 | * Copyright (C) 2003 Bernardo Innocenti <bernie@develer.com> |
| 5 | * Based on former asm-ppc/div64.h and asm-m68knommu/div64.h |
| 6 | * |
| 7 | * The semantics of do_div() are: |
| 8 | * |
| 9 | * uint32_t do_div(uint64_t *n, uint32_t base) |
| 10 | * { |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 11 | * uint32_t remainder = *n % base; |
| 12 | * *n = *n / base; |
| 13 | * return remainder; |
Wolfgang Denk | 7b64fef | 2006-10-24 14:21:16 +0200 | [diff] [blame] | 14 | * } |
| 15 | * |
| 16 | * NOTE: macro parameter n is evaluated multiple times, |
| 17 | * beware of side effects! |
| 18 | */ |
| 19 | |
| 20 | #include <linux/types.h> |
| 21 | |
| 22 | extern uint32_t __div64_32(uint64_t *dividend, uint32_t divisor); |
| 23 | |
| 24 | /* The unnecessary pointer compare is there |
| 25 | * to check for type safety (n must be 64bit) |
| 26 | */ |
| 27 | # define do_div(n,base) ({ \ |
| 28 | uint32_t __base = (base); \ |
| 29 | uint32_t __rem; \ |
| 30 | (void)(((typeof((n)) *)0) == ((uint64_t *)0)); \ |
| 31 | if (((n) >> 32) == 0) { \ |
| 32 | __rem = (uint32_t)(n) % __base; \ |
| 33 | (n) = (uint32_t)(n) / __base; \ |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 34 | } else \ |
Wolfgang Denk | 7b64fef | 2006-10-24 14:21:16 +0200 | [diff] [blame] | 35 | __rem = __div64_32(&(n), __base); \ |
| 36 | __rem; \ |
| 37 | }) |
| 38 | |
Sergei Poselenov | 3feb647 | 2008-11-04 13:51:18 +0100 | [diff] [blame] | 39 | /* Wrapper for do_div(). Doesn't modify dividend and returns |
| 40 | * the result, not reminder. |
| 41 | */ |
| 42 | static inline uint64_t lldiv(uint64_t dividend, uint32_t divisor) |
| 43 | { |
| 44 | uint64_t __res = dividend; |
| 45 | do_div(__res, divisor); |
| 46 | return(__res); |
| 47 | } |
| 48 | |
Wolfgang Denk | 7b64fef | 2006-10-24 14:21:16 +0200 | [diff] [blame] | 49 | #endif /* _ASM_GENERIC_DIV64_H */ |