[PATCH] disas/sparc: Use unsigned long for shift operation

Ankush posted 1 patch 2 weeks, 5 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261103192559.297757-1-mondalankush9851@gmail.com
Maintainers: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>, Artyom Tarasenko <atar4qemu@gmail.com>
disas/sparc.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
[PATCH] disas/sparc: Use unsigned long for shift operation
Posted by Ankush 2 weeks, 5 days ago
The shift expression uses an integer constant of type int.
When i is 31, the result sets the sign bit, and its
conversion to int does not portably preserve the intended
mask value.

Use an unsigned long constant (1ul) for the left operand
to ensure that the shift is performed using unsigned long.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/2618

Signed-off-by: Ankush <mondalankush9851@gmail.com>
---
 disas/sparc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/disas/sparc.c b/disas/sparc.c
index 5689533ce1..40c1164554 100644
--- a/disas/sparc.c
+++ b/disas/sparc.c
@@ -2515,7 +2515,7 @@ compare_opcodes (const void * a, const void * b)
      another, it is important to order the opcodes in the right order.  */
   for (i = 0; i < 32; ++i)
     {
-      unsigned long int x = 1 << i;
+      unsigned long int x = 1ul << i;
       int x0 = (match0 & x) != 0;
       int x1 = (match1 & x) != 0;
 
@@ -2525,7 +2525,7 @@ compare_opcodes (const void * a, const void * b)
 
   for (i = 0; i < 32; ++i)
     {
-      unsigned long int x = 1 << i;
+      unsigned long int x = 1ul << i;
       int x0 = (lose0 & x) != 0;
       int x1 = (lose1 & x) != 0;
 
-- 
2.34.1
Re: [PATCH] disas/sparc: Use unsigned long for shift operation
Posted by Peter Maydell 2 weeks, 5 days ago
On Tue, 3 Nov 2026 at 20:51, Ankush <mondalankush9851@gmail.com> wrote:
>
> The shift expression uses an integer constant of type int.
> When i is 31, the result sets the sign bit, and its
> conversion to int does not portably preserve the intended
> mask value.
>
> Use an unsigned long constant (1ul) for the left operand
> to ensure that the shift is performed using unsigned long.

We could mention that because QEMU uses -fwrapv this is
not undefined behaviour and won't cause us to produce a
wrong result.

> Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/2618
>
> Signed-off-by: Ankush <mondalankush9851@gmail.com>
> ---
>  disas/sparc.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/disas/sparc.c b/disas/sparc.c
> index 5689533ce1..40c1164554 100644
> --- a/disas/sparc.c
> +++ b/disas/sparc.c
> @@ -2515,7 +2515,7 @@ compare_opcodes (const void * a, const void * b)
>       another, it is important to order the opcodes in the right order.  */
>    for (i = 0; i < 32; ++i)
>      {
> -      unsigned long int x = 1 << i;
> +      unsigned long int x = 1ul << i;
>        int x0 = (match0 & x) != 0;
>        int x1 = (match1 & x) != 0;
>
> @@ -2525,7 +2525,7 @@ compare_opcodes (const void * a, const void * b)
>
>    for (i = 0; i < 32; ++i)
>      {
> -      unsigned long int x = 1 << i;
> +      unsigned long int x = 1ul << i;
>        int x0 = (lose0 & x) != 0;
>        int x1 = (lose1 & x) != 0;

Reviewed-by: Peter Maydell <peter.maydell@linaro.org>

thanks
-- PMM