Skip to contentSkip to conversation

powerpc/lib/sstep: Fix count leading zeros instructions

PATCH Applied Sandipan Das 3 participants 3 messages Oct 10, 2017 – Oct 12, 2017 PowerPC

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 powerpc/lib/sstep: Fix count leading zeros instructions

According to the GCC documentation, the behaviour of __builtin_clz() and __builtin_clzl() is undefined if the value of the input argument is zero. Without handling this special case, these builtins have been used for emulating the following instructions:
* Count Leading Zeros Word (cntlzw[.])
* Count Leading Zeros Doubleword (cntlzd[.])

This fixes the emulated behaviour of these instructions by adding an additional check for this special case.

Fixes 3cdfcbfd32b9d ("powerpc: Change analyse_instr so it doesn't modify *regs")

---
v2: Make zero-checking condition more compact.
Add details of original commit that is being fixed here.

---
 arch/powerpc/lib/sstep.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
arch/powerpc/lib/sstep.c +4 -2
index 5118110c3983..8c3955e183d4 100644
@@ -1699,11 +1699,13 @@ int analyse_instr(struct instruction_op *op, const struct pt_regs *regs,
  * Logical instructions
  */
 		case 26:	/* cntlzw */
-			op->val = __builtin_clz((unsigned int) regs->gpr[rd]);
+			val = (unsigned int) regs->gpr[rd];
+			op->val = ( val ? __builtin_clz(val) : 32 );
 			goto logical_done;
 #ifdef __powerpc64__
 		case 58:	/* cntlzd */
-			op->val = __builtin_clzl(regs->gpr[rd]);
+			val = regs->gpr[rd];
+			op->val = ( val ? __builtin_clzl(val) : 64 );
 			goto logical_done;
 #endif
 		case 28:	/* and */
Signature · 1 line
2.13.6
Reviewed-by on v2 1/1
#1 lore raw
Quoted 7 lines · Sandipan Das

According to the GCC documentation, the behaviour of __builtin_clz() and __builtin_clzl() is undefined if the value of the input argument is zero. Without handling this special case, these builtins have been used for emulating the following instructions:
* Count Leading Zeros Word (cntlzw[.])
* Count Leading Zeros Doubleword (cntlzd[.])

Quoted 5 lines

This fixes the emulated behaviour of these instructions by adding an additional check for this special case.

Fixes 3cdfcbfd32b9d ("powerpc: Change analyse_instr so it doesn't modify *regs")
Quoted 30 lines

---
v2: Make zero-checking condition more compact.
Add details of original commit that is being fixed here.

---
 arch/powerpc/lib/sstep.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
arch/powerpc/lib/sstep.c +4 -2
index 5118110c3983..8c3955e183d4 100644
@@ -1699,11 +1699,13 @@ int analyse_instr(struct instruction_op *op, const struct pt_regs *regs,
  * Logical instructions
  */
 		case 26:	/* cntlzw */
-			op->val = __builtin_clz((unsigned int) regs->gpr[rd]);
+			val = (unsigned int) regs->gpr[rd];
+			op->val = ( val ? __builtin_clz(val) : 32 );
 			goto logical_done;
 #ifdef __powerpc64__
 		case 58:	/* cntlzd */
-			op->val = __builtin_clzl(regs->gpr[rd]);
+			val = regs->gpr[rd];
+			op->val = ( val ? __builtin_clzl(val) : 64 );
 			goto logical_done;
 #endif
 		case 28:	/* and */
Signature · 1 line
2.13.6
#2 lore raw
In reply to Sandipan Das
Quoted 7 lines · Sandipan Das

According to the GCC documentation, the behaviour of __builtin_clz() and __builtin_clzl() is undefined if the value of the input argument is zero. Without handling this special case, these builtins have been used for emulating the following instructions:
* Count Leading Zeros Word (cntlzw[.])
* Count Leading Zeros Doubleword (cntlzd[.])

Quoted 6 lines

This fixes the emulated behaviour of these instructions by adding an additional check for this special case.

Fixes 3cdfcbfd32b9d ("powerpc: Change analyse_instr so it doesn't modify *regs")

Applied to powerpc fixes, thanks.

https://git.kernel.org/powerpc/c/b0490a04e736356e427e227902b17f

cheers