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