From patchwork Wed Feb 12 06:19:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1236642 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-519381-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha1 header.s=default header.b=EUGuH4Uq; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=EDf35rxF; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48HTzJ1LNsz9s1x for ; Wed, 12 Feb 2020 17:20:41 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:reply-to:references:mime-version :in-reply-to:content-type:content-transfer-encoding; q=dns; s= default; b=Sq4Xr51hDs/qiiwMFllS7qxt2m9KmNpsNaOSlBTZqEoJxkwgT8goG 1Azz3WBQgRgB4g38V3yTmb0legr9HUVSewnImk/4/cXAZ4r5XSJ1wKhAlw/xAXqg U1V+2+OMVEAsBZD74IJ7CJSdvwHVrj090diOCpQ8rmBgUBrdAnKn8c= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:reply-to:references:mime-version :in-reply-to:content-type:content-transfer-encoding; s=default; bh=PTpRe5cOh31inF3AG5u0yTfBKAc=; b=EUGuH4UqakM7BBHhU/l1vqqog+dC KGbxMnv3dATNDjHlxRMFIV9xtffIHWEfZT+EbkoxGcm2nG25wuO54tALNRcS91IC Kt5MtH+2qlRqN2UeBZ0HGiyxkOnGJlBblGdV0008grFIyyIE2IqOJd2vtbyzM6Av ya8Im/LcyJMG4Pk= Received: (qmail 5298 invoked by alias); 12 Feb 2020 06:20:33 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 5284 invoked by uid 89); 12 Feb 2020 06:20:32 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-8.0 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 spammy=005, 2020-01-12, 20200112 X-HELO: us-smtp-delivery-1.mimecast.com Received: from us-smtp-1.mimecast.com (HELO us-smtp-delivery-1.mimecast.com) (207.211.31.81) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 12 Feb 2020 06:20:31 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1581488429; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=U7GXyolNZQrmicmBZupnSjkGbPnKmfW2x77+gjFCLN8=; b=EDf35rxFkroFvaniHdGuZlsPCMhiFCxOII2L3p8PPzJcG7gStr+LdSHjZeyU4OfSIBvMAn HkgtZg28JvFn9Cbh8l2UyJPogPYCEyTQVpOkSMcAkoRWbpW2+9WCC/qRx6F6I3yn67q1ep 29B+Xfx4NtxN8+QAdYAkF/lr5Xi4xdk= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-331-wgakTH35PLS3CCcGe-i-HA-1; Wed, 12 Feb 2020 01:20:26 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D845D8014CE; Wed, 12 Feb 2020 06:20:24 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-116-51.ams2.redhat.com [10.36.116.51]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6DD4087B34; Wed, 12 Feb 2020 06:20:24 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id 01C6JbqB016097; Wed, 12 Feb 2020 07:19:57 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id 01C6J2vQ016086; Wed, 12 Feb 2020 07:19:02 +0100 Date: Wed, 12 Feb 2020 07:19:01 +0100 From: Jakub Jelinek To: Joseph Myers Cc: Tejas Joshi , gcc-patches@gcc.gnu.org Subject: [PATCH] real: Fix roundeven on inf/nan [PR93663] Message-ID: <20200212061901.GV17695@tucnak> Reply-To: Jakub Jelinek References: MIME-Version: 1.0 In-Reply-To: User-Agent: Mutt/1.11.3 (2019-02-01) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-IsSubscribed: yes On Fri, Aug 23, 2019 at 08:38:43PM +0000, Joseph Myers wrote: > > +/* Return true if R is halfway between two integers, else return > > + false. The function is not valid for rvc_inf and rvc_nan classes. */ > > + > > +bool > > +is_halfway_below (const REAL_VALUE_TYPE *r) > > +{ > > + gcc_assert (r->cl != rvc_inf); > > + gcc_assert (r->cl != rvc_nan); > > + int i; > > Explicitly check for rvc_zero and return false in that case (that seems to > be the convention in real.c, rather than relying on code using REAL_EXP to > do something sensible for zero, which has REAL_EXP of 0). As can be seen in the testcase, roundeven with inf or nan arguments ICE because of those asserts where nothing prevents from is_halfway_below being called with those arguments. The following patch fixes that by just returning false for rvc_inf/rvc_nan like it returns for rvc_zero, so that we handle roundeven with all those values as round. Inf/NaN are not halfway in between two integers... Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2020-02-12 Jakub Jelinek PR middle-end/93663 * real.c (is_even): Make static. Function comment fix. (is_halfway_below): Make static, don't assert R is not inf/nan, instead return false for those. Small formatting fixes. * gcc.dg/torture/builtin-round-roundeven.c (main): Add tests for DBL_MAX, inf, their negations and nan. Jakub --- gcc/real.c.jj 2020-01-23 16:16:16.456601333 +0100 +++ gcc/real.c 2020-02-11 12:40:06.571398420 +0100 @@ -5150,10 +5150,10 @@ real_round (REAL_VALUE_TYPE *r, format_h real_convert (r, fmt, r); } -/* Return true including 0 if integer part of R is even, else return +/* Return true (including 0) if integer part of R is even, else return false. The function is not valid for rvc_inf and rvc_nan classes. */ -bool +static bool is_even (REAL_VALUE_TYPE *r) { gcc_assert (r->cl != rvc_inf); @@ -5184,16 +5184,12 @@ is_even (REAL_VALUE_TYPE *r) } /* Return true if R is halfway between two integers, else return - false. The function is not valid for rvc_inf and rvc_nan classes. */ + false. */ -bool +static bool is_halfway_below (const REAL_VALUE_TYPE *r) { - gcc_assert (r->cl != rvc_inf); - gcc_assert (r->cl != rvc_nan); - int i; - - if (r->cl == rvc_zero) + if (r->cl != rvc_normal) return false; /* For numbers (-0.5,0) and (0,0.5). */ @@ -5205,13 +5201,13 @@ is_halfway_below (const REAL_VALUE_TYPE unsigned int n = SIGNIFICAND_BITS - REAL_EXP (r) - 1; int w = n / HOST_BITS_PER_LONG; - for (i = 0; i < w; ++i) + for (int i = 0; i < w; ++i) if (r->sig[i] != 0) return false; - unsigned long num = ((unsigned long)1 << (n % HOST_BITS_PER_LONG)); + unsigned long num = 1UL << (n % HOST_BITS_PER_LONG); - if (((r->sig[w] & num) != 0) && ((r->sig[w] & (num-1)) == 0)) + if ((r->sig[w] & num) != 0 && (r->sig[w] & (num - 1)) == 0) return true; } return false; --- gcc/testsuite/gcc.dg/torture/builtin-round-roundeven.c.jj 2020-01-12 11:54:37.547396300 +0100 +++ gcc/testsuite/gcc.dg/torture/builtin-round-roundeven.c 2020-02-11 12:47:55.346354893 +0100 @@ -18,6 +18,13 @@ main (void) TEST(roundeven, -1.5, -2); TEST(roundeven, 3.499, 3); TEST(roundeven, 3.501, 4); + TEST(roundeven, __DBL_MAX__, __DBL_MAX__); + TEST(roundeven, -__DBL_MAX__, -__DBL_MAX__); + TEST(roundeven, __builtin_inf (), __builtin_inf ()); + TEST(roundeven, -__builtin_inf (), -__builtin_inf ()); + + if (!__builtin_isnan (__builtin_roundeven (__builtin_nan ("")))) + link_error (__LINE__); if (__builtin_copysign (1, __builtin_roundeven (-0.5)) != -1) link_error (__LINE__); @@ -31,6 +38,5 @@ main (void) link_error (__LINE__); if (__builtin_copysign (-1, __builtin_roundeven (0.25)) != 1) link_error (__LINE__); - return 0; + return 0; } -