Skip to contentSkip to conversation

iio: common: scmi_sensors: Replace const_ilog2() with ilog2()

PATCH In review Andy Shevchenko 2 participants 7 messages Oct 31, 2025 – Nov 3, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: common: scmi_sensors: Replace const_ilog2() with ilog2()

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

---
 drivers/iio/common/scmi_sensors/scmi_iio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/common/scmi_sensors/scmi_iio.c +1 -1
index 39c61c47022a..b40c6d6442e6 100644
@@ -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;
Signature · 1 line
2.50.1
#1 lore raw
Quoted 13 lines · On Fri, 31 Oct 2025 08:45:00 +0100 Andy Shevchenko…

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

---
 drivers/iio/common/scmi_sensors/scmi_iio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/common/scmi_sensors/scmi_iio.c +0 -0
index 39c61c47022a..b40c6d6442e6 100644

drivers/iio/common/scmi_sensors/scmi_iio.c static int scmi_iio_sensor_update_cb(struct notifier_block *nb, · line 72

Quoted 5 lines
drivers/iio/common/scmi_sensors/scmi_iio.c +1 -1
@@ -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

Quoted 3 lines
drivers/iio/common/scmi_sensors/scmi_iio.c +0 -0
 		if (tstamp_scale < 0) {
 			do_div(time, int_pow(10, abs(tstamp_scale)));
 			time_ns = time;
#2 lore raw
Quoted 2 lines · David Laight

On Fri, 31 Oct 2025 08:45:00 +0100
Andy Shevchenko <[email protected]> wrote:

...

Quoted 5 lines

drivers/iio/common/scmi_sensors/scmi_iio.c static int scmi_iio_sensor_update_cb(struct notifier_block *nb, · line 72

Quoted 3 lines
drivers/iio/common/scmi_sensors/scmi_iio.c +1 -1
 		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.

Quoted 6 lines

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.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
Quoted 6 lines · On Fri, 31 Oct 2025 11:54:30 +0200 Andy Shevchenko…
Quoted 2 lines · David Laight

On Fri, 31 Oct 2025 08:45:00 +0100
Andy Shevchenko <[email protected]> wrote:

...

Quoted 8 lines
Quoted 5 lines

drivers/iio/common/scmi_sensors/scmi_iio.c static int scmi_iio_sensor_update_cb(struct notifier_block *nb, · line 72

Quoted 3 lines
drivers/iio/common/scmi_sensors/scmi_iio.c +1 -1
 		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.

Quoted 1 line

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.)

Quoted 10 lines
Quoted 7 lines

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

#4 lore raw
Quoted 5 lines · David Laight
Quoted 3 lines · On Fri, 31 Oct 2025 11:54:30 +0200 Andy Shevchenko…
Quoted 2 lines · David Laight

On Fri, 31 Oct 2025 08:45:00 +0100
Andy Shevchenko <[email protected]> wrote:

...

Quoted 9 lines
Quoted 8 lines
Quoted 5 lines

drivers/iio/common/scmi_sensors/scmi_iio.c static int scmi_iio_sensor_update_cb(struct notifier_block *nb, · line 72

Quoted 3 lines
drivers/iio/common/scmi_sensors/scmi_iio.c +1 -1
 		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 :-)

Quoted 8 lines

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

Quoted 1 line

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?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
Quoted 21 lines · On Fri, 31 Oct 2025 14:51:57 +0200 Andy Shevchenko…
Quoted 5 lines · David Laight
Quoted 3 lines · On Fri, 31 Oct 2025 11:54:30 +0200 Andy Shevchenko…
Quoted 2 lines · David Laight

On Fri, 31 Oct 2025 08:45:00 +0100
Andy Shevchenko <[email protected]> wrote:

...

Quoted 12 lines
Quoted 8 lines
Quoted 5 lines

drivers/iio/common/scmi_sensors/scmi_iio.c static int scmi_iio_sensor_update_cb(struct notifier_block *nb, · line 72

Quoted 3 lines
drivers/iio/common/scmi_sensors/scmi_iio.c +1 -1
 		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.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

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

#6 lore raw
Quoted 3 lines · David Laight
Quoted 1 line · On Fri, 31 Oct 2025 14:51:57 +0200 Andy Shevchenko…

On Fri, Oct 31, 2025 at 12:45:30PM +0000, David Laight wrote:

...

Quoted 8 lines
Quoted 1 line

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: [email protected]

Signature · 2 lines
With Best Regards,
Andy Shevchenko