1 // SPDX-License-Identifier: GPL-2.0-only << 2 /* 1 /* 3 * From lib/bitmap.c 2 * From lib/bitmap.c 4 * Helper functions for bitmap.h. 3 * Helper functions for bitmap.h. >> 4 * >> 5 * This source code is licensed under the GNU General Public License, >> 6 * Version 2. See the file COPYING for more details. 5 */ 7 */ 6 #include <linux/bitmap.h> 8 #include <linux/bitmap.h> 7 9 8 unsigned int __bitmap_weight(const unsigned lo !! 10 int __bitmap_weight(const unsigned long *bitmap, int bits) 9 { 11 { 10 unsigned int k, w = 0, lim = bits/BITS !! 12 int k, w = 0, lim = bits/BITS_PER_LONG; 11 13 12 for (k = 0; k < lim; k++) 14 for (k = 0; k < lim; k++) 13 w += hweight_long(bitmap[k]); 15 w += hweight_long(bitmap[k]); 14 16 15 if (bits % BITS_PER_LONG) 17 if (bits % BITS_PER_LONG) 16 w += hweight_long(bitmap[k] & 18 w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits)); 17 19 18 return w; 20 return w; 19 } 21 } 20 22 21 void __bitmap_or(unsigned long *dst, const uns 23 void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1, 22 const unsigned long *bitmap2, 24 const unsigned long *bitmap2, int bits) 23 { 25 { 24 int k; 26 int k; 25 int nr = BITS_TO_LONGS(bits); 27 int nr = BITS_TO_LONGS(bits); 26 28 27 for (k = 0; k < nr; k++) 29 for (k = 0; k < nr; k++) 28 dst[k] = bitmap1[k] | bitmap2[ 30 dst[k] = bitmap1[k] | bitmap2[k]; 29 } 31 } 30 32 31 size_t bitmap_scnprintf(unsigned long *bitmap, !! 33 size_t bitmap_scnprintf(unsigned long *bitmap, int nbits, 32 char *buf, size_t size 34 char *buf, size_t size) 33 { 35 { 34 /* current bit is 'cur', most recently 36 /* current bit is 'cur', most recently seen range is [rbot, rtop] */ 35 unsigned int cur, rbot, rtop; !! 37 int cur, rbot, rtop; 36 bool first = true; 38 bool first = true; 37 size_t ret = 0; 39 size_t ret = 0; 38 40 39 rbot = cur = find_first_bit(bitmap, nb 41 rbot = cur = find_first_bit(bitmap, nbits); 40 while (cur < nbits) { 42 while (cur < nbits) { 41 rtop = cur; 43 rtop = cur; 42 cur = find_next_bit(bitmap, nb 44 cur = find_next_bit(bitmap, nbits, cur + 1); 43 if (cur < nbits && cur <= rtop 45 if (cur < nbits && cur <= rtop + 1) 44 continue; 46 continue; 45 47 46 if (!first) 48 if (!first) 47 ret += scnprintf(buf + 49 ret += scnprintf(buf + ret, size - ret, ","); 48 50 49 first = false; 51 first = false; 50 52 51 ret += scnprintf(buf + ret, si 53 ret += scnprintf(buf + ret, size - ret, "%d", rbot); 52 if (rbot < rtop) 54 if (rbot < rtop) 53 ret += scnprintf(buf + 55 ret += scnprintf(buf + ret, size - ret, "-%d", rtop); 54 56 55 rbot = cur; 57 rbot = cur; 56 } 58 } 57 return ret; 59 return ret; 58 } 60 } 59 61 60 bool __bitmap_and(unsigned long *dst, const un !! 62 int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1, 61 const unsigned long *bitmap2, 63 const unsigned long *bitmap2, unsigned int bits) 62 { 64 { 63 unsigned int k; 65 unsigned int k; 64 unsigned int lim = bits/BITS_PER_LONG; 66 unsigned int lim = bits/BITS_PER_LONG; 65 unsigned long result = 0; 67 unsigned long result = 0; 66 68 67 for (k = 0; k < lim; k++) 69 for (k = 0; k < lim; k++) 68 result |= (dst[k] = bitmap1[k] 70 result |= (dst[k] = bitmap1[k] & bitmap2[k]); 69 if (bits % BITS_PER_LONG) 71 if (bits % BITS_PER_LONG) 70 result |= (dst[k] = bitmap1[k] 72 result |= (dst[k] = bitmap1[k] & bitmap2[k] & 71 BITMAP_LAST_WORD_MA 73 BITMAP_LAST_WORD_MASK(bits)); 72 return result != 0; 74 return result != 0; 73 } << 74 << 75 bool __bitmap_equal(const unsigned long *bitma << 76 const unsigned long *bitma << 77 { << 78 unsigned int k, lim = bits/BITS_PER_LO << 79 for (k = 0; k < lim; ++k) << 80 if (bitmap1[k] != bitmap2[k]) << 81 return false; << 82 << 83 if (bits % BITS_PER_LONG) << 84 if ((bitmap1[k] ^ bitmap2[k]) << 85 return false; << 86 << 87 return true; << 88 } << 89 << 90 bool __bitmap_intersects(const unsigned long * << 91 const unsigned long * << 92 { << 93 unsigned int k, lim = bits/BITS_PER_LO << 94 for (k = 0; k < lim; ++k) << 95 if (bitmap1[k] & bitmap2[k]) << 96 return true; << 97 << 98 if (bits % BITS_PER_LONG) << 99 if ((bitmap1[k] & bitmap2[k]) << 100 return true; << 101 return false; << 102 } << 103 << 104 void __bitmap_clear(unsigned long *map, unsign << 105 { << 106 unsigned long *p = map + BIT_WORD(star << 107 const unsigned int size = start + len; << 108 int bits_to_clear = BITS_PER_LONG - (s << 109 unsigned long mask_to_clear = BITMAP_F << 110 << 111 while (len - bits_to_clear >= 0) { << 112 *p &= ~mask_to_clear; << 113 len -= bits_to_clear; << 114 bits_to_clear = BITS_PER_LONG; << 115 mask_to_clear = ~0UL; << 116 p++; << 117 } << 118 if (len) { << 119 mask_to_clear &= BITMAP_LAST_W << 120 *p &= ~mask_to_clear; << 121 } << 122 } 75 } 123 76
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.