public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1 1/1] iio: common: scmi_sensors: Replace const_ilog2() with ilog2()
@ 2025-10-31  7:45 Andy Shevchenko
  2025-10-31  9:43 ` David Laight
  0 siblings, 1 reply; 7+ messages in thread
From: Andy Shevchenko @ 2025-10-31  7:45 UTC (permalink / raw)
  To: Jonathan Cameron, Andy Shevchenko, linux-iio, linux-kernel
  Cc: Jyoti Bhayana, Jonathan Cameron, David Lechner, Nuno Sá,
	Andy Shevchenko

const_ilog2() was a workaround of some sparse issue, which was
never appeared in the C functions. Replace it with ilog2().

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/iio/common/scmi_sensors/scmi_iio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iio/common/scmi_sensors/scmi_iio.c b/drivers/iio/common/scmi_sensors/scmi_iio.c
index 39c61c47022a..b40c6d6442e6 100644
--- a/drivers/iio/common/scmi_sensors/scmi_iio.c
+++ b/drivers/iio/common/scmi_sensors/scmi_iio.c
@@ -69,7 +69,7 @@ static int scmi_iio_sensor_update_cb(struct notifier_block *nb,
 		 *  Converting the timestamp to nanoseconds below.
 		 */
 		tstamp_scale = sensor->sensor_info->tstamp_scale +
-			       const_ilog2(NSEC_PER_SEC) / const_ilog2(10);
+			       ilog2(NSEC_PER_SEC) / ilog2(10);
 		if (tstamp_scale < 0) {
 			do_div(time, int_pow(10, abs(tstamp_scale)));
 			time_ns = time;
-- 
2.50.1


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

* Re: [PATCH v1 1/1] iio: common: scmi_sensors: Replace const_ilog2() with ilog2()
  2025-10-31  7:45 [PATCH v1 1/1] iio: common: scmi_sensors: Replace const_ilog2() with ilog2() Andy Shevchenko
@ 2025-10-31  9:43 ` David Laight
  2025-10-31  9:54   ` Andy Shevchenko
  0 siblings, 1 reply; 7+ messages in thread
From: David Laight @ 2025-10-31  9:43 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Jonathan Cameron, linux-iio, linux-kernel, Jyoti Bhayana,
	Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko

On Fri, 31 Oct 2025 08:45:00 +0100
Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:

> const_ilog2() was a workaround of some sparse issue, which was
> never appeared in the C functions. Replace it with ilog2().
> 
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
>  drivers/iio/common/scmi_sensors/scmi_iio.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/common/scmi_sensors/scmi_iio.c b/drivers/iio/common/scmi_sensors/scmi_iio.c
> index 39c61c47022a..b40c6d6442e6 100644
> --- a/drivers/iio/common/scmi_sensors/scmi_iio.c
> +++ b/drivers/iio/common/scmi_sensors/scmi_iio.c
> @@ -69,7 +69,7 @@ static int scmi_iio_sensor_update_cb(struct notifier_block *nb,
>  		 *  Converting the timestamp to nanoseconds below.
>  		 */
>  		tstamp_scale = sensor->sensor_info->tstamp_scale +
> -			       const_ilog2(NSEC_PER_SEC) / const_ilog2(10);
> +			       ilog2(NSEC_PER_SEC) / ilog2(10);

Is that just a strange way of writing 9 ?
Mathematically log2(x)/log2(10) is log10(x) - which would be 9.
The code does seem to be 'in luck' though.
NSEC_PER_SEC is 10^9 or 0x3b9aca00, so ilog2(NSEC_PER_SEC) is 29.
ilog2(10) is 3, and 29/3 is 9.

Do the same for 10^10 and you get 11.

	David

>  		if (tstamp_scale < 0) {
>  			do_div(time, int_pow(10, abs(tstamp_scale)));
>  			time_ns = time;


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

* Re: [PATCH v1 1/1] iio: common: scmi_sensors: Replace const_ilog2() with ilog2()
  2025-10-31  9:43 ` David Laight
@ 2025-10-31  9:54   ` Andy Shevchenko
  2025-10-31 12:45     ` David Laight
  0 siblings, 1 reply; 7+ messages in thread
From: Andy Shevchenko @ 2025-10-31  9:54 UTC (permalink / raw)
  To: David Laight
  Cc: Jonathan Cameron, linux-iio, linux-kernel, Jyoti Bhayana,
	Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko

