From: Richard Henderson <richard.henderson@linaro.org>
To: WANG Rui <wangrui@loongson.cn>
Cc: "Song Gao" <gaosong@loongson.cn>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
qemu-devel@nongnu.org, qemu@hev.cc,
mengqinggang <mengqinggang@loongson.cn>
Subject: Re: [RFC PATCH v2] target/loongarch: Fix incorrect rounding in fnm{add,sub} under certain modes
Date: Wed, 7 May 2025 09:16:18 -0700 [thread overview]
Message-ID: <0e8eb12f-cf6c-4021-842d-ebed0ffb1fe4@linaro.org> (raw)
In-Reply-To: <CAHirt9jd4LGkGyp0xKFQYKM3CnPYUs4xJmSyg4je6iMKR_0b5A@mail.gmail.com>
On 5/7/25 08:25, WANG Rui wrote:
> Hi Richard,
>
> On Wed, May 7, 2025 at 11:04 PM Richard Henderson
> <richard.henderson@linaro.org> wrote:
>>
>> On 5/7/25 02:14, WANG Rui wrote:
>>> This patch fixes incorrect results for `[xv]fnm{add,sub}.{s,d}`
>>> instructions when rounding toward {zero, positive, negative}.
>>>
>>> According to the LoongArch ISA specification, the result of an
>>> instruction like `FNMSUB.D` is computed as:
>>>
>>> FR[fd] = -FP64_fusedMultiplyAdd(FR[fj], FR[fk], -FR[fa])
>>>
>>> Here, `FP64_fusedMultiplyAdd()` performs a fused multiply-add operation
>>> compliant with IEEE 754-2008. The negation is applied to the fully
>>> rounded result of the fused operation - not to any intermediate value.
>>> This behavior is specifiec to LoongArch and differs from other arches,
>>> which is why the existing `float_muladd_negate_result` flag does not
>>> model it correctly.
>>
>> Loongarch does not differ from other arches; we got it wrong for everyone.
>> There's no need for a new flag.
>
> I'm not familiar with PowerPC and s390x. The official PowerPC docs[^1]
> doesn't clearly specify the order of negation and rounding operations.
Certainly it does.
This instruction produces the same result as would be
obtained by using the Floating Multiply-Subtract
instruction and then negating the result, with the
following exceptions.
Round is done the same as FMSUB, *and then negating*.
So the negation must happen second.
> I also don't have access to the hardware to run experiments. However,
> I did find the Linux kernel's emulation code[^2][^3] for the PowerPC
> `fnmsub` instruction, which seems to suggest that negation occurs
> before rounding - though it's possible that interpretation is
> incorrect.
That code gets this case wrong, just like we did.
>
> [^1]: https://www.ibm.com/docs/en/aix/7.2?topic=set-fnmsub-fnms-floating-negative-multiply-subtract-instruction
> [^2]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/powerpc/math-emu/fnmsub.c?h=v6.15-rc5#n49
> [^3}: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/powerpc/math-emu/fnmsub.c?h=v6.15-rc5#n55
>
> Regards,
> -hev
>
>>
>>
>> r~
>
prev parent reply other threads:[~2025-05-07 16:16 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-07 9:14 [RFC PATCH v2] target/loongarch: Fix incorrect rounding in fnm{add, sub} under certain modes WANG Rui
2025-05-07 15:04 ` [RFC PATCH v2] target/loongarch: Fix incorrect rounding in fnm{add,sub} " Richard Henderson
2025-05-07 15:25 ` WANG Rui
2025-05-07 16:16 ` Richard Henderson [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=0e8eb12f-cf6c-4021-842d-ebed0ffb1fe4@linaro.org \
--to=richard.henderson@linaro.org \
--cc=gaosong@loongson.cn \
--cc=mengqinggang@loongson.cn \
--cc=philmd@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu@hev.cc \
--cc=wangrui@loongson.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).