Re: [Qemu-devel] [PATCH] fpu/softfloat: check for Inf / x or 0 / x before /0

2018-04-20 Thread Alex Bennée

Richard Henderson  writes:

> On 04/17/2018 01:08 PM, Peter Maydell wrote:
>> On 18 April 2018 at 00:01, Peter Maydell  wrote:
>>> I don't have the original IEEE754 spec to hand though;
>>> that may have left this unspecified.
>>
>> Having located a copy of 754-1985 I think that also is
>> clear enough that float-float conversion is an operation
>> that must quieten SNaN and raise Invalid.
>
> That does seem to match actual processor behaviour.
> The attached test case produces
>
> 7fa0
> 7ffc - 1
> 7ff4
> 7fe0 - 1
>
> on both x86_64 and aarch64.
>
> For ppc64, I believe there's a compiler bug:
>
> 7fa0
> 7ff4 - 0
> 7ff4
> 7fe0 - 536870912
>
> convert float to double:
> 174c:   60 00 01 c0 lfs f0,96(r1)
> 1750:   68 00 01 d8 stfdf0,104(r1)
>
> convert double to float:
> 17a8:   68 00 01 c8 lfd f0,104(r1)
> 17ac:   18 00 00 fc frspf0,f0
> 17b0:   60 00 01 d0 stfsf0,96(r1)
>
> Floating point numbers are held in a "register" format that largely 
> corresponds
> to double-precision.  Thus the compiler believes that loading a
> single-precision value and then storing it out again as a double is 
> sufficient.
>  However, as we can see above that does not consider SNaN.
>
> There is no ppc "convert single to double" instruction, there is only a "round
> to single precision" instruction -- frsp.  However, if we assume that the
> single precision number is already properly rounded, then we can add an extra
> frsp and it will not normally affect the value at all; it will only change the
> contents for snan.
>
> If I manually add the frsp insn to the code generated for the test case I get
>
> 7fa0
> 7ffc - 536870912
> 7ff4
> 7fe0 - 536870912
>
> exactly as we expect.  I'll file a bug vs gcc.

It does seem to be floating point is a rabbit hole that everyone gets
something wrong if you look too closely!

--
Alex Bennée



Re: [Qemu-devel] [PATCH] fpu/softfloat: check for Inf / x or 0 / x before /0

2018-04-19 Thread Peter Maydell
On 19 April 2018 at 20:12, Richard Henderson
 wrote:
> On 04/19/2018 09:06 AM, Richard Henderson wrote:
>> For ppc64, I believe there's a compiler bug:
>
> Bah.  Apparently one must now use -fsignalling-nans.
> With that option we do generate the conversion insn.

"This option is experimental", the gcc docs say :-)

thanks
-- PMM



Re: [Qemu-devel] [PATCH] fpu/softfloat: check for Inf / x or 0 / x before /0

2018-04-19 Thread Richard Henderson
On 04/19/2018 09:06 AM, Richard Henderson wrote:
> For ppc64, I believe there's a compiler bug:

Bah.  Apparently one must now use -fsignalling-nans.
With that option we do generate the conversion insn.


r~



Re: [Qemu-devel] [PATCH] fpu/softfloat: check for Inf / x or 0 / x before /0

2018-04-19 Thread Richard Henderson
On 04/17/2018 01:08 PM, Peter Maydell wrote:
> On 18 April 2018 at 00:01, Peter Maydell  wrote:
>> I don't have the original IEEE754 spec to hand though;
>> that may have left this unspecified.
> 
> Having located a copy of 754-1985 I think that also is
> clear enough that float-float conversion is an operation
> that must quieten SNaN and raise Invalid.

That does seem to match actual processor behaviour.
The attached test case produces

7fa0
7ffc - 1
7ff4
7fe0 - 1

on both x86_64 and aarch64.

For ppc64, I believe there's a compiler bug:

7fa0
7ff4 - 0
7ff4
7fe0 - 536870912

convert float to double:
174c:   60 00 01 c0 lfs f0,96(r1)
1750:   68 00 01 d8 stfdf0,104(r1)

convert double to float:
17a8:   68 00 01 c8 lfd f0,104(r1)
17ac:   18 00 00 fc frspf0,f0
17b0:   60 00 01 d0 stfsf0,96(r1)

