RE: [PATCH] powerpc/lib/sstep: Fix count leading zeros instructions
From: David Laight <hidden>
Date: 2017-10-09 13:49:29
From: Sandipan Das
Sent: 09 October 2017 12:07 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[.]) =20 This fixes the emulated behaviour of these instructions by adding an additional check for this special case.
Presumably the result is undefined because the underlying cpu instruction is used - and it's return value is implementation defined. Here you are emulating the cpu instruction - so executing one will give the same answer as it the 'real' one were execucted. Indeed it might be worth an asm statement that definitely executes the cpu instruction? David
quoted hunk ↗ jump to hunk
=20 Signed-off-by: Sandipan Das <redacted> --- arch/powerpc/lib/sstep.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) =20diff --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, cons=
t struct pt_regs *regs,
* Logical instructions */ case 26: /* cntlzw */ - op->val =3D __builtin_clz((unsigned int) regs->gpr[rd]); + val =3D (unsigned int) regs->gpr[rd]; + if (val =3D=3D 0) + op->val =3D 32; + else + op->val =3D __builtin_clz(val); goto logical_done; #ifdef __powerpc64__ case 58: /* cntlzd */ - op->val =3D __builtin_clzl(regs->gpr[rd]); + val =3D regs->gpr[rd]; + if (val =3D=3D 0) + op->val =3D 64; + else + op->val =3D __builtin_clzl(val); goto logical_done; #endif case 28: /* and */ -- 2.13.6