From patchwork Mon Jun 19 15:57:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carl Love X-Patchwork-Id: 110084 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp3098700vqr; Mon, 19 Jun 2023 08:58:25 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6zp8lR4Zwnm3Pqo3WvaZAfEMf7FzeKzPOnOUX5Bjf60qiN14MlSF3YJvu+q4lGMrxbROap X-Received: by 2002:a17:907:72c4:b0:97e:56d5:b87e with SMTP id du4-20020a17090772c400b0097e56d5b87emr13706377ejc.2.1687190305244; Mon, 19 Jun 2023 08:58:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687190305; cv=none; d=google.com; s=arc-20160816; b=LYwrI4aTr/tEjxJNszcxv6Z8wB/TXM58nbt3En0dStdzoFAp8lPZzCFzHA+yxv40Uz wJ3c5SQrw0rJmxJ1VkEfEUKA9v9LE/G3CMGhyd24ZRyEOffWPjmF4FWRVIi45RqD8fI+ CBnvz7nXJNXdUCQL3JccZ1Tq05bVoCC7E4Bv1MoMO3i9hxUiTpZGGP67UljHbi6lmS5M nnYi5pFWTyP5/BG+Yk9Eqnjk6wJRNJ+XDQrYZCVNibz1TRdYaHAbLcxUE8e0gTC2/tNb 0tg+DjC7nk4GjaV3O3W5ChaL9js9AVhFQAhdawwyRsyoaJXWYXaesDWefURtZlCCzeMc g1+Q== 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:mime-version :content-transfer-encoding:date:cc:to:subject:message-id :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=ZII+KvWQzQ6FQVwf+SVS+9gf3B0eLL/hpgzzMWGyfqI=; b=I8Hc2yP1tMCvWUm309WJuVw2Dgg15BIcImPQzWLpO48BCWRYrGbp90s/eN2P8VYKEU ZahoALTKC8LBf42o4u7SdBIixvQRrnSl8Zrc2YKTSi6vKbIrieLxMJbZT0EKZU0zHx+I z1OgJPc/ZZ34ONqDti2PbzqiXypDtJgLVWPIiwSaIdINcehPJdwmXsxq28pJzjckYyiZ 5oxMU/yvx3xLnssl0H8/WyTlvY7SzM0Yksxx4ciyRF1ol8XoP/GVX2/437X7L0kdTzRj zoWK9Gqe7f+7xbgH56zO155CW8KYAGW5+6oANg/H4KTB/RaiXZeVjNjy7DlU15k8h4/7 2R3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Xzi50b9q; 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 a16-20020a1709063a5000b00974fb8ff39asi10128220ejf.580.2023.06.19.08.58.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Jun 2023 08:58:25 -0700 (PDT) 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=Xzi50b9q; 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 C0E60385802F for ; Mon, 19 Jun 2023 15:58:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C0E60385802F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1687190303; bh=ZII+KvWQzQ6FQVwf+SVS+9gf3B0eLL/hpgzzMWGyfqI=; h=Subject:To:Cc:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=Xzi50b9qWkEYEmm3EDWr9A0GMLufUF7SJhphinWYcN2FgsjlCi/AhE3FUxWdJEQrS Ymek0jOxDt7Wn1cnAIWQTyv4pk7aY3WdrRSBj6Dd3mc3UFB7fsga0WaLa/yHuRxdC5 0+WE/g8vhSVHaR8goTaII2gZR2+9ico92tMaUF74= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by sourceware.org (Postfix) with ESMTPS id 6FFB13858D1E for ; Mon, 19 Jun 2023 15:57:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6FFB13858D1E Received: from pps.filterd (m0353729.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 35JFjOhV009916; Mon, 19 Jun 2023 15:57:38 GMT Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3rash78tnp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jun 2023 15:57:37 +0000 Received: from m0353729.ppops.net (m0353729.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 35JFkGKE013148; Mon, 19 Jun 2023 15:57:37 GMT Received: from ppma05wdc.us.ibm.com (1b.90.2fa9.ip4.static.sl-reverse.com [169.47.144.27]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3rash78tn7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jun 2023 15:57:37 +0000 Received: from pps.filterd (ppma05wdc.us.ibm.com [127.0.0.1]) by ppma05wdc.us.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 35JCdgkn001284; Mon, 19 Jun 2023 15:57:36 GMT Received: from smtprelay04.wdc07v.mail.ibm.com ([9.208.129.114]) by ppma05wdc.us.ibm.com (PPS) with ESMTPS id 3r94f5u853-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jun 2023 15:57:36 +0000 Received: from smtpav04.wdc07v.mail.ibm.com (smtpav04.wdc07v.mail.ibm.com [10.39.53.231]) by smtprelay04.wdc07v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 35JFvYDp25428658 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 19 Jun 2023 15:57:34 GMT Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 35B4758054; Mon, 19 Jun 2023 15:57:34 +0000 (GMT) Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6B8FC58052; Mon, 19 Jun 2023 15:57:33 +0000 (GMT) Received: from li-e362e14c-2378-11b2-a85c-87d605f3c641.ibm.com (unknown [9.61.18.149]) by smtpav04.wdc07v.mail.ibm.com (Postfix) with ESMTP; Mon, 19 Jun 2023 15:57:33 +0000 (GMT) Message-ID: <42d27e659f56f16796c6bfab0799616bbdf6046a.camel@us.ibm.com> Subject: [PATCH] rs6000, __builtin_set_fpscr_rn add retrun value To: "Kewen.Lin" , Segher Boessenkool , gcc-patches@gcc.gnu.org, dje.gcc@gmail.com Cc: Peter Bergner , cel@us.ibm.com Date: Mon, 19 Jun 2023 08:57:32 -0700 X-Mailer: Evolution 3.28.5 (3.28.5-18.el8) X-TM-AS-GCONF: 00 X-Proofpoint-GUID: nB-nT_gnRfSQ-Hz7WvPE3NzAvksX8ahO X-Proofpoint-ORIG-GUID: p3pi50apmxnSD003EEHac70ii-8JFomB X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-06-19_11,2023-06-16_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 clxscore=1015 bulkscore=0 mlxscore=0 lowpriorityscore=0 adultscore=0 malwarescore=0 spamscore=0 mlxlogscore=999 priorityscore=1501 phishscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2305260000 definitions=main-2306190143 X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, 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-Patchwork-Original-From: Carl Love via Gcc-patches From: Carl Love Reply-To: Carl Love 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?1769147261495693071?= X-GMAIL-MSGID: =?utf-8?q?1769147261495693071?= GCC maintainers: The GLibC team requested a builtin to replace the mffscrn and mffscrniinline asm instructions in the GLibC code. Previously there was discussion on adding builtins for the mffscrn instructions. https://gcc.gnu.org/pipermail/gcc-patches/2023-May/620261.html In the end, it was felt that it would be to extend the existing __builtin_set_fpscr_rn builtin to return a double instead of a void type. The desire is that we could have the functionality of the mffscrn and mffscrni instructions on older ISAs. The two instructions were initially added in ISA 3.0. The __builtin_set_fpscr_rn has the needed functionality to set the RN field using the mffscrn and mffscrni instructions if ISA 3.0 is supported or fall back to using logical instructions to mask and set the bits for earlier ISAs. The instructions return the current value of the FPSCR fields DRN, VE, OE, UE, ZE, XE, NI, RN bit positions then update the RN bit positions with the new RN value provided. The current __builtin_set_fpscr_rn builtin has a return type of void. So, changing the return type to double and returning the FPSCR fields DRN, VE, OE, UE, ZE, XE, NI, RN bit positions would then give the functionally equivalent of the mffscrn and mffscrni instructions. Any current uses of the builtin would just ignore the return value yet any new uses could use the return value. So the requirement is for the change to the __builtin_set_fpscr_rn builtin to be backwardly compatible and work for all ISAs. The following patch changes the return type of the __builtin_set_fpscr_rn builtin from void to double. The return value is the current value of the various FPSCR fields DRN, VE, OE, UE, ZE, XE, NI, RN bit positions when the builtin is called. The builtin then updated the RN field with the new value provided as an argument to the builtin. The patch adds new testcases to test_fpscr_rn_builtin.c to check that the builtin returns the current value of the FPSCR fields and then updates the RN field. The GLibC team has reviewed the patch to make sure it met their needs as a drop in replacement for the inline asm mffscr and mffscrni statements in the GLibC code. T The patch has been tested on Power 8 LE/BE, Power 9 LE/BE and Power 10 LE. Please let me know if the patch is acceptable for mainline. Thanks. Carl -------------------------------- rs6000, __builtin_set_fpscr_rn add retrun value Change the return value from void to double. The return value consists of the FPSCR fields DRN, VE, OE, UE, ZE, XE, NI, RN bit positions. Add an overloaded version which accepts a double argument. The test powerpc/test_fpscr_rn_builtin.c is updated to add tests for the double reterun value and the new double argument. gcc/ChangeLog: * config/rs6000/rs6000-builtins.def (__builtin_set_fpscr_rn): Delete. (__builtin_set_fpscr_rn_i): New builtin definition. (__builtin_set_fpscr_rn_d): New builtin definition. * config/rs6000/rs6000-overload.def (__builtin_set_fpscr_rn): New overloaded definition. * config/rs6000/rs6000.md ((rs6000_get_fpscr_fields): New define_expand. (rs6000_update_fpscr_rn_field): New define_expand. (rs6000_set_fpscr_rn_d): New define expand. (rs6000_set_fpscr_rn_i): Renamed from rs6000_set_fpscr_rn, Added return argument. Updated to use new rs6000_get_fpscr_fields and rs6000_update_fpscr_rn_field define _expands. * doc/extend.texi (__builtin_set_fpscr_rn): Update description for the return value and new double argument. gcc/testsuite/ChangeLog: gcc.target/powerpc/test_fpscr_rn_builtin.c: Add new tests th check double return value. Add tests for overloaded double argument. re --- gcc/config/rs6000/rs6000-builtins.def | 7 +- gcc/config/rs6000/rs6000-overload.def | 6 + gcc/config/rs6000/rs6000.md | 122 ++++++++++++--- gcc/doc/extend.texi | 25 ++- .../powerpc/test_fpscr_rn_builtin.c | 143 +++++++++++++++++- 5 files changed, 262 insertions(+), 41 deletions(-) diff --git a/gcc/config/rs6000/rs6000-builtins.def b/gcc/config/rs6000/rs6000-builtins.def index 289a37998b1..30e0b0bb06d 100644 --- a/gcc/config/rs6000/rs6000-builtins.def +++ b/gcc/config/rs6000/rs6000-builtins.def @@ -237,8 +237,11 @@ const __ibm128 __builtin_pack_ibm128 (double, double); PACK_IF packif {ibm128} - void __builtin_set_fpscr_rn (const int[0,3]); - SET_FPSCR_RN rs6000_set_fpscr_rn {nosoft} + double __builtin_set_fpscr_rn_i (const int[0,3]); + SET_FPSCR_RN_I rs6000_set_fpscr_rn_i {nosoft} + + double __builtin_set_fpscr_rn_d (double); + SET_FPSCR_RN_D rs6000_set_fpscr_rn_d {nosoft} const double __builtin_unpack_ibm128 (__ibm128, const int<1>); UNPACK_IF unpackif {ibm128} diff --git a/gcc/config/rs6000/rs6000-overload.def b/gcc/config/rs6000/rs6000-overload.def index c582490c084..bb904b85820 100644 --- a/gcc/config/rs6000/rs6000-overload.def +++ b/gcc/config/rs6000/rs6000-overload.def @@ -195,6 +195,12 @@ unsigned long long __builtin_cmpb (unsigned long long, unsigned long long); CMPB +[SET_FPSCR_RN, set_fpscr_rn, __builtin_set_fpscr_rn] + double __builtin_set_fpscr_rn (int); + SET_FPSCR_RN_I + double __builtin_set_fpscr_rn (double); + SET_FPSCR_RN_D + [VEC_ABS, vec_abs, __builtin_vec_abs] vsc __builtin_vec_abs (vsc); ABS_V16QI diff --git a/gcc/config/rs6000/rs6000.md b/gcc/config/rs6000/rs6000.md index b0db8ae508d..5c53383c879 100644 --- a/gcc/config/rs6000/rs6000.md +++ b/gcc/config/rs6000/rs6000.md @@ -6440,8 +6440,84 @@ "mffscdrn %0,%1" [(set_attr "type" "fp")]) -(define_expand "rs6000_set_fpscr_rn" - [(match_operand:SI 0 "reg_or_cint_operand")] + +(define_expand "rs6000_get_fpscr_fields" + [(match_operand:DF 0 "gpc_reg_operand")] + "TARGET_HARD_FLOAT" +{ + /* Extract fields bits 29:31 (DRN) and bits 56:63 (VE, OE, UE, ZE, XE, NI, + RN) from the FPSCR and return them. */ + rtx tmp_df = gen_reg_rtx (DFmode); + rtx tmp_di = gen_reg_rtx (DImode); + + emit_insn (gen_rs6000_mffs (tmp_df)); + tmp_di = simplify_gen_subreg (DImode, tmp_df, DFmode, 0); + emit_insn (gen_anddi3 (tmp_di, tmp_di, GEN_INT (0x00000007000000FFULL))); + rtx tmp_rtn = simplify_gen_subreg (DFmode, tmp_di, DImode, 0); + emit_move_insn (operands[0], tmp_rtn); + DONE; +}) + +(define_expand "rs6000_update_fpscr_rn_field" + [(match_operand:DI 0 "gpc_reg_operand")] + "TARGET_HARD_FLOAT" +{ + /* Insert the new RN value from operands[0] into FPSCR bit [62:63]. */ + rtx tmp_di = gen_reg_rtx (DImode); + rtx tmp_df = gen_reg_rtx (DFmode); + + emit_insn (gen_rs6000_mffs (tmp_df)); + tmp_di = simplify_gen_subreg (DImode, tmp_df, DFmode, 0); + emit_insn (gen_anddi3 (tmp_di, tmp_di, GEN_INT (-4))); + emit_insn (gen_iordi3 (tmp_di, tmp_di, operands[0])); + + /* Need to write to field k=15. The fields are [0:15]. Hence with + L=0, W=0, FLM_i must be equal to 8, 16 = i + 8*(1-W). FLM is an + 8-bit field[0:7]. Need to set the bit that corresponds to the + value of i that you want [0:7]. */ + + tmp_df = simplify_gen_subreg (DFmode, tmp_di, DImode, 0); + emit_insn (gen_rs6000_mtfsf (GEN_INT (0x01), tmp_df)); + DONE; +}) + +(define_expand "rs6000_set_fpscr_rn_d" + [(set (match_operand:DF 0 "gpc_reg_operand") + (unspec_volatile:DF [(match_operand:DF 1 "gpc_reg_operand")] + UNSPECV_MFFSCDRN))] + "TARGET_HARD_FLOAT" +{ + rtx tmp_df = gen_reg_rtx (DFmode); + + if (TARGET_P9_MISC) + { + emit_insn (gen_rs6000_mffscrn (tmp_df, operands[1])); + emit_move_insn (operands[0], tmp_df); + DONE; + } + + /* Emulate the behavior of the mffscrni, mffscrn instructions for earlier + ISAs. Return bits 29:31 (DRN) and bits 56:63 (VE, OE, UE, ZE, XE, NI, + RN) from the FPSCR. Set the RN field based on the value in operands[1]. + */ + + /* Get the return value */ + emit_insn (gen_rs6000_get_fpscr_fields (operands[0])); + + /* Extract new RN mode from operand. */ + rtx op1 = simplify_gen_subreg (DImode, operands[1], DFmode, 0); + rtx tmp_rn = gen_reg_rtx (DImode); + emit_insn (gen_anddi3 (tmp_rn, op1, GEN_INT (3))); + + /* Insert new RN mode into FSCPR. */ + emit_insn (gen_rs6000_update_fpscr_rn_field (tmp_rn)); + DONE; +}) + +(define_expand "rs6000_set_fpscr_rn_i" + [(set (match_operand:DF 0 "gpc_reg_operand") + (unspec_volatile:DF [(match_operand:SI 1 "reg_or_cint_operand")] + UNSPECV_MFFSCDRN))] "TARGET_HARD_FLOAT" { rtx tmp_df = gen_reg_rtx (DFmode); @@ -6450,25 +6526,34 @@ new rounding mode bits from operands[0][62:63] into FPSCR[62:63]. */ if (TARGET_P9_MISC) { - if (const_0_to_3_operand (operands[0], VOIDmode)) - emit_insn (gen_rs6000_mffscrni (tmp_df, operands[0])); + if (const_0_to_3_operand (operands[1], VOIDmode)) + emit_insn (gen_rs6000_mffscrni (tmp_df, operands[1])); else { - rtx op0 = convert_to_mode (DImode, operands[0], false); - rtx src_df = simplify_gen_subreg (DFmode, op0, DImode, 0); + rtx op1 = convert_to_mode (DImode, operands[1], false); + rtx src_df = simplify_gen_subreg (DFmode, op1, DImode, 0); emit_insn (gen_rs6000_mffscrn (tmp_df, src_df)); } - DONE; + emit_move_insn (operands[0], tmp_df); + DONE; } - if (CONST_INT_P (operands[0])) + /* Emulate the behavior of the mffscrni, mffscrn instructions for earlier + ISAs. Return bits 29:31 (DRN) and bits 56:63 (VE, OE, UE, ZE, XE, NI, + RN) from the FPSCR. Set the RN field based on the value in operands[1]. + */ + + /* Get the current FPSCR fields to return. */ + emit_insn (gen_rs6000_get_fpscr_fields (operands[0])); + + if (CONST_INT_P (operands[1])) { - if ((INTVAL (operands[0]) & 0x1) == 0x1) + if ((INTVAL (operands[1]) & 0x1) == 0x1) emit_insn (gen_rs6000_mtfsb1 (GEN_INT (31))); else emit_insn (gen_rs6000_mtfsb0 (GEN_INT (31))); - if ((INTVAL (operands[0]) & 0x2) == 0x2) + if ((INTVAL (operands[1]) & 0x2) == 0x2) emit_insn (gen_rs6000_mtfsb1 (GEN_INT (30))); else emit_insn (gen_rs6000_mtfsb0 (GEN_INT (30))); @@ -6476,24 +6561,13 @@ else { rtx tmp_rn = gen_reg_rtx (DImode); - rtx tmp_di = gen_reg_rtx (DImode); /* Extract new RN mode from operand. */ - rtx op0 = convert_to_mode (DImode, operands[0], false); - emit_insn (gen_anddi3 (tmp_rn, op0, GEN_INT (3))); + rtx op1 = convert_to_mode (DImode, operands[1], false); + emit_insn (gen_anddi3 (tmp_rn, op1, GEN_INT (3))); /* Insert new RN mode into FSCPR. */ - emit_insn (gen_rs6000_mffs (tmp_df)); - tmp_di = simplify_gen_subreg (DImode, tmp_df, DFmode, 0); - emit_insn (gen_anddi3 (tmp_di, tmp_di, GEN_INT (-4))); - emit_insn (gen_iordi3 (tmp_di, tmp_di, tmp_rn)); - - /* Need to write to field k=15. The fields are [0:15]. Hence with - L=0, W=0, FLM_i must be equal to 8, 16 = i + 8*(1-W). FLM is an - 8-bit field[0:7]. Need to set the bit that corresponds to the - value of i that you want [0:7]. */ - tmp_df = simplify_gen_subreg (DFmode, tmp_di, DImode, 0); - emit_insn (gen_rs6000_mtfsf (GEN_INT (0x01), tmp_df)); + emit_insn (gen_rs6000_update_fpscr_rn_field (tmp_rn)); } DONE; }) diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index cdbd4b34a35..c6ad3a44d26 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -18188,7 +18188,6 @@ double __builtin_mffs (void); void __builtin_mtfsf (const int, double); void __builtin_mtfsb0 (const int); void __builtin_mtfsb1 (const int); -void __builtin_set_fpscr_rn (int); @end smallexample The @code{__builtin_ppc_get_timebase} and @code{__builtin_ppc_mftb} @@ -18209,13 +18208,23 @@ values to selected fields of the FPSCR. The as an argument. The valid bit range is between 0 and 31. The builtins map to the @code{mtfsb0} and @code{mtfsb1} instructions which take the argument and add 32. Hence these instructions only modify the FPSCR[32:63] bits by -changing the specified bit to a zero or one respectively. The -@code{__builtin_set_fpscr_rn} builtin allows changing both of the floating -point rounding mode bits. The argument is a 2-bit value. The argument can -either be a @code{const int} or stored in a variable. The builtin uses -the ISA 3.0 -instruction @code{mffscrn} if available, otherwise it reads the FPSCR, masks -the current rounding mode bits out and OR's in the new value. +changing the specified bit to a zero or one respectively. + +@smallexample +double __builtin_set_fpscr_rn (int); +double __builtin_set_fpscr_rn (double); +@end smallexample + +The @code{__builtin_set_fpscr_rn} builtin allows changing both of the floating +point rounding mode bits and returning the various FPSCR fields before the RN +field is updated. The builtin returns a double consisting of the initial value +of the FPSCR fields DRN, VE, OE, UE, ZE, XE, NI, and RN bit positions with all +other bits set to zero. The builtin argument is a 2-bit value for the new RN +field value. The argument can either be a @code{const int} or stored in a +variable. Additionally, the argument can be a variable of type double. The +builtin uses the ISA 3.0 instruction @code{mffscrn} if available, otherwise it +reads the FPSCR, masks the current rounding mode bits out and OR'sin the new +RN field value. @node Basic PowerPC Built-in Functions Available on ISA 2.05 @subsubsection Basic PowerPC Built-in Functions Available on ISA 2.05 diff --git a/gcc/testsuite/gcc.target/powerpc/test_fpscr_rn_builtin.c b/gcc/testsuite/gcc.target/powerpc/test_fpscr_rn_builtin.c index 04707ad8a56..1522983102d 100644 --- a/gcc/testsuite/gcc.target/powerpc/test_fpscr_rn_builtin.c +++ b/gcc/testsuite/gcc.target/powerpc/test_fpscr_rn_builtin.c @@ -6,11 +6,70 @@ #endif #define RN_MASK 0x3LL /* RN field mask */ +#define FIELD_MASK 0x00000007000000FFULL + +union blah { + double d; + unsigned long long ll; +} conv_val; void abort (void); -void __attribute__ ((noipa)) wrap_set_fpscr_rn (int val) +double __attribute__ ((noipa)) wrap_set_fpscr_rn (int val) { - __builtin_set_fpscr_rn (val); + return __builtin_set_fpscr_rn (val); +} + +double __attribute__ ((noipa)) wrap_set_fpscr_rn_d (double val) +{ + return __builtin_set_fpscr_rn (val); +} + +double __attribute__ ((noipa)) wrap_const_fpscr_rn (int val) +{ + switch (val) + { + case 0: return __builtin_set_fpscr_rn (0x0); + case 1: return __builtin_set_fpscr_rn (0x1); + case 2: return __builtin_set_fpscr_rn (0x2); + case 3: return __builtin_set_fpscr_rn (0x3); + } +} + +void check_builtin_set_fpscr_rn (unsigned long long initial_fpscr, + int new_RN, double result) +{ + register double f14; + unsigned long long masked_fpscr = initial_fpscr & FIELD_MASK; + + conv_val.d = result; + + /* Check the result. */ + if (conv_val.ll != masked_fpscr) + { +#ifdef DEBUG + printf("ERROR, __builtin_set_fpscr_rn(%d) did not return expected value %llx.\n", + new_RN, masked_fpscr); + printf("fpscr_val_initial = 0x%llx\n", initial_fpscr); + printf("result = 0x%llx\n", conv_val.ll); +#else + abort(); +#endif + } + + /* Check to see if the RN field was updated. */ + __asm __volatile ("mffs %0" : "=f"(f14)); + conv_val.d = f14; + + if ((conv_val.ll & RN_MASK) != new_RN) +#ifdef DEBUG + { + printf("ERROR, __builtin_set_fpscr_rn(%d) did not update RN to %llx.\n", + new_RN, new_RN); + printf(" conv_val.ll = 0x%llx\n", conv_val.ll); + } +#else + abort(); +#endif } int main () @@ -18,12 +77,10 @@ int main () int i; int val, bit; double fpscr_val; - union blah { - double d; - unsigned long long ll; - } conv_val; + unsigned long long fpscr_val_initial; unsigned long long ll_value; + union blah src_double; register double f14; /* __builtin_set_fpscr_rn() builtin can take a const or a variable @@ -190,4 +247,76 @@ int main () abort(); #endif } -} + + + /* Test return value from __builtin_set_fpscr_rn. The fields (DRN, VE, OE, + UE, ZE, XE, NI, RN) are returned and the RN field of FPSCR is updated + with the specified argument for the builtin. */ + + /* Check immediate argument cases */ + __asm __volatile ("mffs %0" : "=f"(f14)); + conv_val.d = f14; + fpscr_val_initial = conv_val.ll; + + val = 0x0; + fpscr_val = wrap_const_fpscr_rn (val); + check_builtin_set_fpscr_rn (fpscr_val_initial, val, fpscr_val); + + __asm __volatile ("mffs %0" : "=f"(f14)); + conv_val.d = f14; + fpscr_val_initial = conv_val.ll; + + val = 0x3; + fpscr_val = wrap_const_fpscr_rn (val); + check_builtin_set_fpscr_rn (fpscr_val_initial, val, fpscr_val); + + /* Check int argument cases */ + __asm __volatile ("mffs %0" : "=f"(f14)); + conv_val.d = f14; + fpscr_val_initial = conv_val.ll; + + val = 0x1; + fpscr_val = wrap_set_fpscr_rn (val); + check_builtin_set_fpscr_rn (fpscr_val_initial, val, fpscr_val); + + __asm __volatile ("mffs %0" : "=f"(f14)); + conv_val.d = f14; + fpscr_val_initial = conv_val.ll; + + val = 0x2; + fpscr_val = wrap_set_fpscr_rn (val); + check_builtin_set_fpscr_rn (fpscr_val_initial, val, fpscr_val); + + /* Check double argument cases */ + __asm __volatile ("mffs %0" : "=f"(f14)); + conv_val.d = f14; + fpscr_val_initial = conv_val.ll; + + conv_val.ll = 0x3; + fpscr_val = wrap_set_fpscr_rn_d (conv_val.d); + check_builtin_set_fpscr_rn (fpscr_val_initial, conv_val.ll, fpscr_val); + + __asm __volatile ("mffs %0" : "=f"(f14)); + conv_val.d = f14; + fpscr_val_initial = conv_val.ll; + + conv_val.ll = 0x0; + fpscr_val = wrap_set_fpscr_rn_d (conv_val.d); + check_builtin_set_fpscr_rn (fpscr_val_initial, conv_val.ll, fpscr_val); + + __asm __volatile ("mffs %0" : "=f"(f14)); + conv_val.d = f14; + fpscr_val_initial = conv_val.ll; + + conv_val.ll = 0x1; + fpscr_val = wrap_set_fpscr_rn_d (conv_val.d); + check_builtin_set_fpscr_rn (fpscr_val_initial, conv_val.ll, fpscr_val); + + __asm __volatile ("mffs %0" : "=f"(f14)); + conv_val.d = f14; + fpscr_val_initial = conv_val.ll; + + conv_val.ll = 0x2; + fpscr_val = wrap_set_fpscr_rn_d (conv_val.d); + check_builtin_set_fpscr_rn (fpscr_val_initial, conv_val.ll, fpscr_val); +}