Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [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 | * { |
| 11 | * uint32_t 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 <linux/compiler.h> |
| 22 | |
| 23 | #if BITS_PER_LONG == 64 |
| 24 | |
| 25 | # define do_div(n,base) ({ \ |
| 26 | uint32_t __base = (base); \ |
| 27 | uint32_t __rem; \ |
| 28 | __rem = ((uint64_t)(n)) % __base; \ |
| 29 | (n) = ((uint64_t)(n)) / __base; \ |
| 30 | __rem; \ |
| 31 | }) |
| 32 | |
Stephen Hemminger | 3927f2e | 2007-03-25 19:54:23 -0700 | [diff] [blame] | 33 | static inline uint64_t div64_64(uint64_t dividend, uint64_t divisor) |
| 34 | { |
| 35 | return dividend / divisor; |
| 36 | } |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #elif BITS_PER_LONG == 32 |
| 39 | |
| 40 | extern uint32_t __div64_32(uint64_t *dividend, uint32_t divisor); |
| 41 | |
| 42 | /* The unnecessary pointer compare is there |
| 43 | * to check for type safety (n must be 64bit) |
| 44 | */ |
| 45 | # define do_div(n,base) ({ \ |
| 46 | uint32_t __base = (base); \ |
| 47 | uint32_t __rem; \ |
| 48 | (void)(((typeof((n)) *)0) == ((uint64_t *)0)); \ |
| 49 | if (likely(((n) >> 32) == 0)) { \ |
| 50 | __rem = (uint32_t)(n) % __base; \ |
| 51 | (n) = (uint32_t)(n) / __base; \ |
| 52 | } else \ |
| 53 | __rem = __div64_32(&(n), __base); \ |
| 54 | __rem; \ |
| 55 | }) |
| 56 | |
Stephen Hemminger | 3927f2e | 2007-03-25 19:54:23 -0700 | [diff] [blame] | 57 | extern uint64_t div64_64(uint64_t dividend, uint64_t divisor); |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #else /* BITS_PER_LONG == ?? */ |
| 60 | |
| 61 | # error do_div() does not yet support the C64 |
| 62 | |
| 63 | #endif /* BITS_PER_LONG */ |
| 64 | |
| 65 | #endif /* _ASM_GENERIC_DIV64_H */ |