All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tom Musta <tommusta@gmail.com>
To: Peter Maydell <peter.maydell@linaro.org>
Cc: "qemu-ppc@nongnu.org" <qemu-ppc@nongnu.org>,
	QEMU Developers <qemu-devel@nongnu.org>
Subject: Re: [Qemu-devel] [Qemu-ppc] [V4 PATCH 01/22] softfloat: Fix float64_to_uint64
Date: Fri, 20 Dec 2013 14:05:24 -0600	[thread overview]
Message-ID: <52B4A304.5030601@gmail.com> (raw)
In-Reply-To: <CAFEAcA8Aum+PR_U=hHfmJ6dn=BEyWHk3QF3FmcP6MqmSaPshkQ@mail.gmail.com>

On 12/19/2013 4:11 PM, Peter Maydell wrote:
> On 18 December 2013 20:19, Tom Musta <tommusta@gmail.com> wrote:
>> The comment preceding the float64_to_uint64 routine suggests that
>> the implementation is broken.  And this is, indeed, the case.
>>
>> This patch properly implements the conversion of a 64-bit floating
>> point number to an unsigned, 64 bit integer.
>>
>> This contribution can be licensed under either the softfloat-2a or -2b
>> license.
>>
>> V2: Added softfloat license statement.
>>
>> V3: Modified to meet QEMU coding conventions.
>>
>> V4: Fixed incorrect handling of small negatives, which, if rounded
>> up to zero should not set the inexact flag.
>>
>> Signed-off-by: Tom Musta <tommusta@gmail.com>
>> ---
>>  fpu/softfloat.c |   98 +++++++++++++++++++++++++++++++++++++++++++++++++-----
>>  1 files changed, 89 insertions(+), 9 deletions(-)
>>
>> diff --git a/fpu/softfloat.c b/fpu/softfloat.c
>> index dbda61b..ec23908 100644
>> --- a/fpu/softfloat.c
>> +++ b/fpu/softfloat.c
>> @@ -161,7 +161,6 @@ static int32 roundAndPackInt32( flag zSign, uint64_t absZ STATUS_PARAM)
>>  | exception is raised and the largest positive or negative integer is
>>  | returned.
>>  *----------------------------------------------------------------------------*/
>> -
>>  static int64 roundAndPackInt64( flag zSign, uint64_t absZ0, uint64_t absZ1 STATUS_PARAM)
>>  {
>>      int8 roundingMode;
>> @@ -204,6 +203,56 @@ static int64 roundAndPackInt64( flag zSign, uint64_t absZ0, uint64_t absZ1 STATU
>>  }
>>
>>  /*----------------------------------------------------------------------------
>> +| Takes the 128-bit fixed-point value formed by concatenating `absZ0' and
>> +| `absZ1', with binary point between bits 63 and 64 (between the input words),
>> +| and returns the properly rounded 64-bit unsigned integer corresponding to the
>> +| input.  Ordinarily, the fixed-point input is simply rounded to an integer,
>> +| with the inexact exception raised if the input cannot be represented exactly
>> +| as an integer.  However, if the fixed-point input is too large, the invalid
>> +| exception is raised and the largest unsigned integer is returned.
>> +*----------------------------------------------------------------------------*/
> 
> You should probably say in this comment what the behaviour is for
> negative inputs.
> 
>> +uint64_t float64_to_uint64(float64 a STATUS_PARAM)
>> +{
>> +    flag aSign;
>> +    int_fast16_t aExp, shiftCount;
>> +    uint64_t aSig, aSigExtra;
>> +    a = float64_squash_input_denormal(a STATUS_VAR);
>>
>> -    return v - INT64_MIN;
>> +    aSig = extractFloat64Frac(a);
>> +    aExp = extractFloat64Exp(a);
>> +    aSign = extractFloat64Sign(a);
>> +    if (aSign && (aExp > 1022)) {
>> +        float_raise(float_flag_invalid STATUS_VAR);
>> +        return 0;
> 
> This incorrectly returns 0 rather than largest-positive-integer
> for NaNs with the sign bit set.
> 
>> +    }
>> +    if (aExp) {
>> +        aSig |= LIT64(0x0010000000000000);
>> +    }
>> +    shiftCount = 0x433 - aExp;
>> +    if (shiftCount <= 0) {
>> +        if (0x43E < aExp) {
>> +            float_raise(float_flag_invalid STATUS_VAR);
>> +            return LIT64(0xFFFFFFFFFFFFFFFF);
>> +        }
>> +        aSigExtra = 0;
>> +        aSig <<= -shiftCount;
>> +    } else {
>> +        shift64ExtraRightJamming(aSig, 0, shiftCount, &aSig, &aSigExtra);
>> +    }
>> +    return roundAndPackUint64(aSign, aSig, aSigExtra STATUS_VAR);
>>  }
> 
> Other than that, the code *looks* OK, but it's really easy for
> "not quite right" code to slip through here (especially on corner
> cases like NaNs, denormals and odd rounding modes). How much
> testing have you given this? I really recommend testing by firing a
> huge pile of random (and semi random) test vectors at whatever
> guest instruction you're implementing and comparing against
> results on reference hardware.
> 
> thanks
> -- PMM
> 

Peter:

I agree with the comments and also with the bug.  I will fix.

I do test like you said ... random patterns with some biasing to try to get into corner
cases.  The bug you found was masked in the PowerPC code that wrapped to call to
float64_to_uint64.  I have constructed a variant of my test harness that invokes
float64_to_uint64 directly and it has uncovered the bug.

  reply	other threads:[~2013-12-20 20:06 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-18 20:18 [Qemu-devel] [V4 PATCH 00/22] PowerPC VSX Stage 3 Tom Musta
2013-12-18 20:19 ` [Qemu-devel] [V4 PATCH 01/22] softfloat: Fix float64_to_uint64 Tom Musta
2013-12-19 22:11   ` Peter Maydell
2013-12-20 20:05     ` Tom Musta [this message]
2013-12-18 20:19 ` [Qemu-devel] [V4 PATCH 02/22] softfloat: Add float32_to_uint64() Tom Musta
2013-12-19 21:31   ` Peter Maydell
2013-12-20 20:07     ` Tom Musta
2013-12-18 20:19 ` [Qemu-devel] [V4 PATCH 03/22] softfloat: Fix float64_to_uint64_round_to_zero Tom Musta
2013-12-19 21:43   ` Peter Maydell
2013-12-18 20:19 ` [Qemu-devel] [V4 PATCH 04/22] softfloat: Fix float64_to_uint32 Tom Musta
2013-12-19 21:48   ` Peter Maydell
2013-12-18 20:19 ` [Qemu-devel] [V4 PATCH 05/22] softfloat: Fix float64_to_uint32_round_to_zero Tom Musta
2013-12-19 21:41   ` Peter Maydell
2013-12-18 20:19 ` [Qemu-devel] [V4 PATCH 06/22] target-ppc: Add set_fprf Argument to fload_invalid_op_excp() Tom Musta
2013-12-18 20:19 ` [Qemu-devel] [V4 PATCH 07/22] target-ppc: General Support for VSX Helpers Tom Musta
2013-12-18 20:19 ` [Qemu-devel] [V4 PATCH 08/22] target-ppc: Add VSX ISA2.06 xadd/xsub Instructions Tom Musta
2013-12-18 20:19 ` [Qemu-devel] [V4 PATCH 09/22] target-ppc: Add VSX ISA2.06 xmul Instructions Tom Musta
2013-12-18 20:19 ` [Qemu-devel] [V4 PATCH 10/22] target-ppc: Add VSX ISA2.06 xdiv Instructions Tom Musta
2013-12-18 20:19 ` [Qemu-devel] [V4 PATCH 11/22] target-ppc: Add VSX ISA2.06 xre Instructions Tom Musta
2013-12-18 20:19 ` [Qemu-devel] [V4 PATCH 12/22] target-ppc: Add VSX ISA2.06 xsqrt Instructions Tom Musta
2013-12-18 20:19 ` [Qemu-devel] [V4 PATCH 13/22] target-ppc: Add VSX ISA2.06 xrsqrte Instructions Tom Musta
2013-12-18 20:19 ` [Qemu-devel] [V4 PATCH 14/22] target-ppc: Add VSX ISA2.06 xtdiv Instructions Tom Musta
2013-12-18 20:19 ` [Qemu-devel] [V4 PATCH 15/22] target-ppc: Add VSX ISA2.06 xtsqrt Instructions Tom Musta
2013-12-18 20:19 ` [Qemu-devel] [V4 PATCH 16/22] target-ppc: Add VSX ISA2.06 Multiply Add Instructions Tom Musta
2013-12-18 20:19 ` [Qemu-devel] [V4 PATCH 17/22] target-ppc: Add VSX xscmp*dp Instructions Tom Musta
2013-12-18 20:19 ` [Qemu-devel] [V4 PATCH 18/22] target-ppc: Add VSX xmax/xmin Instructions Tom Musta
2013-12-24 16:23   ` Richard Henderson
2013-12-18 20:19 ` [Qemu-devel] [V4 PATCH 19/22] target-ppc: Add VSX Vector Compare Instructions Tom Musta
2013-12-18 20:19 ` [Qemu-devel] [V4 PATCH 20/22] target-ppc: Add VSX Floating Point to Floating Point Conversion Instructions Tom Musta
2013-12-18 20:19 ` [Qemu-devel] [V4 PATCH 21/22] target-ppc: Add VSX ISA2.06 Integer " Tom Musta
2013-12-18 20:19 ` [Qemu-devel] [V4 PATCH 22/22] target-ppc: Add VSX Rounding Instructions Tom Musta

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=52B4A304.5030601@gmail.com \
    --to=tommusta@gmail.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-ppc@nongnu.org \
    /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 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.