On Fri, Oct 31, 2025 at 09:43:36AM +0000, David Laight wrote:
> On Fri, 31 Oct 2025 08:45:00 +0100
> Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:

...

> >  		tstamp_scale = sensor->sensor_info->tstamp_scale +
> > -			       const_ilog2(NSEC_PER_SEC) / const_ilog2(10);
> > +			       ilog2(NSEC_PER_SEC) / ilog2(10);
> 
> Is that just a strange way of writing 9 ?

Why? It's correct way of writing log¹⁰(NSEC_PER_SEC), the problem here is that
"i" people do not think about :-) But we have intlog10(), I completely forgot
about it.

> Mathematically log2(x)/log2(10) is log10(x) - which would be 9.
> The code does seem to be 'in luck' though.
> NSEC_PER_SEC is 10^9 or 0x3b9aca00, so ilog2(NSEC_PER_SEC) is 29.
> ilog2(10) is 3, and 29/3 is 9.
> 
> Do the same for 10^10 and you get 11.

That code looks like working by luck entirely, TBH. I just took the scope of
the patch to start dropping const_ilog2() usages.

-- 
With Best Regards,
Andy Shevchenko



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

* Re: [PATCH v1 1/1] iio: common: scmi_sensors: Replace const_ilog2() with ilog2()
  2025-10-31  9:54   ` Andy Shevchenko
@ 2025-10-31 12:45     ` David Laight
  2025-10-31 12:51       ` Andy Shevchenko
  0 siblings, 1 reply; 7+ messages in thread
From: David Laight @ 2025-10-31 12:45 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Jonathan Cameron, linux-iio, linux-kernel, Jyoti Bhayana,
	Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko

On Fri, 31 Oct 2025 11:54:30 +0200
Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:

> On Fri, Oct 31, 2025 at 09:43:36AM +0000, David Laight wrote:
> > On Fri, 31 Oct 2025 08:45:00 +0100
> > Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:  
> 
> ...
> 
> > >  		tstamp_scale = sensor->sensor_info->tstamp_scale +
> > > -			       const_ilog2(NSEC_PER_SEC) / const_ilog2(10);
> > > +			       ilog2(NSEC_PER_SEC) / ilog2(10);  
> > 
> > Is that just a strange way of writing 9 ?  
> 
> Why? It's correct way of writing log¹⁰(NSEC_PER_SEC), the problem here is that
> "i" people do not think about :-)

Even without the "i" the division could easily give 8.999999.
So you'd be relying on rounding to get the required integral value.

> But we have intlog10(), I completely forgot about it.

And it isn't the function the code is looking for.
(The result is shifted left 24 and it doesn't have an optimisation
for constants.)

> 
> > Mathematically log2(x)/log2(10) is log10(x) - which would be 9.
> > The code does seem to be 'in luck' though.
> > NSEC_PER_SEC is 10^9 or 0x3b9aca00, so ilog2(NSEC_PER_SEC) is 29.
> > ilog2(10) is 3, and 29/3 is 9.
> > 
> > Do the same for 10^10 and you get 11.  
> 
> That code looks like working by luck entirely, TBH. I just took the scope of
> the patch to start dropping const_ilog2() usages.

Something always crawls out of the woodwork...

	David



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

* Re: [PATCH v1 1/1] iio: common: scmi_sensors: Replace const_ilog2() with ilog2()
  2025-10-31 12:45     ` David Laight
@ 2025-10-31 12:51       ` Andy Shevchenko
  2025-10-31 16:13         ` David Laight
  0 siblings, 1 reply; 7+ messages in thread
From: Andy Shevchenko @ 2025-10-31 12:51 UTC (permalink / raw)
  To: David Laight
  Cc: Jonathan Cameron, linux-iio, linux-kernel, Jyoti Bhayana,
	Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko

On Fri, Oct 31, 2025 at 12:45:30PM +0000, David Laight wrote:
> On Fri, 31 Oct 2025 11:54:30 +0200
> Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
> > On Fri, Oct 31, 2025 at 09:43:36AM +0000, David Laight wrote:
> > > On Fri, 31 Oct 2025 08:45:00 +0100
> > > Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:  

...

