From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 A50133857C49 for ; Mon, 15 Aug 2022 10:20:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A50133857C49 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-262-OfC_z76pPkKeqyMKlICq6Q-1; Mon, 15 Aug 2022 06:20:49 -0400 X-MC-Unique: OfC_z76pPkKeqyMKlICq6Q-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2AE46A0CA66; Mon, 15 Aug 2022 10:20:49 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.41]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DFFDE492CA2; Mon, 15 Aug 2022 10:20:48 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 27FAKkAc1240910 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 15 Aug 2022 12:20:46 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 27FAKj9B1240909; Mon, 15 Aug 2022 12:20:45 +0200 Date: Mon, 15 Aug 2022 12:20:45 +0200 From: Jakub Jelinek To: FX Cc: gcc-patches@gcc.gnu.org, fortran@gcc.gnu.org Subject: [PATCH] fortran: Expand ieee_arithmetic module's ieee_value inline [PR106579] Message-ID: Reply-To: Jakub Jelinek MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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-List-Received-Date: Mon, 15 Aug 2022 10:20:54 -0000 Hi! The following patch expands IEEE_VALUE function inline in the FE. Bootstrapped/regtested on x86_64-linux, i686-linux, powerpc64le-linux and powerpc64-linux, ok for trunk? 2022-08-15 Jakub Jelinek PR fortran/106579 * trans-intrinsic.cc: Include realmpfr.h. (conv_intrinsic_ieee_value): New function. (gfc_conv_ieee_arithmetic_function): Handle ieee_value. --- gcc/fortran/trans-intrinsic.cc.jj 2022-08-12 18:51:28.095927643 +0200 +++ gcc/fortran/trans-intrinsic.cc 2022-08-13 13:24:37.446768877 +0200 @@ -41,6 +41,7 @@ along with GCC; see the file COPYING3. #include "trans-array.h" #include "dependency.h" /* For CAF array alias analysis. */ #include "attribs.h" +#include "realmpfr.h" /* Only for gfc_trans_assign and gfc_trans_pointer_assign. */ @@ -10085,6 +10086,115 @@ conv_intrinsic_ieee_class (gfc_se *se, g } +/* Generate code for IEEE_VALUE. */ + +static void +conv_intrinsic_ieee_value (gfc_se *se, gfc_expr *expr) +{ + tree args[2], arg, ret, tmp; + stmtblock_t body; + + /* Convert args, evaluate the second one only once. */ + conv_ieee_function_args (se, expr, args, 2); + arg = gfc_evaluate_now (args[1], &se->pre); + + tree type = TREE_TYPE (arg); + gcc_assert (TREE_CODE (type) == RECORD_TYPE); + tree field = NULL_TREE; + for (tree f = TYPE_FIELDS (type); f != NULL_TREE; f = DECL_CHAIN (f)) + if (TREE_CODE (f) == FIELD_DECL) + { + gcc_assert (field == NULL_TREE); + field = f; + } + gcc_assert (field); + arg = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field), + arg, field, NULL_TREE); + arg = gfc_evaluate_now (arg, &se->pre); + + type = gfc_typenode_for_spec (&expr->ts); + gcc_assert (TREE_CODE (type) == REAL_TYPE); + ret = gfc_create_var (type, NULL); + + gfc_init_block (&body); + + tree end_label = gfc_build_label_decl (NULL_TREE); + for (int c = IEEE_SIGNALING_NAN; c <= IEEE_POSITIVE_INF; ++c) + { + tree label = gfc_build_label_decl (NULL_TREE); + tree low = build_int_cst (TREE_TYPE (arg), c); + tmp = build_case_label (low, low, label); + gfc_add_expr_to_block (&body, tmp); + + REAL_VALUE_TYPE real; + int k; + switch (c) + { + case IEEE_SIGNALING_NAN: + real_nan (&real, "", 0, TYPE_MODE (type)); + break; + case IEEE_QUIET_NAN: + real_nan (&real, "", 1, TYPE_MODE (type)); + break; + case IEEE_NEGATIVE_INF: + real_inf (&real); + real = real_value_negate (&real); + break; + case IEEE_NEGATIVE_NORMAL: + real_from_integer (&real, TYPE_MODE (type), -42, SIGNED); + break; + case IEEE_NEGATIVE_DENORMAL: + k = gfc_validate_kind (BT_REAL, expr->ts.kind, false); + real_from_mpfr (&real, gfc_real_kinds[k].tiny, + type, GFC_RND_MODE); + real_arithmetic (&real, RDIV_EXPR, &real, &dconst2); + real = real_value_negate (&real); + break; + case IEEE_NEGATIVE_ZERO: + real_from_integer (&real, TYPE_MODE (type), 0, SIGNED); + real = real_value_negate (&real); + break; + case IEEE_POSITIVE_ZERO: + /* Make this also the default: label. */ + label = gfc_build_label_decl (NULL_TREE); + tmp = build_case_label (NULL_TREE, NULL_TREE, label); + gfc_add_expr_to_block (&body, tmp); + real_from_integer (&real, TYPE_MODE (type), 0, SIGNED); + break; + case IEEE_POSITIVE_DENORMAL: + k = gfc_validate_kind (BT_REAL, expr->ts.kind, false); + real_from_mpfr (&real, gfc_real_kinds[k].tiny, + type, GFC_RND_MODE); + real_arithmetic (&real, RDIV_EXPR, &real, &dconst2); + break; + case IEEE_POSITIVE_NORMAL: + real_from_integer (&real, TYPE_MODE (type), 42, SIGNED); + break; + case IEEE_POSITIVE_INF: + real_inf (&real); + break; + default: + gcc_unreachable (); + } + + tree val = build_real (type, real); + gfc_add_modify (&body, ret, val); + + tmp = build1_v (GOTO_EXPR, end_label); + gfc_add_expr_to_block (&body, tmp); + } + + tmp = gfc_finish_block (&body); + tmp = fold_build2_loc (input_location, SWITCH_EXPR, NULL_TREE, arg, tmp); + gfc_add_expr_to_block (&se->pre, tmp); + + tmp = build1_v (LABEL_EXPR, end_label); + gfc_add_expr_to_block (&se->pre, tmp); + + se->expr = ret; +} + + /* Generate code for an intrinsic function from the IEEE_ARITHMETIC module. */ @@ -10117,6 +10227,8 @@ gfc_conv_ieee_arithmetic_function (gfc_s conv_intrinsic_ieee_logb_rint (se, expr, BUILT_IN_RINT); else if (startswith (name, "ieee_class_") && ISDIGIT (name[11])) conv_intrinsic_ieee_class (se, expr); + else if (startswith (name, "ieee_value_") && ISDIGIT (name[11])) + conv_intrinsic_ieee_value (se, expr); else /* It is not among the functions we translate directly. We return false, so a library function call is emitted. */ Jakub