Skip to content

Commit bb76cc4

Browse files
M-Vaittinenjic23
authored andcommitted
iio: gts-helper: Fix division loop
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: 38416c2 ("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>
1 parent 3ad0e7e commit bb76cc4

File tree

1 file changed

+1
-14
lines changed

1 file changed

+1
-14
lines changed

drivers/iio/industrialio-gts-helper.c

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,11 @@
3434
static int iio_gts_get_gain(const u64 max, const u64 scale)
3535
{
3636
u64 full = max;
37-
int tmp = 1;
3837

3938
if (scale > full || !scale)
4039
return -EINVAL;
4140

42-
if (U64_MAX - full < scale) {
43-
/* Risk of overflow */
44-
if (full - scale < scale)
45-
return 1;
46-
47-
full -= scale;
48-
tmp++;
49-
}
50-
51-
while (full > scale * (u64)tmp)
52-
tmp++;
53-
54-
return tmp;
41+
return div64_u64(full, scale);
5542
}
5643

5744
/**

0 commit comments

Comments
 (0)