locking: ignore false positives caused by perf_event_enable/period()
[smatch.git] / bits.h
blob9908190d8c2fbf512571c316626309a9fcb8b2b9
1 /* SPDX-License-Identifier: MIT */
2 /*
3 * Helper functions for manipulation & testing of integer values
4 * like zero or sign-extensions.
6 * Copyright (C) 2017 Luc Van Oostenryck
8 */
10 #ifndef BITS_H
11 #define BITS_H
13 static inline unsigned long long sign_bit(unsigned size)
15 return 1ULL << (size - 1);
18 static inline unsigned long long sign_mask(unsigned size)
20 unsigned long long sbit = sign_bit(size);
21 return sbit - 1;
24 static inline unsigned long long bits_mask(unsigned size)
26 unsigned long long sbit = sign_bit(size);
27 return sbit | (sbit - 1);
31 static inline long long zero_extend(long long val, unsigned size)
33 return val & bits_mask(size);
36 static inline long long sign_extend(long long val, unsigned size)
38 if (val & sign_bit(size))
39 val |= ~sign_mask(size);
40 return val;
43 ///
44 // sign extend @val but only if exactly representable
45 static inline long long sign_extend_safe(long long val, unsigned size)
47 unsigned long long mask = bits_mask(size);
48 if (!(val & ~mask))
49 val = sign_extend(val, size);
50 return val;
53 static inline long long bits_extend(long long val, unsigned size, int is_signed)
55 val = zero_extend(val, size);
56 if (is_signed)
57 val = sign_extend(val, size);
58 return val;
61 static inline int is_power_of_2(long long val)
63 return val && !(val & (val - 1));
66 ///
67 // log base 2 of an exact power-of-2
68 static inline int log2_exact(unsigned long long val)
70 return 8 * sizeof(val) - __builtin_clzl(val) - 1;
73 #endif