Skip to contentSkip to conversation

target-arm: fix neon vsri, vshl and vsli ops

PATCH Superseded [email protected] 2 participants 6 messages Oct 21, 2009 – Oct 22, 2009

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 7/12 target-arm: fix neon vsri, vshl and vsli ops

Shift immediate value is incorrectly overwritten by a temporary variable in the processing of NEON vsri, vshl and vsli instructions.

---

target-arm/translate.c +0 -0
index 59bf7bc..c92ecc6 100644
@@ -4094,7 +4094,7 @@ static int disas_neon_data_insn(CPUState * env,  

DisasContext *s, uint32_t insn)

      int pairwise;
      int u;
      int n;
-    uint32_t imm;
+    uint32_t imm, imm2;
      TCGv tmp;
      TCGv tmp2;
      TCGv tmp3;
@@ -4624,31 +4624,31 @@ static int disas_neon_data_insn(CPUState *  
env, DisasContext *s, uint32_t insn)
                              switch (size) {
                              case 0:
                                  if (op == 4)
-                                    imm = 0xff >> -shift;
+                                    imm2 = 0xff >> -shift;
                                  else
-                                    imm = (uint8_t)(0xff << shift);
-                                imm |= imm << 8;
-                                imm |= imm << 16;
+                                    imm2 = (uint8_t)(0xff << shift);
+                                imm2 |= imm2 << 8;
+                                imm2 |= imm2 << 16;
                                  break;
                              case 1:
                                  if (op == 4)
-                                    imm = 0xffff >> -shift;
+                                    imm2 = 0xffff >> -shift;
                                  else
-                                    imm = (uint16_t)(0xffff << shift);
-                                imm |= imm << 16;
+                                    imm2 = (uint16_t)(0xffff << shift);
+                                imm2 |= imm2 << 16;
                                  break;
                              case 2:
                                  if (op == 4)

- imm = 0xffffffffu >> -shift;
+ imm2 = 0xffffffffu >> -shift;
else
- imm = 0xffffffffu << shift;

+                                    imm2 = 0xffffffffu << shift;
                                  break;
                              default:
                                  abort();
                              }
                              tmp2 = neon_load_reg(rd, pass);
-                            tcg_gen_andi_i32(tmp, tmp, imm);
-                            tcg_gen_andi_i32(tmp2, tmp2, ~imm);
+                            tcg_gen_andi_i32(tmp, tmp, imm2);
+                            tcg_gen_andi_i32(tmp2, tmp2, ~imm2);
                              tcg_gen_or_i32(tmp, tmp, tmp2);
                              dead_tmp(tmp2);
                          }
target-arm/translate.c +12 -12
index 59bf7bc..c92ecc6 100644
@@ -4094,7 +4094,7 @@ static int disas_neon_data_insn(CPUState * env, DisasContext *s, uint32_t insn)
     int pairwise;
     int u;
     int n;
-    uint32_t imm;
+    uint32_t imm, imm2;
     TCGv tmp;
     TCGv tmp2;
     TCGv tmp3;
@@ -4624,31 +4624,31 @@ static int disas_neon_data_insn(CPUState * env, DisasContext *s, uint32_t insn)
                             switch (size) {
                             case 0:
                                 if (op == 4)
-                                    imm = 0xff >> -shift;
+                                    imm2 = 0xff >> -shift;
                                 else
-                                    imm = (uint8_t)(0xff << shift);
-                                imm |= imm << 8;
-                                imm |= imm << 16;
+                                    imm2 = (uint8_t)(0xff << shift);
+                                imm2 |= imm2 << 8;
+                                imm2 |= imm2 << 16;
                                 break;
                             case 1:
                                 if (op == 4)
-                                    imm = 0xffff >> -shift;
+                                    imm2 = 0xffff >> -shift;
                                 else
-                                    imm = (uint16_t)(0xffff << shift);
-                                imm |= imm << 16;
+                                    imm2 = (uint16_t)(0xffff << shift);
+                                imm2 |= imm2 << 16;
                                 break;
                             case 2:
                                 if (op == 4)
-                                    imm = 0xffffffffu >> -shift;
+                                    imm2 = 0xffffffffu >> -shift;
                                 else
-                                    imm = 0xffffffffu << shift;
+                                    imm2 = 0xffffffffu << shift;
                                 break;
                             default:
                                 abort();
                             }
                             tmp2 = neon_load_reg(rd, pass);
