blob: fcb4ce682c6fae37d71aa6138007b51cd0d8a2b3 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01002#include <linux/kernel.h>
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08003#include <asm/div64.h>
4#include <linux/reciprocal_div.h>
Eric Dumazet8af2a212011-12-08 06:06:03 +00005#include <linux/export.h>
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08006
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01007/*
8 * For a description of the algorithm please have a look at
9 * include/linux/reciprocal_div.h
10 */
11
12struct reciprocal_value reciprocal_value(u32 d)
Eric Dumazet6a2d7a92006-12-13 00:34:27 -080013{
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +010014 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 Dumazet6a2d7a92006-12-13 00:34:27 -080027}
Eric Dumazet8af2a212011-12-08 06:06:03 +000028EXPORT_SYMBOL(reciprocal_value);