linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ALSA: mips/hal2: Refactor and improve hal2_compute_rate()
@ 2025-06-30 21:45 Thorsten Blum
  2025-07-01  9:25 ` Takashi Iwai
  0 siblings, 1 reply; 4+ messages in thread
From: Thorsten Blum @ 2025-06-30 21:45 UTC (permalink / raw)
  To: Jaroslav Kysela, Takashi Iwai, Uwe Kleine-König
  Cc: Thorsten Blum, Takashi Iwai, linux-sound, linux-kernel

Assign 'codec->inc' first and then use it instead of the hardcoded
value 4 repeatedly.

Replace the if/else statement with a ternary operator and calculate
'codec->mod' directly. Remove the unnecessary local variable 'mod'.

Return the computed rate directly instead of updating the local variable
first.

No functional changes intended.

Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
 sound/mips/hal2.c | 16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)

diff --git a/sound/mips/hal2.c b/sound/mips/hal2.c
index 991793e6bda9..dd74bab531b4 100644
--- a/sound/mips/hal2.c
+++ b/sound/mips/hal2.c
@@ -313,21 +313,11 @@ static irqreturn_t hal2_interrupt(int irq, void *dev_id)
 
 static int hal2_compute_rate(struct hal2_codec *codec, unsigned int rate)
 {
-	unsigned short mod;
-
-	if (44100 % rate < 48000 % rate) {
-		mod = 4 * 44100 / rate;
-		codec->master = 44100;
-	} else {
-		mod = 4 * 48000 / rate;
-		codec->master = 48000;
-	}
-
 	codec->inc = 4;
-	codec->mod = mod;
-	rate = 4 * codec->master / mod;
+	codec->master = (44100 % rate < 48000 % rate) ? 44100 : 48000;
+	codec->mod = codec->inc * codec->master / rate;
 
-	return rate;
+	return codec->inc * codec->master / codec->mod;
 }
 
 static void hal2_set_dac_rate(struct snd_hal2 *hal2)
-- 
2.50.0


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

* Re: [PATCH] ALSA: mips/hal2: Refactor and improve hal2_compute_rate()
  2025-06-30 21:45 [PATCH] ALSA: mips/hal2: Refactor and improve hal2_compute_rate() Thorsten Blum
@ 2025-07-01  9:25 ` Takashi Iwai
  2025-07-01 10:18   ` Thorsten Blum
  0 siblings, 1 reply; 4+ messages in thread
From: Takashi Iwai @ 2025-07-01  9:25 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Jaroslav Kysela, Takashi Iwai, Uwe Kleine-König,
	Takashi Iwai, linux-sound, linux-kernel

On Mon, 30 Jun 2025 23:45:52 +0200,
Thorsten Blum wrote:
> 
> Assign 'codec->inc' first and then use it instead of the hardcoded
> value 4 repeatedly.
> 
> Replace the if/else statement with a ternary operator and calculate
> 'codec->mod' directly. Remove the unnecessary local variable 'mod'.
> 
> Return the computed rate directly instead of updating the local variable
> first.
> 
> No functional changes intended.
> 
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> ---
>  sound/mips/hal2.c | 16 +++-------------
>  1 file changed, 3 insertions(+), 13 deletions(-)
> 
> diff --git a/sound/mips/hal2.c b/sound/mips/hal2.c
> index 991793e6bda9..dd74bab531b4 100644
> --- a/sound/mips/hal2.c
> +++ b/sound/mips/hal2.c
> @@ -313,21 +313,11 @@ static irqreturn_t hal2_interrupt(int irq, void *dev_id)
>  
>  static int hal2_compute_rate(struct hal2_codec *codec, unsigned int rate)
>  {
> -	unsigned short mod;
> -
> -	if (44100 % rate < 48000 % rate) {
> -		mod = 4 * 44100 / rate;
> -		codec->master = 44100;
> -	} else {
> -		mod = 4 * 48000 / rate;
> -		codec->master = 48000;
> -	}
> -
>  	codec->inc = 4;
> -	codec->mod = mod;
> -	rate = 4 * codec->master / mod;
> +	codec->master = (44100 % rate < 48000 % rate) ? 44100 : 48000;
> +	codec->mod = codec->inc * codec->master / rate;
>  
> -	return rate;
> +	return codec->inc * codec->master / codec->mod;

IMHO, this doesn't look improving the code readability than the
original code.  And the generated code doesn't seem significantly
better, either.


thanks,

Takashi

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

* Re: [PATCH] ALSA: mips/hal2: Refactor and improve hal2_compute_rate()
  2025-07-01  9:25 ` Takashi Iwai
