div64.h

Go to the documentation of this file.
00001 #ifndef ARCH_GENERIC_DIV64_H
00002 #define ARCH_GENERIC_DIV64_H
00003 /*
00004  * Copyright (C) 2003 Bernardo Innocenti <bernie@develer.com>
00005  * Based on former asm-ppc/div64.h and asm-m68knommu/div64.h
00006  *
00007  * The semantics of do_div() are:
00008  *
00009  * uint32_t do_div(uint64_t *n, uint32_t base)
00010  * {
00011  *      uint32_t remainder = *n % base;
00012  *      *n = *n / base;
00013  *      return remainder;
00014  * }
00015  *
00016  * NOTE: macro parameter n is evaluated multiple times,
00017  *       beware of side effects!
00018  */
00019 
00020 #ifndef ULONG_MAX
00021 #include <limits.h>
00022 #endif
00023 #include <stdint.h>
00024 
00025 #if ULONG_MAX == 4294967295
00026 
00027 extern uint32_t __div64_32(uint64_t *dividend, uint32_t divisor);
00028 
00029 /* The unnecessary pointer compare is there
00030  * to check for type safety (n must be 64bit)
00031  */
00032 # define do_div(n,base) ({                              \
00033         uint32_t __base = (base);                       \
00034         uint32_t __rem;                                 \
00035         (void)(((typeof((n)) *)0) == ((uint64_t *)0));  \
00036         if (((n) >> 32) == 0) {                         \
00037                 __rem = (uint32_t)(n) % __base;         \
00038                 (n) = (uint32_t)(n) / __base;           \
00039         } else                                          \
00040                 __rem = __div64_32(&(n), __base);       \
00041         __rem;                                          \
00042  })
00043 
00044 #elif ULONG_MAX == 18446744073709551615
00045 
00046 # define do_div(n,base) ({                                      \
00047         uint32_t __base = (base);                               \
00048         uint32_t __rem;                                         \
00049         __rem = ((uint64_t)(n)) % __base;                       \
00050         (n) = ((uint64_t)(n)) / __base;                         \
00051         __rem;                                                  \
00052  })
00053 
00054 
00055 #else /* BITS_PER_LONG == ?? */
00056 
00057 # error do_div() does not yet support the C64
00058 
00059 #endif /* BITS_PER_LONG */
00060 
00061 #endif /* ARCH_GENERIC_DIV64_H */

Generated on Mon Dec 29 10:54:06 2008 for coreboot by  doxygen 1.5.5