]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - lib/div64.c
common implementation of iterative div/mod
[karo-tx-linux.git] / lib / div64.c
index bb5bd0c0f030a9cf44c57ef07b50fac3e7170ce8..76c01542d3e13101e6e300bb7c5d70ae7df8c86d 100644 (file)
@@ -98,3 +98,26 @@ EXPORT_SYMBOL(div64_u64);
 #endif
 
 #endif /* BITS_PER_LONG == 32 */
+
+/*
+ * Iterative div/mod for use when dividend is not expected to be much
+ * bigger than divisor.
+ */
+u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder)
+{
+       u32 ret = 0;
+
+       while (dividend >= divisor) {
+               /* The following asm() prevents the compiler from
+                  optimising this loop into a modulo operation.  */
+               asm("" : "+rm"(dividend));
+
+               dividend -= divisor;
+               ret++;
+       }
+
+       *remainder = dividend;
+
+       return ret;
+}
+EXPORT_SYMBOL(iter_div_u64_rem);