Floating point numbers are held in a "register" format that largely corresponds
to double-precision.  Thus the compiler believes that loading a
single-precision value and then storing it out again as a double is sufficient.
 However, as we can see above that does not consider SNaN.

There is no ppc "convert single to double" instruction, there is only a "round
to single precision" instruction -- frsp.  However, if we assume that the
single precision number is already properly rounded, then we can add an extra
frsp and it will not normally affect the value at all; it will only change the
contents for snan.

If I manually add the frsp insn to the code generated for the test case I get

7fa0
7ffc - 536870912
7ff4
7fe0 - 536870912

exactly as we expect.  I'll file a bug vs gcc.


r~
#include 
#include 

int main() {
volatile union { unsigned i; float f; } u;
volatile union { unsigned long l; double d; } v;

feclearexcept(FE_ALL_EXCEPT);
u.f = __builtin_nansf("");
v.d = u.f;
printf("%08x\n", u.i);
printf("%016lx - %d\n", v.l, fetestexcept(FE_INVALID));

feclearexcept(FE_ALL_EXCEPT);
v.d = __builtin_nans("");
u.f = v.d;
printf("%016lx\n", v.l);
printf("%08x - %d\n", u.i, fetestexcept(FE_INVALID));

return 0;
}


Re: [Qemu-devel] [PATCH] fpu/softfloat: check for Inf / x or 0 / x before /0

2018-04-17 Thread Peter Maydell
On 18 April 2018 at 00:01, Peter Maydell  wrote:
> I don't have the original IEEE754 spec to hand though;
> that may have left this unspecified.

Having located a copy of 754-1985 I think that also is
clear enough that float-float conversion is an operation
that must quieten SNaN and raise Invalid.

thanks
-- PMM



Re: [Qemu-devel] [PATCH] fpu/softfloat: check for Inf / x or 0 / x before /0

2018-04-17 Thread Peter Maydell
On 17 April 2018 at 23:49, Richard Henderson
 wrote:
> On 04/17/2018 12:38 PM, Emilio G. Cota wrote:
>> On Tue, Apr 17, 2018 at 22:45:51 +0100, Peter Maydell wrote:
>>> On 17 April 2018 at 22:27, Emilio G. Cota  wrote:
 (...)
 +cff 0xffb0, expected: 0x7ff8, returned: 
 0x7ff4, \
   expected exceptions: i, returned: none
 +error: flags mismatch for input @ ibm/Basic-Types-Inputs.fptest:26170:
 +b32b64cff =0 S -> Q i
>>>
>>> SNaN conversion from 32 bit to 64 bit. Here I agree
>>> with the test -- we should quieten the NaN and raise
>>> Invalid -- which implies that the hardware is wrong ?!?
>>
>> This passes on an Intel host, and fails on both Power7 and 8 hosts I have
>> access to. I don't have the Power ISA spec in front of me, but I hope
>> there's something about this specified in it.
>
> IIRC this is unspecified and does vary by implementation.

I think 754-2008 does specify it: s6.2 says that you get
'set Invalid and return a QNaN if an input is an SNaN' for
"every general-computational and signaling-computational
operation except for the conversions described in 5.12".
So the only exceptions are:
 1) the s5.12 conversions, which are to/from strings-of-characters
 2) quiet-computational operations, which are just
copy, abs, negate, copySign, and some re-encoding
operations involving decimal formats

float-to-float conversions are general-computational.

I don't have the original IEEE754 spec to hand though;
that may have left this unspecified.

thanks
-- PMM



Re: [Qemu-devel] [PATCH] fpu/softfloat: check for Inf / x or 0 / x before /0

2018-04-17 Thread Richard Henderson
On 04/17/2018 12:38 PM, Emilio G. Cota wrote:
> On Tue, Apr 17, 2018 at 22:45:51 +0100, Peter Maydell wrote:
>> On 17 April 2018 at 22:27, Emilio G. Cota  wrote:
>>> BTW I just checked with -t host on an IBM Power8, and we get
>>> the same 1049 flag errors we get with -t soft plus two additional ones:
>>>
>>> +A 0xffb0, expected: 0x7fa0, returned: 0x7fa0, \
>>>   expected exceptions: i, returned: none
>>> +error: flags mismatch for input @ ibm/Basic-Types-Inputs.fptest:382:
>>> +b32A =0 S -> S i
>>
>> That's Abs of an SNaN; the test expects Invalid, which is wrong,
>> because IEEE754 says absolute-value is a "quiet-computational
>> operation" that never signals an exception.
>>
>> What's odd is that we don't report that error for the softfloat
>> implementation! I also don't understand why the expected value
>> isn't just the input value with the sign bit flipped.
> 
> With -t soft we don't handle "abs" and we don't get the error -- we get
> a "not handled" instead.
> Is there a function that we could use for abs? The only ones I've seen
> are floatX_abs() which mask out the sign bit and do nothing else.

