diff options
author | Stefan Schmidt <stefan@datenfreihafen.org> | 2018-08-06 09:04:48 +0200 |
---|---|---|
committer | Stefan Schmidt <stefan@datenfreihafen.org> | 2018-08-06 09:04:48 +0200 |
commit | a30461080366214b690a367225a48c95d7a6a189 (patch) | |
tree | 02cd7cfb8cea14cebe1ab2a1638edd8c9b9e0d3f /lib/reciprocal_div.c | |
parent | 811e299f4645588cc7a1b78d97b6847c155324b9 (diff) | |
parent | 981467033a37d916649647fa3afe1fe99bba1817 (diff) | |
download | linux-a30461080366214b690a367225a48c95d7a6a189.tar.bz2 |
Merge remote-tracking branch 'net-next/master'
Diffstat (limited to 'lib/reciprocal_div.c')
-rw-r--r-- | lib/reciprocal_div.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/reciprocal_div.c b/lib/reciprocal_div.c index fcb4ce682c6f..bf043258fa00 100644 --- a/lib/reciprocal_div.c +++ b/lib/reciprocal_div.c @@ -1,4 +1,5 @@ // SPDX-License-Identifier: GPL-2.0 +#include <linux/bug.h> #include <linux/kernel.h> #include <asm/div64.h> #include <linux/reciprocal_div.h> @@ -26,3 +27,43 @@ struct reciprocal_value reciprocal_value(u32 d) return R; } EXPORT_SYMBOL(reciprocal_value); + +struct reciprocal_value_adv reciprocal_value_adv(u32 d, u8 prec) +{ + struct reciprocal_value_adv R; + u32 l, post_shift; + u64 mhigh, mlow; + + /* ceil(log2(d)) */ + l = fls(d - 1); + /* NOTE: mlow/mhigh could overflow u64 when l == 32. This case needs to + * be handled before calling "reciprocal_value_adv", please see the + * comment at include/linux/reciprocal_div.h. + */ + WARN(l == 32, + "ceil(log2(0x%08x)) == 32, %s doesn't support such divisor", + d, __func__); + post_shift = l; + mlow = 1ULL << (32 + l); + do_div(mlow, d); + mhigh = (1ULL << (32 + l)) + (1ULL << (32 + l - prec)); + do_div(mhigh, d); + + for (; post_shift > 0; post_shift--) { + u64 lo = mlow >> 1, hi = mhigh >> 1; + + if (lo >= hi) + break; + + mlow = lo; + mhigh = hi; + } + + R.m = (u32)mhigh; + R.sh = post_shift; + R.exp = l; + R.is_wide_m = mhigh > U32_MAX; + + return R; +} +EXPORT_SYMBOL(reciprocal_value_adv); |