Blame view
lib/reciprocal_div.c
1.4 KB
b24413180 License cleanup: ... |
1 |
// SPDX-License-Identifier: GPL-2.0 |
06ae48269 lib: reciprocal_d... |
2 |
#include <linux/bug.h> |
809fa972f reciprocal_divide... |
3 |
#include <linux/kernel.h> |
6a2d7a955 [PATCH] SLAB: use... |
4 5 |
#include <asm/div64.h> #include <linux/reciprocal_div.h> |
8af2a218d sch_red: Adaptati... |
6 |
#include <linux/export.h> |
6a2d7a955 [PATCH] SLAB: use... |
7 |
|
809fa972f reciprocal_divide... |
8 9 10 11 12 13 |
/* * For a description of the algorithm please have a look at * include/linux/reciprocal_div.h */ struct reciprocal_value reciprocal_value(u32 d) |
6a2d7a955 [PATCH] SLAB: use... |
14 |
{ |
809fa972f reciprocal_divide... |
15 16 17 18 19 20 21 22 23 24 25 26 27 |
struct reciprocal_value R; u64 m; int l; l = fls(d - 1); m = ((1ULL << 32) * ((1ULL << l) - d)); do_div(m, d); ++m; R.m = (u32)m; R.sh1 = min(l, 1); R.sh2 = max(l - 1, 0); return R; |
6a2d7a955 [PATCH] SLAB: use... |
28 |
} |
8af2a218d sch_red: Adaptati... |
29 |
EXPORT_SYMBOL(reciprocal_value); |
06ae48269 lib: reciprocal_d... |
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
struct reciprocal_value_adv reciprocal_value_adv(u32 d, u8 prec) { struct reciprocal_value_adv R; u32 l, post_shift; u64 mhigh, mlow; /* ceil(log2(d)) */ l = fls(d - 1); /* NOTE: mlow/mhigh could overflow u64 when l == 32. This case needs to * be handled before calling "reciprocal_value_adv", please see the * comment at include/linux/reciprocal_div.h. */ WARN(l == 32, "ceil(log2(0x%08x)) == 32, %s doesn't support such divisor", d, __func__); post_shift = l; mlow = 1ULL << (32 + l); do_div(mlow, d); mhigh = (1ULL << (32 + l)) + (1ULL << (32 + l - prec)); do_div(mhigh, d); for (; post_shift > 0; post_shift--) { u64 lo = mlow >> 1, hi = mhigh >> 1; if (lo >= hi) break; mlow = lo; mhigh = hi; } R.m = (u32)mhigh; R.sh = post_shift; R.exp = l; R.is_wide_m = mhigh > U32_MAX; return R; } EXPORT_SYMBOL(reciprocal_value_adv); |