Both abs and neg are pure bit operations.  So, yes, floatX_abs (and floatX_chs)
are the softfloat functions for these.

And if fp-test thinks Invalid should be raised, it's wrong.

>>> (...)
>>> +cff 0xffb0, expected: 0x7ff8, returned: 
>>> 0x7ff4, \
>>>   expected exceptions: i, returned: none
>>> +error: flags mismatch for input @ ibm/Basic-Types-Inputs.fptest:26170:
>>> +b32b64cff =0 S -> Q i
>>
>> SNaN conversion from 32 bit to 64 bit. Here I agree
>> with the test -- we should quieten the NaN and raise
>> Invalid -- which implies that the hardware is wrong ?!?
> 
> This passes on an Intel host, and fails on both Power7 and 8 hosts I have
> access to. I don't have the Power ISA spec in front of me, but I hope
> there's something about this specified in it.

IIRC this is unspecified and does vary by implementation.


r~



Re: [Qemu-devel] [PATCH] fpu/softfloat: check for Inf / x or 0 / x before /0

2018-04-17 Thread Emilio G. Cota
On Tue, Apr 17, 2018 at 22:45:51 +0100, Peter Maydell wrote:
> On 17 April 2018 at 22:27, Emilio G. Cota  wrote:
> > BTW I just checked with -t host on an IBM Power8, and we get
> > the same 1049 flag errors we get with -t soft plus two additional ones:
> >
> > +A 0xffb0, expected: 0x7fa0, returned: 0x7fa0, \
> >   expected exceptions: i, returned: none
> > +error: flags mismatch for input @ ibm/Basic-Types-Inputs.fptest:382:
> > +b32A =0 S -> S i
> 
> That's Abs of an SNaN; the test expects Invalid, which is wrong,
> because IEEE754 says absolute-value is a "quiet-computational
> operation" that never signals an exception.
> 
> What's odd is that we don't report that error for the softfloat
> implementation! I also don't understand why the expected value
> isn't just the input value with the sign bit flipped.

With -t soft we don't handle "abs" and we don't get the error -- we get
a "not handled" instead.
Is there a function that we could use for abs? The only ones I've seen
are floatX_abs() which mask out the sign bit and do nothing else.

> > (...)
> > +cff 0xffb0, expected: 0x7ff8, returned: 
> > 0x7ff4, \
> >   expected exceptions: i, returned: none
> > +error: flags mismatch for input @ ibm/Basic-Types-Inputs.fptest:26170:
> > +b32b64cff =0 S -> Q i
> 
> SNaN conversion from 32 bit to 64 bit. Here I agree
> with the test -- we should quieten the NaN and raise
> Invalid -- which implies that the hardware is wrong ?!?

This passes on an Intel host, and fails on both Power7 and 8 hosts I have
access to. I don't have the Power ISA spec in front of me, but I hope
there's something about this specified in it.

E.



Re: [Qemu-devel] [PATCH] fpu/softfloat: check for Inf / x or 0 / x before /0

2018-04-17 Thread Peter Maydell
On 17 April 2018 at 22:27, Emilio G. Cota  wrote:
> BTW I just checked with -t host on an IBM Power8, and we get
> the same 1049 flag errors we get with -t soft plus two additional ones:
>
> +A 0xffb0, expected: 0x7fa0, returned: 0x7fa0, \
>   expected exceptions: i, returned: none
> +error: flags mismatch for input @ ibm/Basic-Types-Inputs.fptest:382:
> +b32A =0 S -> S i

That's Abs of an SNaN; the test expects Invalid, which is wrong,
because IEEE754 says absolute-value is a "quiet-computational
operation" that never signals an exception.

