public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 1/4] Remove architecture specific sched_cpucount optimizations
@ 2021-03-29 18:25 Adhemerval Zanella
  2021-03-29 18:25 ` [PATCH 2/4] linux: Use sched_getaffinity for __get_nprocs (BZ #27645) Adhemerval Zanella
                   ` (4 more replies)
  0 siblings, 5 replies; 28+ messages in thread
From: Adhemerval Zanella @ 2021-03-29 18:25 UTC (permalink / raw)
  To: libc-alpha; +Cc: crrodriguez

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.
---
 posix/sched_cpucount.c                       | 28 +++------------
 sysdeps/i386/i686/multiarch/sched_cpucount.c |  1 -
 sysdeps/ia64/sched_cpucount.c                | 20 -----------
 sysdeps/powerpc/sched_cpucount.c             | 22 ------------
 sysdeps/x86_64/multiarch/sched_cpucount.c    | 36 --------------------
 sysdeps/x86_64/sched_cpucount.c              | 25 --------------
 6 files changed, 4 insertions(+), 128 deletions(-)
 delete mode 100644 sysdeps/i386/i686/multiarch/sched_cpucount.c
 delete mode 100644 sysdeps/ia64/sched_cpucount.c
 delete mode 100644 sysdeps/powerpc/sched_cpucount.c
 delete mode 100644 sysdeps/x86_64/multiarch/sched_cpucount.c
 delete mode 100644 sysdeps/x86_64/sched_cpucount.c

diff --git a/posix/sched_cpucount.c b/posix/sched_cpucount.c
index b0ca4ea7bc..529286e777 100644
--- a/posix/sched_cpucount.c
+++ b/posix/sched_cpucount.c
@@ -22,31 +22,11 @@ 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)
+  for (int i = 0; i < setsize / sizeof (__cpu_mask); i++)
     {
-      __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
+      __cpu_mask si = setp->__bits[i];
+      /* Clear the least significant bit set.  */
+      for (; si != 0; si &= si - 1, s++);
     }
-
   return s;
 }
diff --git a/sysdeps/i386/i686/multiarch/sched_cpucount.c b/sysdeps/i386/i686/multiarch/sched_cpucount.c
deleted file mode 100644
index 7db31b02f8..0000000000
--- a/sysdeps/i386/i686/multiarch/sched_cpucount.c
+++ /dev/null
@@ -1 +0,0 @@
-#include <sysdeps/x86_64/multiarch/sched_cpucount.c>
diff --git a/sysdeps/ia64/sched_cpucount.c b/sysdeps/ia64/sched_cpucount.c
deleted file mode 100644
index 8440864b02..0000000000
--- a/sysdeps/ia64/sched_cpucount.c
+++ /dev/null
@@ -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>
diff --git a/sysdeps/powerpc/sched_cpucount.c b/sysdeps/powerpc/sched_cpucount.c
deleted file mode 100644
index 8f00e3dbc8..0000000000
--- a/sysdeps/powerpc/sched_cpucount.c
+++ /dev/null
@@ -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>
diff --git a/sysdeps/x86_64/multiarch/sched_cpucount.c b/sysdeps/x86_64/multiarch/sched_cpucount.c
deleted file mode 100644
index 5180a11434..0000000000
--- a/sysdeps/x86_64/multiarch/sched_cpucount.c
+++ /dev/null
@@ -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);
diff --git a/sysdeps/x86_64/sched_cpucount.c b/sysdeps/x86_64/sched_cpucount.c
deleted file mode 100644
index 5a27336d6d..0000000000
--- a/sysdeps/x86_64/sched_cpucount.c
+++ /dev/null
@@ -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>
-- 
2.27.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

end of thread, other threads:[~2021-05-07 12:43 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-29 18:25 [PATCH 1/4] Remove architecture specific sched_cpucount optimizations Adhemerval Zanella
2021-03-29 18:25 ` [PATCH 2/4] linux: Use sched_getaffinity for __get_nprocs (BZ #27645) Adhemerval Zanella
2021-04-27 15:38   ` Florian Weimer
2021-03-29 18:25 ` [PATCH 3/4] linux: Use /sys/devices/system/cpu/possible on __get_nprocs_conf Adhemerval Zanella
2021-05-05 16:53   ` Adhemerval Zanella
2021-05-05 17:54   ` Florian Weimer
2021-05-05 18:06     ` Florian Weimer
2021-05-06 13:03       ` Adhemerval Zanella
2021-05-06 13:51         ` Florian Weimer
2021-05-06 20:07           ` Adhemerval Zanella
2021-05-07 11:07             ` Florian Weimer
2021-05-07 12:43               ` Adhemerval Zanella
2021-05-06 13:17     ` Adhemerval Zanella
2021-03-29 18:25 ` [PATCH 4/4] linux: Remove /proc/cpuinfo fallback on alpha and sparc Adhemerval Zanella
2021-05-05 16:53   ` Adhemerval Zanella
2021-05-05 16:52 ` [PATCH 1/4] Remove architecture specific sched_cpucount optimizations Adhemerval Zanella
2021-05-05 17:28 ` Florian Weimer
2021-05-05 18:25   ` Paul Eggert
2021-05-05 19:52     ` Noah Goldstein
2021-05-06 12:22       ` Adhemerval Zanella
2021-05-06 18:34         ` Noah Goldstein
2021-05-06 13:33     ` Adhemerval Zanella
2021-05-06 13:43       ` Florian Weimer
2021-05-06 16:16         ` Adhemerval Zanella
2021-05-06 16:42           ` Florian Weimer
2021-05-06 16:54             ` Adhemerval Zanella
2021-05-06 17:12       ` Paul Eggert
2021-05-06 17:51         ` Adhemerval Zanella

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).