1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/bitops.h>
4 #include <linux/export.h>
5 #include <linux/limits.h>
6 #include <linux/math.h>
7 #include <linux/minmax.h>
8 #include <linux/types.h>
10 #include <linux/reciprocal_div.h>
13 * For a description of the algorithm please have a look at
14 * include/linux/reciprocal_div.h
17 struct reciprocal_value
reciprocal_value(u32 d
)
19 struct reciprocal_value R
;
24 m
= ((1ULL << 32) * ((1ULL << l
) - d
));
29 R
.sh2
= max(l
- 1, 0);
33 EXPORT_SYMBOL(reciprocal_value
);
35 struct reciprocal_value_adv
reciprocal_value_adv(u32 d
, u8 prec
)
37 struct reciprocal_value_adv R
;
43 /* NOTE: mlow/mhigh could overflow u64 when l == 32. This case needs to
44 * be handled before calling "reciprocal_value_adv", please see the
45 * comment at include/linux/reciprocal_div.h.
48 "ceil(log2(0x%08x)) == 32, %s doesn't support such divisor",
51 mlow
= 1ULL << (32 + l
);
53 mhigh
= (1ULL << (32 + l
)) + (1ULL << (32 + l
- prec
));
56 for (; post_shift
> 0; post_shift
--) {
57 u64 lo
= mlow
>> 1, hi
= mhigh
>> 1;
69 R
.is_wide_m
= mhigh
> U32_MAX
;
73 EXPORT_SYMBOL(reciprocal_value_adv
);