Skip to content

Commit 7919a57

Browse files
Andreas Herrmannlinvjw
authored andcommitted
bitops: Provide generic sign_extend32 function
This patch moves code out from wireless drivers where two different functions are defined in three code locations for the same purpose and provides a common function to sign extend a 32-bit value. Signed-off-by: Andreas Herrmann <[email protected]> Signed-off-by: John W. Linville <[email protected]>
1 parent e4b3fdb commit 7919a57

File tree

7 files changed

+30
-47
lines changed

7 files changed

+30
-47
lines changed

drivers/net/wireless/ath/ath5k/phy.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,18 +1102,12 @@ int ath5k_hw_channel(struct ath5k_hw *ah, struct ieee80211_channel *channel)
11021102
PHY calibration
11031103
\*****************/
11041104

1105-
static int sign_extend(int val, const int nbits)
1106-
{
1107-
int order = BIT(nbits-1);
1108-
return (val ^ order) - order;
1109-
}
1110-
11111105
static s32 ath5k_hw_read_measured_noise_floor(struct ath5k_hw *ah)
11121106
{
11131107
s32 val;
11141108

11151109
val = ath5k_hw_reg_read(ah, AR5K_PHY_NF);
1116-
return sign_extend(AR5K_REG_MS(val, AR5K_PHY_NF_MINCCA_PWR), 9);
1110+
return sign_extend32(AR5K_REG_MS(val, AR5K_PHY_NF_MINCCA_PWR), 8);
11171111
}
11181112

11191113
void ath5k_hw_init_nfcal_hist(struct ath5k_hw *ah)

drivers/net/wireless/ath/ath9k/ar5008_phy.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,25 +1490,25 @@ static void ar5008_hw_do_getnf(struct ath_hw *ah,
14901490
int16_t nf;
14911491

14921492
nf = MS(REG_READ(ah, AR_PHY_CCA), AR_PHY_MINCCA_PWR);
1493-
nfarray[0] = sign_extend(nf, 9);
1493+
nfarray[0] = sign_extend32(nf, 8);
14941494

14951495
nf = MS(REG_READ(ah, AR_PHY_CH1_CCA), AR_PHY_CH1_MINCCA_PWR);
1496-
nfarray[1] = sign_extend(nf, 9);
1496+
nfarray[1] = sign_extend32(nf, 8);
14971497

14981498
nf = MS(REG_READ(ah, AR_PHY_CH2_CCA), AR_PHY_CH2_MINCCA_PWR);
1499-
nfarray[2] = sign_extend(nf, 9);
1499+
nfarray[2] = sign_extend32(nf, 8);
15001500

15011501
if (!IS_CHAN_HT40(ah->curchan))
15021502
return;
15031503

15041504
nf = MS(REG_READ(ah, AR_PHY_EXT_CCA), AR_PHY_EXT_MINCCA_PWR);
1505-
nfarray[3] = sign_extend(nf, 9);
1505+
nfarray[3] = sign_extend32(nf, 8);
15061506

15071507
nf = MS(REG_READ(ah, AR_PHY_CH1_EXT_CCA), AR_PHY_CH1_EXT_MINCCA_PWR);
1508-
nfarray[4] = sign_extend(nf, 9);
1508+
nfarray[4] = sign_extend32(nf, 8);
15091509

15101510
nf = MS(REG_READ(ah, AR_PHY_CH2_EXT_CCA), AR_PHY_CH2_EXT_MINCCA_PWR);
1511-
nfarray[5] = sign_extend(nf, 9);
1511+
nfarray[5] = sign_extend32(nf, 8);
15121512
}
15131513

