All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [Bug 645662] x87 fpu emulation not accurate enough
@ 2017-11-29  8:34 william lin
  2017-11-29  9:39 ` KONRAD Frederic
  0 siblings, 1 reply; 10+ messages in thread
From: william lin @ 2017-11-29  8:34 UTC (permalink / raw)
  To: qemu-devel

Hello,

I have been looking into this bug
<https://bugs.launchpad.net/qemu/+bug/645662> for a school project, and
after reproducing the bug and tracing through the code, I have run into a
wall trying to figure out the best way to approach this. If there is anyone
who is familiar with qemu's fpu and is willing, any tips or suggestions
will be fantastic.

Thanks,

Will & Wes

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Qemu-devel] [Bug 645662] x87 fpu emulation not accurate enough
  2017-11-29  8:34 [Qemu-devel] [Bug 645662] x87 fpu emulation not accurate enough william lin
@ 2017-11-29  9:39 ` KONRAD Frederic
  2017-11-29  9:59   ` william lin
  0 siblings, 1 reply; 10+ messages in thread
From: KONRAD Frederic @ 2017-11-29  9:39 UTC (permalink / raw)
  To: william lin; +Cc: qemu-devel

Hi,

I have the same problem here with some of our math testsuites.
It's about some precision issue with trigonometric tests.

I didn't had time to figure out what happen neither what is
really going wrong. eg: trigonometric function or?

A quick workaround is to use KVM when possible.

Fred

On 11/29/2017 09:34 AM, william lin wrote:
> Hello,
> 
> I have been looking into this bug
> <https://bugs.launchpad.net/qemu/+bug/645662> for a school project, and
> after reproducing the bug and tracing through the code, I have run into a
> wall trying to figure out the best way to approach this. If there is anyone
> who is familiar with qemu's fpu and is willing, any tips or suggestions
> will be fantastic.
> 
> Thanks,
> 
> Will & Wes
> 

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Qemu-devel] [Bug 645662] x87 fpu emulation not accurate enough
  2017-11-29  9:39 ` KONRAD Frederic
@ 2017-11-29  9:59   ` william lin
  2017-11-29 10:17     ` KONRAD Frederic
  2017-11-29 10:27     ` Peter Maydell
  0 siblings, 2 replies; 10+ messages in thread
From: william lin @ 2017-11-29  9:59 UTC (permalink / raw)
  To: KONRAD Frederic; +Cc: qemu-devel

Hi,

The goal of the project is to fix the bug. Sorry I should have been more
clear.

This bug has been around for 7+ years now. Qemu switched to softfloat and
it did not fix this. I think its most likely caused by Qemu's internal
representation not being accurate enough or accuracy lost during
conversion. However this is basically what is given to us in the bug
report, and I have not been able to make much progress past this point, so
we are asking for help.

Will



On Nov 29, 2017 3:39 AM, "KONRAD Frederic" <frederic.konrad@adacore.com>
wrote:

Hi,

I have the same problem here with some of our math testsuites.
It's about some precision issue with trigonometric tests.

I didn't had time to figure out what happen neither what is
really going wrong. eg: trigonometric function or?

A quick workaround is to use KVM when possible.

Fred


On 11/29/2017 09:34 AM, william lin wrote:

> Hello,
>
> I have been looking into this bug
> <https://bugs.launchpad.net/qemu/+bug/645662> for a school project, and
>
> after reproducing the bug and tracing through the code, I have run into a
> wall trying to figure out the best way to approach this. If there is anyone
> who is familiar with qemu's fpu and is willing, any tips or suggestions
> will be fantastic.
>
> Thanks,
>
> Will & Wes
>
>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Qemu-devel] [Bug 645662] x87 fpu emulation not accurate enough
  2017-11-29  9:59   ` william lin
@ 2017-11-29 10:17     ` KONRAD Frederic
  2017-11-29 10:27     ` Peter Maydell
  1 sibling, 0 replies; 10+ messages in thread
From: KONRAD Frederic @ 2017-11-29 10:17 UTC (permalink / raw)
  To: william lin; +Cc: qemu-devel

I would write some more basic tests. eg: at instruction level and
compare KVM vs TCG mode to find out which trigo instructions are
differents (probably not only one).

BTW I'm not floating point expert at all :).

Fred

