Back to home page

LXR

 
 

    


0001 /*
0002  * Copyright (C) 2003 Bernardo Innocenti <bernie@develer.com>
0003  *
0004  * Based on former do_div() implementation from asm-parisc/div64.h:
0005  *  Copyright (C) 1999 Hewlett-Packard Co
0006  *  Copyright (C) 1999 David Mosberger-Tang <davidm@hpl.hp.com>
0007  *
0008  *
0009  * Generic C version of 64bit/32bit division and modulo, with
0010  * 64bit result and 32bit remainder.
0011  *
0012  * The fast case for (n>>32 == 0) is handled inline by do_div(). 
0013  *
0014  * Code generated for this function might be very inefficient
0015  * for some CPUs. __div64_32() can be overridden by linking arch-specific
0016  * assembly versions such as arch/ppc/lib/div64.S and arch/sh/lib/div64.S
0017  * or by defining a preprocessor macro in arch/include/asm/div64.h.
0018  */
0019 
0020 #include <linux/export.h>
0021 #include <linux/kernel.h>
0022 #include <linux/math64.h>
0023 
0024 /* Not needed on 64bit architectures */
0025 #if BITS_PER_LONG == 32
0026 
0027 #ifndef __div64_32
0028 uint32_t __attribute__((weak)) __div64_32(uint64_t *n, uint32_t base)
0029 {
0030     uint64_t rem = *n;
0031     uint64_t b = base;
0032     uint64_t res, d = 1;
0033     uint32_t high = rem >> 32;
0034 
0035     /* Reduce the thing a bit first */
0036     res = 0;
0037     if (high >= base) {
0038         high /= base;
0039         res = (uint64_t) high << 32;
0040         rem -= (uint64_t) (high*base) << 32;
0041     }
0042 
0043     while ((int64_t)b > 0 && b < rem) {
0044         b = b+b;
0045         d = d+d;
0046     }
0047 
0048     do {
0049         if (rem >= b) {
0050             rem -= b;
0051             res += d;
0052         }
0053         b >>= 1;
0054         d >>= 1;
0055     } while (d);
0056 
0057     *n = res;
0058     return rem;
0059 }
0060 EXPORT_SYMBOL(__div64_32);
0061 #endif
0062 
0063 #ifndef div_s64_rem
0064 s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder)
0065 {
0066     u64 quotient;
0067 
0068     if (dividend < 0) {
0069         quotient = div_u64_rem(-dividend, abs(divisor), (u32 *)remainder);
0070         *remainder = -*remainder;
0071         if (divisor > 0)
0072             quotient = -quotient;
0073     } else {
0074         quotient = div_u64_rem(dividend, abs(divisor), (u32 *)remainder);
0075         if (divisor < 0)
0076             quotient = -quotient;
0077     }
0078     return quotient;
0079 }
0080 EXPORT_SYMBOL(div_s64_rem);
0081 #endif
0082 
0083 /**
0084  * div64_u64_rem - unsigned 64bit divide with 64bit divisor and remainder
0085  * @dividend:   64bit dividend
0086  * @divisor:    64bit divisor
0087  * @remainder:  64bit remainder
0088  *
0089  * This implementation is a comparable to algorithm used by div64_u64.
0090  * But this operation, which includes math for calculating the remainder,
0091  * is kept distinct to avoid slowing down the div64_u64 operation on 32bit
0092  * systems.
0093  */
0094 #ifndef div64_u64_rem
0095 u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder)
0096 {
0097     u32 high = divisor >> 32;
0098     u64 quot;
0099 
0100     if (high == 0) {
0101         u32 rem32;
0102         quot = div_u64_rem(dividend, divisor, &rem32);
0103         *remainder = rem32;
0104     } else {
0105         int n = 1 + fls(high);
0106         quot = div_u64(dividend >> n, divisor >> n);
0107 
0108         if (quot != 0)
0109             quot--;
0110 
0111         *remainder = dividend - quot * divisor;
0112         if (*remainder >= divisor) {
0113             quot++;
0114             *remainder -= divisor;
0115         }
0116     }
0117 
0118     return quot;
0119 }
0120 EXPORT_SYMBOL(div64_u64_rem);
0121 #endif
0122 
0123 /**
0124  * div64_u64 - unsigned 64bit divide with 64bit divisor
0125  * @dividend:   64bit dividend
0126  * @divisor:    64bit divisor
0127  *
0128  * This implementation is a modified version of the algorithm proposed
0129  * by the book 'Hacker's Delight'.  The original source and full proof
0130  * can be found here and is available for use without restriction.
0131  *
0132  * 'http://www.hackersdelight.org/hdcodetxt/divDouble.c.txt'
0133  */
0134 #ifndef div64_u64
0135 u64 div64_u64(u64 dividend, u64 divisor)
0136 {
0137     u32 high = divisor >> 32;
0138     u64 quot;
0139 
0140     if (high == 0) {
0141         quot = div_u64(dividend, divisor);
0142     } else {
0143         int n = 1 + fls(high);
0144         quot = div_u64(dividend >> n, divisor >> n);
0145 
0146         if (quot != 0)
0147             quot--;
0148         if ((dividend - quot * divisor) >= divisor)
0149             quot++;
0150     }
0151 
0152     return quot;
0153 }
0154 EXPORT_SYMBOL(div64_u64);
0155 #endif
0156 
0157 /**
0158  * div64_s64 - signed 64bit divide with 64bit divisor
0159  * @dividend:   64bit dividend
0160  * @divisor:    64bit divisor
0161  */
0162 #ifndef div64_s64
0163 s64 div64_s64(s64 dividend, s64 divisor)
0164 {
0165     s64 quot, t;
0166 
0167     quot = div64_u64(abs(dividend), abs(divisor));
0168     t = (dividend ^ divisor) >> 63;
0169 
0170     return (quot ^ t) - t;
0171 }
0172 EXPORT_SYMBOL(div64_s64);
0173 #endif
0174 
0175 #endif /* BITS_PER_LONG == 32 */
0176 
0177 /*
0178  * Iterative div/mod for use when dividend is not expected to be much
0179  * bigger than divisor.
0180  */
0181 u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder)
0182 {
0183     return __iter_div_u64_rem(dividend, divisor, remainder);
0184 }
0185 EXPORT_SYMBOL(iter_div_u64_rem);