xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/bitops.h (revision b69c34dad3717624ff6b4f32b71014ee05b6a678)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2011, 2019 by Delphix. All rights reserved.
24  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
25  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
26  * Copyright 2013 Saso Kiselkov. All rights reserved.
27  * Copyright (c) 2014 Integros [integros.com]
28  * Copyright 2017 Joyent, Inc.
29  * Copyright (c) 2017 Datto Inc.
30  */
31 
32 #ifndef _SYS_BITOPS_H
33 #define	_SYS_BITOPS_H
34 
35 #include <sys/zfs_context.h>
36 
37 #ifdef	__cplusplus
38 extern "C" {
39 #endif
40 
41 /*
42  * General-purpose 32-bit and 64-bit bitfield encodings.
43  */
44 #define	BF32_DECODE(x, low, len)	P2PHASE((x) >> (low), 1U << (len))
45 #define	BF64_DECODE(x, low, len)	P2PHASE((x) >> (low), 1ULL << (len))
46 #define	BF32_ENCODE(x, low, len)	(P2PHASE((x), 1U << (len)) << (low))
47 #define	BF64_ENCODE(x, low, len)	(P2PHASE((x), 1ULL << (len)) << (low))
48 
49 #define	BF32_GET(x, low, len)		BF32_DECODE(x, low, len)
50 #define	BF64_GET(x, low, len)		BF64_DECODE(x, low, len)
51 
52 #define	BF32_SET(x, low, len, val) do { \
53 	ASSERT3U(val, <, 1U << (len)); \
54 	ASSERT3U(low + len, <=, 32); \
55 	(x) ^= BF32_ENCODE((x >> low) ^ (val), low, len); \
56 _NOTE(CONSTCOND) } while (0)
57 
58 #define	BF64_SET(x, low, len, val) do { \
59 	ASSERT3U(val, <, 1ULL << (len)); \
60 	ASSERT3U(low + len, <=, 64); \
61 	((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len)); \
62 _NOTE(CONSTCOND) } while (0)
63 
64 #define	BF32_GET_SB(x, low, len, shift, bias)	\
65 	((BF32_GET(x, low, len) + (bias)) << (shift))
66 #define	BF64_GET_SB(x, low, len, shift, bias)	\
67 	((BF64_GET(x, low, len) + (bias)) << (shift))
68 
69 /*
70  * We use ASSERT3U instead of ASSERT in these macros to prevent a lint error in
71  * the case where val is a constant.  We can't fix ASSERT because it's used as
72  * an expression in several places in the kernel; as a result, changing it to
73  * the do{} while() syntax to allow us to _NOTE the CONSTCOND is not an option.
74  */
75 #define	BF32_SET_SB(x, low, len, shift, bias, val) do { \
76 	ASSERT3U(IS_P2ALIGNED(val, 1U << shift), !=, B_FALSE); \
77 	ASSERT3S((val) >> (shift), >=, bias); \
78 	BF32_SET(x, low, len, ((val) >> (shift)) - (bias)); \
79 _NOTE(CONSTCOND) } while (0)
80 #define	BF64_SET_SB(x, low, len, shift, bias, val) do { \
81 	ASSERT3U(IS_P2ALIGNED(val, 1ULL << shift), !=, B_FALSE); \
82 	ASSERT3S((val) >> (shift), >=, bias); \
83 	BF64_SET(x, low, len, ((val) >> (shift)) - (bias)); \
84 _NOTE(CONSTCOND) } while (0)
85 
86 #ifdef	__cplusplus
87 }
88 #endif
89 
90 #endif /* _SYS_BITOPS_H */
91