Skip to contentSkip to conversation

timers: Simplify calc_index()

PATCH Applied Thomas Gleixner 2 participants 2 messages Apr 4, 2022 – Apr 9, 2022 High-resolution timers, timer wheel, clockevents

lore
Share & tools
lore
#0 lore raw

The level granularity round up of calc_index() does:

(x + (1 << n)) >> n

which is obviously equivalent to

(x >> n) + 1

but compilers can't figure that out despite the fact that the input range is known to not cause an overflow. It's neither intuitive to read.

Just write out the obvious.

---
 kernel/time/timer.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/timer.c +1 -1
@@ -502,7 +502,7 @@ static inline unsigned calc_index(unsign
 	 *
 	 * Round up with level granularity to prevent this.
 	 */
-	expires = (expires + LVL_GRAN(lvl)) >> LVL_SHIFT(lvl);
+	expires = (expires >> LVL_SHIFT(lvl)) + 1;
 	*bucket_expiry = expires << LVL_SHIFT(lvl);
 	return LVL_OFFS(lvl) + (expires & LVL_MASK);
 }
#1 lore raw
The following commit has been merged into the timers/core branch of tip:

The following commit has been merged into the timers/core branch of tip:

Commit-ID:     a2026e44eff5d74a83d7ffee6325a007bef85385
Gitweb:        https://git.kernel.org/tip/a2026e44eff5d74a83d7ffee6325a007bef85385
Author:        Thomas Gleixner <[email protected]>
AuthorDate:    Mon, 04 Apr 2022 16:47:55 +02:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Sat, 09 Apr 2022 22:19:39 +02:00

timers: Simplify calc_index()

The level granularity round up of calc_index() does:

(x + (1 << n)) >> n

which is obviously equivalent to

(x >> n) + 1

but compilers can't figure that out despite the fact that the input range is known to not cause an overflow. It's neither intuitive to read.

Just write out the obvious.

---
 kernel/time/timer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/timer.c +1 -1
index 204d6cd..60aebf2 100644
@@ -502,7 +502,7 @@ static inline unsigned calc_index(unsigned long expires, unsigned lvl,
 	 *
 	 * Round up with level granularity to prevent this.
 	 */
-	expires = (expires + LVL_GRAN(lvl)) >> LVL_SHIFT(lvl);
+	expires = (expires >> LVL_SHIFT(lvl)) + 1;
 	*bucket_expiry = expires << LVL_SHIFT(lvl);
 	return LVL_OFFS(lvl) + (expires & LVL_MASK);
 }