1 /* $OpenBSD: bitmap.h,v 1.1 2019/04/14 10:14:53 jsg Exp $ */ 2 /* 3 * Copyright (c) 2013, 2014, 2015 Mark Kettenis 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #ifndef _LINUX_BITMAP_H 19 #define _LINUX_BITMAP_H 20 21 #include <linux/bitops.h> 22 23 #define bitmap_empty(p, n) (find_first_bit(p, n) == n) 24 25 static inline void 26 bitmap_set(void *p, int b, u_int n) 27 { 28 u_int end = b + n; 29 30 for (; b < end; b++) 31 __set_bit(b, p); 32 } 33 34 static inline void 35 bitmap_clear(void *p, int b, u_int n) 36 { 37 u_int end = b + n; 38 39 for (; b < end; b++) 40 __clear_bit(b, p); 41 } 42 43 static inline void 44 bitmap_zero(void *p, u_int n) 45 { 46 u_int *ptr = p; 47 u_int b; 48 49 for (b = 0; b < n; b += 32) 50 ptr[b >> 5] = 0; 51 } 52 53 static inline void 54 bitmap_or(void *d, void *s1, void *s2, u_int n) 55 { 56 u_int *dst = d; 57 u_int *src1 = s1; 58 u_int *src2 = s2; 59 u_int b; 60 61 for (b = 0; b < n; b += 32) 62 dst[b >> 5] = src1[b >> 5] | src2[b >> 5]; 63 } 64 65 static inline void 66 bitmap_complement(void *d, void *s, u_int n) 67 { 68 u_int *dst = d; 69 u_int *src = s; 70 u_int b; 71 72 for (b = 0; b < n; b += 32) 73 dst[b >> 5] = ~src[b >> 5]; 74 } 75 76 static inline int 77 bitmap_weight(void *p, u_int n) 78 { 79 u_int *ptr = p; 80 u_int b; 81 int sum = 0; 82 83 for (b = 0; b < n; b += 32) 84 sum += hweight32(ptr[b >> 5]); 85 return sum; 86 } 87 88 #endif 89