Skip to content

Commit 69acbdb

Browse files
Nikolay Borisovbp3tk0v
authored andcommitted
RAS/AMD/ATL: Replace bitwise_xor_bits() with hweight16()
Doing hweight16() and checking whether the lsb is set is functionally equivalent to what bitwise_xor_bits() does. In addition, it results in better generated code as before gcc would inline the function 4 times. With hweight16(), the resulting code boils down to 2 instructions - POPCNT and AND, and all relevant CPUs support POPCNT. An alternative would have been to use the __builtin_parity() function provided by both Clang/GCC, however under some circumstances the compiler can choose not to inline it but generate a library call which is unsupported in the kernel. No functional changes. [ bp: Massage commit message. ] Signed-off-by: Nikolay Borisov <nik.borisov@suse.com> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Link: https://patch.msgid.link/20251124142517.1708451-1-nik.borisov@suse.com
1 parent 187d1b2 commit 69acbdb

1 file changed

Lines changed: 5 additions & 16 deletions

File tree

drivers/ras/amd/atl/umc.c

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,6 @@ static u8 get_coh_st_inst_id_mi300(struct atl_err *err)
4949
return i;
5050
}
5151

52-
/* XOR the bits in @val. */
53-
static u16 bitwise_xor_bits(u16 val)
54-
{
55-
u16 tmp = 0;
56-
u8 i;
57-
58-
for (i = 0; i < 16; i++)
59-
tmp ^= (val >> i) & 0x1;
60-
61-
return tmp;
62-
}
6352

6453
struct xor_bits {
6554
bool xor_enable;
@@ -250,17 +239,17 @@ static unsigned long convert_dram_to_norm_addr_mi300(unsigned long addr)
250239
if (!addr_hash.bank[i].xor_enable)
251240
continue;
252241

253-
temp = bitwise_xor_bits(col & addr_hash.bank[i].col_xor);
254-
temp ^= bitwise_xor_bits(row & addr_hash.bank[i].row_xor);
242+
temp = hweight16(col & addr_hash.bank[i].col_xor) & 1;
243+
temp ^= hweight16(row & addr_hash.bank[i].row_xor) & 1;
255244
bank ^= temp << i;
256245
}
257246

258247
/* Calculate hash for PC bit. */
259248
if (addr_hash.pc.xor_enable) {
260-
temp = bitwise_xor_bits(col & addr_hash.pc.col_xor);
261-
temp ^= bitwise_xor_bits(row & addr_hash.pc.row_xor);
249+
temp = hweight16(col & addr_hash.pc.col_xor) & 1;
250+
temp ^= hweight16(row & addr_hash.pc.row_xor) & 1;
262251
/* Bits SID[1:0] act as Bank[5:4] for PC hash, so apply them here. */
263-
temp ^= bitwise_xor_bits((bank | sid << NUM_BANK_BITS) & addr_hash.bank_xor);
252+
temp ^= hweight16((bank | sid << NUM_BANK_BITS) & addr_hash.bank_xor) & 1;
264253
pc ^= temp;
265254
}
266255

0 commit comments

Comments
 (0)