Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Andy Shevchenko | aa6159a | 2020-12-15 20:42:48 -0800 | [diff] [blame] | 2 | #include <linux/bitops.h> |
Jiong Wang | 06ae482 | 2018-07-06 15:13:18 -0700 | [diff] [blame] | 3 | #include <linux/bug.h> |
Eric Dumazet | 8af2a21 | 2011-12-08 06:06:03 +0000 | [diff] [blame] | 4 | #include <linux/export.h> |
Andy Shevchenko | aa6159a | 2020-12-15 20:42:48 -0800 | [diff] [blame] | 5 | #include <linux/limits.h> |
| 6 | #include <linux/math.h> |
Andy Shevchenko | b296a6d | 2020-10-15 20:10:21 -0700 | [diff] [blame] | 7 | #include <linux/minmax.h> |
Andy Shevchenko | aa6159a | 2020-12-15 20:42:48 -0800 | [diff] [blame] | 8 | #include <linux/types.h> |
| 9 | |
| 10 | #include <linux/reciprocal_div.h> |
Eric Dumazet | 6a2d7a9 | 2006-12-13 00:34:27 -0800 | [diff] [blame] | 11 | |
Hannes Frederic Sowa | 809fa97 | 2014-01-22 02:29:41 +0100 | [diff] [blame] | 12 | /* |
| 13 | * For a description of the algorithm please have a look at |
| 14 | * include/linux/reciprocal_div.h |
| 15 | */ |
| 16 | |
| 17 | struct reciprocal_value reciprocal_value(u32 d) |
Eric Dumazet | 6a2d7a9 | 2006-12-13 00:34:27 -0800 | [diff] [blame] | 18 | { |
Hannes Frederic Sowa | 809fa97 | 2014-01-22 02:29:41 +0100 | [diff] [blame] | 19 | struct reciprocal_value R; |
| 20 | u64 m; |
| 21 | int l; |
| 22 | |
| 23 | l = fls(d - 1); |
| 24 | m = ((1ULL << 32) * ((1ULL << l) - d)); |
| 25 | do_div(m, d); |
| 26 | ++m; |
| 27 | R.m = (u32)m; |
| 28 | R.sh1 = min(l, 1); |
| 29 | R.sh2 = max(l - 1, 0); |
| 30 | |
| 31 | return R; |
Eric Dumazet | 6a2d7a9 | 2006-12-13 00:34:27 -0800 | [diff] [blame] | 32 | } |
Eric Dumazet | 8af2a21 | 2011-12-08 06:06:03 +0000 | [diff] [blame] | 33 | EXPORT_SYMBOL(reciprocal_value); |
Jiong Wang | 06ae482 | 2018-07-06 15:13:18 -0700 | [diff] [blame] | 34 | |
| 35 | struct reciprocal_value_adv reciprocal_value_adv(u32 d, u8 prec) |
| 36 | { |
| 37 | struct reciprocal_value_adv R; |
| 38 | u32 l, post_shift; |
| 39 | u64 mhigh, mlow; |
| 40 | |
| 41 | /* ceil(log2(d)) */ |
| 42 | l = fls(d - 1); |
| 43 | /* NOTE: mlow/mhigh could overflow u64 when l == 32. This case needs to |
| 44 | * be handled before calling "reciprocal_value_adv", please see the |
| 45 | * comment at include/linux/reciprocal_div.h. |
| 46 | */ |
| 47 | WARN(l == 32, |
| 48 | "ceil(log2(0x%08x)) == 32, %s doesn't support such divisor", |
| 49 | d, __func__); |
| 50 | post_shift = l; |
| 51 | mlow = 1ULL << (32 + l); |
| 52 | do_div(mlow, d); |
| 53 | mhigh = (1ULL << (32 + l)) + (1ULL << (32 + l - prec)); |
| 54 | do_div(mhigh, d); |
| 55 | |
| 56 | for (; post_shift > 0; post_shift--) { |
| 57 | u64 lo = mlow >> 1, hi = mhigh >> 1; |
| 58 | |
| 59 | if (lo >= hi) |
| 60 | break; |
| 61 | |
| 62 | mlow = lo; |
| 63 | mhigh = hi; |
| 64 | } |
| 65 | |
| 66 | R.m = (u32)mhigh; |
| 67 | R.sh = post_shift; |
| 68 | R.exp = l; |
| 69 | R.is_wide_m = mhigh > U32_MAX; |
| 70 | |
| 71 | return R; |
| 72 | } |
| 73 | EXPORT_SYMBOL(reciprocal_value_adv); |