15141514
/*

drivers/net/wireless/ath/ath9k/ar9002_phy.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -473,21 +473,21 @@ static void ar9002_hw_do_getnf(struct ath_hw *ah,
473473
int16_t nf;
474474

475475
nf = MS(REG_READ(ah, AR_PHY_CCA), AR9280_PHY_MINCCA_PWR);
476-
nfarray[0] = sign_extend(nf, 9);
476+
nfarray[0] = sign_extend32(nf, 8);
477477

478478
nf = MS(REG_READ(ah, AR_PHY_EXT_CCA), AR9280_PHY_EXT_MINCCA_PWR);
479479
if (IS_CHAN_HT40(ah->curchan))
480-
nfarray[3] = sign_extend(nf, 9);
480+
nfarray[3] = sign_extend32(nf, 8);
481481

482482
if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
483483
return;
484484

485485
nf = MS(REG_READ(ah, AR_PHY_CH1_CCA), AR9280_PHY_CH1_MINCCA_PWR);
486-
nfarray[1] = sign_extend(nf, 9);
486+
nfarray[1] = sign_extend32(nf, 8);
487487

488488
nf = MS(REG_READ(ah, AR_PHY_CH1_EXT_CCA), AR9280_PHY_CH1_EXT_MINCCA_PWR);
489489
if (IS_CHAN_HT40(ah->curchan))
490-
nfarray[4] = sign_extend(nf, 9);
490+
nfarray[4] = sign_extend32(nf, 8);
491491
}
492492

493493
static void ar9002_hw_set_nf_limits(struct ath_hw *ah)

drivers/net/wireless/ath/ath9k/ar9003_phy.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,25 +1023,25 @@ static void ar9003_hw_do_getnf(struct ath_hw *ah,
10231023
int16_t nf;
10241024

10251025
nf = MS(REG_READ(ah, AR_PHY_CCA_0), AR_PHY_MINCCA_PWR);
1026-
nfarray[0] = sign_extend(nf, 9);
1026+
nfarray[0] = sign_extend32(nf, 8);
10271027

10281028
nf = MS(REG_READ(ah, AR_PHY_CCA_1), AR_PHY_CH1_MINCCA_PWR);
1029-
nfarray[1] = sign_extend(nf, 9);
1029+
nfarray[1] = sign_extend32(nf, 8);
10301030

10311031
nf = MS(REG_READ(ah, AR_PHY_CCA_2), AR_PHY_CH2_MINCCA_PWR);
1032-
nfarray[2] = sign_extend(nf, 9);
1032+
nfarray[2] = sign_extend32(nf, 8);
10331033

10341034
if (!IS_CHAN_HT40(ah->curchan))
10351035
return;
10361036

10371037
nf = MS(REG_READ(ah, AR_PHY_EXT_CCA), AR_PHY_EXT_MINCCA_PWR);
1038-
nfarray[3] = sign_extend(nf, 9);
1038+
nfarray[3] = sign_extend32(nf, 8);
10391039

10401040
nf = MS(REG_READ(ah, AR_PHY_EXT_CCA_1), AR_PHY_CH1_EXT_MINCCA_PWR);
1041-
nfarray[4] = sign_extend(nf, 9);
1041+
nfarray[4] = sign_extend32(nf, 8);
10421042

10431043
nf = MS(REG_READ(ah, AR_PHY_EXT_CCA_2), AR_PHY_CH2_EXT_MINCCA_PWR);
1044-
nfarray[5] = sign_extend(nf, 9);
1044+
nfarray[5] = sign_extend32(nf, 8);
10451045
}
10461046

10471047
static void ar9003_hw_set_nf_limits(struct ath_hw *ah)

drivers/net/wireless/ath/ath9k/hw.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -825,12 +825,6 @@ static inline struct ath_hw_ops *ath9k_hw_ops(struct ath_hw *ah)
825825
return &ah->ops;
826826
}
827827

828-
static inline int sign_extend(int val, const int nbits)
829-
{
830-
int order = BIT(nbits-1);
831-
return (val ^ order) - order;
832-
}
833-
834828
/* Initialization, Detach, Reset */
835829
const char *ath9k_hw_probe(u16 vendorid, u16 devid);
836830
void ath9k_hw_deinit(struct ath_hw *ah);

drivers/net/wireless/iwlwifi/iwl-4965.c

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,22 +1686,6 @@ static void iwl4965_txq_update_byte_cnt_tbl(struct iwl_priv *priv,
16861686
tfd_offset[TFD_QUEUE_SIZE_MAX + write_ptr] = bc_ent;
16871687
}
16881688

1689-
/**
1690-
* sign_extend - Sign extend a value using specified bit as sign-bit
1691-
*
1692-
* Example: sign_extend(9, 3) would return -7 as bit3 of 1001b is 1
1693-
* and bit0..2 is 001b which when sign extended to 1111111111111001b is -7.
1694-
*
1695-
* @param oper value to sign extend
1696-
* @param index 0 based bit index (0<=index<32) to sign bit
1697-
*/
1698-
static s32 sign_extend(u32 oper, int index)
1699-
{
1700-
u8 shift = 31 - index;
1701-
1702-
return (s32)(oper << shift) >> shift;
1703-
}
1704-
17051689
/**
17061690
* iwl4965_hw_get_temperature - return the calibrated temperature (in Kelvin)
17071691
* @statistics: Provides the temperature reading from the uCode
@@ -1739,9 +1723,9 @@ static int iwl4965_hw_get_temperature(struct iwl_priv *priv)
17391723
* "initialize" ALIVE response.
17401724
*/
17411725
if (!test_bit(STATUS_TEMPERATURE, &priv->status))
1742-
vt = sign_extend(R4, 23);
1726+
vt = sign_extend32(R4, 23);
17431727
else
1744-
vt = sign_extend(le32_to_cpu(priv->_agn.statistics.
1728+
vt = sign_extend32(le32_to_cpu(priv->_agn.statistics.
17451729
general.common.temperature), 23);
17461730

17471731
IWL_DEBUG_TEMP(priv, "Calib values R[1-3]: %d %d %d R4: %d\n", R1, R2, R3, vt);

include/linux/bitops.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ static inline __u8 ror8(__u8 word, unsigned int shift)
109109
return (word >> shift) | (word << (8 - shift));
110110
}
111111

112+
/**
113+
* sign_extend32 - sign extend a 32-bit value using specified bit as sign-bit
114+
* @value: value to sign extend
115+
* @index: 0 based bit index (0<=index<32) to sign bit
116+
*/
117+
static inline __s32 sign_extend32(__u32 value, int index)
118+
{
119+
__u8 shift = 31 - index;
120+
return (__s32)(value << shift) >> shift;
121+
}
122+
112123
static inline unsigned fls_long(unsigned long l)
113124
{
114125
if (sizeof(l) == 4)

0 commit comments

Comments
 (0)