From patchwork Sat May 6 17:26:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 1778035 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=nextmovesoftware.com header.i=@nextmovesoftware.com header.a=rsa-sha256 header.s=default header.b=pSVQXEU9; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QDDxJ231vz213r for ; Sun, 7 May 2023 03:26:28 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E7A213857030 for ; Sat, 6 May 2023 17:26:25 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from server.nextmovesoftware.com (server.nextmovesoftware.com [162.254.253.69]) by sourceware.org (Postfix) with ESMTPS id C10263858D1E for ; Sat, 6 May 2023 17:26:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C10263858D1E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nextmovesoftware.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=nextmovesoftware.com; s=default; h=Content-Type:MIME-Version:Message-ID: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=aZQTtW0y5VpDdZmJCHRUHFtHMh5WBFHFMzCv3X4PXlc=; b=pSVQXEU9DEj+F0L0KrLIHUp3p+ wf7tYsX/9ZWgxHcookNHfLEMQhk39djO0w103sf8QGQFVhw+Z7wxv1JUlQ+OePzyPDsjInBZQku41 FNai0DNagoP55VUmL1xTLidhmFA+tCmMagtXcwUzGXeMX6NeEEcDks58kOF2PaVwlUi32gQ5yWlNK UyqoG7X77c5cWtE/nPetLNoIrQWU90O1TZOtk/em8gQ38xtvtbsWbJh3ZE3nbDf/lxSqSQK12c6FC NlnqkoJbJESXCmAPX1GtO7Z2a1tkwtc/mGICv+gY8EqF8mo4+B9e63HDQZMNKvi++ZuprBKWmwp8L VR/+Rosg==; Received: from host86-169-41-81.range86-169.btcentralplus.com ([86.169.41.81]:50163 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1pvLfh-0004tw-0T for gcc-patches@gcc.gnu.org; Sat, 06 May 2023 13:26:13 -0400 From: "Roger Sayle" To: "'GCC Patches'" Subject: [libgcc PATCH] Add bit reversal functions __bitrev[qhsd]i2. Date: Sat, 6 May 2023 18:26:11 +0100 Message-ID: <00c401d9803f$dafe3c90$90fab5b0$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdmAO2YVL782eB0dQCCYTLokdiovGw== Content-Language: en-gb X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - server.nextmovesoftware.com X-AntiAbuse: Original Domain - gcc.gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - nextmovesoftware.com X-Get-Message-Sender-Via: server.nextmovesoftware.com: authenticated_id: roger@nextmovesoftware.com X-Authenticated-Sender: server.nextmovesoftware.com: roger@nextmovesoftware.com X-Source: X-Source-Args: X-Source-Dir: X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, SPF_HELO_NONE, SPF_PASS, 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: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" This patch proposes adding run-time library support for bit reversal, by adding a __bitrevsi2 function to libgcc. Thoughts/opinions? I'm also tempted to add __popcount[qh]i2 and __parity[qh]i2 to libgcc, to allow the RTL optimizers to perform narrowing operations, but I'm curious to hear whether QImode and HImode support, though more efficient, is frowned by the libgcc maintainers/philosophy. This patch has been tested on x86_64-pc-linux-gnu with make bootstrap and make -k check, both with and without --target_board=unix{-m32} and on nvptx-none, with no new regressions. Ok for mainline? 2023-05-06 Roger Sayle gcc/ChangeLog * doc/libgcc.texi (__bitrevqi2): Document bit reversal run-time functions; __bitrevqi2, __bitrevhi2, __bitrevsi2 and __bitrevdi2. libgcc/ChangeLog * Makfile.in (lib2funcs): Add __bitrev[qhsd]i2. * libgcc-std.ver.in (GCC_14.0.0): Add __bitrev[qhsd]i2. * libgcc2.c (__bitrevqi2): New function. (__bitrevhi2): Likewise. (__bitrevsi2): Likewise. (__bitrevdi2): Likewise. * libgcc2.h (__bitrevqi2): Prototype here. (__bitrevhi2): Likewise. (__bitrevsi2): Likewise. (__bitrevdi2): Likewise. Thanks in advance, Roger diff --git a/gcc/doc/libgcc.texi b/gcc/doc/libgcc.texi index 73aa803..7611347 100644 --- a/gcc/doc/libgcc.texi +++ b/gcc/doc/libgcc.texi @@ -218,6 +218,13 @@ These functions return the number of bits set in @var{a}. These functions return the @var{a} byteswapped. @end deftypefn +@deftypefn {Runtime Function} int8_t __bitrevqi2 (int8_t @var{a}) +@deftypefnx {Runtime Function} int16_t __bitrevhi2 (int16_t @var{a}) +@deftypefnx {Runtime Function} int32_t __bitrevsi2 (int32_t @var{a}) +@deftypefnx {Runtime Function} int64_t __bitrevdi2 (int64_t @var{a}) +These functions return the bit reversed @var{a}. +@end deftypefn + @node Soft float library routines @section Routines for floating point emulation @cindex soft float library diff --git a/libgcc/Makefile.in b/libgcc/Makefile.in index 6c4dc79..67c54df 100644 --- a/libgcc/Makefile.in +++ b/libgcc/Makefile.in @@ -446,7 +446,7 @@ lib2funcs = _muldi3 _negdi2 _lshrdi3 _ashldi3 _ashrdi3 _cmpdi2 _ucmpdi2 \ _paritysi2 _paritydi2 _powisf2 _powidf2 _powixf2 _powitf2 \ _mulhc3 _mulsc3 _muldc3 _mulxc3 _multc3 _divhc3 _divsc3 \ _divdc3 _divxc3 _divtc3 _bswapsi2 _bswapdi2 _clrsbsi2 \ - _clrsbdi2 + _clrsbdi2 _bitrevqi2 _bitrevhi2 _bitrevsi2 _bitrevdi2 # The floating-point conversion routines that involve a single-word integer. # XX stands for the integer mode. diff --git a/libgcc/libgcc-std.ver.in b/libgcc/libgcc-std.ver.in index c4f87a5..2198b0e 100644 --- a/libgcc/libgcc-std.ver.in +++ b/libgcc/libgcc-std.ver.in @@ -1944,3 +1944,12 @@ GCC_7.0.0 { __PFX__divmoddi4 __PFX__divmodti4 } + +%inherit GCC_14.0.0 GCC_7.0.0 +GCC_14.0.0 { + # bit reversal functions + __PFX__bitrevqi2 + __PFX__bitrevhi2 + __PFX__bitrevsi2 + __PFX__bitrevdi2 +} diff --git a/libgcc/libgcc2.c b/libgcc/libgcc2.c index e0017d1..2bef2a1 100644 --- a/libgcc/libgcc2.c +++ b/libgcc/libgcc2.c @@ -488,6 +488,54 @@ __bswapdi2 (DItype u) | (((u) & 0x00000000000000ffull) << 56)); } #endif + +#ifdef L_bitrevqi2 +QItype +__bitrevqi2 (QItype x) +{ + UQItype u = x; + u = (((u) >> 1) & 0x55) | (((u) & 0x55) << 1); + u = (((u) >> 2) & 0x33) | (((u) & 0x33) << 2); + return ((u) >> 4) | ((u) << 4); +} +#endif +#ifdef L_bitrevhi2 +HItype +__bitrevhi2 (HItype x) +{ + UHItype u = x; + u = (((u) >> 1) & 0x5555) | (((u) & 0x5555) << 1); + u = (((u) >> 2) & 0x3333) | (((u) & 0x3333) << 2); + u = (((u) >> 4) & 0x0f0f) | (((u) & 0x0f0f) << 4); + return ((u) >> 8) | ((u) << 8); +} +#endif +#ifdef L_bitrevsi2 +SItype +__bitrevsi2 (SItype x) +{ + USItype u = x; + u = (((u) >> 1) & 0x55555555) | (((u) & 0x55555555) << 1); + u = (((u) >> 2) & 0x33333333) | (((u) & 0x33333333) << 2); + u = (((u) >> 4) & 0x0f0f0f0f) | (((u) & 0x0f0f0f0f) << 4); + return __bswapsi2 (u); +} +#endif +#ifdef L_bitrevdi2 +DItype +__bitrevdi2 (DItype x) +{ + UDItype u = x; + u = (((u) >> 1) & 0x5555555555555555ll) + | (((u) & 0x5555555555555555ll) << 1); + u = (((u) >> 2) & 0x3333333333333333ll) + | (((u) & 0x3333333333333333ll) << 2); + u = (((u) >> 4) & 0x0f0f0f0f0f0f0f0fll) + | (((u) & 0x0f0f0f0f0f0f0f0fll) << 4); + return __bswapdi2 (u); +} +#endif + #ifdef L_ffssi2 #undef int int diff --git a/libgcc/libgcc2.h b/libgcc/libgcc2.h index 3ec9bbd..e1abc0d 100644 --- a/libgcc/libgcc2.h +++ b/libgcc/libgcc2.h @@ -338,6 +338,10 @@ typedef int shift_count_type __attribute__((mode (__libgcc_shift_count__))); #define __udiv_w_sdiv __N(udiv_w_sdiv) #define __clear_cache __N(clear_cache) #define __enable_execute_stack __N(enable_execute_stack) +#define __bitrevqi2 __N(bitrevqi2) +#define __bitrevhi2 __N(bitrevhi2) +#define __bitrevsi2 __N(bitrevsi2) +#define __bitrevdi2 __N(bitrevdi2) #ifndef __powisf2 #define __powisf2 __N(powisf2) @@ -426,6 +430,15 @@ extern DWtype __subvDI3 (DWtype, DWtype); extern DWtype __mulvDI3 (DWtype, DWtype); extern DWtype __negvDI2 (DWtype); +extern QItype __bitrevqi2 (QItype); +extern HItype __bitrevhi2 (HItype); +#if MIN_UNITS_PER_WORD > 1 +extern SItype __bitrevsi2 (SItype); +#endif +#if __SIZEOF_LONG_LONG__ > 4 +extern DItype __bitrevdi2 (DItype); +#endif + #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC #define __absvsi2 __N(absvsi2) #define __negvsi2 __N(negvsi2)