-                            tcg_gen_andi_i32(tmp, tmp, imm);
-                            tcg_gen_andi_i32(tmp2, tmp2, ~imm);
+                            tcg_gen_andi_i32(tmp, tmp, imm2);
+                            tcg_gen_andi_i32(tmp2, tmp2, ~imm2);
                             tcg_gen_or_i32(tmp, tmp, tmp2);
                             dead_tmp(tmp2);
                         }
#1 lore raw
Quoted 10 lines · On Wed, Oct 21, 2009 at 12:17 PM, <[email protected]> wrote:

Shift immediate value is incorrectly overwritten by a temporary variable in the processing of NEON vsri, vshl and vsli instructions.

---

target-arm/translate.c +0 -0
index 59bf7bc..c92ecc6 100644

target-arm/translate.c static int disas_neon_data_insn(CPUState * env, · line 4098

Quoted 6 lines
target-arm/translate.c +1 -1
@@ -4094,7 +4094,7 @@ static int disas_neon_data_insn(CPUState * env,
 DisasContext *s, uint32_t insn)
      int pairwise;
      int u;
      int n;
-    uint32_t imm;
+    uint32_t imm, imm2;

I would prefer mask to imm2, but that's personal taste :-)

Quoted 39 lines
target-arm/translate.c +0 -0
      TCGv tmp;
      TCGv tmp2;

     TCGv tmp3;

target-arm/translate.c +0 -0
@@ -4624,31 +4624,31 @@ static int disas_neon_data_insn(CPUState *
 env, DisasContext *s, uint32_t insn)
                              switch (size) {

                             case 0:
                                 if (op == 4)
-                                    imm = 0xff >> -shift;
+                                    imm2 = 0xff >> -shift; else
-                                    imm = (uint8_t)(0xff << shift);
-                                imm |= imm << 8;
-                                imm |= imm << 16;
+                                    imm2 = (uint8_t)(0xff << shift);
+                                imm2 |= imm2 << 8;
+                                imm2 |= imm2 << 16;
                                 break;
                             case 1:
                                 if (op == 4)
-                                    imm = 0xffff >> -shift;
+                                    imm2 = 0xffff >> -shift; else
-                                    imm = (uint16_t)(0xffff << shift);
-                                imm |= imm << 16;
+                                    imm2 = (uint16_t)(0xffff << shift);
+                                imm2 |= imm2 << 16;
                                 break;
                             case 2:
                                 if (op == 4)
-                                    imm = 0xffffffffu >> -shift;
+                                    imm2 = 0xffffffffu >> -shift; else
-                                    imm = 0xffffffffu << shift;
+                                    imm2 = 0xffffffffu << shift; break;
                             default:
                                 abort();
                             }

Quoted 8 lines

                             tmp2 = neon_load_reg(rd, pass);
-                            tcg_gen_andi_i32(tmp, tmp, imm);
-                            tcg_gen_andi_i32(tmp2, tmp2, ~imm);
+                            tcg_gen_andi_i32(tmp, tmp, imm2);
+                            tcg_gen_andi_i32(tmp2, tmp2, ~imm2); tcg_gen_or_i32(tmp, tmp, tmp2); dead_tmp(tmp2);
                         }

I mostly agree with this, except there's a mistake already present in the original code: for a size of 2 the shift amount can be 32 (look at VSRI in the ARM Architecture Reference Manual). Note in this case, shift will be -32.

Laurent

