Fix false assertion in dmu_tx_dirty_buf() on cloning
[zfs.git] / include / sys / bitops.h
blob5c477b38b20512bacfab8b27a2861b2a08ea648b
1 /*
2 * CDDL HEADER START
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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
19 * CDDL HEADER END
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.
32 #ifndef _SYS_BITOPS_H
33 #define _SYS_BITOPS_H
35 #include <sys/zfs_context.h>
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
42 * General-purpose 32-bit and 64-bit bitfield encodings.
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))
49 #define BF32_GET(x, low, len) BF32_DECODE(x, low, len)
50 #define BF64_GET(x, low, len) BF64_DECODE(x, low, len)
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 } while (0)
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 } while (0)
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))
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.
74 #define BF32_SET_SB(x, low, len, shift, bias, val) do { \
75 ASSERT3U(IS_P2ALIGNED(val, 1U << shift), !=, B_FALSE); \
76 ASSERT3S((val) >> (shift), >=, bias); \
77 BF32_SET(x, low, len, ((val) >> (shift)) - (bias)); \
78 } while (0)
79 #define BF64_SET_SB(x, low, len, shift, bias, val) do { \
80 ASSERT3U(IS_P2ALIGNED(val, 1ULL << shift), !=, B_FALSE); \
81 ASSERT3S((val) >> (shift), >=, bias); \
82 BF64_SET(x, low, len, ((val) >> (shift)) - (bias)); \
83 } while (0)
85 #ifdef __cplusplus
87 #endif
89 #endif /* _SYS_BITOPS_H */