From patchwork Thu Sep 19 13:12:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Malcomson X-Patchwork-Id: 97702 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 A87C8385B503 for ; Thu, 19 Sep 2024 13:13:48 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2061d.outbound.protection.outlook.com [IPv6:2a01:111:f403:2414::61d]) by sourceware.org (Postfix) with ESMTPS id C33493858C39 for ; Thu, 19 Sep 2024 13:12:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C33493858C39 Authentication-Results: sourceware.org; dmarc=fail (p=reject dis=none) header.from=nvidia.com Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=nvidia.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C33493858C39 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=2a01:111:f403:2414::61d ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1726751574; cv=pass; b=mLoebUprrA+VAyl3pS2KW+lGs4VkFWDNSirHq2gNH0I5gmRAKVtTmrlPW1Vf5tOyUnAfXhWwZppsG3Zthc1P9Y5ZClQ4N9DajRL5t+i5BYZW/xlt2S2bB6msZdj27BjiqCP8J+n6Om0vA9m9AdG5ZApOzWXW61sYGBri9pa74xg= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1726751574; c=relaxed/simple; bh=DOxFCdoQrzaujKKMw+a2VIaQ29X2UTH04Z6gj7sLDkQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=X/Lmki9tmhAD+ARZ0CMRmmLyx/w+t4i+bcP3hU8i+wXWoXFpw+UWMDPfP9c8g3jfvN9CK9L8zhLipwDBcJyG1re78MJky+xYL6d+I17NNN3MrsZa1WwrWfaP41zBcpTQwSIOvDXcFud7L9xxygTTqqKRTg0+unmJzOO9EgAk/TU= ARC-Authentication-Results: i=2; server2.sourceware.org ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=gbEPfoHrXXSS/LUwRMkcNhTQV5zfEixaFPFB04fMQoWGf/gxb1swwek0Jc7+WHj+z2QGtII3a4Up+XK11gzjTAaNSRcV0UHlT2Y1G3U6YltM9gc1QIpVSIiY8IQW6FiHNh4a55ckLzt7vEopvyr3xwAGNizYO9PuUiXGvbEen5wvoowc9NpCiiTMP2Myh0XH7i/kV+1yIKuX4g0mREFT37GlbSh/eJ1Kh8Ivh2l/mkluAkocPWWc5JyNF7+nUf+Ehd3XH1rC/j/kRl3sNFetoaI9r9oBdvqTSOqA3uk4osazdENHOD53lUym8ZcPCVLZ8NhZY7dxoK9WzIWhSar/AA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=NdzawkysR5ZsSLa++BHnN9Po8pTvAOLUg9Q+OZOQ/1E=; b=xL83fgoPzufM0Q6EA7OZNPxZN6G4lv9iZBqklV/xsidQgodRlaN357ACOUFcQ8bqFAI+sM/lHGLP91Vzulpss81xsrBJK6gGdaxBuyDUrNsHuS4bml0TrKwbf6x3Cx/6Y22kkHVXxxN0GR9l5oA6u8hQaAKDyh+y952vkkz+XACnDQKv8IppeadUgb63rfuCWq1y2hg/WaGrZx+JYrYhXVxy5gzKT4lp9+7FQspa51HhDj8XMQtKKkxHrVgnYWxp/dPJXQ6OC4/KTEO2GfUHIv9Lngm0aKu74SzgUmLh3/h9WibxHImvHmdJvmtNge9QZhb4L9ar0T3ikbwYP1RuAw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=gcc.gnu.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=NdzawkysR5ZsSLa++BHnN9Po8pTvAOLUg9Q+OZOQ/1E=; b=XnzrCHfas/bKokmX59Px7k/jrf3dFvET/1NQSkOsIRPuWsAkQZBxA50gpq4cP6+wwjVIg/i31NsQd7Do/MKIlLIkNNL7aQw2Ml3Ax9nwmSXihpui1HBSMxEz5R7lATmwWEz5of2KNuIPpOuzUHZ4jYRzykekQamMLtq5ZCea0SblVdu2N2H/EIjmZCwDhNpJsUfy9SHY7tE9wOfCFfOFKU9WGs3Gc/DSsiHHBf/WcZxfJkNhAgutBFJqNuf6wrkXaFFSI4raZ4ozDL7PN8iWEVsxE/H24s7NCRWnZRK4SNYQzmpmNOhv7mZ++CYBsgP4rYR/tdIqtbFSEB9UIU0J9w== Received: from SJ0PR05CA0054.namprd05.prod.outlook.com (2603:10b6:a03:33f::29) by SN7PR12MB8145.namprd12.prod.outlook.com (2603:10b6:806:350::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7982.22; Thu, 19 Sep 2024 13:12:45 +0000 Received: from CO1PEPF000066E7.namprd05.prod.outlook.com (2603:10b6:a03:33f:cafe::24) by SJ0PR05CA0054.outlook.office365.com (2603:10b6:a03:33f::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7939.24 via Frontend Transport; Thu, 19 Sep 2024 13:12:45 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by CO1PEPF000066E7.mail.protection.outlook.com (10.167.249.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.13 via Frontend Transport; Thu, 19 Sep 2024 13:12:45 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Thu, 19 Sep 2024 06:12:31 -0700 Received: from f2784bb-lcelt.nvidia.com (10.126.230.35) by rnnvmail201.nvidia.com (10.129.68.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Thu, 19 Sep 2024 06:12:29 -0700 From: To: CC: Jonathan Wakely , Joseph Myers , Richard Biener , Matthew Malcomson Subject: [PATCH 5/8] [RFC] Use new builtins in libstdc++ Date: Thu, 19 Sep 2024 14:12:01 +0100 Message-ID: <20240919131204.3865854-6-mmalcomson@nvidia.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240919131204.3865854-1-mmalcomson@nvidia.com> References: <20240919131204.3865854-1-mmalcomson@nvidia.com> MIME-Version: 1.0 X-Originating-IP: [10.126.230.35] X-ClientProxiedBy: rnnvmail202.nvidia.com (10.129.68.7) To rnnvmail201.nvidia.com (10.129.68.8) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PEPF000066E7:EE_|SN7PR12MB8145:EE_ X-MS-Office365-Filtering-Correlation-Id: 8e37688c-e74d-4930-fd9d-08dcd8acc09a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|82310400026|36860700013|1800799024|376014; X-Microsoft-Antispam-Message-Info: CUQi2c841sBi0rcXIRlXB2IEoq61CeHVzPVi5bes5knqyXVzDJyezRNshCseCwz/5pboZ4Je+QY4K3HuzVlQeAS4wSFMXo6nth1GYU3Oau4jMSs26RpnJ85Xf9zyEYkUMifLWRZ0ZagAW1q2qzByORmaUgjOKJeDKVCAwTW2HFyPY4zvtwQQkNJ4r4/FsSL5lbeBa6lQt33WnXXBXgHC9lzfgHdlKyyYJGwm8wV8F6FTUTB/xDktxx4T/cPxqfz1/14RG8kvFXx/yQR1MGgr50aHwQkgUoRf6R5e+EGSwkv03ArneAjD1ZElgzP4jV6Nc8sybsAequfgXzLb3sOWTPAhwtOS3Yq7iyfnXpFt0/CsAYtrW3xYsmtpQ+E9Cn0AGLy8hSyibhsPIVENd2bp5hj8JxftbKZNvu4zdrS3RV1SBCQfeYW9ViIUmaCNcSmYAyuyC3XBL0UyrsPL+lmFXFULxTa4cmn999bXLCggyyXCIjId+Nne8qxFmob6ylPNB8hbO/pSMW0TLdcn/Xmz2/5gonqZTkOwG5TN/ew0L1TnjeIJM5YDdtL/vj/QwwoesIEWSJsmvcIWF3DjILQxO7lgZhJ7NI/Lh/bn4UN4nKvD/In//2ZI3EvKF1zSWsxOcuICDSWR19Q1waLirmmcvxCsTqNI0kmSJ2hf2qSElRSikGuv0dD3QjUBcbxP8s0eSj3Kw5GCxD0hWM1WTGzDkzZMiHnv/NjhNz4zPp+Zs7xuPxRvXDHr9bCEiwl8q1yEzideEH5CTSfcmAko4c4pt8JvkXn1KoOx5XWP/nEZHQulm3ounwVtr8SzJHuJDfioICkepn63MrbmBcrgxcDWzkGfdIegMeXFuIkhsYA0IeMU/TpzuillTyFoVc5DBdko9x0vTAAy9XILLo63r4PV0Wym4QMhnV6DZVv6WuXwc2t2VmF24Kwl7EIzvpmnea88ZELnrQeIlT/kzih5TA+DbeCSlr0ybwWkwm4eqYfHM/P+/yPZXVLa5b3TVPlkYJc4wJ87SMduXBPyCmoSrbnfD6/Cy+QCk7Lv7gr8U6GARoMp1Nl/dzHCq904P/xoC8HF0yg5z8ZZ80LB6gp1l41SJ6N6bBkNkdCUxhb/+i0nlpP3tUWxfXkJCopy3/63ymfC/ZAsjhRx27uzAldU9Ya5EjLGbD3zJnA7ya2jv4oulOzIoQk7bhJoQJYeTSXZwmpK9NdvdFahDO9C4TeX1+QKV0HpgOUxWdLJAbF7Igu1fvHHIJnYO8fuOkTOrX7mjJn6eOIdtWQljTvryLFmkz+A7OeDGomF7Y8aRocFNWGecwg2CUD4Xd6yJjpYJx+KUb8RQBL5SyYNhV8N8xkFFE3r6OxAxFascwK6D8jeeJudiIg= X-Forefront-Antispam-Report: CIP:216.228.117.160; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge1.nvidia.com; CAT:NONE; SFS:(13230040)(82310400026)(36860700013)(1800799024)(376014); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Sep 2024 13:12:45.6187 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 8e37688c-e74d-4930-fd9d-08dcd8acc09a X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.117.160]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1PEPF000066E7.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB8145 X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FORGED_SPF_HELO, GIT_PATCH_0, SPF_HELO_PASS, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org From: Matthew Malcomson Points to question here are: 1) Whether checking for this particular internal builtin is OK (this one happens to be the one implementing the operation for a `double`, we would have to rely on the approach that if anyone implements this operation for a `double` they implement it for all the floating point types that their C++ frontend and libstdc++ handle). 2) Whether the `#if` bit should be somewhere else instead of put in the `__fetch_add_flt` function. I put it there because that's where it seemed natural, but am not familiar enough with libstdc++ to be confident in that decision. We still need the CAS loop fallback for any compiler that doesn't implement this builtin, and hence will still need some extra choice to be made for floating point types. Once all compilers we care about implement this we can remove this special handling and merge the floating point and integral operations into the same template. Signed-off-by: Matthew Malcomson --- libstdc++-v3/include/bits/atomic_base.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/libstdc++-v3/include/bits/atomic_base.h b/libstdc++-v3/include/bits/atomic_base.h index 1c2367b39b6..d3b1a022db2 100644 --- a/libstdc++-v3/include/bits/atomic_base.h +++ b/libstdc++-v3/include/bits/atomic_base.h @@ -1217,30 +1217,41 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Tp __fetch_add_flt(_Tp* __ptr, _Val<_Tp> __i, memory_order __m) noexcept { +#if __has_builtin(__atomic_fetch_add_fp) + return __atomic_fetch_add(__ptr, __i, int(__m)); +#else _Val<_Tp> __oldval = load(__ptr, memory_order_relaxed); _Val<_Tp> __newval = __oldval + __i; while (!compare_exchange_weak(__ptr, __oldval, __newval, __m, memory_order_relaxed)) __newval = __oldval + __i; return __oldval; +#endif } template _Tp __fetch_sub_flt(_Tp* __ptr, _Val<_Tp> __i, memory_order __m) noexcept { +#if __has_builtin(__atomic_fetch_sub) + return __atomic_fetch_sub(__ptr, __i, int(__m)); +#else _Val<_Tp> __oldval = load(__ptr, memory_order_relaxed); _Val<_Tp> __newval = __oldval - __i; while (!compare_exchange_weak(__ptr, __oldval, __newval, __m, memory_order_relaxed)) __newval = __oldval - __i; return __oldval; +#endif } template _Tp __add_fetch_flt(_Tp* __ptr, _Val<_Tp> __i) noexcept { +#if __has_builtin(__atomic_add_fetch) + return __atomic_add_fetch(__ptr, __i, __ATOMIC_SEQ_CST); +#else _Val<_Tp> __oldval = load(__ptr, memory_order_relaxed); _Val<_Tp> __newval = __oldval + __i; while (!compare_exchange_weak(__ptr, __oldval, __newval, @@ -1248,12 +1259,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION memory_order_relaxed)) __newval = __oldval + __i; return __newval; +#endif } template _Tp __sub_fetch_flt(_Tp* __ptr, _Val<_Tp> __i) noexcept { +#if __has_builtin(__atomic_sub_fetch) + return __atomic_sub_fetch(__ptr, __i, __ATOMIC_SEQ_CST); +#else _Val<_Tp> __oldval = load(__ptr, memory_order_relaxed); _Val<_Tp> __newval = __oldval - __i; while (!compare_exchange_weak(__ptr, __oldval, __newval, @@ -1261,6 +1276,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION memory_order_relaxed)) __newval = __oldval - __i; return __newval; +#endif } } // namespace __atomic_impl