1 // Copyright 2016 The Fuchsia Authors
2 //
3 // Use of this source code is governed by a MIT-style
4 // license that can be found in the LICENSE file or at
5 // https://opensource.org/licenses/MIT
6 
7 #pragma once
8 
9 #include <assert.h>
10 #include <stdint.h>
11 
12 // types and routines for dealing with lists of cpus and cpu masks
13 
14 typedef uint32_t cpu_mask_t;
15 typedef uint32_t cpu_num_t;
16 
17 static_assert(SMP_MAX_CPUS <= sizeof(cpu_mask_t) * CHAR_BIT, "");
18 
19 #define INVALID_CPU ((cpu_num_t)-1)
20 #define CPU_MASK_ALL ((cpu_mask_t)-1)
21 
is_valid_cpu_num(cpu_num_t num)22 static inline bool is_valid_cpu_num(cpu_num_t num) {
23     return (num < SMP_MAX_CPUS);
24 }
25 
cpu_num_to_mask(cpu_num_t num)26 static inline cpu_mask_t cpu_num_to_mask(cpu_num_t num) {
27     if (!is_valid_cpu_num(num)) {
28         return 0;
29     }
30 
31     return ((cpu_mask_t)1u << num);
32 }
33 
highest_cpu_set(cpu_mask_t mask)34 static inline cpu_num_t highest_cpu_set(cpu_mask_t mask) {
35     if (mask == 0) {
36         return 0;
37     }
38 
39     return (cpu_num_t)(sizeof(cpu_mask_t) * CHAR_BIT - 1) - __builtin_clz(mask);
40 }
41 
lowest_cpu_set(cpu_mask_t mask)42 static inline cpu_num_t lowest_cpu_set(cpu_mask_t mask) {
43     if (mask == 0) {
44         return 0;
45     }
46 
47     return (cpu_num_t)(__builtin_ctz(mask));
48 }
49