staging: vt6656: Use fls instead of for loop in vnt_update_top_rates
PATCH 1/1 staging: vt6656: Use fls instead of for loop in vnt_update_top_rates
Replace the for loops of the vnt_update_top_rates function by the fls function.
The purpose of the two for loops is to find the most significant bit set in a range of bits. So, they can be replace by the fls function (find last set) with a previous mask to define the range.
This way avoid the iteration over unnecessary for loops.
The header "linux/bits.h" can be remove as it is included in the header "linux/bitops.h".
--- drivers/staging/vt6656/card.c | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-)
drivers/staging/vt6656/card.c +6 -22
@@ -26,7 +26,7 @@* */ -#include <linux/bits.h> +#include <linux/bitops.h> #include "device.h" #include "card.h" #include "baseband.h"
@@ -223,29 +223,13 @@ void vnt_update_ifs(struct vnt_private *priv)void vnt_update_top_rates (struct vnt_private *priv) { -u8 top_ofdm =RATE_24M , top_cck =RATE_1M ; -u8 i; +int pos; -/*Determines the highest basic rate.*/ -for (i =RATE_54M ; i >=RATE_6M ; i--) { -if (priv->basic_rates & (u16)(1 << i)) { - top_ofdm = i; -break ; - } - } - - priv->top_ofdm_basic_rate = top_ofdm; - -for (i =RATE_11M ;; i--) { -if (priv->basic_rates & (u16)(1 << i)) { - top_cck = i; -break ; - } -if (i ==RATE_1M ) -break ; - } + pos =fls (priv->basic_rates &GENMASK (RATE_54M ,RATE_6M )); + priv->top_ofdm_basic_rate = pos ? pos-- :RATE_24M ; - priv->top_cck_basic_rate = top_cck; + pos =fls (priv->basic_rates &GENMASK (RATE_11M ,RATE_1M )); + priv->top_cck_basic_rate = pos ? pos-- :RATE_1M ; }int vnt_ofdm_min_rate (struct vnt_private *priv)
--
2.20.1
drivers/staging/vt6656/card.c void vnt_update_ifs(struct vnt_private *priv) · line 229
Quoted 10 lines · Oscar Carter
drivers/staging/vt6656/card.c +2
-8-for (i =RATE_11M ;; i--) { -if (priv->basic_rates & (u16)(1 << i)) { - top_cck = i; -break ; - } -if (i ==RATE_1M ) -break ; - } + pos =fls (priv->basic_rates &GENMASK (RATE_54M ,RATE_6M )); + priv->top_ofdm_basic_rate = pos ? pos-- :RATE_24M ;
^^^^^
Argh... Come on. I don't want to have to break out the C standard to see if this is defined behavior and where the sequence points are. A pre-op would be clear but the most clear thing is to write it like this:
priv->top_ofdm_basic_rate = pos ? (pos - 1) : RATE_24M;
drivers/staging/vt6656/card.c void vnt_update_ifs(struct vnt_private *priv) · line 232
Quoted 4 lines
drivers/staging/vt6656/card.c +2
-1- priv->top_cck_basic_rate = top_cck; + pos =fls (priv->basic_rates &GENMASK (RATE_11M ,RATE_1M )); + priv->top_cck_basic_rate = pos ? pos-- :RATE_1M ;
^^^^^
Same.
regards,
dan carpenter
Quoted 12 lines · Dan Carpenter
drivers/staging/vt6656/card.c void vnt_update_ifs(struct vnt_private *priv) · line 229
Quoted 10 lines · Oscar Carter
drivers/staging/vt6656/card.c +2
-8-for (i =RATE_11M ;; i--) { -if (priv->basic_rates & (u16)(1 << i)) { - top_cck = i; -break ; - } -if (i ==RATE_1M ) -break ; - } + pos =fls (priv->basic_rates &GENMASK (RATE_54M ,RATE_6M )); + priv->top_ofdm_basic_rate = pos ? pos-- :RATE_24M ;^^^^^
Quoted 6 lines
Argh... Come on. I don't want to have to break out the C standard to see if this is defined behavior and where the sequence points are. A pre-op would be clear but the most clear thing is to write it like this:
priv->top_ofdm_basic_rate = pos ? (pos - 1) : RATE_24M;
Ok, I do the modification as you suggested and resend a new version.
Quoted 11 lines
drivers/staging/vt6656/card.c void vnt_update_ifs(struct vnt_private *priv) · line 232
Quoted 5 lines
drivers/staging/vt6656/card.c +2
-1- priv->top_cck_basic_rate = top_cck; + pos =fls (priv->basic_rates &GENMASK (RATE_11M ,RATE_1M )); + priv->top_cck_basic_rate = pos ? pos-- :RATE_1M ;^^^^^Same.
regards,
dan carpenter
thanks,
oscar carter