1ad59256aSFrançois Tigeot /*
21dedbd3bSFrançois Tigeot * Copyright (c) 2016-2019 François Tigeot <ftigeot@wolfpond.org>
3ad59256aSFrançois Tigeot * All rights reserved.
4ad59256aSFrançois Tigeot *
5ad59256aSFrançois Tigeot * Redistribution and use in source and binary forms, with or without
6ad59256aSFrançois Tigeot * modification, are permitted provided that the following conditions
7ad59256aSFrançois Tigeot * are met:
8ad59256aSFrançois Tigeot * 1. Redistributions of source code must retain the above copyright
9ad59256aSFrançois Tigeot * notice unmodified, this list of conditions, and the following
10ad59256aSFrançois Tigeot * disclaimer.
11ad59256aSFrançois Tigeot * 2. Redistributions in binary form must reproduce the above copyright
12ad59256aSFrançois Tigeot * notice, this list of conditions and the following disclaimer in the
13ad59256aSFrançois Tigeot * documentation and/or other materials provided with the distribution.
14ad59256aSFrançois Tigeot *
15ad59256aSFrançois Tigeot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16ad59256aSFrançois Tigeot * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17ad59256aSFrançois Tigeot * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18ad59256aSFrançois Tigeot * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19ad59256aSFrançois Tigeot * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20ad59256aSFrançois Tigeot * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21ad59256aSFrançois Tigeot * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22ad59256aSFrançois Tigeot * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23ad59256aSFrançois Tigeot * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24ad59256aSFrançois Tigeot * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25ad59256aSFrançois Tigeot */
26ad59256aSFrançois Tigeot
27ad59256aSFrançois Tigeot #ifndef _LINUX_BITMAP_H_
28ad59256aSFrançois Tigeot #define _LINUX_BITMAP_H_
29ad59256aSFrançois Tigeot
301dedbd3bSFrançois Tigeot #include <linux/types.h>
311dedbd3bSFrançois Tigeot #include <linux/bitops.h>
321dedbd3bSFrançois Tigeot #include <linux/string.h>
331dedbd3bSFrançois Tigeot #include <linux/kernel.h>
341dedbd3bSFrançois Tigeot
35ad59256aSFrançois Tigeot static inline void
bitmap_or(unsigned long * dst,const unsigned long * src1,const unsigned long * src2,unsigned int nbits)36ad59256aSFrançois Tigeot bitmap_or(unsigned long *dst, const unsigned long *src1,
37ad59256aSFrançois Tigeot const unsigned long *src2, unsigned int nbits)
38ad59256aSFrançois Tigeot {
39ad59256aSFrançois Tigeot if (nbits <= BITS_PER_LONG) {
40ad59256aSFrançois Tigeot *dst = *src1 | *src2;
41ad59256aSFrançois Tigeot } else {
42ad59256aSFrançois Tigeot int chunks = DIV_ROUND_UP(nbits, BITS_PER_LONG);
43ad59256aSFrançois Tigeot
44ad59256aSFrançois Tigeot for (int i = 0;i < chunks;i++)
45ad59256aSFrançois Tigeot dst[i] = src1[i] | src2[i];
46ad59256aSFrançois Tigeot }
47ad59256aSFrançois Tigeot }
48ad59256aSFrançois Tigeot
49593c19b7SFrançois Tigeot static inline int
bitmap_weight(unsigned long * bitmap,unsigned int nbits)50593c19b7SFrançois Tigeot bitmap_weight(unsigned long *bitmap, unsigned int nbits)
51593c19b7SFrançois Tigeot {
52593c19b7SFrançois Tigeot unsigned int bit;
53593c19b7SFrançois Tigeot unsigned int retval = 0;
54593c19b7SFrançois Tigeot
55593c19b7SFrançois Tigeot for_each_set_bit(bit, bitmap, nbits)
56593c19b7SFrançois Tigeot retval++;
57593c19b7SFrançois Tigeot return (retval);
58593c19b7SFrançois Tigeot }
59593c19b7SFrançois Tigeot
60*78973132SSergey Zigachev static inline void
bitmap_complement(void * d,void * s,u_int n)61*78973132SSergey Zigachev bitmap_complement(void *d, void *s, u_int n)
62*78973132SSergey Zigachev {
63*78973132SSergey Zigachev u_int *dst = d;
64*78973132SSergey Zigachev u_int *src = s;
65*78973132SSergey Zigachev u_int b;
66*78973132SSergey Zigachev
67*78973132SSergey Zigachev for (b = 0; b < n; b += 32)
68*78973132SSergey Zigachev dst[b >> 5] = ~src[b >> 5];
69*78973132SSergey Zigachev }
70*78973132SSergey Zigachev
71ad59256aSFrançois Tigeot #endif /* _LINUX_BITMAP_H_ */
72