On 11/29/2017 10:59 AM, william lin wrote:
> Hi,
> 
> The goal of the project is to fix the bug. Sorry I should have been more
> clear.
> 
> This bug has been around for 7+ years now. Qemu switched to softfloat and
> it did not fix this. I think its most likely caused by Qemu's internal
> representation not being accurate enough or accuracy lost during
> conversion. However this is basically what is given to us in the bug
> report, and I have not been able to make much progress past this point, so
> we are asking for help.
> 
> Will
> 
> 
> 
> On Nov 29, 2017 3:39 AM, "KONRAD Frederic" <frederic.konrad@adacore.com>
> wrote:
> 
> Hi,
> 
> I have the same problem here with some of our math testsuites.
> It's about some precision issue with trigonometric tests.
> 
> I didn't had time to figure out what happen neither what is
> really going wrong. eg: trigonometric function or?
> 
> A quick workaround is to use KVM when possible.
> 
> Fred
> 
> 
> On 11/29/2017 09:34 AM, william lin wrote:
> 
>> Hello,
>>
>> I have been looking into this bug
>> <https://bugs.launchpad.net/qemu/+bug/645662> for a school project, and
>>
>> after reproducing the bug and tracing through the code, I have run into a
>> wall trying to figure out the best way to approach this. If there is anyone
>> who is familiar with qemu's fpu and is willing, any tips or suggestions
>> will be fantastic.
>>
>> Thanks,
>>
>> Will & Wes
>>
>>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Qemu-devel] [Bug 645662] x87 fpu emulation not accurate enough
  2017-11-29  9:59   ` william lin
  2017-11-29 10:17     ` KONRAD Frederic
@ 2017-11-29 10:27     ` Peter Maydell
  2017-11-29 12:47       ` KONRAD Frederic
  1 sibling, 1 reply; 10+ messages in thread
From: Peter Maydell @ 2017-11-29 10:27 UTC (permalink / raw)
  To: william lin; +Cc: KONRAD Frederic, QEMU Developers

On 29 November 2017 at 09:59, william lin <wlsaidhi@gmail.com> wrote:
> This bug has been around for 7+ years now. Qemu switched to softfloat and
> it did not fix this. I think its most likely caused by Qemu's internal
> representation not being accurate enough or accuracy lost during
> conversion. However this is basically what is given to us in the bug
> report, and I have not been able to make much progress past this point, so
> we are asking for help.

Looking at the bug, it seems like inaccuracies in the results of
some of the transcendental operations (sin, cos, etc). This is
almost certainly the because our implementations of these in
target/i386/fpu_helper.c do this:

void helper_fsin(CPUX86State *env)
{
    double fptemp = floatx80_to_double(env, ST0);

    if ((fptemp > MAXTAN) || (fptemp < -MAXTAN)) {
        env->fpus |= 0x400;
    } else {
        ST0 = double_to_floatx80(env, sin(fptemp));
        env->fpus &= ~0x400;  /* C2 <-- 0 */
        /* the above code is for |arg| < 2**53 only */
    }
}

ie convert the 80-bit value in the emulated FPU to a host double,
call the host libc sin() function and convert the resulting
double to 80-bit.

Fixing this to be bit-for-bit accurate will require that we
write our own transcendental arithmetic functions that can handle
80-bit precision, which is a bit of a hairy task. (Probably all the
functions that call floatx80_to_double() would need to be rewritten.
We currently use this approach for the "complicated" operations
that the x87 provides but which softfloat doesn't: sin, cos,
powers, logs, that sort of thing.)

