time: move the CONFIG_SYS_TIMER_RATE handling to the compiler
authorMichael Walle <michael@walle.cc>
Wed, 17 Aug 2022 19:37:48 +0000 (21:37 +0200)
committerStefan Roese <sr@denx.de>
Tue, 23 Aug 2022 10:38:30 +0000 (12:38 +0200)
CONFIG_SYS_TIMER_RATE might be a dynamic value, i.e. a function call
instead of a static value, thus it has to be evaluated at runtime. If it
is a static value, the compiler should be able to optimize the unused
branches out.

This will be needed for kirkwoods dynamic CONFIG_SYS_TCLK setting.

Cc: Pali Rohár <pali@kernel.org>
Signed-off-by: Michael Walle <michael@walle.cc>
Reviewed-by: Pali Rohár <pali@kernel.org>
Reviewed-by: Stefan Roese <sr@denx.de>
lib/time.c

index 96074b84af69342f1e58b10a5c9fe4ab9299d264..bbf191f6732381d9c1cf223a65c10c82f04f7514 100644 (file)
@@ -47,12 +47,15 @@ ulong timer_get_boot_us(void)
 {
        ulong count = timer_read_counter();
 
-#if CONFIG_SYS_TIMER_RATE == 1000000
-       return count;
-#elif CONFIG_SYS_TIMER_RATE > 1000000
-       return lldiv(count, CONFIG_SYS_TIMER_RATE / 1000000);
-#elif defined(CONFIG_SYS_TIMER_RATE)
-       return (unsigned long long)count * 1000000 / CONFIG_SYS_TIMER_RATE;
+#ifdef CONFIG_SYS_TIMER_RATE
+       const ulong timer_rate = CONFIG_SYS_TIMER_RATE;
+
+       if (timer_rate == 1000000)
+               return count;
+       else if (timer_rate > 1000000)
+               return lldiv(count, timer_rate / 1000000);
+       else
+               return (unsigned long long)count * 1000000 / timer_rate;
 #else
        /* Assume the counter is in microseconds */
        return count;