-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpu.h
94 lines (76 loc) · 2.31 KB
/
cpu.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* Copyright (C) 2009 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License (not later!)
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
#ifndef _CPU_H
#define _CPU_H
static inline int cpu_isset(guint64 *cpu_mask, gint cpu)
{
guint64 mask;
mask = *(cpu_mask + (cpu >> 6));
return mask & (1ULL << (cpu & ((1ULL << 6) - 1)));
}
static inline void cpu_set(guint64 *cpu_mask, gint cpu)
{
guint64 *mask;
mask = cpu_mask + (cpu >> 6);
*mask |= (1ULL << (cpu & ((1ULL << 6) - 1)));
}
static inline void cpu_clear(guint64 *cpu_mask, gint cpu)
{
guint64 *mask;
mask = cpu_mask + (cpu >> 6);
*mask &= ~(1ULL << (cpu & ((1ULL << 6) - 1)));
}
static inline void set_cpus(guint64 *cpu_mask, gint cpus)
{
gint idx;
for (idx = 0; idx < (cpus >> 6); idx++) {
*(cpu_mask + idx) = -1ULL;
}
*(cpu_mask) = (1ULL << (cpus & ((1ULL << 6) - 1))) - 1;
}
static inline gboolean cpus_equal(guint64 *a_mask, guint64 *b_mask,
gint cpus)
{
gint idx;
for (idx = 0; idx < (cpus >> 6) + 1; idx++) {
if (*(a_mask + idx) != *(b_mask + idx))
return FALSE;
}
return TRUE;
}
/* Hamming weight */
static inline guint hweight(guint mask)
{
guint64 w = mask;
w -= (w >> 1) & 0x5555555555555555ul;
w = (w & 0x3333333333333333ul) + ((w >> 2) & 0x3333333333333333ul);
w = (w + (w >> 4)) & 0x0f0f0f0f0f0f0f0ful;
return (w * 0x0101010101010101ul) >> 56;
}
static inline guint cpu_weight(guint64 *cpu_mask, guint cpus)
{
guint weight = 0;
gint idx;
for (idx = 0; idx < (cpus >> 6) + 1; idx++)
weight += hweight(*(cpu_mask + idx));
return weight;
}
#endif /* _CPU_H */