I was going to suggest that we borrow the code to do this from
bochs, which does seem to have implemented complete software
emulation of these complicated x87 instructions, but unfortunately
the relevant files in bochs are under the softfloat-2b license, which
isn't (by the QEMU project's reading of the license) compatible
with the GPLv2 which we use :-(

thanks
-- PMM

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Qemu-devel] [Bug 645662] x87 fpu emulation not accurate enough
  2017-11-29 10:27     ` Peter Maydell
@ 2017-11-29 12:47       ` KONRAD Frederic
  2017-11-29 12:51         ` Peter Maydell
  0 siblings, 1 reply; 10+ messages in thread
From: KONRAD Frederic @ 2017-11-29 12:47 UTC (permalink / raw)
  To: Peter Maydell, william lin; +Cc: QEMU Developers



On 11/29/2017 11:27 AM, Peter Maydell wrote:
> On 29 November 2017 at 09:59, william lin <wlsaidhi@gmail.com> wrote:
>> This bug has been around for 7+ years now. Qemu switched to softfloat and
>> it did not fix this. I think its most likely caused by Qemu's internal
>> representation not being accurate enough or accuracy lost during
>> conversion. However this is basically what is given to us in the bug
>> report, and I have not been able to make much progress past this point, so
>> we are asking for help.
> 
> Looking at the bug, it seems like inaccuracies in the results of
> some of the transcendental operations (sin, cos, etc). This is
> almost certainly the because our implementations of these in
> target/i386/fpu_helper.c do this:
> 
> void helper_fsin(CPUX86State *env)
> {
>      double fptemp = floatx80_to_double(env, ST0);
> 
>      if ((fptemp > MAXTAN) || (fptemp < -MAXTAN)) {
>          env->fpus |= 0x400;
>      } else {
>          ST0 = double_to_floatx80(env, sin(fptemp));
>          env->fpus &= ~0x400;  /* C2 <-- 0 */
>          /* the above code is for |arg| < 2**53 only */
>      }
> }
> 
> ie convert the 80-bit value in the emulated FPU to a host double,
> call the host libc sin() function and convert the resulting
> double to 80-bit.
> 
> Fixing this to be bit-for-bit accurate will require that we
> write our own transcendental arithmetic functions that can handle
> 80-bit precision, which is a bit of a hairy task. (Probably all the
> functions that call floatx80_to_double() would need to be rewritten.
> We currently use this approach for the "complicated" operations
> that the x87 provides but which softfloat doesn't: sin, cos,
> powers, logs, that sort of thing.)
> 
> I was going to suggest that we borrow the code to do this from
> bochs, which does seem to have implemented complete software
> emulation of these complicated x87 instructions, but unfortunately
> the relevant files in bochs are under the softfloat-2b license, which
> isn't (by the QEMU project's reading of the license) compatible
> with the GPLv2 which we use :-(
> 
> thanks
> -- PMM
> 

I agree this is quite tricky to do.
Maybe a little hack might work for x86 on x86 though.
Something like hardcoding the helper with an inline assembly
code?

Fred

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Qemu-devel] [Bug 645662] x87 fpu emulation not accurate enough
  2017-11-29 12:47       ` KONRAD Frederic
@ 2017-11-29 12:51         ` Peter Maydell
  2017-11-29 15:20           ` KONRAD Frederic
  0 siblings, 1 reply; 10+ messages in thread
From: Peter Maydell @ 2017-11-29 12:51 UTC (permalink / raw)
  To: KONRAD Frederic; +Cc: william lin, QEMU Developers

On 29 November 2017 at 12:47, KONRAD Frederic
<frederic.konrad@adacore.com> wrote:
> Maybe a little hack might work for x86 on x86 though.
> Something like hardcoding the helper with an inline assembly
> code?

The set of people who want to emulate x86 on x86 is surely
even smaller than the already tiny set of people who want to
emulate x86 at all. I don't think it makes sense to add
inline assembly hacks for that: QEMU should (as far as
is reasonably possible) behave the same on all hosts.

thanks
-- PMM

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Qemu-devel] [Bug 645662] x87 fpu emulation not accurate enough
  2017-11-29 12:51         ` Peter Maydell
@ 2017-11-29 15:20           ` KONRAD Frederic
  2017-11-29 15:32             ` Thomas Huth
  0 siblings, 1 reply; 10+ messages in thread
From: KONRAD Frederic @ 2017-11-29 15:20 UTC (permalink / raw)
  To: Peter Maydell; +Cc: QEMU Developers, william lin



On 11/29/2017 01:51 PM, Peter Maydell wrote:
> On 29 November 2017 at 12:47, KONRAD Frederic
> <frederic.konrad@adacore.com> wrote:
>> Maybe a little hack might work for x86 on x86 though.
>> Something like hardcoding the helper with an inline assembly
>> code?
> 
> The set of people who want to emulate x86 on x86 is surely
> even smaller than the already tiny set of people who want to
> emulate x86 at all. I don't think it makes sense to add
> inline assembly hacks for that: QEMU should (as far as
> is reasonably possible) behave the same on all hosts.
> 
> thanks
> -- PMM
> 

Sure it was a first step suggestion.

Fred

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Qemu-devel] [Bug 645662] x87 fpu emulation not accurate enough
  2017-11-29 15:20           ` KONRAD Frederic
