From patchwork Wed Nov 9 07:07:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aldy Hernandez X-Patchwork-Id: 17362 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp189098wru; Tue, 8 Nov 2022 23:09:02 -0800 (PST) X-Google-Smtp-Source: AMsMyM6Ga67gC7jyiQDvXR8dwBgS1NetWh+yi5YgEwS+LNa+vYX3PMq2652G4MUun/VlWXN/UpeM X-Received: by 2002:a17:907:3e23:b0:7ad:baed:2a42 with SMTP id hp35-20020a1709073e2300b007adbaed2a42mr52248609ejc.346.1667977742776; Tue, 08 Nov 2022 23:09:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667977742; cv=none; d=google.com; s=arc-20160816; b=QneKf2Ij48K/t2nFu8KEB28OQIgg+eUZyEyVb8LJYCK+ErTgMCNyFD1qgdzsGPTsER vQ9SPzPdialEt79z/gHwc1zjCCbDXKZrhoxfExCMarIi6uAtzhohzgSGboZ7izTNnWUs MIDoHjVekZwO1cugiEktdJ1/waDm9/a+2BD3/K0GK8359UN4Jw/JKXO2Y7cWN5RAusMn hLRvfyx6gLsjgeLwOZcgdirOpT262qkjmFWaBu8sSi5UVxdYyzyIhCtcdri9u+WDpnQF 0o03JFccVOd6j5jX3T2BZ664AvVSC0ji2xOmOtcnevonzHzPsokcjm3wxcuWUY1xfrOY G21A== 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=NdkWNdek0G9nRUOVCjUPBIA1xJCl7iqnQh1z0yaHaZU=; b=P9xNMt2kOEAndJbizA1RqDm6YTePVTmSXG20ElkMFB7LXQ5duQ74HhNspzs/1RgaiX G7foIxFiVn0+grmbpI7iKWMc/FjI+IFpba11E/Vb46FlsRUMISokcs2htB/FHH35VnaJ lIW0HZ8tOmsB+TAACRQ1AHDENqg91Yi3pvRI6axj0sPzYXK1BD5SPDKmq5gh9Ypphsk4 stazoVbXkAWR0sjM3y1FsGNMn2v3clHtK709XRMwxBJD5N1/OGj33o0FvsM74EXKj5re /xkkje7QLNY8o0ui9HIy7RduzecxJSENDMY4HLDQy4S+tjL4YCk0y3W/vwdygPEB2EBm YI9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=yzIhbH0j; 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 r13-20020a05640251cd00b004631f24f2d1si17969949edd.610.2022.11.08.23.09.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Nov 2022 23:09:02 -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=yzIhbH0j; 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 09F6138460AD for ; Wed, 9 Nov 2022 07:08:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 09F6138460AD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667977733; bh=NdkWNdek0G9nRUOVCjUPBIA1xJCl7iqnQh1z0yaHaZU=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=yzIhbH0jByPY7PJG7Ip//BiaTtKOyI6iMwLZ8wQyWE/7icX/VgyH/WRysa6+RYTdg mV6vfooXbvBP2O9xUARdeJk4Sn3ySFWXFqn2GuI3j5Ts9MwVmjUNRVwmM+PSvPki2Z 9OQUb7ObmFCKuRFHOtYd/6DqvFcNv9tZANS9uneI= 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 0782B3858D1E for ; Wed, 9 Nov 2022 07:08:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0782B3858D1E 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-442-9XaR-c3yMT2pPfsmuSw9Fg-1; Wed, 09 Nov 2022 02:08:07 -0500 X-MC-Unique: 9XaR-c3yMT2pPfsmuSw9Fg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 85E4A101A54E for ; Wed, 9 Nov 2022 07:08:07 +0000 (UTC) Received: from abulafia.quesejoda.com (unknown [10.39.192.190]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3D1EC140EBF3; Wed, 9 Nov 2022 07:08:07 +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 2A9783Lw1030649 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 9 Nov 2022 08:08:03 +0100 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.17.1/8.17.1/Submit) id 2A9783Ik1030648; Wed, 9 Nov 2022 08:08:03 +0100 To: GCC patches Cc: Jakub Jelinek , Andrew MacLeod , Aldy Hernandez Subject: [COMMITTED] [range-op-float] Abstract out binary operator code out of PLUS_EXPR entry. Date: Wed, 9 Nov 2022 08:07:57 +0100 Message-Id: <20221109070758.1030615-1-aldyh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 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?1749001429443561184?= X-GMAIL-MSGID: =?utf-8?q?1749001429443561184?= The PLUS_EXPR was always meant to be a template for further development, since most of the binary operators will share a similar structure. This patch abstracts out the common bits into the default definition for range_operator_float::fold_range() and provides an rv_fold() to be implemented by the individual entries wishing to use the generic folder. This is akin to what we do with fold_range() and wi_fold() in the integer version of range-ops. gcc/ChangeLog: * range-op-float.cc (range_operator_float::fold_range): Abstract out from foperator_plus. (range_operator_float::rv_fold): New. (foperator_plus::fold_range): Remove. (foperator_plus::rv_fold): New. (propagate_nans): Remove. * range-op.h (class range_operator_float): Add rv_fold. --- gcc/range-op-float.cc | 156 +++++++++++++++++++++--------------------- gcc/range-op.h | 7 ++ 2 files changed, 84 insertions(+), 79 deletions(-) diff --git a/gcc/range-op-float.cc b/gcc/range-op-float.cc index 8282c912fc4..7075c25442a 100644 --- a/gcc/range-op-float.cc +++ b/gcc/range-op-float.cc @@ -49,13 +49,66 @@ along with GCC; see the file COPYING3. If not see // Default definitions for floating point operators. bool -range_operator_float::fold_range (frange &r ATTRIBUTE_UNUSED, - tree type ATTRIBUTE_UNUSED, - const frange &lh ATTRIBUTE_UNUSED, - const frange &rh ATTRIBUTE_UNUSED, +range_operator_float::fold_range (frange &r, tree type, + const frange &op1, const frange &op2, relation_trio) const { - return false; + if (empty_range_varying (r, type, op1, op2)) + return true; + if (op1.known_isnan () || op2.known_isnan ()) + { + r.set_nan (op1.type ()); + return true; + } + + REAL_VALUE_TYPE lb, ub; + bool maybe_nan; + rv_fold (lb, ub, maybe_nan, type, + op1.lower_bound (), op1.upper_bound (), + op2.lower_bound (), op2.upper_bound ()); + + // Handle possible NANs by saturating to the appropriate INF if only + // one end is a NAN. If both ends are a NAN, just return a NAN. + bool lb_nan = real_isnan (&lb); + bool ub_nan = real_isnan (&ub); + if (lb_nan && ub_nan) + { + r.set_nan (type); + return true; + } + if (lb_nan) + lb = dconstninf; + else if (ub_nan) + ub = dconstinf; + + r.set (type, lb, ub); + + if (lb_nan || ub_nan || maybe_nan) + // Keep the default NAN (with a varying sign) set by the setter. + ; + else if (!op1.maybe_isnan () && !op2.maybe_isnan ()) + r.clear_nan (); + + return true; +} + +// For a given operation, fold two sets of ranges into [lb, ub]. +// MAYBE_NAN is set to TRUE if, in addition to any result in LB or +// UB, the final range has the possiblity of a NAN. +void +range_operator_float::rv_fold (REAL_VALUE_TYPE &lb, + REAL_VALUE_TYPE &ub, + bool &maybe_nan, + tree type ATTRIBUTE_UNUSED, + const REAL_VALUE_TYPE &lh_lb ATTRIBUTE_UNUSED, + const REAL_VALUE_TYPE &lh_ub ATTRIBUTE_UNUSED, + const REAL_VALUE_TYPE &rh_lb ATTRIBUTE_UNUSED, + const REAL_VALUE_TYPE &rh_ub ATTRIBUTE_UNUSED) + const +{ + lb = dconstninf; + ub = dconstinf; + maybe_nan = true; } bool @@ -192,19 +245,6 @@ frelop_early_resolve (irange &r, tree type, && relop_early_resolve (r, type, op1, op2, rel, my_rel)); } -// If either operand is a NAN, set R to NAN and return TRUE. - -inline bool -propagate_nans (frange &r, const frange &op1, const frange &op2) -{ - if (op1.known_isnan () || op2.known_isnan ()) - { - r.set_nan (op1.type ()); - return true; - } - return false; -} - // Set VALUE to its next real value, or INF if the operation overflows. inline void @@ -1822,69 +1862,27 @@ foperator_unordered_equal::op1_range (frange &r, tree type, class foperator_plus : public range_operator_float { - using range_operator_float::fold_range; - -public: - bool fold_range (frange &r, tree type, - const frange &lh, - const frange &rh, - relation_trio = TRIO_VARYING) const final override; + void rv_fold (REAL_VALUE_TYPE &lb, REAL_VALUE_TYPE &ub, bool &maybe_nan, + tree type, + const REAL_VALUE_TYPE &lh_lb, + const REAL_VALUE_TYPE &lh_ub, + const REAL_VALUE_TYPE &rh_lb, + const REAL_VALUE_TYPE &rh_ub) const final override + { + frange_arithmetic (PLUS_EXPR, type, lb, lh_lb, rh_lb, dconstninf); + frange_arithmetic (PLUS_EXPR, type, ub, lh_ub, rh_ub, dconstinf); + + // [-INF] + [+INF] = NAN + if (real_isinf (&lh_lb, true) && real_isinf (&rh_ub, false)) + maybe_nan = true; + // [+INF] + [-INF] = NAN + else if (real_isinf (&lh_ub, false) && real_isinf (&rh_lb, true)) + maybe_nan = true; + else + maybe_nan = false; + } } fop_plus; -bool -foperator_plus::fold_range (frange &r, tree type, - const frange &op1, const frange &op2, - relation_trio) const -{ - if (empty_range_varying (r, type, op1, op2)) - return true; - if (propagate_nans (r, op1, op2)) - return true; - - REAL_VALUE_TYPE lb, ub; - frange_arithmetic (PLUS_EXPR, type, lb, - op1.lower_bound (), op2.lower_bound (), dconstninf); - frange_arithmetic (PLUS_EXPR, type, ub, - op1.upper_bound (), op2.upper_bound (), dconstinf); - - // Handle possible NANs by saturating to the appropriate INF if only - // one end is a NAN. If both ends are a NAN, just return a NAN. - bool lb_nan = real_isnan (&lb); - bool ub_nan = real_isnan (&ub); - if (lb_nan && ub_nan) - { - r.set_nan (type); - return true; - } - if (lb_nan) - lb = dconstninf; - else if (ub_nan) - ub = dconstinf; - - r.set (type, lb, ub); - - // Some combinations can yield a NAN even if no operands have the - // possibility of a NAN. - bool maybe_nan; - // [-INF] + [+INF] = NAN - if (real_isinf (&op1.lower_bound (), true) - && real_isinf (&op2.upper_bound (), false)) - maybe_nan = true; - // [+INF] + [-INF] = NAN - else if (real_isinf (&op1.upper_bound (), false) - && real_isinf (&op2.lower_bound (), true)) - maybe_nan = true; - else - maybe_nan = false; - - if (lb_nan || ub_nan || maybe_nan) - // Keep the default NAN (with a varying sign) set by the setter. - ; - else if (!op1.maybe_isnan () && !op2.maybe_isnan ()) - r.clear_nan (); - - return true; -} // Instantiate a range_op_table for floating point operations. static floating_op_table global_floating_table; diff --git a/gcc/range-op.h b/gcc/range-op.h index c7249890142..442a6e1d299 100644 --- a/gcc/range-op.h +++ b/gcc/range-op.h @@ -117,6 +117,13 @@ public: const frange &lh, const frange &rh, relation_trio = TRIO_VARYING) const; + virtual void rv_fold (REAL_VALUE_TYPE &lb, REAL_VALUE_TYPE &ub, + bool &maybe_nan, + tree type, + const REAL_VALUE_TYPE &lh_lb, + const REAL_VALUE_TYPE &lh_ub, + const REAL_VALUE_TYPE &rh_lb, + const REAL_VALUE_TYPE &rh_ub) const; // Unary operations have the range of the LHS as op2. virtual bool fold_range (irange &r, tree type, const frange &lh,