#2 lore raw
Quoted 43 lines · ext Laurent Desnogues
Quoted 43 lines
target-arm/translate.c +11 -11
@@ -4624,31 +4624,31 @@ static int disas_neon_data_insn(CPUState *
 env, DisasContext *s, uint32_t insn)
                             switch (size) {
                             case 0:
                                 if (op == 4)
-                                    imm = 0xff >> -shift;
+                                    imm2 = 0xff >> -shift;
                                 else
-                                    imm = (uint8_t)(0xff << shift);
-                                imm |= imm << 8;
-                                imm |= imm << 16;
+                                    imm2 = (uint8_t)(0xff << shift);
+                                imm2 |= imm2 << 8;
+                                imm2 |= imm2 << 16;
                                 break;
                             case 1:
                                 if (op == 4)
-                                    imm = 0xffff >> -shift;
+                                    imm2 = 0xffff >> -shift;
                                 else
-                                    imm = (uint16_t)(0xffff <<  
 shift);
-                                imm |= imm << 16;
+                                    imm2 = (uint16_t)(0xffff <<  
 shift);
+                                imm2 |= imm2 << 16;
                                 break;
                             case 2:
                                 if (op == 4)
-                                    imm = 0xffffffffu >> -shift;
+                                    imm2 = 0xffffffffu >> -shift;
                                 else
-                                    imm = 0xffffffffu << shift;
+                                    imm2 = 0xffffffffu << shift;
                                 break;
                             default:
                                 abort();
                             }
                             tmp2 = neon_load_reg(rd, pass);
-                            tcg_gen_andi_i32(tmp, tmp, imm);
-                            tcg_gen_andi_i32(tmp2, tmp2, ~imm);
+                            tcg_gen_andi_i32(tmp, tmp, imm2);
+                            tcg_gen_andi_i32(tmp2, tmp2, ~imm2);
Quoted 8 lines
Quoted 3 lines
                             tcg_gen_or_i32(tmp, tmp, tmp2);
                             dead_tmp(tmp2);
                         }

I mostly agree with this, except there's a mistake already present in the original code: for a size of 2 the shift amount can be 32 (look at VSRI in the ARM Architecture Reference Manual). Note in this case, shift will be -32.

True. However, I'm not sure if this causes incorrect behavior or not. I'm not a NEON expert but how I understand the VSRI instruction is that it will shift the element value before it is inserted in the destination vector, therefore with element size 2 and shift 32 the result would be always zero and I guess that would still apply if the shift was -32: does it matter in which direction you shift if you anyway shift all the bits out?

Regards,
Juha

#3 lore raw
Quoted 55 lines · On Thu, Oct 22, 2009 at 8:49 AM, <[email protected]> wrote:
Quoted 43 lines · ext Laurent Desnogues
Quoted 43 lines
(quoted diff) +0 -0
@@ -4624,31 +4624,31 @@ static int disas_neon_data_insn(CPUState *
 env, DisasContext *s, uint32_t insn)
                              switch (size) {

                             case 0:
                                 if (op == 4)
-                                    imm = 0xff >> -shift;
+                                    imm2 = 0xff >> -shift; else
-                                    imm = (uint8_t)(0xff << shift);
-                                imm |= imm << 8;
-                                imm |= imm << 16;
+                                    imm2 = (uint8_t)(0xff << shift);
+                                imm2 |= imm2 << 8;
+                                imm2 |= imm2 << 16; break;
                             case 1:
                                 if (op == 4)
-                                    imm = 0xffff >> -shift;
+                                    imm2 = 0xffff >> -shift; else
-                                    imm = (uint16_t)(0xffff << shift);
-                                imm |= imm << 16;
+                                    imm2 = (uint16_t)(0xffff << shift);
+                                imm2 |= imm2 << 16; break;
                             case 2:
                                 if (op == 4)
-                                    imm = 0xffffffffu >> -shift;
+                                    imm2 = 0xffffffffu >> -shift; else
-                                    imm = 0xffffffffu << shift;
+                                    imm2 = 0xffffffffu << shift; break;
                             default:
                                 abort();
                             }
                             tmp2 = neon_load_reg(rd, pass);
