On Mon, 16 Nov 2026 at 08:37, Philippe Mathieu-Daudé
<[email protected]> wrote:
> On 13/11/26 17:34, Peter Maydell wrote:
> > 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:

> > 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.


> 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?

I think that's OK as a single patch if you explain in the
commit message which cases the old code was getting wrong
and what the intent of the code is.

-- PMM

Reply via email to