Remove architecture specific sched_cpucount optimizations

And replace the generic algorithm with the Brian Kernighan's one.
GCC optimize it with popcnt if the architecture supports, so there
is no need to add the extra POPCNT define to enable it.

This is really a micro-optimization that only adds complexity:
recent ABIs already support it (x86-64-v2 or power64le) and it
simplifies the code for internal usage, since i686 does not allow an
internal iFUNC call.

Checked on x86_64-linux-gnu, aarch64-linux-gnu, and
powerpc64le-linux-gnu.
This commit is contained in:
Adhemerval Zanella 2021-03-24 18:56:34 -03:00
parent 69e0a5eb0d
commit db373e4c57
6 changed files with 14 additions and 130 deletions

View file

@ -17,36 +17,24 @@
#include <sched.h>
/* Counting bits set, Brian Kernighan's way.
Using a open-coded routine is slight better for architectures that
do not have a popcount instruction (compiler might emit a library
call). */
static inline int
countbits (__cpu_mask v)
{
int s = 0;
for (; v != 0; s++)
v &= v - 1;
return s;
}
int
__sched_cpucount (size_t setsize, const cpu_set_t *setp)
{
int s = 0;
const __cpu_mask *p = setp->__bits;
const __cpu_mask *end = &setp->__bits[setsize / sizeof (__cpu_mask)];
while (p < end)
{
__cpu_mask l = *p++;
#ifdef POPCNT
s += POPCNT (l);
#else
if (l == 0)
continue;
_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
}
for (int i = 0; i < setsize / sizeof (__cpu_mask); i++)
s += countbits (setp->__bits[i]);
return s;
}

View file

@ -1 +0,0 @@
#include <sysdeps/x86_64/multiarch/sched_cpucount.c>

View file

@ -1,20 +0,0 @@
/* Copyright (C) 2007-2021 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#define POPCNT(l) __builtin_popcountl (l)
#include <posix/sched_cpucount.c>

View file

@ -1,22 +0,0 @@
/* Copyright (C) 2007-2021 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#ifdef _ARCH_PWR5
# define POPCNT(l) __builtin_popcountl (l)
#endif
#include <posix/sched_cpucount.c>

View file

@ -1,36 +0,0 @@
/* Count bits in CPU set. x86-64 multi-arch version.
This file is part of the GNU C Library.
Copyright (C) 2008-2021 Free Software Foundation, Inc.
Contributed by Ulrich Drepper <drepper@redhat.com>.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <sched.h>
#include "init-arch.h"
#define __sched_cpucount static generic_cpucount
#include <posix/sched_cpucount.c>
#undef __sched_cpucount
#define POPCNT(l) \
({ __cpu_mask r; \
asm ("popcnt %1, %0" : "=r" (r) : "0" (l));\
r; })
#define __sched_cpucount static popcount_cpucount
#include <posix/sched_cpucount.c>
#undef __sched_cpucount
libc_ifunc (__sched_cpucount,
CPU_FEATURE_USABLE (POPCNT) ? popcount_cpucount : generic_cpucount);

View file

@ -1,25 +0,0 @@
/* Copyright (C) 2007-2021 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#ifdef __amdfam10
# define POPCNT(l) \
({ __cpu_mask r; \
asm ("popcntq %1, %0" : "=r" (r) : "0" (l)); \
r; })
#endif
#include <posix/sched_cpucount.c>