-                            tcg_gen_andi_i32(tmp, tmp, imm);
-                            tcg_gen_andi_i32(tmp2, tmp2, ~imm);
+                            tcg_gen_andi_i32(tmp, tmp, imm2);
+                            tcg_gen_andi_i32(tmp2, tmp2, ~imm2);

Quoted 8 lines
Quoted 3 lines

                             tcg_gen_or_i32(tmp, tmp, tmp2); dead_tmp(tmp2);
                         }

I mostly agree with this, except there's a mistake already present in the original code:  for a size of 2 the shift amount can be 32 (look at VSRI in the ARM Architecture Reference Manual).  Note in this case, shift will be -32.

Quoted 7 lines

True. However, I'm not sure if this causes incorrect behavior or not. I'm not a NEON expert but how I understand the VSRI instruction is that it will shift the element value before it is inserted in the destination vector, therefore with element size 2 and shift 32 the result would be always zero and I guess that would still apply if the shift was -32: does it matter in which direction you shift if you anyway shift all the bits out?

The problem is not in the NEON semantics but rather the C
one: the behaviour of a shift by an amount greater than or equal to the bit-width of the type is undefined; in this case imm2 is 32-bit and the shift is 32-bit. What you want is imm2 = 0 if shift is -32, as you correctly guessed.

Laurent

#4 lore raw
Quoted 66 lines · ext Laurent Desnogues
Quoted 57 lines · On Thu, Oct 22, 2009 at 8:49 AM, <[email protected]> wrote:
Quoted 45 lines · ext Laurent Desnogues
Quoted 45 lines
target-arm/translate.c +11 -11
@@ -4624,31 +4624,31 @@ static int disas_neon_data_insn(CPUState *
 env, DisasContext *s, uint32_t insn)
                             switch (size) {
                             case 0:
                                 if (op == 4)
-                                    imm = 0xff >> -shift;
+                                    imm2 = 0xff >> -shift;
                                 else
-                                    imm = (uint8_t)(0xff <<  
 shift);
-                                imm |= imm << 8;
-                                imm |= imm << 16;
+                                    imm2 = (uint8_t)(0xff <<  
 shift);
+                                imm2 |= imm2 << 8;
+                                imm2 |= imm2 << 16;
                                 break;
                             case 1:
                                 if (op == 4)
-                                    imm = 0xffff >> -shift;
+                                    imm2 = 0xffff >> -shift;
                                 else
-                                    imm = (uint16_t)(0xffff <<
 shift);
-                                imm |= imm << 16;
+                                    imm2 = (uint16_t)(0xffff <<
 shift);
+                                imm2 |= imm2 << 16;
                                 break;
                             case 2:
                                 if (op == 4)
-                                    imm = 0xffffffffu >> -shift;
+                                    imm2 = 0xffffffffu >> -shift;
                                 else
-                                    imm = 0xffffffffu << shift;
+                                    imm2 = 0xffffffffu << shift;
                                 break;
                             default:
                                 abort();
                             }
                             tmp2 = neon_load_reg(rd, pass);
-                            tcg_gen_andi_i32(tmp, tmp, imm);
-                            tcg_gen_andi_i32(tmp2, tmp2, ~imm);
+                            tcg_gen_andi_i32(tmp, tmp, imm2);
+                            tcg_gen_andi_i32(tmp2, tmp2, ~imm2);
Quoted 8 lines
Quoted 3 lines
                             tcg_gen_or_i32(tmp, tmp, tmp2);
                             dead_tmp(tmp2);
                         }

I mostly agree with this, except there's a mistake already present in the original code: for a size of 2 the shift amount can be 32 (look at VSRI in the ARM Architecture Reference Manual). Note in this case, shift will be -32.

Quoted 7 lines

True. However, I'm not sure if this causes incorrect behavior or not. I'm not a NEON expert but how I understand the VSRI instruction is that it will shift the element value before it is inserted in the destination vector, therefore with element size 2 and shift 32 the result would be always zero and I guess that would still apply if the shift was -32: does it matter in which direction you shift if you anyway shift all the bits out?