What's odd is that we don't report that error for the softfloat
implementation! I also don't understand why the expected value
isn't just the input value with the sign bit flipped.

> (...)
> +cff 0xffb0, expected: 0x7ff8, returned: 0x7ff4, \
>   expected exceptions: i, returned: none
> +error: flags mismatch for input @ ibm/Basic-Types-Inputs.fptest:26170:
> +b32b64cff =0 S -> Q i

SNaN conversion from 32 bit to 64 bit. Here I agree
with the test -- we should quieten the NaN and raise
Invalid -- which implies that the hardware is wrong ?!?

thanks
-- PMM



Re: [Qemu-devel] [PATCH] fpu/softfloat: check for Inf / x or 0 / x before /0

2018-04-17 Thread Emilio G. Cota
On Tue, Apr 17, 2018 at 21:54:03 +0100, Peter Maydell wrote:
> On 17 April 2018 at 20:04, Emilio G. Cota  wrote:
> > Note that in fp-test I am not checking for flags that are raised
> > when none are expected, because doing so gives quite a few errors.
> > Just noticed that enabling this check yields 1049 of these errors for
> > v2.11, and before this patch that number was 1087. After this
> > patch, it is again brought down to 1049. IOW, the test cases in
> > fp-test raise exactly the same flags as v2.11, which is good to know.
> >
> > The 1049 errors are probably false positives -- at least a big
> > chunk of them should be, given that "-t host" gives even more errors.
> > I am tempted to keep the flag check and whitelist these errors
> > though, which would catch regressions such as the one we're fixing here.
> 
> I strongly suspect we do have a few cases where we get the answers
> wrong and/or don't report the flags right, so ideally we'd have
> a look at them in more detail...
> 
> > Here is the report file with the 1049 failing test cases:
> >   http://www.cs.columbia.edu/~cota/qemu/fp-test-after-inf-patch.txt
> 
> Syntax for interpreting the report:
> https://www.research.ibm.com/haifa/projects/verification/fpgen/syntax.txt
> 
> Here's the first one, am I reading it right?
> 
> + 0xffc0 0xffb0, expected: 0xffc0, returned: 0xffc0,
> expected exceptions: none, returned: i
> error: flags mismatch for input @ ibm/Basic-Types-Inputs.fptest:1346:
> b32+ =0 Q S -> Q
> 
> That's a float32 addition where the first input is a QNaN
> and the second an SNaN (presumably the test is configured
> for what in QEMU is snan_bit_is_one == 0), and it expects
> the result to be the QNaN, with no exceptions set. But
> we raise Invalid.
> =0 is the rounding mode, not relevant here.

Yes, you're reading it right.

> IEEE754-2008 s6.2 seems pretty clear that if there's
> an SNaN as an operand then operations like addition should
> signal Invalid. So this looks like a bug in the test case
> input. (Which is weird, because IBM must have tested this,
> so it's odd to see an obvious error in it.)

Yes sometimes the input files don't make much sense -- that's why
I ended up whitelisting some of them.

BTW I just checked with -t host on an IBM Power8, and we get
the same 1049 flag errors we get with -t soft plus two additional ones:

+A 0xffb0, expected: 0x7fa0, returned: 0x7fa0, \
  expected exceptions: i, returned: none
+error: flags mismatch for input @ ibm/Basic-Types-Inputs.fptest:382:
+b32A =0 S -> S i
(...)
+cff 0xffb0, expected: 0x7ff8, returned: 0x7ff4, \
  expected exceptions: i, returned: none
+error: flags mismatch for input @ ibm/Basic-Types-Inputs.fptest:26170:
+b32b64cff =0 S -> Q i

On x86 with -t host we again get a strict superset of what we get
with -t soft.

So yeah, I don't know what these test cases are about.

> Most of the "expected none, returned i" lines look
> like the same thing. We should look at the others, though.

Given the above, whitelisting the 1049 cases and forcing the flag
checks for all tests (as below) seems reasonable to me.

--- a/tests/fp/fp-test.c
+++ b/tests/fp/fp-test.c
@@ -247,7 +247,7 @@ static enum error tester_check(const struct test_op *t, 
uint64_t res64,
 goto out;
 }
 }
