1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
5 #include <linux/reciprocal_div.h>
6 #include <linux/export.h>
9 * For a description of the algorithm please have a look at
10 * include/linux/reciprocal_div.h
13 struct reciprocal_value reciprocal_value(u32 d)
15 struct reciprocal_value R;
20 m = ((1ULL << 32) * ((1ULL << l) - d));
25 R.sh2 = max(l - 1, 0);
29 EXPORT_SYMBOL(reciprocal_value);
31 struct reciprocal_value_adv reciprocal_value_adv(u32 d, u8 prec)
33 struct reciprocal_value_adv R;
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.
44 "ceil(log2(0x%08x)) == 32, %s doesn't support such divisor",
47 mlow = 1ULL << (32 + l);
49 mhigh = (1ULL << (32 + l)) + (1ULL << (32 + l - prec));
52 for (; post_shift > 0; post_shift--) {
53 u64 lo = mlow >> 1, hi = mhigh >> 1;
65 R.is_wide_m = mhigh > U32_MAX;
69 EXPORT_SYMBOL(reciprocal_value_adv);