Hi Peter,
On 13/11/26 17:34, Peter Maydell wrote:
> On Fri, 13 Nov 2026 at 16:13, Philippe Mathieu-Daudé
> <philmd@oss.qualcomm.com> wrote:
>>
>> Various individual fixes noted while reviewing manuals
>> during decodetree conversion, and using a RISU prototype.
>
> If you have a little time for MIPS could I get you to have a look at
> Coverity issue 1689817 ? It's not urgent, but Coverity complains about
> what it thinks is some dead code in the dextr_s_h helper:
>
> } else if ((temp127 == 1) &&
> (temp[1] < 0xFFFFFFFFFFFFFFFFll
> || temp[0] < 0xFFFFFFFFFFFF1000ll)) {
>
> The compare with temp[1] is very odd here. The constant has the "ll"
> suffix indicating that the author thought this would be signed,
> but the value is 0xFFFF_FFFF_FFFF_FFFF so it doesn't fit in a
> signed 64-bit integer type. So the C spec says it has type
> "unsigned long long" (because it's a hex constant). That means
> that the comparison is int64_t against uint64_t, which causes
> temp[1] to be cast to unsigned and we do an unsigned comparison.
>
> The upshot is that "temp[1] < 0xFFFFFFFFFFFFFFFFll" is a funny way of
> writing "temp[1] != -1". So I think Coverity is wrong about there
> being dead code, but it's confused because the code is written in a
> confusing and possibly wrong way.
>
> The comparison with temp[] is also 0xFFFF_FFFF_FFFF_1000 with the
> LL suffix, so it's also not really a signed value or comparison.
>
> I don't know what this instruction is supposed to be doing, but
> it would be helpful if somebody who does could have a look and
> rewrite the code to either do the right thing if the current
> code is wrong, or else to rephrase it so it is clearer about
> what it's doing if it's getting the right result in a weird way.
Although MD00375 (MIPS64 DSP) and MD00765 (microMIPS64 DSP) manuals
list the DEXTP* or DEXTR* instructions encoding tables, they do not
contains these instruction descriptions. We can infer from the MIPS32
EXTR_S.H semantics which saturates to [-0x8000, 0x7FFF], sign-extend
to 64 bits and apply to the 128-bit HI:LO.
0xFFFF_FFFF_FFFF_1000 is a typo, should be 0xFFFF_FFFF_FFFF_8000.
Fix using uint64_t and explicit comparisons (positive values with
bit 63 set in the low half were not saturated, as that half was
compared as signed):
-- >8 --
diff --git a/target/mips/tcg/dsp_helper.c b/target/mips/tcg/dsp_helper.c
index 73934cab7786..1d4d54897dd6 100644
--- a/target/mips/tcg/dsp_helper.c
+++ b/target/mips/tcg/dsp_helper.c
@@ -3379,22 +3379,21 @@ target_ulong HELPER(extr_s_h)(target_ulong ac,
target_ulong shift,
target_ulong HELPER(dextr_s_h)(target_ulong ac, target_ulong shift,
CPUMIPSState *env)
{
- int64_t temp[2];
+ uint64_t temp[2];
uint32_t temp127;
shift = shift & 0x1F;
- mipsdsp_rashift_acc((uint64_t *)temp, ac, shift, env);
+ mipsdsp_rashift_acc(temp, ac, shift, env);
temp127 = (temp[1] >> 63) & 0x01;
- if ((temp127 == 0) && (temp[1] > 0 || temp[0] > 32767)) {
+ if (temp127 == 0 && (temp[1] != 0 || temp[0] > 0x7FFF)) {
temp[0] &= 0xFFFF0000;
temp[0] |= 0x00007FFF;
set_DSPControl_overflow_flag(1, 23, env);
- } else if ((temp127 == 1) &&
- (temp[1] < 0xFFFFFFFFFFFFFFFFll
- || temp[0] < 0xFFFFFFFFFFFF1000ll)) {
+ } else if (temp127 == 1 &&
+ (temp[1] != ~0ull || temp[0] < 0xFFFFFFFFFFFF8000ull)) {
temp[0] &= 0xFFFF0000;
temp[0] |= 0x00008000;
set_DSPControl_overflow_flag(1, 23, env);
---
But this is a good opportunity to rewrite in a more readable style
(I'm not a fan of complex condition checks in particular when 'else if'
is involved, I rather use local self-descriptive booleans); the same
fix becomes:
-- >8 --
diff --git a/target/mips/tcg/dsp_helper.c b/target/mips/tcg/dsp_helper.c
index 73934cab7786..82a9a29c0213 100644
--- a/target/mips/tcg/dsp_helper.c
+++ b/target/mips/tcg/dsp_helper.c
@@ -3379,28 +3379,25 @@ target_ulong HELPER(extr_s_h)(target_ulong ac,
target_ulong shift,
target_ulong HELPER(dextr_s_h)(target_ulong ac, target_ulong shift,
CPUMIPSState *env)
{
- int64_t temp[2];
- uint32_t temp127;
+ uint64_t temp[2];
+ int64_t lo;
+ bool negative, fits_s64, fits_s16;
shift = shift & 0x1F;
- mipsdsp_rashift_acc((uint64_t *)temp, ac, shift, env);
+ mipsdsp_rashift_acc(temp, ac, shift, env);
- temp127 = (temp[1] >> 63) & 0x01;
+ lo = temp[0];
+ negative = temp[1] >> 63;
+ fits_s64 = temp[1] == (uint64_t)(lo >> 63);
+ fits_s16 = lo == (int16_t)lo;
- if ((temp127 == 0) && (temp[1] > 0 || temp[0] > 32767)) {
- temp[0] &= 0xFFFF0000;
- temp[0] |= 0x00007FFF;
- set_DSPControl_overflow_flag(1, 23, env);
- } else if ((temp127 == 1) &&
- (temp[1] < 0xFFFFFFFFFFFFFFFFll
- || temp[0] < 0xFFFFFFFFFFFF1000ll)) {
- temp[0] &= 0xFFFF0000;
- temp[0] |= 0x00008000;
- set_DSPControl_overflow_flag(1, 23, env);
+ if (fits_s64 && fits_s16) {
+ return lo;
}
-
- return (int64_t)(int16_t)(temp[0] & MIPSDSP_LO);
+ set_DSPControl_overflow_flag(1, 23, env);
+ return negative ? INT16_MIN : INT16_MAX;
}
---
Is it reviewable-enough to be a single fix patch or do you rather
that split in 1 fix and 1 refactor?
Thanks,
Phil.