-if (t->exceptions && flags != (t->exceptions | default_exceptions)) {
+if (flags != (t->exceptions | default_exceptions)) {
 err = ERROR_EXCEPTIONS;
 goto out;
 }

Thanks,

E.



Re: [Qemu-devel] [PATCH] fpu/softfloat: check for Inf / x or 0 / x before /0

2018-04-17 Thread Peter Maydell
On 17 April 2018 at 20:04, Emilio G. Cota  wrote:
> Note that in fp-test I am not checking for flags that are raised
> when none are expected, because doing so gives quite a few errors.
> Just noticed that enabling this check yields 1049 of these errors for
> v2.11, and before this patch that number was 1087. After this
> patch, it is again brought down to 1049. IOW, the test cases in
> fp-test raise exactly the same flags as v2.11, which is good to know.
>
> The 1049 errors are probably false positives -- at least a big
> chunk of them should be, given that "-t host" gives even more errors.
> I am tempted to keep the flag check and whitelist these errors
> though, which would catch regressions such as the one we're fixing here.

I strongly suspect we do have a few cases where we get the answers
wrong and/or don't report the flags right, so ideally we'd have
a look at them in more detail...

> Here is the report file with the 1049 failing test cases:
>   http://www.cs.columbia.edu/~cota/qemu/fp-test-after-inf-patch.txt

Syntax for interpreting the report:
https://www.research.ibm.com/haifa/projects/verification/fpgen/syntax.txt

Here's the first one, am I reading it right?

+ 0xffc0 0xffb0, expected: 0xffc0, returned: 0xffc0,
expected exceptions: none, returned: i
error: flags mismatch for input @ ibm/Basic-Types-Inputs.fptest:1346:
b32+ =0 Q S -> Q

That's a float32 addition where the first input is a QNaN
and the second an SNaN (presumably the test is configured
for what in QEMU is snan_bit_is_one == 0), and it expects
the result to be the QNaN, with no exceptions set. But
we raise Invalid.
=0 is the rounding mode, not relevant here.

IEEE754-2008 s6.2 seems pretty clear that if there's
an SNaN as an operand then operations like addition should
signal Invalid. So this looks like a bug in the test case
input. (Which is weird, because IBM must have tested this,
so it's odd to see an obvious error in it.)

Most of the "expected none, returned i" lines look
like the same thing. We should look at the others, though.

thanks
-- PMM



Re: [Qemu-devel] [PATCH] fpu/softfloat: check for Inf / x or 0 / x before /0

2018-04-17 Thread Emilio G. Cota
On Mon, Apr 16, 2018 at 14:54:42 +0100, Alex Bennée wrote:
> The re-factoring of div_floats changed the order of checking meaning
> an operation like -inf/0 erroneously raises the divbyzero flag.
> IEEE-754 (2008) specifies this should only occur for operations on
> finite operands.
> 
> We fix this by moving the check on the dividend being Inf/0 to before
> the divisor is zero check.
> 
> Signed-off-by: Alex Bennée 
> Cc: Bastian Koppelmann 

I can confirm this fixes the issue -- just checked with a modified
version of fp-test, see below.

Note that in fp-test I am not checking for flags that are raised
when none are expected, because doing so gives quite a few errors.
Just noticed that enabling this check yields 1049 of these errors for
v2.11, and before this patch that number was 1087. After this
patch, it is again brought down to 1049. IOW, the test cases in
fp-test raise exactly the same flags as v2.11, which is good to know.

The 1049 errors are probably false positives -- at least a big
chunk of them should be, given that "-t host" gives even more errors.
I am tempted to keep the flag check and whitelist these errors
though, which would catch regressions such as the one we're fixing here.

Here is the report file with the 1049 failing test cases:
  http://www.cs.columbia.edu/~cota/qemu/fp-test-after-inf-patch.txt

Thanks,

Emilio



Re: [Qemu-devel] [PATCH] fpu/softfloat: check for Inf / x or 0 / x before /0

2018-04-17 Thread Peter Maydell
On 16 April 2018 at 14:54, Alex Bennée  wrote:
> The re-factoring of div_floats changed the order of checking meaning
> an operation like -inf/0 erroneously raises the divbyzero flag.
> IEEE-754 (2008) specifies this should only occur for operations on
> finite operands.
>
> We fix this by moving the check on the dividend being Inf/0 to before
> the divisor is zero check.
>
> Signed-off-by: Alex Bennée 
> Cc: Bastian Koppelmann 

Applied, thanks.

-- PMM



Re: [Qemu-devel] [PATCH] fpu/softfloat: check for Inf / x or 0 / x before /0

2018-04-17 Thread Peter Maydell
On 16 April 2018 at 20:39, Richard Henderson
 wrote:
> On 04/16/2018 03:54 AM, Alex Bennée wrote:
>> +/* Inf / x or 0 / x */
>> +if (a.cls == float_class_inf || a.cls == float_class_zero) {
>> +a.sign = sign;
>> +return a;
>> +}
>
> 0/0 should raise an exception.

It does -- we check for 0/0 and Inf/Inf first, just before this check.

thanks
-- PMM



Re: [Qemu-devel] [PATCH] fpu/softfloat: check for Inf / x or 0 / x before /0

2018-04-16 Thread Richard Henderson
On 04/16/2018 03:54 AM, Alex Bennée wrote:
> +/* Inf / x or 0 / x */
> +if (a.cls == float_class_inf || a.cls == float_class_zero) {
> +a.sign = sign;
> +return a;
> +}

0/0 should raise an exception.

I find inf/0 non-intuitive, but there ya go.

r~



Re: [Qemu-devel] [PATCH] fpu/softfloat: check for Inf / x or 0 / x before /0

2018-04-16 Thread Peter Maydell
On 16 April 2018 at 15:42, Alex Bennée  wrote:
>
> Bastian Koppelmann  writes:
>
>> On 04/16/2018 03:54 PM, Alex Bennée wrote:
>>> The re-factoring of div_floats changed the order of checking meaning
>>> an operation like -inf/0 erroneously raises the divbyzero flag.
>>> IEEE-754 (2008) specifies this should only occur for operations on
>>> finite operands.
>>>
>>> We fix this by moving the check on the dividend being Inf/0 to before
>>> the divisor is zero check.
>>>
>>> Signed-off-by: Alex Bennée 
>>> Cc: Bastian Koppelmann 
>>> ---
>>>  fpu/softfloat.c | 10 +-
>>>  1 file changed, 5 insertions(+), 5 deletions(-)
>>
>> Reviewed-by: Bastian Koppelmann 
>> Tested-by: Bastian Koppelmann 
>
> Peter are you going to grab these tags or do you want me to re-spin?

'patches' will grab them automatically when it applies the patch.

thanks
-- PMM



Re: [Qemu-devel] [PATCH] fpu/softfloat: check for Inf / x or 0 / x before /0

2018-04-16 Thread Alex Bennée

Bastian Koppelmann  writes:

> On 04/16/2018 03:54 PM, Alex Bennée wrote:
>> The re-factoring of div_floats changed the order of checking meaning
>> an operation like -inf/0 erroneously raises the divbyzero flag.
>> IEEE-754 (2008) specifies this should only occur for operations on
>> finite operands.
>>
>> We fix this by moving the check on the dividend being Inf/0 to before
>> the divisor is zero check.
>>
>> Signed-off-by: Alex Bennée 
>> Cc: Bastian Koppelmann 
>> ---
>>  fpu/softfloat.c | 10 +-
>>  1 file changed, 5 insertions(+), 5 deletions(-)
>
> Reviewed-by: Bastian Koppelmann 
> Tested-by: Bastian Koppelmann 

Peter are you going to grab these tags or do you want me to re-spin?


--
Alex Bennée



Re: [Qemu-devel] [PATCH] fpu/softfloat: check for Inf / x or 0 / x before /0

2018-04-16 Thread Bastian Koppelmann
On 04/16/2018 03:54 PM, Alex Bennée wrote:
> The re-factoring of div_floats changed the order of checking meaning
> an operation like -inf/0 erroneously raises the divbyzero flag.
> IEEE-754 (2008) specifies this should only occur for operations on
> finite operands.
> 
> We fix this by moving the check on the dividend being Inf/0 to before
> the divisor is zero check.
> 
> Signed-off-by: Alex Bennée 
> Cc: Bastian Koppelmann 
> ---
>  fpu/softfloat.c | 10 +-
>  1 file changed, 5 insertions(+), 5 deletions(-)

Reviewed-by: Bastian Koppelmann 
Tested-by: Bastian Koppelmann 

Cheers,
Bastian