From patchwork Fri Apr 12 15:37:52 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 16119 Return-Path: X-Original-To: linaro@staging.patches.linaro.org Delivered-To: linaro@staging.patches.linaro.org Received: from mail-vb0-f69.google.com (mail-vb0-f69.google.com [209.85.212.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 1B8FD26EB0 for ; Fri, 12 Apr 2013 15:38:24 +0000 (UTC) Received: by mail-vb0-f69.google.com with SMTP id p12sf4566726vbe.8 for ; Fri, 12 Apr 2013 08:38:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:mime-version:x-beenthere:x-received:received-spf :x-received:x-forwarded-to:x-forwarded-for:delivered-to:x-received :received-spf:from:to:cc:subject:date:message-id:x-mailer :x-gm-message-state:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :x-google-group-id:list-post:list-help:list-archive:list-unsubscribe; bh=XNhPI/7Rbin5mqxjf5YXzeTSzJPV4jOE32rSm3VSAWE=; b=Z0VGrDCEAp5QZrqyMWUdfBsX/zBBXHXf9vuOz6KyG8lmzbFjK7zBYMBI79uDyJGvsF pkJw2y8wT/N4NBI3wmRa/rLmhp3fsIUJho596ZdxOHeNUFRG3s+RoGaSxwytC6toH8Mb C2lG4EfzMq6LcsgRcGfHYfgpXFBQFqL9gDn0js4cydRmp/DGWxo5//GhzaluopFNutyG SUKFf1r1H0dJQSF1WvlUve7RbCguka1sFJn27cfsdIylqJTt2v8lb+9ah3SC09mki3QB AIQ/sHHnzl8UU9himXOv+hC3TMD/6OSavaJnsaYWUEQoySpzwMefH1AgPc7OPeuesX9g jf9w== X-Received: by 10.236.77.37 with SMTP id c25mr5575124yhe.57.1365781079919; Fri, 12 Apr 2013 08:37:59 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.86.68 with SMTP id n4ls653965qez.90.gmail; Fri, 12 Apr 2013 08:37:59 -0700 (PDT) X-Received: by 10.52.90.112 with SMTP id bv16mr7387802vdb.62.1365781079630; Fri, 12 Apr 2013 08:37:59 -0700 (PDT) Received: from mail-ve0-f169.google.com (mail-ve0-f169.google.com [209.85.128.169]) by mx.google.com with ESMTPS id ez9si2377982vdb.68.2013.04.12.08.37.59 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 12 Apr 2013 08:37:59 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.128.169 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.128.169; Received: by mail-ve0-f169.google.com with SMTP id d10so2571294vea.28 for ; Fri, 12 Apr 2013 08:37:59 -0700 (PDT) X-Received: by 10.52.34.168 with SMTP id a8mr7534663vdj.75.1365781079193; Fri, 12 Apr 2013 08:37:59 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.58.127.98 with SMTP id nf2csp71013veb; Fri, 12 Apr 2013 08:37:58 -0700 (PDT) X-Received: by 10.180.188.141 with SMTP id ga13mr5028440wic.9.1365781078202; Fri, 12 Apr 2013 08:37:58 -0700 (PDT) Received: from mnementh.archaic.org.uk (1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.d.1.0.0.b.8.0.1.0.0.2.ip6.arpa. [2001:8b0:1d0::1]) by mx.google.com with ESMTPS id bv10si1034894wib.32.2013.04.12.08.37.57 (version=TLSv1 cipher=RC4-SHA bits=128/128); Fri, 12 Apr 2013 08:37:58 -0700 (PDT) Received-SPF: neutral (google.com: 2001:8b0:1d0::1 is neither permitted nor denied by best guess record for domain of pm215@archaic.org.uk) client-ip=2001:8b0:1d0::1; Received: from pm215 by mnementh.archaic.org.uk with local (Exim 4.72) (envelope-from ) id 1UQg2y-0006VL-QQ; Fri, 12 Apr 2013 16:37:52 +0100 From: Peter Maydell To: qemu-devel@nongnu.org Cc: patches@linaro.org, Anthony Liguori , Aurelien Jarno , Blue Swirl Subject: [PATCH] fpu: Correct edgecase in float64_muladd Date: Fri, 12 Apr 2013 16:37:52 +0100 Message-Id: <1365781072-24979-1-git-send-email-peter.maydell@linaro.org> X-Mailer: git-send-email 1.7.2.5 X-Gm-Message-State: ALoCoQn8EzF+XoqZmbrrwp4SsgBS2bagg8kUuRsEPlAQJl0yyzQa0fJ0vE4+WJZBvOGv6ohAI24Z X-Original-Sender: peter.maydell@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.128.169 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , In handling float64_muladd, if we end up doing a subtraction of the product and c, and the 128 bit result of this subtraction happens to have its most significant bit in bit 63, we weren't handling this correctly when attempting to normalize to put the most significant bit into bit 126. We would end up doing a right shift by a negative number (undefined behaviour in C) so at best we would return an incorrect result to the guest. MSB in bit 63 has to be handled as a special case separately from MSB in 0..62 and MSB in 63..126. (MSB in 127 is not possible.) Signed-off-by: Peter Maydell Reviewed-by: Aurelien Jarno --- Specific test vector which triggers this: a = 3fffffffffe00000 b = 3fffffffffe00000 c = c00fffffffc00000 Also tested with my usual set of random test vectors. fpu/softfloat.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/fpu/softfloat.c b/fpu/softfloat.c index 83ccc4b..7ba51b6 100644 --- a/fpu/softfloat.c +++ b/fpu/softfloat.c @@ -3898,9 +3898,15 @@ float64 float64_muladd(float64 a, float64 b, float64 c, int flags STATUS_PARAM) } zExp -= shiftcount; } else { - shiftcount = countLeadingZeros64(zSig1) - 1; - zSig0 = zSig1 << shiftcount; - zExp -= (shiftcount + 64); + shiftcount = countLeadingZeros64(zSig1); + if (shiftcount == 0) { + zSig0 = (zSig1 >> 1) | (zSig1 & 1); + zExp -= 63; + } else { + shiftcount--; + zSig0 = zSig1 << shiftcount; + zExp -= (shiftcount + 64); + } } return roundAndPackFloat64(zSign, zExp, zSig0 STATUS_VAR); }