From patchwork Fri Nov 11 18:11:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aldy Hernandez X-Patchwork-Id: 18937 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp894158wru; Fri, 11 Nov 2022 10:12:44 -0800 (PST) X-Google-Smtp-Source: AA0mqf793VSYiypbVLrTPcbXdU6lf5pmfMUskSzmmPcVHb5eMYN28tjsWExLC0Hh6tu02Zw3U/61 X-Received: by 2002:a50:bb2c:0:b0:463:71ef:b9ce with SMTP id y41-20020a50bb2c000000b0046371efb9cemr2512296ede.75.1668190364275; Fri, 11 Nov 2022 10:12:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668190364; cv=none; d=google.com; s=arc-20160816; b=kGA8+zJoM0RpYMnImJ3++K0NW5GP6lAl2OfTVhYYYTey0GuZoqwjkw82IaOCaUqzS6 HIr+JJwR9DJhWCIR1ssV9VHyWJHvJzrtRrXl5g27cP3epQv50UpL7b8OiJfXZeYlkIzy NszMZtnIvsuE6xtheAThnYJsnMI4ShOmfz7TI03/L3AAi8EoGKBiTVXb3ekT+F+R//hv CJ9X0icQvoyUnnhlGh8DOyuj02d4T00VZeAep1gY4fyn1iOnahWOwMTkphuhFcz8DNtV ZHB9kZqDy2XK4CU7wYLRiyKKLEvY7YoYiU8i8zQadJgiGGe4f38yEP1xjXMT/k1JTg5o 6SjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=USNA+4Z7/gu0qfit/FIc8uMz50lHYMTGb3O2A9pl6uU=; b=AdCJjswYCFmn2MTq3AB/qeeaXEEz3QkBEI51pox7PS0Ixob5tUKXM4Rtk+8IQf6Lry xXfjGzjNE9W4qzwIF5SYFrfQptcbd1S62t8ovuregxR7De6J6fFLfU+5P3h2B/8GsvS0 KykdOHkQrhKBT5Iw85MBHH3M06mJxaH0Y4R2KYi/s8TkLM9NJ9ISmZ72vjkXIAOoxKXs aT5ouifHeb6WK24yjlDa/deDtNn04bKH67CSjzkAZaVPIgHPiURMxVtvAyV7Lha2aaXL B/TPJut1vI1cUdgaXBmS8pMPR1eWqyi3NJgfGM41BapuUC/Z7++cqgRV/ckCeqThdyz0 +afQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=s1jPahyj; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id u1-20020a1709064ac100b0078db3762061si1995151ejt.439.2022.11.11.10.12.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Nov 2022 10:12:44 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=s1jPahyj; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BDD5738582AC for ; Fri, 11 Nov 2022 18:12:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BDD5738582AC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668190360; bh=USNA+4Z7/gu0qfit/FIc8uMz50lHYMTGb3O2A9pl6uU=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=s1jPahyjiNTh7jSm8oUmXHpOwtDG5SCFsV0C/sekHmmZVve0vef2a0dG7rFX+efvP H9VR17G977Ohn2AuCCni5vtb8IfqBmbQH6bKl1fIKApNiGjPQCwxjnlanW9Aq9WcUj xrdqIieMa7HZaV8Ur8i9QQnfdeYrxbTsv3Q2AXjc= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 96A14385841C for ; Fri, 11 Nov 2022 18:11:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 96A14385841C Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-308-8ri4Bbt3N4qZYPx5b3OGmA-1; Fri, 11 Nov 2022 13:11:53 -0500 X-MC-Unique: 8ri4Bbt3N4qZYPx5b3OGmA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id AEE2F811E67 for ; Fri, 11 Nov 2022 18:11:53 +0000 (UTC) Received: from abulafia.quesejoda.com (unknown [10.39.192.220]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 56F3940C845B; Fri, 11 Nov 2022 18:11:53 +0000 (UTC) Received: from abulafia.quesejoda.com (localhost [127.0.0.1]) by abulafia.quesejoda.com (8.17.1/8.17.1) with ESMTPS id 2ABIBopm278583 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 11 Nov 2022 19:11:51 +0100 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.17.1/8.17.1/Submit) id 2ABIBon2278582; Fri, 11 Nov 2022 19:11:50 +0100 To: Jakub Jelinek Cc: GCC patches , Andrew MacLeod , Aldy Hernandez Subject: [PATCH] [range-ops] Add ability to represent open intervals in frange. Date: Fri, 11 Nov 2022 19:11:47 +0100 Message-Id: <20221111181147.278546-1-aldyh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Aldy Hernandez via Gcc-patches From: Aldy Hernandez Reply-To: Aldy Hernandez Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749224379362108533?= X-GMAIL-MSGID: =?utf-8?q?1749224379362108533?= Currently we represent < and > with a closed interval. So < 3.0 is represented as [-INF, +3.0]. This means 3.0 is included in the range, and though not ideal, is conservatively correct. Jakub has found a couple cases where properly representing < and > would help optimizations and tests, and this patch allows representing open intervals with real_nextafter. There are a few caveats. First, we leave MODE_COMPOSITE_P types pessimistically as a closed interval. Second, for -ffinite-math-only, real_nextafter will will saturate the maximum representable number into +INF. However, this will still do the right thing, as frange::set() will crop things appropriately. Finally, and most frustratingly, representing < and > -+0.0 is problematic because we flush denormals to zero. Let me explain... real_nextafter(+0.0, +INF) gives 0x0.8p-148 as expected, but setting a range to this value yields [+0.0, 0x0.8p-148] because of the flushing. On the other hand, real_nextafter(+0.0, -INF) (surprisingly) gives -0.0.8p-148, but setting a range to that value yields [-0.0x8p-148, -0.0]. I say surprising, because according to cppreference.com, std::nextafter(+0.0, -INF) should give -0.0. But that's neither here nor there because our flushing denormals to zero prevents us from even representing ranges involving small values around 0.0. We ultimately end up with ranges looking like this: > +0.0 => [+0.0, INF] > -0.0 => [+0.0, INF] < +0.0 => [-INF, -0.0] < -0.0 => [-INF, -0.0] I suppose this is no worse off that what we had before with closed intervals. One could even argue that we're better because we at least have the right sign now ;-). All other (non-zero) values look sane. Lightly tested. Thoughts? gcc/ChangeLog: * range-op-float.cc (build_lt): Adjust with frange_nextafter instead of default to a closed range. (build_gt): Same. --- gcc/range-op-float.cc | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/gcc/range-op-float.cc b/gcc/range-op-float.cc index 380142b4c14..402393097b2 100644 --- a/gcc/range-op-float.cc +++ b/gcc/range-op-float.cc @@ -381,9 +381,17 @@ build_lt (frange &r, tree type, const frange &val) r.set_undefined (); return false; } - // We only support closed intervals. + REAL_VALUE_TYPE ninf = frange_val_min (type); - r.set (type, ninf, val.upper_bound ()); + REAL_VALUE_TYPE prev = val.upper_bound (); + machine_mode mode = TYPE_MODE (type); + // Default to the conservatively correct closed ranges for + // MODE_COMPOSITE_P, otherwise use nextafter. Note that for + // !HONOR_INFINITIES, nextafter will yield -INF, but frange::set() + // will crop the range appropriately. + if (!MODE_COMPOSITE_P (mode)) + frange_nextafter (mode, prev, ninf); + r.set (type, ninf, prev); return true; } @@ -424,9 +432,16 @@ build_gt (frange &r, tree type, const frange &val) return false; } - // We only support closed intervals. REAL_VALUE_TYPE inf = frange_val_max (type); - r.set (type, val.lower_bound (), inf); + REAL_VALUE_TYPE next = val.lower_bound (); + machine_mode mode = TYPE_MODE (type); + // Default to the conservatively correct closed ranges for + // MODE_COMPOSITE_P, otherwise use nextafter. Note that for + // !HONOR_INFINITIES, nextafter will yield +INF, but frange::set() + // will crop the range appropriately. + if (!MODE_COMPOSITE_P (mode)) + frange_nextafter (mode, next, inf); + r.set (type, next, inf); return true; }