@ 2017-11-29 15:32             ` Thomas Huth
  2017-12-06 11:15               ` KONRAD Frederic
  0 siblings, 1 reply; 10+ messages in thread
From: Thomas Huth @ 2017-11-29 15:32 UTC (permalink / raw)
  To: KONRAD Frederic, Peter Maydell; +Cc: QEMU Developers, william lin

On 29.11.2017 16:20, KONRAD Frederic wrote:
> 
> 
> On 11/29/2017 01:51 PM, Peter Maydell wrote:
>> On 29 November 2017 at 12:47, KONRAD Frederic
>> <frederic.konrad@adacore.com> wrote:
>>> Maybe a little hack might work for x86 on x86 though.
>>> Something like hardcoding the helper with an inline assembly
>>> code?
>>
>> The set of people who want to emulate x86 on x86 is surely
>> even smaller than the already tiny set of people who want to
>> emulate x86 at all. I don't think it makes sense to add
>> inline assembly hacks for that: QEMU should (as far as
>> is reasonably possible) behave the same on all hosts.
>>
> 
> Sure it was a first step suggestion.

FYI, there is some code in WinUAE / Previous which might be usable, see
e.g. WinUAE sources here:

https://github.com/tonioni/WinUAE/blob/master/softfloat/softfloat_fpsp.cpp

But the code is targetted for m68k only so far, so it likely needs some
work, and we're currently discussing whether it's feasible to include it
into QEMU at all, see this thread here:

https://lists.gnu.org/archive/html/qemu-devel/2017-11/msg05342.html

 Thomas

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Qemu-devel] [Bug 645662] x87 fpu emulation not accurate enough
  2017-11-29 15:32             ` Thomas Huth
@ 2017-12-06 11:15               ` KONRAD Frederic
  0 siblings, 0 replies; 10+ messages in thread
From: KONRAD Frederic @ 2017-12-06 11:15 UTC (permalink / raw)
  To: Thomas Huth, Peter Maydell; +Cc: QEMU Developers, william lin



On 11/29/2017 04:32 PM, Thomas Huth wrote:
> On 29.11.2017 16:20, KONRAD Frederic wrote:
>>
>>
>> On 11/29/2017 01:51 PM, Peter Maydell wrote:
>>> On 29 November 2017 at 12:47, KONRAD Frederic
>>> <frederic.konrad@adacore.com> wrote:
>>>> Maybe a little hack might work for x86 on x86 though.
>>>> Something like hardcoding the helper with an inline assembly
>>>> code?
>>>
>>> The set of people who want to emulate x86 on x86 is surely
>>> even smaller than the already tiny set of people who want to
>>> emulate x86 at all. I don't think it makes sense to add
>>> inline assembly hacks for that: QEMU should (as far as
>>> is reasonably possible) behave the same on all hosts.
>>>
>>
>> Sure it was a first step suggestion.
> 
> FYI, there is some code in WinUAE / Previous which might be usable, see
> e.g. WinUAE sources here:
> 
> https://github.com/tonioni/WinUAE/blob/master/softfloat/softfloat_fpsp.cpp
> 
> But the code is targetted for m68k only so far, so it likely needs some
> work, and we're currently discussing whether it's feasible to include it
> into QEMU at all, see this thread here:
> 
> https://lists.gnu.org/archive/html/qemu-devel/2017-11/msg05342.html
> 
>   Thomas
> 

oops.

Hi Thomas sorry missed this mail.

I saw this thread, I'm not sure if m68k use the 80bits
arithmetic either.

BTW why don't we just use the long double cosl / sinl in the
helper?

It seems to me it can just fix the bug here (on linux host at
least, I think windows doesn't support them). We just lack the
long double <-> FloatX80 conversions here.

Fred

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2017-12-06 11:16 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-29  8:34 [Qemu-devel] [Bug 645662] x87 fpu emulation not accurate enough william lin
2017-11-29  9:39 ` KONRAD Frederic
2017-11-29  9:59   ` william lin
2017-11-29 10:17     ` KONRAD Frederic
2017-11-29 10:27     ` Peter Maydell
2017-11-29 12:47       ` KONRAD Frederic
2017-11-29 12:51         ` Peter Maydell
2017-11-29 15:20           ` KONRAD Frederic
2017-11-29 15:32             ` Thomas Huth
2017-12-06 11:15               ` KONRAD Frederic

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.