Linux IIO development
 help / color / mirror / Atom feed
* [PATCH] iio: gts-helper: Fix division loop
@ 2024-01-04  9:34 Matti Vaittinen
  2024-01-07 16:22 ` Jonathan Cameron
  0 siblings, 1 reply; 11+ messages in thread
From: Matti Vaittinen @ 2024-01-04  9:34 UTC (permalink / raw)
  To: Matti Vaittinen, Matti Vaittinen
  Cc: Jonathan Cameron, Lars-Peter Clausen, Matti Vaittinen, linux-iio,
	linux-kernel

[-- Attachment #1: Type: text/plain, Size: 2064 bytes --]

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.

Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com>
Fixes: 38416c28e168 ("iio: light: Add gain-time-scale helpers")
---

I've implemented also a fixup series for supporting rounding of
gains/scales:
https://lore.kernel.org/lkml/37d3aa193e69577353d314e94463a08d488ddd8d.1701780964.git.mazziesaccount@gmail.com/

That series does also remove the offending loop.

We don't currently have any in-tree users of GTS helpers which would
need the rounding support so pushing the rounding is not urgent (and I
haven't heard of Subjahit whose driver required the rounding). Hence, we
may want to only take this loop fix in for now (?) and reconsider
rounding when someone need that.

Jonathan, what's your take on this?

 drivers/iio/industrialio-gts-helper.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/industrialio-gts-helper.c b/drivers/iio/industrialio-gts-helper.c
index 7653261d2dc2..abcab2d38589 100644
--- a/drivers/iio/industrialio-gts-helper.c
+++ b/drivers/iio/industrialio-gts-helper.c
@@ -34,7 +34,7 @@
 static int iio_gts_get_gain(const u64 max, const u64 scale)
 {
 	u64 full = max;
-	int tmp = 1;
+	int tmp = 0;
 
 	if (scale > full || !scale)
 		return -EINVAL;
@@ -48,8 +48,7 @@ static int iio_gts_get_gain(const u64 max, const u64 scale)
 		tmp++;
 	}
 
-	while (full > scale * (u64)tmp)
-		tmp++;
+	tmp += div64_u64(full, scale);
 
 	return tmp;
 }

base-commit: 2cc14f52aeb78ce3f29677c2de1f06c0e91471ab
-- 
2.41.0


-- 
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =] 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2024-02-05  9:20 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-04  9:34 [PATCH] iio: gts-helper: Fix division loop Matti Vaittinen
2024-01-07 16:22 ` Jonathan Cameron
2024-01-08  6:35   ` Matti Vaittinen
2024-01-19 11:56   ` Subhajit Ghosh
2024-01-22  6:50     ` Matti Vaittinen
2024-01-22  9:58       ` Subhajit Ghosh
2024-02-04 13:49         ` Subhajit Ghosh
2024-02-05  9:19           ` Matti Vaittinen
2024-01-22 16:27       ` David Laight
2024-01-22 19:24         ` Jonathan Cameron
2024-01-23 10:30         ` Matti Vaittinen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox