xref: /aosp_15_r20/external/pciutils/bitops.h (revision c2e0c6b56a71da9abe8df5c8348fb3eb5c2c9251)
1 /*
2  *	The PCI Utilities -- Decode bits and bit fields
3  *
4  *	Copyright (c) 2023 Martin Mares <[email protected]>
5  *	Copyright (c) 2023 KNS Group LLC (YADRO)
6  *
7  *	Can be freely distributed and used under the terms of the GNU GPL v2+.
8  *
9  *	SPDX-License-Identifier: GPL-2.0-or-later
10  */
11 
12 #ifndef _BITOPS_H
13 #define _BITOPS_H
14 
15 #ifndef _PCI_LIB_H
16 #error Import only from pci.h
17 #endif
18 
19 /* Useful macros for decoding of bits and bit fields */
20 
21 #define FLAG(x, y) ((x & y) ? '+' : '-')
22 
23 // Generate mask
24 
25 #define BIT(at) ((u64)1 << (at))
26 // Boundaries inclusive
27 #define MASK(h, l)   ((((u64)1 << ((h) + 1)) - 1) & ~(((u64)1 << (l)) - 1))
28 
29 // Get/set from register
30 
31 #define BITS(x, at, width)      (((x) >> (at)) & ((1 << (width)) - 1))
32 #define GET_REG_MASK(reg, mask) (((reg) & (mask)) / ((mask) & ~((mask) << 1)))
33 #define SET_REG_MASK(reg, mask, val)                                                               \
34   (((reg) & ~(mask)) | (((val) * ((mask) & ~((mask) << 1))) & (mask)))
35 
36 #define TABLE(tab, x, buf)                                                                         \
37   ((x) < sizeof(tab) / sizeof((tab)[0]) ? (tab)[x] : (sprintf((buf), "??%d", (x)), (buf)))
38 
39 #endif
40