> > > >  		tstamp_scale = sensor->sensor_info->tstamp_scale +
> > > > -			       const_ilog2(NSEC_PER_SEC) / const_ilog2(10);
> > > > +			       ilog2(NSEC_PER_SEC) / ilog2(10);  
> > > 
> > > Is that just a strange way of writing 9 ?  
> > 
> > Why? It's correct way of writing log¹⁰(NSEC_PER_SEC), the problem here is that
> > "i" people do not think about :-)
> 
> Even without the "i" the division could easily give 8.999999.
> So you'd be relying on rounding to get the required integral value.
> 
> > But we have intlog10(), I completely forgot about it.
> 
> And it isn't the function the code is looking for.
> (The result is shifted left 24 and it doesn't have an optimisation
> for constants.)

Do you have an idea how to improve that?

-- 
With Best Regards,
Andy Shevchenko



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

* Re: [PATCH v1 1/1] iio: common: scmi_sensors: Replace const_ilog2() with ilog2()
  2025-10-31 12:51       ` Andy Shevchenko
@ 2025-10-31 16:13         ` David Laight
  2025-11-03  8:30           ` Andy Shevchenko
  0 siblings, 1 reply; 7+ messages in thread
From: David Laight @ 2025-10-31 16:13 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Jonathan Cameron, linux-iio, linux-kernel, Jyoti Bhayana,
	Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko

On Fri, 31 Oct 2025 14:51:57 +0200
Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:

> On Fri, Oct 31, 2025 at 12:45:30PM +0000, David Laight wrote:
> > On Fri, 31 Oct 2025 11:54:30 +0200
> > Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:  
> > > On Fri, Oct 31, 2025 at 09:43:36AM +0000, David Laight wrote:  
> > > > On Fri, 31 Oct 2025 08:45:00 +0100
> > > > Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:    
> 
> ...
> 
> > > > >  		tstamp_scale = sensor->sensor_info->tstamp_scale +
> > > > > -			       const_ilog2(NSEC_PER_SEC) / const_ilog2(10);
> > > > > +			       ilog2(NSEC_PER_SEC) / ilog2(10);    
> > > > 
> > > > Is that just a strange way of writing 9 ?    
> > > 
> > > Why? It's correct way of writing log¹⁰(NSEC_PER_SEC), the problem here is that
> > > "i" people do not think about :-)  
> > 
> > Even without the "i" the division could easily give 8.999999.
> > So you'd be relying on rounding to get the required integral value.
> >   
> > > But we have intlog10(), I completely forgot about it.  
> > 
> > And it isn't the function the code is looking for.
> > (The result is shifted left 24 and it doesn't have an optimisation
> > for constants.)  
> 
> Do you have an idea how to improve that?

Not sure I'd want to get cpp to generate a high-precision log.
It if definitely doable, but will be a mind-blowing mess.
(and I'm not sure how many MB the expanded line would be).
An ilog10() would be easier (probably looking like const_ilog2()).

But for this code just use '+ 9' and add a suitable comment :-)

	David



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

* Re: [PATCH v1 1/1] iio: common: scmi_sensors: Replace const_ilog2() with ilog2()
  2025-10-31 16:13         ` David Laight
@ 2025-11-03  8:30           ` Andy Shevchenko
  0 siblings, 0 replies; 7+ messages in thread
From: Andy Shevchenko @ 2025-11-03  8:30 UTC (permalink / raw)
  To: David Laight
  Cc: Jonathan Cameron, linux-iio, linux-kernel, Jyoti Bhayana,
	Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko

On Fri, Oct 31, 2025 at 04:13:31PM +0000, David Laight wrote:
> On Fri, 31 Oct 2025 14:51:57 +0200
> Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
> > On Fri, Oct 31, 2025 at 12:45:30PM +0000, David Laight wrote:

...

> > Do you have an idea how to improve that?
> 
> Not sure I'd want to get cpp to generate a high-precision log.
> It if definitely doable, but will be a mind-blowing mess.
> (and I'm not sure how many MB the expanded line would be).
> An ilog10() would be easier (probably looking like const_ilog2()).
> 
> But for this code just use '+ 9' and add a suitable comment :-)

v2 is sent: 20251103082937.4081863-1-andriy.shevchenko@linux.intel.com


-- 
With Best Regards,
Andy Shevchenko



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

end of thread, other threads:[~2025-11-03  8:30 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-31  7:45 [PATCH v1 1/1] iio: common: scmi_sensors: Replace const_ilog2() with ilog2() Andy Shevchenko
2025-10-31  9:43 ` David Laight
2025-10-31  9:54   ` Andy Shevchenko
2025-10-31 12:45     ` David Laight
2025-10-31 12:51       ` Andy Shevchenko
2025-10-31 16:13         ` David Laight
2025-11-03  8:30           ` Andy Shevchenko

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