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: 60445 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id ADB54385800A for ; Fri, 11 Nov 2022 18:12:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ADB54385800A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668190346; 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=D8QAOE8XfRBCLRfed8J3zzV65Qxef3oQSAEoaUG9m+1ZAAvUQpTwIrzaWTCpLM4mp 4kEC8m+OZoISySYrZdZajUMNhe7ozMTCtTFpCdSBcoVbVlZPrF0FMWNbD2eJT8z09A AnTKXA4iv/hYvRViTZnBERmd9VQ7Z1z++1TpmvEM= 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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; }