blob: 464152410c5123e191324a4cfb3ca5ea8ff66687 [file] [log] [blame]
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01001#include <linux/kernel.h>
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08002#include <asm/div64.h>
3#include <linux/reciprocal_div.h>
Eric Dumazet8af2a212011-12-08 06:06:03 +00004#include <linux/export.h>
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08005
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01006/*
7 * For a description of the algorithm please have a look at
8 * include/linux/reciprocal_div.h
9 */
10
11struct reciprocal_value reciprocal_value(u32 d)
Eric Dumazet6a2d7a92006-12-13 00:34:27 -080012{
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +010013 struct reciprocal_value R;
14 u64 m;
15 int l;
16
17 l = fls(d - 1);
18 m = ((1ULL << 32) * ((1ULL << l) - d));
19 do_div(m, d);
20 ++m;
21 R.m = (u32)m;
22 R.sh1 = min(l, 1);
23 R.sh2 = max(l - 1, 0);
24
25 return R;
Eric Dumazet6a2d7a92006-12-13 00:34:27 -080026}
Eric Dumazet8af2a212011-12-08 06:06:03 +000027EXPORT_SYMBOL(reciprocal_value);