Home | History | Annotate | Line # | Download | only in linux
bitmap.h revision 1.3
      1  1.3  riastrad /*	$NetBSD: bitmap.h,v 1.3 2018/08/27 07:13:45 riastradh Exp $	*/
      2  1.2  riastrad 
      3  1.2  riastrad /*-
      4  1.3  riastrad  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  1.2  riastrad  * All rights reserved.
      6  1.2  riastrad  *
      7  1.2  riastrad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2  riastrad  * by Taylor R. Campbell.
      9  1.2  riastrad  *
     10  1.2  riastrad  * Redistribution and use in source and binary forms, with or without
     11  1.2  riastrad  * modification, are permitted provided that the following conditions
     12  1.2  riastrad  * are met:
     13  1.2  riastrad  * 1. Redistributions of source code must retain the above copyright
     14  1.2  riastrad  *    notice, this list of conditions and the following disclaimer.
     15  1.2  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2  riastrad  *    notice, this list of conditions and the following disclaimer in the
     17  1.2  riastrad  *    documentation and/or other materials provided with the distribution.
     18  1.2  riastrad  *
     19  1.2  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.2  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.2  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.2  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.2  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.2  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.2  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.2  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.2  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.2  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.2  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     30  1.2  riastrad  */
     31  1.2  riastrad 
     32  1.2  riastrad #ifndef _LINUX_BITMAP_H_
     33  1.2  riastrad #define _LINUX_BITMAP_H_
     34  1.2  riastrad 
     35  1.3  riastrad #include <sys/param.h>
     36  1.3  riastrad #include <sys/types.h>
     37  1.3  riastrad #include <sys/systm.h>
     38  1.3  riastrad 
     39  1.3  riastrad /*
     40  1.3  riastrad  * bitmap_zero(bitmap, nbits)
     41  1.3  riastrad  *
     42  1.3  riastrad  *	Zero a bitmap that was allocated to have nbits bits.  Yes, this
     43  1.3  riastrad  *	zeros bits past nbits.
     44  1.3  riastrad  */
     45  1.3  riastrad static inline void
     46  1.3  riastrad bitmap_zero(unsigned long *bitmap, size_t nbits)
     47  1.3  riastrad {
     48  1.3  riastrad 	const size_t bpl = NBBY * sizeof(*bitmap);
     49  1.3  riastrad 
     50  1.3  riastrad 	memset(bitmap, 0, howmany(nbits, bpl) * sizeof(*bitmap));
     51  1.3  riastrad }
     52  1.3  riastrad 
     53  1.3  riastrad /*
     54  1.3  riastrad  * bitmap_empty(bitmap, nbits)
     55  1.3  riastrad  *
     56  1.3  riastrad  *	Return true if all bits at 0, 1, 2, ..., nbits-2, nbits-1 are
     57  1.3  riastrad  *	0, or false if any of them is 1.
     58  1.3  riastrad  */
     59  1.3  riastrad static inline bool
     60  1.3  riastrad bitmap_empty(const unsigned long *bitmap, size_t nbits)
     61  1.3  riastrad {
     62  1.3  riastrad 	const size_t bpl = NBBY * sizeof(*bitmap);
     63  1.3  riastrad 
     64  1.3  riastrad 	for (; bpl <= nbits; nbits -= bpl) {
     65  1.3  riastrad 		if (*bitmap++)
     66  1.3  riastrad 			return false;
     67  1.3  riastrad 	}
     68  1.3  riastrad 
     69  1.3  riastrad 	if (nbits) {
     70  1.3  riastrad 		if (*bitmap & ~(~0UL << nbits))
     71  1.3  riastrad 			return false;
     72  1.3  riastrad 	}
     73  1.3  riastrad 
     74  1.3  riastrad 	return true;
     75  1.3  riastrad }
     76  1.3  riastrad 
     77  1.3  riastrad /*
     78  1.3  riastrad  * bitmap_weight(bitmap, nbits)
     79  1.3  riastrad  *
     80  1.3  riastrad  *	Compute the number of 1 bits at 0, 1, 2, ..., nbits-2, nbits-1.
     81  1.3  riastrad  */
     82  1.3  riastrad static inline int
     83  1.3  riastrad bitmap_weight(const unsigned long *bitmap, size_t nbits)
     84  1.3  riastrad {
     85  1.3  riastrad 	const size_t bpl = NBBY * sizeof(*bitmap);
     86  1.3  riastrad 	int weight = 0;
     87  1.3  riastrad 
     88  1.3  riastrad 	for (; bpl <= nbits; nbits -= bpl)
     89  1.3  riastrad 		weight += popcountl(*bitmap++);
     90  1.3  riastrad 	if (nbits)
     91  1.3  riastrad 		weight += popcountl(*bitmap & ~(~0UL << nbits));
     92  1.3  riastrad 
     93  1.3  riastrad 	return weight;
     94  1.3  riastrad }
     95  1.3  riastrad 
     96  1.3  riastrad /*
     97  1.3  riastrad  * bitmap_set(bitmap, startbit, nbits)
     98  1.3  riastrad  *
     99  1.3  riastrad  *	Set bits at startbit, startbit+1, ..., nbits-2, nbits-1 to 1.
    100  1.3  riastrad  */
    101  1.3  riastrad static inline void
    102  1.3  riastrad bitmap_set(unsigned long *bitmap, size_t startbit, size_t nbits)
    103  1.3  riastrad {
    104  1.3  riastrad 	const size_t bpl = NBBY * sizeof(*bitmap);
    105  1.3  riastrad 	unsigned long *p;
    106  1.3  riastrad 	unsigned long mask;
    107  1.3  riastrad 
    108  1.3  riastrad 	for (p = bitmap + startbit/bpl, mask = ~(~0UL << (startbit%bpl));
    109  1.3  riastrad 	     nbits >= bpl;
    110  1.3  riastrad 	     p++, nbits -= bpl, mask = ~0UL)
    111  1.3  riastrad 		*p |= mask;
    112  1.3  riastrad 
    113  1.3  riastrad 	if (nbits)
    114  1.3  riastrad 		*p |= mask & (~0UL << nbits);
    115  1.3  riastrad }
    116  1.3  riastrad 
    117  1.3  riastrad /*
    118  1.3  riastrad  * bitmap_set(bitmap, startbit, nbits)
    119  1.3  riastrad  *
    120  1.3  riastrad  *	Clear bits at startbit, startbit+1, ..., nbits-2, nbits-1,
    121  1.3  riastrad  *	replacing them by 0.
    122  1.3  riastrad  */
    123  1.3  riastrad static inline void
    124  1.3  riastrad bitmap_clear(unsigned long *bitmap, size_t startbit, size_t nbits)
    125  1.3  riastrad {
    126  1.3  riastrad 	const size_t bpl = NBBY * sizeof(*bitmap);
    127  1.3  riastrad 	unsigned long *p;
    128  1.3  riastrad 	unsigned long mask;
    129  1.3  riastrad 
    130  1.3  riastrad 	for (p = bitmap + startbit/bpl, mask = ~0UL << (startbit%bpl);
    131  1.3  riastrad 	     nbits >= bpl;
    132  1.3  riastrad 	     p++, nbits -= bpl, mask = 0UL)
    133  1.3  riastrad 		*p &= mask;
    134  1.3  riastrad 
    135  1.3  riastrad 	if (nbits)
    136  1.3  riastrad 		*p &= mask | ~(~0UL << nbits);
    137  1.3  riastrad }
    138  1.3  riastrad 
    139  1.3  riastrad /*
    140  1.3  riastrad  * bitmap_and(dst, src1, src2, nbits)
    141  1.3  riastrad  *
    142  1.3  riastrad  *	Set dst to be the bitwise AND of src1 and src2, all bitmaps
    143  1.3  riastrad  *	allocated to have nbits bits.  Yes, this modifies bits past
    144  1.3  riastrad  *	nbits.
    145  1.3  riastrad  */
    146  1.3  riastrad static inline void
    147  1.3  riastrad bitmap_and(unsigned long *dst, const unsigned long *src1,
    148  1.3  riastrad     const unsigned long *src2, size_t nbits)
    149  1.3  riastrad {
    150  1.3  riastrad 	size_t n = howmany(nbits, NBBY * sizeof(unsigned long));
    151  1.3  riastrad 
    152  1.3  riastrad 	while (n --> 0)
    153  1.3  riastrad 		*dst++ = *src1++ & *src2++;
    154  1.3  riastrad }
    155  1.3  riastrad 
    156  1.3  riastrad /*
    157  1.3  riastrad  * bitmap_and(dst, src1, src2, nbits)
    158  1.3  riastrad  *
    159  1.3  riastrad  *	Set dst to be the bitwise inclusive-OR of src1 and src2, all
    160  1.3  riastrad  *	bitmaps allocated to have nbits bits.  Yes, this modifies bits
    161  1.3  riastrad  *	past nbits.
    162  1.3  riastrad  */
    163  1.3  riastrad static inline void
    164  1.3  riastrad bitmap_or(unsigned long *dst, const unsigned long *src1,
    165  1.3  riastrad     const unsigned long *src2, size_t nbits)
    166  1.3  riastrad {
    167  1.3  riastrad 	size_t n = howmany(nbits, NBBY * sizeof(unsigned long));
    168  1.3  riastrad 
    169  1.3  riastrad 	while (n --> 0)
    170  1.3  riastrad 		*dst++ = *src1++ | *src2++;
    171  1.3  riastrad }
    172  1.3  riastrad 
    173  1.2  riastrad #endif  /* _LINUX_BITMAP_H_ */
    174