treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / lib / math / reciprocal_div.c
blobbf043258fa0082b4cc1100a982d2ed38e21b1c0e
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/bug.h>
3 #include <linux/kernel.h>
4 #include <asm/div64.h>
5 #include <linux/reciprocal_div.h>
6 #include <linux/export.h>
8 /*
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;
16 u64 m;
17 int l;
19 l = fls(d - 1);
20 m = ((1ULL << 32) * ((1ULL << l) - d));
21 do_div(m, d);
22 ++m;
23 R.m = (u32)m;
24 R.sh1 = min(l, 1);
25 R.sh2 = max(l - 1, 0);
27 return R;
29 EXPORT_SYMBOL(reciprocal_value);
31 struct reciprocal_value_adv reciprocal_value_adv(u32 d, u8 prec)
33 struct reciprocal_value_adv R;
34 u32 l, post_shift;
35 u64 mhigh, mlow;
37 /* ceil(log2(d)) */
38 l = fls(d - 1);
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.
43 WARN(l == 32,
44 "ceil(log2(0x%08x)) == 32, %s doesn't support such divisor",
45 d, __func__);
46 post_shift = l;
47 mlow = 1ULL << (32 + l);
48 do_div(mlow, d);
49 mhigh = (1ULL << (32 + l)) + (1ULL << (32 + l - prec));
50 do_div(mhigh, d);
52 for (; post_shift > 0; post_shift--) {
53 u64 lo = mlow >> 1, hi = mhigh >> 1;
55 if (lo >= hi)
56 break;
58 mlow = lo;
59 mhigh = hi;
62 R.m = (u32)mhigh;
63 R.sh = post_shift;
64 R.exp = l;
65 R.is_wide_m = mhigh > U32_MAX;
67 return R;
69 EXPORT_SYMBOL(reciprocal_value_adv);