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