All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] powerpc/lib/sstep: Fix count leading zeros instructions
@ 2017-10-09 11:07 Sandipan Das
  2017-10-09 13:49 ` David Laight
  2017-10-09 14:45 ` Naveen N. Rao
  0 siblings, 2 replies; 8+ messages in thread
From: Sandipan Das @ 2017-10-09 11:07 UTC (permalink / raw)
  To: mpe; +Cc: naveen.n.rao, paulus, anton, segher, linuxppc-dev

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.

Signed-off-by: Sandipan Das <sandipan@linux.vnet.ibm.com>
---
 arch/powerpc/lib/sstep.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/arch/powerpc/lib/sstep.c b/arch/powerpc/lib/sstep.c
index 0f7e41bd7e88..ebbc0b92650c 100644
--- a/arch/powerpc/lib/sstep.c
+++ b/arch/powerpc/lib/sstep.c
@@ -1717,11 +1717,19 @@ 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];
+			if (val == 0)
+				op->val = 32;
+			else
+				op->val = __builtin_clz(val);
 			goto logical_done;
 #ifdef __powerpc64__
 		case 58:	/* cntlzd */
-			op->val = __builtin_clzl(regs->gpr[rd]);
+			val = regs->gpr[rd];
+			if (val == 0)
+				op->val = 64;
+			else
+				op->val = __builtin_clzl(val);
 			goto logical_done;
 #endif
 		case 28:	/* and */
-- 
2.13.6

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

end of thread, other threads:[~2017-10-09 15:24 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-10-09 11:07 [PATCH] powerpc/lib/sstep: Fix count leading zeros instructions Sandipan Das
2017-10-09 13:49 ` David Laight
2017-10-09 14:20   ` Segher Boessenkool
2017-10-09 14:43     ` David Laight
2017-10-09 14:47       ` naveen.n.rao
2017-10-09 15:24         ` David Laight
2017-10-09 14:47       ` Segher Boessenkool
2017-10-09 14:45 ` Naveen N. Rao

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.