mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-09-28 13:22:57 +00:00
iio: gts-helper: Fix division loop
[ Upstream commitbb76cc45dc
] The loop based 64bit division may run for a long time when dividend is a lot bigger than the divider. Replace the division loop by the div64_u64() which implementation may be significantly faster. Tested-by: Subhajit Ghosh <subhajit.ghosh@tweaklogic.com> Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com> Fixes:38416c28e1
("iio: light: Add gain-time-scale helpers") Link: https://lore.kernel.org/r/Zcn-6e-0-nh2WcfU@drtxq0yyyyyyyyyyyyyby-3.rev.dnainternet.fi Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
parent
257b262937
commit
f8ca7ac743
1 changed files with 1 additions and 14 deletions
|
@ -34,24 +34,11 @@
|
||||||
static int iio_gts_get_gain(const u64 max, const u64 scale)
|
static int iio_gts_get_gain(const u64 max, const u64 scale)
|
||||||
{
|
{
|
||||||
u64 full = max;
|
u64 full = max;
|
||||||
int tmp = 1;
|
|
||||||
|
|
||||||
if (scale > full || !scale)
|
if (scale > full || !scale)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
if (U64_MAX - full < scale) {
|
return div64_u64(full, scale);
|
||||||
/* Risk of overflow */
|
|
||||||
if (full - scale < scale)
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
full -= scale;
|
|
||||||
tmp++;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (full > scale * (u64)tmp)
|
|
||||||
tmp++;
|
|
||||||
|
|
||||||
return tmp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue