diff mbox series

[01/22] time: move the CONFIG_SYS_TIMER_RATE handling to the compiler

Message ID 20220817193809.1059688-2-michael@walle.cc
State Accepted
Commit 616278bd2c563a6d12a2347ce5cfc3512f77ee42
Delegated to: Stefan Roese
Headers show
Series board: lsxl: major update and DM conversion | expand

Commit Message

Michael Walle Aug. 17, 2022, 7:37 p.m. UTC
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>
---
 lib/time.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

Comments

Pali Rohár Aug. 17, 2022, 7:51 p.m. UTC | #1
On Wednesday 17 August 2022 21:37:48 Michael Walle wrote:
> 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>

> ---
>  lib/time.c | 15 +++++++++------
>  1 file changed, 9 insertions(+), 6 deletions(-)
> 
> diff --git a/lib/time.c b/lib/time.c
> index 96074b84af..bbf191f673 100644
> --- a/lib/time.c
> +++ b/lib/time.c
> @@ -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;
> -- 
> 2.30.2
>
diff mbox series

Patch

diff --git a/lib/time.c b/lib/time.c
index 96074b84af..bbf191f673 100644
--- a/lib/time.c
+++ b/lib/time.c
@@ -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;