1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
5 #include <linux/reciprocal_div.h>
6 #include <linux/export.h>
7 #include <linux/minmax.h>
10 * For a description of the algorithm please have a look at
11 * include/linux/reciprocal_div.h
14 struct reciprocal_value reciprocal_value(u32 d)
16 struct reciprocal_value R;
21 m = ((1ULL << 32) * ((1ULL << l) - d));
26 R.sh2 = max(l - 1, 0);
30 EXPORT_SYMBOL(reciprocal_value);
32 struct reciprocal_value_adv reciprocal_value_adv(u32 d, u8 prec)
34 struct reciprocal_value_adv R;
40 /* NOTE: mlow/mhigh could overflow u64 when l == 32. This case needs to
41 * be handled before calling "reciprocal_value_adv", please see the
42 * comment at include/linux/reciprocal_div.h.
45 "ceil(log2(0x%08x)) == 32, %s doesn't support such divisor",
48 mlow = 1ULL << (32 + l);
50 mhigh = (1ULL << (32 + l)) + (1ULL << (32 + l - prec));
53 for (; post_shift > 0; post_shift--) {
54 u64 lo = mlow >> 1, hi = mhigh >> 1;
66 R.is_wide_m = mhigh > U32_MAX;
70 EXPORT_SYMBOL(reciprocal_value_adv);