From patchwork Wed Oct 12 14:23:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xi Ruoyao X-Patchwork-Id: 1958 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp2640692wrs; Wed, 12 Oct 2022 07:24:47 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6HpIsKpghzrKFh/LZpYJrN+oa+ClbWfwkKJfWwRnWv2qyQKnctrY2FIe7z4Facy7befl8+ X-Received: by 2002:a05:6402:1686:b0:459:4ddf:8f05 with SMTP id a6-20020a056402168600b004594ddf8f05mr27352303edv.375.1665584687045; Wed, 12 Oct 2022 07:24:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665584687; cv=none; d=google.com; s=arc-20160816; b=JwzW0R76mtOkN+Vb/YSIv6bj9fyzFyH5yaYWeHBpBWAe9nHYca70qcdTk2SM3+m26j INnQNKcoQuBSTrQXFHUBjT6fTZ/hrULcgNtWHtZmDzzTWlmv7E9zsUvVNjCWQ6ohUIGP AlDTuNipcUxCajmi4bQD8Q5oqck8O42P//HIQtwBIf4xaQ+3VNxs1yJcsSNLvE8vSERn ucHVODmhCEMWqQ79vG6uKBfbQj0Y98Q7GxM9v9Ut9+MxNAvUmnVtYKCcaAzDHM+9uUK0 hymFyIeOfNMvdScJXmveTuhcZWxSkYb9SNIn85yv3Nrb6Ud1yYAEoWJxet5ZpmzVFkIm 2LmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc: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:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=hVDUv+kiADhh2yEMtKlyxloaCj6ohrVeUG2owOHx18w=; b=0hp5DaaBBawBoOb/JsnO88kyHdx7styxSTiYFfpwaUZw0IMZJLOfDW+wtecorOVxSi HnlSCRwCG9/rCj28eekminLkKTb5EMgzrlDPoKXMcAlV7sSbg/V+yH+S8edsTNhd9Cj5 ECHOL06UjLh7YtnLOO/2INq1Sa7lc8/rG48opqVkh006vAlbjW95qSbygGRn+bfeNNwG Ll9WHXe0vy1SecbXfALlKGFhG2jK1dqDbWDgJMorxndrjODW29/4b69PucTsIYvqVyWn dFvcwWN10nYvufVAIj0Cy9xTUDGox9DtPXkAkLOTFgMCWnZ507bzG0ZUhr+rGLxWKlWb WDaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=wxT7cKoC; 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-20020a1709062b1000b0077d562462f5si12565067ejg.381.2022.10.12.07.24.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Oct 2022 07:24:47 -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=wxT7cKoC; 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 12E04381FE46 for ; Wed, 12 Oct 2022 14:24:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 12E04381FE46 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1665584685; bh=hVDUv+kiADhh2yEMtKlyxloaCj6ohrVeUG2owOHx18w=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=wxT7cKoC4UwBEhMjFw6Hbk6aXoGy285cIrAodwx4gDMbc90qWUnp4ze2hRpQq2lyG hlDoWVmYJ9FaAhxZ3/1jpFYn5qObPvk1nCEwleBl3p6nFEw/XUIPovuf7cy0+OAHKL HG83/Tl5GI0QTr0s9EUQa0XlVq9aadNMOdXfYugw= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from xry111.site (xry111.site [IPv6:2001:470:683e::1]) by sourceware.org (Postfix) with ESMTPS id AB1DB3850219 for ; Wed, 12 Oct 2022 14:24:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AB1DB3850219 Received: from xry111-x57s1.. (unknown [IPv6:240e:358:110f:4d00:dc73:854d:832e:2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id 7029666805; Wed, 12 Oct 2022 10:23:52 -0400 (EDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] LoongArch: implement count_{leading,trailing}_zeros Date: Wed, 12 Oct 2022 22:23:00 +0800 Message-Id: <20221012142300.16833-1-xry111@xry111.site> X-Mailer: git-send-email 2.38.0 MIME-Version: 1.0 X-Spam-Status: No, score=-7.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FROM_SUSPICIOUS_NTLD, FROM_SUSPICIOUS_NTLD_FP, GIT_PATCH_0, LIKELY_SPAM_FROM, SPF_HELO_PASS, SPF_PASS, TXREP, T_PDS_OTHER_BAD_TLD 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: Xi Ruoyao via Gcc-patches From: Xi Ruoyao Reply-To: Xi Ruoyao Cc: Chenghua Xu , Lulu Cheng , Wang Xuerui 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?1746492128541913876?= X-GMAIL-MSGID: =?utf-8?q?1746492128541913876?= LoongArch always support clz and ctz instructions, so we can always use __builtin_{clz,ctz} for count_{leading,trailing}_zeros. This improves the code of libgcc, and also benefits Glibc once we merge longlong.h there. Bootstrapped and regtested on loongarch64-linux-gnu. include/ChangeLog: * longlong.h [__loongarch__] (count_leading_zeros): Define. [__loongarch__] (count_trailing_zeros): Likewise. [__loongarch__] (COUNT_LEADING_ZEROS_0): Likewise. --- include/longlong.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/include/longlong.h b/include/longlong.h index 64a7b10f9b2..c3a6f1e7eaa 100644 --- a/include/longlong.h +++ b/include/longlong.h @@ -593,6 +593,18 @@ extern UDItype __umulsidi3 (USItype, USItype); #define UMUL_TIME 14 #endif +#ifdef __loongarch__ +# if W_TYPE_SIZE == 32 +# define count_leading_zeros(count, x) ((count) = __builtin_clz (x)) +# define count_trailing_zeros(count, x) ((count) = __builtin_ctz (x)) +# define COUNT_LEADING_ZEROS_0 32 +# elif W_TYPE_SIZE == 64 +# define count_leading_zeros(count, x) ((count) = __builtin_clzll (x)) +# define count_trailing_zeros(count, x) ((count) = __builtin_ctzll (x)) +# define COUNT_LEADING_ZEROS_0 64 +# endif +#endif + #if defined (__M32R__) && W_TYPE_SIZE == 32 #define add_ssaaaa(sh, sl, ah, al, bh, bl) \ /* The cmp clears the condition bit. */ \