Skip to contentSkip to conversation

target/arm/tcg/vfp_helper: Fix incorrect bit field deposition in rsqrte_f64

PATCH In review Li-Hang Lin 4 participants 10 messages Dec 26, 2025 – Mar 22

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/arm/tcg/vfp_helper: Fix incorrect bit field deposition in rsqrte_f64

Fix an error in rsqrte_f64() where the sign bit was being placed incorrectly. Specifically, ensure f64_sign is deposited into bit 63.

Additionally, update the comments regarding the exponent (exp) bit width to reflect the correct double-precision specifications.

---
 target/arm/tcg/vfp_helper.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/arm/tcg/vfp_helper.c +2 -2
index e156e3774a..60188b2c7e 100644
@@ -1078,8 +1078,8 @@ float64 HELPER(rsqrte_f64)(float64 input, float_status *s)
 
     f64_frac = recip_sqrt_estimate(&f64_exp, 3068, f64_frac, false);
 
-    /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(44) */
-    val = deposit64(0, 61, 1, f64_sign);
+    /* result = sign : result_exp<10:0> : estimate<7:0> : Zeros(44) */
+    val = deposit64(0, 63, 1, f64_sign);
     val = deposit64(val, 52, 11, f64_exp);
     val = deposit64(val, 44, 8, extract64(f64_frac, 52 - 8, 8));
     return make_float64(val);
Signature · 1 line
2.52.0.351.gbe84eed79e-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Li-Hang Lin

Fix an error in rsqrte_f64() where the sign bit was being placed incorrectly. Specifically, ensure f64_sign is deposited into bit 63.

Additionally, update the comments regarding the exponent (exp) bit width to reflect the correct double-precision specifications.

Fixes d719cbc7641 ("arm/helper.c: re-factor rsqrte and add rsqrte_f16")
Quoted 20 lines
---
  target/arm/tcg/vfp_helper.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
target/arm/tcg/vfp_helper.c +2 -2
index e156e3774a..60188b2c7e 100644
@@ -1078,8 +1078,8 @@ float64 HELPER(rsqrte_f64)(float64 input, float_status *s)
 
      f64_frac = recip_sqrt_estimate(&f64_exp, 3068, f64_frac, false);
 
-    /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(44) */
-    val = deposit64(0, 61, 1, f64_sign);
+    /* result = sign : result_exp<10:0> : estimate<7:0> : Zeros(44) */
+    val = deposit64(0, 63, 1, f64_sign);
      val = deposit64(val, 52, 11, f64_exp);
      val = deposit64(val, 44, 8, extract64(f64_frac, 52 - 8, 8));
      return make_float64(val);
#2 lore raw
Quoted 10 lines · Philippe Mathieu-Daudé
Quoted 7 lines · Li-Hang Lin

Fix an error in rsqrte_f64() where the sign bit was being placed incorrectly. Specifically, ensure f64_sign is deposited into bit 63.

Additionally, update the comments regarding the exponent (exp) bit width to reflect the correct double-precision specifications.

Fixes d719cbc7641 ("arm/helper.c: re-factor rsqrte and add rsqrte_f16")

That's.. 2.12, 2018 - not too far ago :)

/mjt

#3 lore raw
In reply to Li-Hang Lin
Quoted 7 lines · Li-Hang Lin

Fix an error in rsqrte_f64() where the sign bit was being placed incorrectly. Specifically, ensure f64_sign is deposited into bit 63.

Additionally, update the comments regarding the exponent (exp) bit width to reflect the correct double-precision specifications.

This seems like it would produce incorrect results -- do you have an example of an instruction plus input data values that p produces a different output value to the hardware?

thanks
-- PMM

#4 lore raw

Hi Peter,

Thank you for your reply.

I initially identified this error while reviewing the code and was curious why it hadn't caused any bugs.
After further testing, it appears the original code behaved correctly by coincidence.

The ASL code in the ARM ARM for FRSQRTE states:

```
elsif sign == '1' then
         result = FPDefaultNaN(fpcr, N);
         if fpexc then FPProcessException(FPExc_InvalidOp, fpcr);
```

As it turns out, the sign bit must be zero by the time it reaches the final deposition code, which explains why the incorrect bit placement did not surface as a functional bug.

Quoted 16 lines · Peter Maydell
Quoted 7 lines · Li-Hang Lin

Fix an error in rsqrte_f64() where the sign bit was being placed incorrectly. Specifically, ensure f64_sign is deposited into bit 63.

Additionally, update the comments regarding the exponent (exp) bit width to reflect the correct double-precision specifications.

This seems like it would produce incorrect results -- do you have an example of an instruction plus input data values that p produces a different output value to the hardware?

thanks
-- PMM

#5 lore raw

Hi Peter,

Is my patch good for you?

Cheers

Quoted 42 lines · Lin Li-Hang

Hi Peter,

Thank you for your reply.

I initially identified this error while reviewing the code and was curious why it hadn't caused any bugs.
After further testing, it appears the original code behaved correctly by coincidence.

The ASL code in the ARM ARM for FRSQRTE states:

```
elsif sign == '1' then
         result = FPDefaultNaN(fpcr, N);
         if fpexc then FPProcessException(FPExc_InvalidOp, fpcr);
```

As it turns out, the sign bit must be zero by the time it reaches the final deposition code, which explains why the incorrect bit placement did not surface as a functional bug.

Quoted 16 lines · Peter Maydell
Quoted 7 lines · Li-Hang Lin

Fix an error in rsqrte_f64() where the sign bit was being placed incorrectly. Specifically, ensure f64_sign is deposited into bit 63.

Additionally, update the comments regarding the exponent (exp) bit width to reflect the correct double-precision specifications.

This seems like it would produce incorrect results -- do you have an example of an instruction plus input data values that p produces a different output value to the hardware?

