From patchwork Sat Jul 1 14:12:09 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 21383 Received: (qmail 58762 invoked by alias); 1 Jul 2017 14:12:15 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 58745 invoked by uid 89); 1 Jul 2017 14:12:14 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.4 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-pg0-f46.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=dBqXA/h/5qiIoGv+2DKJII7R/qemrrtC3Yc6jopUUOE=; b=h81R24irNKKTtXK5bB7xecajwBIDwnzH7mN7mwVpsBo1ei2xS/pBzJoLxjM8obrv5f qpFBIc4B8aeiAo3RfBwEoIgTt3yUCVKuedKtSisO+Mb/LblPGiG6ifaVAjKur3grzrHV UTCnKJpEb2QJdtzDrrPwQfZgOTR2/74m3/9lc63Ga9tyW6rSknB2n1YAkA6GQmEcSL0C E9kOjOXp0wRYOLhd63ESZ8ptYC4U2Cu9sUhR3+p2tzcdSfYxWXo9C1qbe5TBvxE7BgVV LW5ofiGj5HKTg1ZOHHG6UfiaSpscfAMeLFO9JI6MYROXGGm4ekOfeip3n1QaGHjxYjZT TXpw== X-Gm-Message-State: AIVw111ZZFl+YuTuikIHizxcu43huTWQLbGIJd8F4cVT5XmUff8pC4oH RuabhCrpRw0VLMGA X-Received: by 10.84.178.101 with SMTP id y92mr936334plb.116.1498918331228; Sat, 01 Jul 2017 07:12:11 -0700 (PDT) Date: Sat, 1 Jul 2017 07:12:09 -0700 From: "H.J. Lu" To: Florian Weimer Cc: GNU C Library Subject: [PATCH] Use __builtin_popcount in __sched_cpucount [BZ #21696] Message-ID: <20170701141209.GA18342@gmail.com> References: <20170701011435.GA24085@gmail.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.8.0 (2017-02-23) On Sat, Jul 01, 2017 at 08:33:19AM +0200, Florian Weimer wrote: > On 07/01/2017 03:14 AM, H.J. Lu wrote: > > posix/sched_cpucount.c assumes that size of __cpu_mask == size of long, > > which is incorrect for x32. This patch generates size of __cpu_mask and > > uses it in posix/sched_cpucount.c. > > > > Tested on i686, x86-64 and x32 with multi-arch disabled. > > > > OK for master? > > > > H.J. > > [BZ #21696] > > * posix/Makefile (gen-as-const-headers): New. > > * posix/cpu_mask.sym: New file. > > * posix/sched_cpucount.c: Include . > > (__sched_cpucount): Check CPU_MASK_SIZE instead of LONG_BIT. > > Replace the ul suffix with the ull suffix for 64-bit cpu_mask. > > Does one of the existing test cases fail reliably due to this? > Yes, posix/tst-cpuset and posix/tst-cpucount fail when multi-arch is disabled or the processor doesn't have popcnt. Otherwise, popcnt instruction is used. > > +gen-as-const-headers = cpu_mask.sym > > This is not needed if you use a regular #if and not a preprocessor > conditional. > > _Static_assert (sizeof (l) == sizeof (unsigned int) > || sizeof (l) == sizeof (unsigned long) > || sizeof (l) == sizeof (unsigned long long), > "sizeof (__cpu_mask")); > if (sizeof (__cpu_mask) == sizeof (unsigned int)) > s += __builtin_popcount (l); > else if (sizeof (__cpu_mask) == sizeof (unsigned long)) > s += __builtin_popcountl (l); > else > s += __builtin_popcountll (l); > > Untested, but __builtin_popcount… has fallback emulation GCC, so this > work. (And GCC 4.8 already has it.) > Thanks. It works. Here is the updated patch. Tested on i686, x86-64 and x32 with multi-arch disabled. OK for master? Thanks. H.J. ---- posix/sched_cpucount.c assumes that size of __cpu_mask == size of long, which is incorrect for x32. This patch uses __builtin_popcount, which is availabe in GCC 4.9, in posix/sched_cpucount.c. Tested on i686, x86-64 and x32 with multi-arch disabled. 2017-07-01 Florian Weimer H.J. Lu [BZ #21696] * posix/sched_cpucount.c: Don't include . (__sched_cpucount): Use __builtin_popcount. --- posix/sched_cpucount.c | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/posix/sched_cpucount.c b/posix/sched_cpucount.c index eaddf61..ab1ff49 100644 --- a/posix/sched_cpucount.c +++ b/posix/sched_cpucount.c @@ -15,7 +15,6 @@ License along with the GNU C Library; if not, see . */ -#include #include @@ -36,22 +35,16 @@ __sched_cpucount (size_t setsize, const cpu_set_t *setp) if (l == 0) continue; -# if LONG_BIT > 32 - l = (l & 0x5555555555555555ul) + ((l >> 1) & 0x5555555555555555ul); - l = (l & 0x3333333333333333ul) + ((l >> 2) & 0x3333333333333333ul); - l = (l & 0x0f0f0f0f0f0f0f0ful) + ((l >> 4) & 0x0f0f0f0f0f0f0f0ful); - l = (l & 0x00ff00ff00ff00fful) + ((l >> 8) & 0x00ff00ff00ff00fful); - l = (l & 0x0000ffff0000fffful) + ((l >> 16) & 0x0000ffff0000fffful); - l = (l & 0x00000000fffffffful) + ((l >> 32) & 0x00000000fffffffful); -# else - l = (l & 0x55555555ul) + ((l >> 1) & 0x55555555ul); - l = (l & 0x33333333ul) + ((l >> 2) & 0x33333333ul); - l = (l & 0x0f0f0f0ful) + ((l >> 4) & 0x0f0f0f0ful); - l = (l & 0x00ff00fful) + ((l >> 8) & 0x00ff00fful); - l = (l & 0x0000fffful) + ((l >> 16) & 0x0000fffful); -# endif - - s += l; + _Static_assert (sizeof (l) == sizeof (unsigned int) + || sizeof (l) == sizeof (unsigned long) + || sizeof (l) == sizeof (unsigned long long), + "sizeof (__cpu_mask"); + if (sizeof (__cpu_mask) == sizeof (unsigned int)) + s += __builtin_popcount (l); + else if (sizeof (__cpu_mask) == sizeof (unsigned long)) + s += __builtin_popcountl (l); + else + s += __builtin_popcountll (l); #endif }