Back to index

glibc  2.9
sched_cpucount.c
Go to the documentation of this file.
00001 /* Copyright (C) 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <limits.h>
00020 #include <sched.h>
00021 
00022 
00023 int
00024 __sched_cpucount (size_t setsize, const cpu_set_t *setp)
00025 {
00026   int s = 0;
00027   const __cpu_mask *p = setp->__bits;
00028   const __cpu_mask *end = &setp->__bits[setsize / sizeof (__cpu_mask)];
00029 
00030   while (p < end)
00031     {
00032       __cpu_mask l = *p++;
00033 
00034 #ifdef POPCNT
00035       s += POPCNT (l);
00036 #else
00037       if (l == 0)
00038        continue;
00039 
00040 # if LONG_BIT > 32
00041       l = (l & 0x5555555555555555ul) + ((l >> 1) & 0x5555555555555555ul);
00042       l = (l & 0x3333333333333333ul) + ((l >> 2) & 0x3333333333333333ul);
00043       l = (l & 0x0f0f0f0f0f0f0f0ful) + ((l >> 4) & 0x0f0f0f0f0f0f0f0ful);
00044       l = (l & 0x00ff00ff00ff00fful) + ((l >> 8) & 0x00ff00ff00ff00fful);
00045       l = (l & 0x0000ffff0000fffful) + ((l >> 16) & 0x0000ffff0000fffful);
00046       l = (l & 0x00000000fffffffful) + ((l >> 32) & 0x00000000fffffffful);
00047 # else
00048       l = (l & 0x55555555ul) + ((l >> 1) & 0x55555555ul);
00049       l = (l & 0x33333333ul) + ((l >> 2) & 0x33333333ul);
00050       l = (l & 0x0f0f0f0ful) + ((l >> 4) & 0x0f0f0f0ful);
00051       l = (l & 0x00ff00fful) + ((l >> 8) & 0x00ff00fful);
00052       l = (l & 0x0000fffful) + ((l >> 16) & 0x0000fffful);
00053 # endif
00054 
00055       s += l;
00056 #endif
00057     }
00058 
00059   return s;
00060 }