Quoted 5 lines

The problem is not in the NEON semantics but rather the C
one: the behaviour of a shift by an amount greater than or equal to the bit-width of the type is undefined; in this case imm2 is 32-bit and the shift is 32-bit. What you want is imm2 = 0 if shift is -32, as you correctly guessed.

Ah, I see. Doesn't the same issue apply for the 8bit and 16bit element sizes as well? I suppose it will be sufficient to check for a negative shift value and in that case force the mask value to zero.

Regards,
Juha

#5 lore raw
Quoted 75 lines · On Thu, Oct 22, 2009 at 9:33 AM, <[email protected]> wrote:
Quoted 66 lines · ext Laurent Desnogues
Quoted 57 lines · On Thu, Oct 22, 2009 at 8:49 AM,  <[email protected]> wrote:
Quoted 45 lines · ext Laurent Desnogues
Quoted 45 lines
(quoted diff) +0 -0
@@ -4624,31 +4624,31 @@ static int disas_neon_data_insn(CPUState *
 env, DisasContext *s, uint32_t insn)
                              switch (size) {

                             case 0:
                                 if (op == 4)
-                                    imm = 0xff >> -shift;
+                                    imm2 = 0xff >> -shift; else
-                                    imm = (uint8_t)(0xff << shift);
-                                imm |= imm << 8;
-                                imm |= imm << 16;
+                                    imm2 = (uint8_t)(0xff << shift);
+                                imm2 |= imm2 << 8;
+                                imm2 |= imm2 << 16; break;
                             case 1:
                                 if (op == 4)
-                                    imm = 0xffff >> -shift;
+                                    imm2 = 0xffff >> -shift; else
-                                    imm = (uint16_t)(0xffff << shift);
-                                imm |= imm << 16;
+                                    imm2 = (uint16_t)(0xffff << shift);
+                                imm2 |= imm2 << 16; break;
                             case 2:
                                 if (op == 4)
-                                    imm = 0xffffffffu >> -shift;
+                                    imm2 = 0xffffffffu >> -shift; else
-                                    imm = 0xffffffffu << shift;
+                                    imm2 = 0xffffffffu << shift; break;
                             default:
                                 abort();
                             }
                             tmp2 = neon_load_reg(rd, pass);
-                            tcg_gen_andi_i32(tmp, tmp, imm);
-                            tcg_gen_andi_i32(tmp2, tmp2, ~imm);
+                            tcg_gen_andi_i32(tmp, tmp, imm2);
+                            tcg_gen_andi_i32(tmp2, tmp2, ~imm2);

Quoted 8 lines
Quoted 3 lines

                             tcg_gen_or_i32(tmp, tmp, tmp2); dead_tmp(tmp2);
                         }

I mostly agree with this, except there's a mistake already present in the original code:  for a size of 2 the shift amount can be 32 (look at VSRI in the ARM Architecture Reference Manual).  Note in this case, shift will be -32.

Quoted 7 lines

True. However, I'm not sure if this causes incorrect behavior or not. I'm not a NEON expert but how I understand the VSRI instruction is that it will shift the element value before it is inserted in the destination vector, therefore with element size 2 and shift 32 the result would be always zero and I guess that would still apply if the shift was -32: does it matter in which direction you shift if you anyway shift all the bits out?

Quoted 5 lines

The problem is not in the NEON semantics but rather the C
one:  the behaviour of a shift by an amount greater than or equal to the bit-width of the type is undefined;  in this case imm2 is 32-bit and the shift is 32-bit.  What you want is imm2 = 0 if shift is -32, as you correctly guessed.

Quoted 3 lines

Ah, I see. Doesn't the same issue apply for the 8bit and 16bit element sizes as well? I suppose it will be sufficient to check for a negative shift value and in that case force the mask value to zero.

For 8- and 16-bit elements, your shift will always be <= 16 (in absolute value I mean :-), so you have no issue for these cases.

Laurent