/*1* Copyright (C) 2003 Bernardo Innocenti <[email protected]>2*3* Based on former do_div() implementation from asm-parisc/div64.h:4* Copyright (C) 1999 Hewlett-Packard Co5* Copyright (C) 1999 David Mosberger-Tang <[email protected]>6*7*8* Generic C version of 64bit/32bit division and modulo, with9* 64bit result and 32bit remainder.10*11* The fast case for (n>>32 == 0) is handled inline by do_div().12*13* Code generated for this function might be very inefficient14* for some CPUs. __div64_32() can be overridden by linking arch-specific15* assembly versions such as arch/ppc/lib/div64.S and arch/sh/lib/div64.S.16*/1718#include <linux/module.h>19#include <linux/math64.h>2021/* Not needed on 64bit architectures */22#if BITS_PER_LONG == 322324uint32_t __attribute__((weak)) __div64_32(uint64_t *n, uint32_t base)25{26uint64_t rem = *n;27uint64_t b = base;28uint64_t res, d = 1;29uint32_t high = rem >> 32;3031/* Reduce the thing a bit first */32res = 0;33if (high >= base) {34high /= base;35res = (uint64_t) high << 32;36rem -= (uint64_t) (high*base) << 32;37}3839while ((int64_t)b > 0 && b < rem) {40b = b+b;41d = d+d;42}4344do {45if (rem >= b) {46rem -= b;47res += d;48}49b >>= 1;50d >>= 1;51} while (d);5253*n = res;54return rem;55}5657EXPORT_SYMBOL(__div64_32);5859#ifndef div_s64_rem60s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder)61{62u64 quotient;6364if (dividend < 0) {65quotient = div_u64_rem(-dividend, abs(divisor), (u32 *)remainder);66*remainder = -*remainder;67if (divisor > 0)68quotient = -quotient;69} else {70quotient = div_u64_rem(dividend, abs(divisor), (u32 *)remainder);71if (divisor < 0)72quotient = -quotient;73}74return quotient;75}76EXPORT_SYMBOL(div_s64_rem);77#endif7879/**80* div64_u64 - unsigned 64bit divide with 64bit divisor81* @dividend: 64bit dividend82* @divisor: 64bit divisor83*84* This implementation is a modified version of the algorithm proposed85* by the book 'Hacker's Delight'. The original source and full proof86* can be found here and is available for use without restriction.87*88* 'http://www.hackersdelight.org/HDcode/newCode/divDouble.c'89*/90#ifndef div64_u6491u64 div64_u64(u64 dividend, u64 divisor)92{93u32 high = divisor >> 32;94u64 quot;9596if (high == 0) {97quot = div_u64(dividend, divisor);98} else {99int n = 1 + fls(high);100quot = div_u64(dividend >> n, divisor >> n);101102if (quot != 0)103quot--;104if ((dividend - quot * divisor) >= divisor)105quot++;106}107108return quot;109}110EXPORT_SYMBOL(div64_u64);111#endif112113/**114* div64_s64 - signed 64bit divide with 64bit divisor115* @dividend: 64bit dividend116* @divisor: 64bit divisor117*/118#ifndef div64_s64119s64 div64_s64(s64 dividend, s64 divisor)120{121s64 quot, t;122123quot = div64_u64(abs64(dividend), abs64(divisor));124t = (dividend ^ divisor) >> 63;125126return (quot ^ t) - t;127}128EXPORT_SYMBOL(div64_s64);129#endif130131#endif /* BITS_PER_LONG == 32 */132133/*134* Iterative div/mod for use when dividend is not expected to be much135* bigger than divisor.136*/137u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder)138{139return __iter_div_u64_rem(dividend, divisor, remainder);140}141EXPORT_SYMBOL(iter_div_u64_rem);142143144