@ 2025-07-01 10:18   ` Thorsten Blum
  2025-07-01 11:49     ` Takashi Iwai
  0 siblings, 1 reply; 4+ messages in thread
From: Thorsten Blum @ 2025-07-01 10:18 UTC (permalink / raw)
  To: Takashi Iwai
  Cc: Jaroslav Kysela, Takashi Iwai, Uwe Kleine-König, linux-sound,
	linux-kernel

On 1. Jul 2025, at 11:25, Takashi Iwai wrote:
> IMHO, this doesn't look improving the code readability than the
> original code.  And the generated code doesn't seem significantly
> better, either.

I didn't look at the generated code, but I think the patch definitely
improves the function (not necessarily its runtime, but its readability
and maintainability).

I think the patch primarily improves maintainability by eliminating the
magic number '4' that was scattered throughout the function. Now the
scaling factor is assigned once to the semantically more meaningful
variable 'codec->inc' and used consistently.

It also improves consistency by using 'codec->master' when calculating
'codec->mod' instead of repeating the constants '44100' and '48000'.

Additionally, it removes the unnecessary local variable 'mod' and the
'rate' update, making the function more concise (4 vs 12 lines).

Thanks,
Thorsten


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

* Re: [PATCH] ALSA: mips/hal2: Refactor and improve hal2_compute_rate()
  2025-07-01 10:18   ` Thorsten Blum
@ 2025-07-01 11:49     ` Takashi Iwai
  0 siblings, 0 replies; 4+ messages in thread
From: Takashi Iwai @ 2025-07-01 11:49 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Takashi Iwai, Jaroslav Kysela, Takashi Iwai,
	Uwe Kleine-König, linux-sound, linux-kernel

On Tue, 01 Jul 2025 12:18:44 +0200,
Thorsten Blum wrote:
> 
> On 1. Jul 2025, at 11:25, Takashi Iwai wrote:
> > IMHO, this doesn't look improving the code readability than the
> > original code.  And the generated code doesn't seem significantly
> > better, either.
> 
> I didn't look at the generated code, but I think the patch definitely
> improves the function (not necessarily its runtime, but its readability
> and maintainability).
> 
> I think the patch primarily improves maintainability by eliminating the
> magic number '4' that was scattered throughout the function. Now the
> scaling factor is assigned once to the semantically more meaningful
> variable 'codec->inc' and used consistently.
> 
> It also improves consistency by using 'codec->master' when calculating
> 'codec->mod' instead of repeating the constants '44100' and '48000'.
> 
> Additionally, it removes the unnecessary local variable 'mod' and the
> 'rate' update, making the function more concise (4 vs 12 lines).

The line length doesn't matter.  It's a small code.  And it's no
hot-path, and no common code used by many drivers, either.
That is, the only question is how better the code is readable.
And, often a simple if-block can be easier to follow the code flow --
that's your case, too.

In anyway, this is really really minor issue and we shouldn't waste
too much time just for this kind of optimization. 


thanks,

Takashi

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

end of thread, other threads:[~2025-07-01 11:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-30 21:45 [PATCH] ALSA: mips/hal2: Refactor and improve hal2_compute_rate() Thorsten Blum
2025-07-01  9:25 ` Takashi Iwai
2025-07-01 10:18   ` Thorsten Blum
2025-07-01 11:49     ` Takashi Iwai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).