thanks
-- PMM

#6 lore raw
In reply to Lin Li-Hang
Quoted 10 lines · Lin Li-Hang

Hi Peter,

Thank you for your reply.

I initially identified this error while reviewing the code and was curious why it hadn't caused any bugs. After further testing, it appears the original code behaved correctly by coincidence.

The ASL code in the ARM ARM for FRSQRTE states:

Quoted 7 lines
```
elsif sign == '1' then
         result = FPDefaultNaN(fpcr, N);
         if fpexc then FPProcessException(FPExc_InvalidOp, fpcr);
```

As it turns out, the sign bit must be zero by the time it reaches the final deposition code, which explains why the incorrect bit placement did not surface as a functional bug.

Thanks for looking that up. I think that although this isn't a bug it's definitely confusing code, so the best approach will be to make our code match how the current Arm ARM FPRSqrtEstimate() treats the output sign bit, which is to say we know it's 0. In the pseudocode that looks like:
result = '0' : result_exp<N-54:0> : estimate<7:0>:Zeros(44);
and for QEMU it should look like updating the comment so that instead of
/* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(44) */

it says
/* result = 0 : result_exp<4:0> : estimate<7:0> : Zeros(44) */ and removing the unnecessary deposit64() call of f64_sign entirely.

We should do this for all of rsqrte_f64, do_rsqrte_f32 and rsqrte_f16.

thanks
-- PMM

#7 lore raw

Hi Peter,

Thanks for your explanation.
Your statement and suggestion is better.

Cheers

Quoted 42 lines · Peter Maydell
Quoted 6 lines · Lin Li-Hang

Hi Peter,

Thank you for your reply.

I initially identified this error while reviewing the code and was

curious why it hadn't caused any bugs.

Quoted 1 line

After further testing, it appears the original code behaved correctly by

coincidence.

Quoted 10 lines

The ASL code in the ARM ARM for FRSQRTE states:

```
elsif sign == '1' then
         result = FPDefaultNaN(fpcr, N);
         if fpexc then FPProcessException(FPExc_InvalidOp, fpcr);
```

As it turns out, the sign bit must be zero by the time it reaches the

final deposition code, which explains why the incorrect bit placement did not surface as a functional bug.

Thanks for looking that up. I think that although this isn't a bug it's definitely confusing code, so the best approach will be to make our code match how the current Arm ARM FPRSqrtEstimate() treats the output sign bit, which is to say we know it's 0. In the pseudocode that looks like:
result = '0' : result_exp<N-54:0> : estimate<7:0>:Zeros(44);
and for QEMU it should look like updating the comment so that instead of
/* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(44) */

it says
/* result = 0 : result_exp<4:0> : estimate<7:0> : Zeros(44) */ and removing the unnecessary deposit64() call of f64_sign entirely.

We should do this for all of rsqrte_f64, do_rsqrte_f32 and rsqrte_f16.

thanks
-- PMM

#8 lore raw
In reply to Li-Hang Lin
Quoted 6 lines · Li-Hang Lin

Fix an error in rsqrte_f64() where the sign bit was being placed incorrectly. Specifically, ensure f64_sign is deposited into bit 63.

Additionally, update the comments regarding the exponent (exp) bit width to reflect the correct double-precision specifications.

Ping? Or is this patch not needed anymore?

Thanks,

/mjt

Quoted 18 lines
  target/arm/tcg/vfp_helper.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
target/arm/tcg/vfp_helper.c +2 -2
index e156e3774a..60188b2c7e 100644
@@ -1078,8 +1078,8 @@ float64 HELPER(rsqrte_f64)(float64 input, float_status *s)
 
      f64_frac = recip_sqrt_estimate(&f64_exp, 3068, f64_frac, false);
 
-    /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(44) */
-    val = deposit64(0, 61, 1, f64_sign);
+    /* result = sign : result_exp<10:0> : estimate<7:0> : Zeros(44) */
+    val = deposit64(0, 63, 1, f64_sign);
      val = deposit64(val, 52, 11, f64_exp);
      val = deposit64(val, 44, 8, extract64(f64_frac, 52 - 8, 8));
      return make_float64(val);
#9 lore raw

Hi mjt,

After discussing with Peter, I did further debugging.
The original code is harmless even with the wrong bit position placement. My patch is not perfect and Peter suggested refactoring those snippets.

Cheers

Quoted 35 lines · Michael Tokarev
Quoted 6 lines · Li-Hang Lin

Fix an error in rsqrte_f64() where the sign bit was being placed incorrectly. Specifically, ensure f64_sign is deposited into bit 63.

Additionally, update the comments regarding the exponent (exp) bit width to reflect the correct double-precision specifications.

Ping? Or is this patch not needed anymore?

Thanks,

/mjt

target/arm/tcg/vfp_helper.c float64 HELPER(rsqrte_f64)(float64 input,

Quoted 8 lines
  target/arm/tcg/vfp_helper.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
target/arm/tcg/vfp_helper.c +0 -0
index e156e3774a..60188b2c7e 100644
@@ -1078,8 +1078,8 @@ float64 HELPER(rsqrte_f64)(float64 input,

float_status *s)

Quoted 10 lines
target/arm/tcg/vfp_helper.c +2 -2
      f64_frac = recip_sqrt_estimate(&f64_exp, 3068, f64_frac, false);
 
-    /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(44) */
-    val = deposit64(0, 61, 1, f64_sign);
+    /* result = sign : result_exp<10:0> : estimate<7:0> : Zeros(44) */
+    val = deposit64(0, 63, 1, f64_sign);
      val = deposit64(val, 52, 11, f64_exp);
      val = deposit64(val, 44, 8, extract64(f64_frac, 52 - 8, 8));
      return make_float64(val);