Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Hannes Frederic Sowa | 809fa97 | 2014-01-22 02:29:41 +0100 | [diff] [blame] | 2 | #include <linux/kernel.h> |
Eric Dumazet | 6a2d7a9 | 2006-12-13 00:34:27 -0800 | [diff] [blame] | 3 | #include <asm/div64.h> |
| 4 | #include <linux/reciprocal_div.h> |
Eric Dumazet | 8af2a21 | 2011-12-08 06:06:03 +0000 | [diff] [blame] | 5 | #include <linux/export.h> |
Eric Dumazet | 6a2d7a9 | 2006-12-13 00:34:27 -0800 | [diff] [blame] | 6 | |
Hannes Frederic Sowa | 809fa97 | 2014-01-22 02:29:41 +0100 | [diff] [blame] | 7 | /* |
| 8 | * For a description of the algorithm please have a look at |
| 9 | * include/linux/reciprocal_div.h |
| 10 | */ |
| 11 | |
| 12 | struct reciprocal_value reciprocal_value(u32 d) |
Eric Dumazet | 6a2d7a9 | 2006-12-13 00:34:27 -0800 | [diff] [blame] | 13 | { |
Hannes Frederic Sowa | 809fa97 | 2014-01-22 02:29:41 +0100 | [diff] [blame] | 14 | struct reciprocal_value R; |
| 15 | u64 m; |
| 16 | int l; |
| 17 | |
| 18 | l = fls(d - 1); |
| 19 | m = ((1ULL << 32) * ((1ULL << l) - d)); |
| 20 | do_div(m, d); |
| 21 | ++m; |
| 22 | R.m = (u32)m; |
| 23 | R.sh1 = min(l, 1); |
| 24 | R.sh2 = max(l - 1, 0); |
| 25 | |
| 26 | return R; |
Eric Dumazet | 6a2d7a9 | 2006-12-13 00:34:27 -0800 | [diff] [blame] | 27 | } |
Eric Dumazet | 8af2a21 | 2011-12-08 06:06:03 +0000 | [diff] [blame] | 28 | EXPORT_SYMBOL(reciprocal_value); |