Home | History | Annotate | Line # | Download | only in linux
bitops.h revision 1.8.6.2
      1  1.8.6.2  christos /*	$NetBSD: bitops.h,v 1.8.6.2 2019/06/10 22:07:44 christos Exp $	*/
      2  1.8.6.2  christos 
      3  1.8.6.2  christos /*-
      4  1.8.6.2  christos  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  1.8.6.2  christos  * All rights reserved.
      6  1.8.6.2  christos  *
      7  1.8.6.2  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.8.6.2  christos  * by Taylor R. Campbell.
      9  1.8.6.2  christos  *
     10  1.8.6.2  christos  * Redistribution and use in source and binary forms, with or without
     11  1.8.6.2  christos  * modification, are permitted provided that the following conditions
     12  1.8.6.2  christos  * are met:
     13  1.8.6.2  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.8.6.2  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.8.6.2  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.8.6.2  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.8.6.2  christos  *    documentation and/or other materials provided with the distribution.
     18  1.8.6.2  christos  *
     19  1.8.6.2  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.8.6.2  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.8.6.2  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.8.6.2  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.8.6.2  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.8.6.2  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.8.6.2  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.8.6.2  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.8.6.2  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.8.6.2  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.8.6.2  christos  * POSSIBILITY OF SUCH DAMAGE.
     30  1.8.6.2  christos  */
     31  1.8.6.2  christos 
     32  1.8.6.2  christos #ifndef _LINUX_BITOPS_H_
     33  1.8.6.2  christos #define _LINUX_BITOPS_H_
     34  1.8.6.2  christos 
     35  1.8.6.2  christos #include <sys/cdefs.h>
     36  1.8.6.2  christos #include <sys/types.h>
     37  1.8.6.2  christos #include <sys/param.h>
     38  1.8.6.2  christos #include <sys/atomic.h>
     39  1.8.6.2  christos #include <sys/bitops.h>
     40  1.8.6.2  christos 
     41  1.8.6.2  christos #include <machine/limits.h>
     42  1.8.6.2  christos 
     43  1.8.6.2  christos #include <lib/libkern/libkern.h>
     44  1.8.6.2  christos 
     45  1.8.6.2  christos /*
     46  1.8.6.2  christos  * Linux __ffs/__ffs64 is zero-based; zero input is undefined.  Our
     47  1.8.6.2  christos  * ffs/ffs64 is one-based; zero input yields zero.
     48  1.8.6.2  christos  */
     49  1.8.6.2  christos static inline unsigned long
     50  1.8.6.2  christos __ffs(unsigned long x)
     51  1.8.6.2  christos {
     52  1.8.6.2  christos 
     53  1.8.6.2  christos 	KASSERT(x != 0);
     54  1.8.6.2  christos 	return ffs64(x) - 1;
     55  1.8.6.2  christos }
     56  1.8.6.2  christos 
     57  1.8.6.2  christos static inline unsigned long
     58  1.8.6.2  christos __ffs64(uint64_t x)
     59  1.8.6.2  christos {
     60  1.8.6.2  christos 
     61  1.8.6.2  christos 	KASSERT(x != 0);
     62  1.8.6.2  christos 	return ffs64(x) - 1;
     63  1.8.6.2  christos }
     64  1.8.6.2  christos 
     65  1.8.6.2  christos /*
     66  1.8.6.2  christos  * Linux fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32, so it matches
     67  1.8.6.2  christos  * our fls semantics.
     68  1.8.6.2  christos  */
     69  1.8.6.2  christos static inline int
     70  1.8.6.2  christos fls(int x)
     71  1.8.6.2  christos {
     72  1.8.6.2  christos 	return fls32(x);
     73  1.8.6.2  christos }
     74  1.8.6.2  christos 
     75  1.8.6.2  christos static inline unsigned int
     76  1.8.6.2  christos hweight8(uint8_t w)
     77  1.8.6.2  christos {
     78  1.8.6.2  christos 	return popcount(w & 0xff);
     79  1.8.6.2  christos }
     80  1.8.6.2  christos 
     81  1.8.6.2  christos static inline unsigned int
     82  1.8.6.2  christos hweight16(uint16_t n)
     83  1.8.6.2  christos {
     84  1.8.6.2  christos 	return popcount32(n);
     85  1.8.6.2  christos }
     86  1.8.6.2  christos 
     87  1.8.6.2  christos static inline unsigned int
     88  1.8.6.2  christos hweight32(uint32_t n)
     89  1.8.6.2  christos {
     90  1.8.6.2  christos 	return popcount32(n);
     91  1.8.6.2  christos }
     92  1.8.6.2  christos 
     93  1.8.6.2  christos static inline unsigned int
     94  1.8.6.2  christos hweight64(uint64_t n)
     95  1.8.6.2  christos {
     96  1.8.6.2  christos 	return popcount64(n);
     97  1.8.6.2  christos }
     98  1.8.6.2  christos 
     99  1.8.6.2  christos /*
    100  1.8.6.2  christos  * XXX Don't define BITS_PER_LONG as sizeof(unsigned long)*CHAR_BIT
    101  1.8.6.2  christos  * because that won't work in preprocessor conditionals, where it often
    102  1.8.6.2  christos  * turns up.
    103  1.8.6.2  christos  */
    104  1.8.6.2  christos 
    105  1.8.6.2  christos #define	BITS_TO_LONGS(n)						\
    106  1.8.6.2  christos 	roundup2((n), (sizeof(unsigned long) * CHAR_BIT))
    107  1.8.6.2  christos 
    108  1.8.6.2  christos #define	BIT(n)	((uintmax_t)1 << (n))
    109  1.8.6.2  christos #define	GENMASK(h,l)	__BITS(h,l)
    110  1.8.6.2  christos 
    111  1.8.6.2  christos static inline int
    112  1.8.6.2  christos test_bit(unsigned int n, const volatile unsigned long *p)
    113  1.8.6.2  christos {
    114  1.8.6.2  christos 	const unsigned units = (sizeof(unsigned long) * CHAR_BIT);
    115  1.8.6.2  christos 
    116  1.8.6.2  christos 	return ((p[n / units] & (1UL << (n % units))) != 0);
    117  1.8.6.2  christos }
    118  1.8.6.2  christos 
    119  1.8.6.2  christos static inline void
    120  1.8.6.2  christos __set_bit(unsigned int n, volatile unsigned long *p)
    121  1.8.6.2  christos {
    122  1.8.6.2  christos 	const unsigned units = (sizeof(unsigned long) * CHAR_BIT);
    123  1.8.6.2  christos 
    124  1.8.6.2  christos 	p[n / units] |= (1UL << (n % units));
    125  1.8.6.2  christos }
    126  1.8.6.2  christos 
    127  1.8.6.2  christos static inline void
    128  1.8.6.2  christos __clear_bit(unsigned int n, volatile unsigned long *p)
    129  1.8.6.2  christos {
    130  1.8.6.2  christos 	const unsigned units = (sizeof(unsigned long) * CHAR_BIT);
    131  1.8.6.2  christos 
    132  1.8.6.2  christos 	p[n / units] &= ~(1UL << (n % units));
    133  1.8.6.2  christos }
    134  1.8.6.2  christos 
    135  1.8.6.2  christos static inline void
    136  1.8.6.2  christos __change_bit(unsigned int n, volatile unsigned long *p)
    137  1.8.6.2  christos {
    138  1.8.6.2  christos 	const unsigned units = (sizeof(unsigned long) * CHAR_BIT);
    139  1.8.6.2  christos 
    140  1.8.6.2  christos 	p[n / units] ^= (1UL << (n % units));
    141  1.8.6.2  christos }
    142  1.8.6.2  christos 
    143  1.8.6.2  christos static inline unsigned long
    144  1.8.6.2  christos __test_and_set_bit(unsigned int bit, volatile unsigned long *ptr)
    145  1.8.6.2  christos {
    146  1.8.6.2  christos 	const unsigned int units = (sizeof(*ptr) * CHAR_BIT);
    147  1.8.6.2  christos 	volatile unsigned long *const p = &ptr[bit / units];
    148  1.8.6.2  christos 	const unsigned long mask = (1UL << (bit % units));
    149  1.8.6.2  christos 	unsigned long v;
    150  1.8.6.2  christos 
    151  1.8.6.2  christos 	v = *p;
    152  1.8.6.2  christos 	*p |= mask;
    153  1.8.6.2  christos 
    154  1.8.6.2  christos 	return ((v & mask) != 0);
    155  1.8.6.2  christos }
    156  1.8.6.2  christos 
    157  1.8.6.2  christos static inline unsigned long
    158  1.8.6.2  christos __test_and_clear_bit(unsigned int bit, volatile unsigned long *ptr)
    159  1.8.6.2  christos {
    160  1.8.6.2  christos 	const unsigned int units = (sizeof(*ptr) * CHAR_BIT);
    161  1.8.6.2  christos 	volatile unsigned long *const p = &ptr[bit / units];
    162  1.8.6.2  christos 	const unsigned long mask = (1UL << (bit % units));
    163  1.8.6.2  christos 	unsigned long v;
    164  1.8.6.2  christos 
    165  1.8.6.2  christos 	v = *p;
    166  1.8.6.2  christos 	*p &= ~mask;
    167  1.8.6.2  christos 
    168  1.8.6.2  christos 	return ((v & mask) != 0);
    169  1.8.6.2  christos }
    170  1.8.6.2  christos 
    171  1.8.6.2  christos static inline unsigned long
    172  1.8.6.2  christos __test_and_change_bit(unsigned int bit, volatile unsigned long *ptr)
    173  1.8.6.2  christos {
    174  1.8.6.2  christos 	const unsigned int units = (sizeof(*ptr) * CHAR_BIT);
    175  1.8.6.2  christos 	volatile unsigned long *const p = &ptr[bit / units];
    176  1.8.6.2  christos 	const unsigned long mask = (1UL << (bit % units));
    177  1.8.6.2  christos 	unsigned long v;
    178  1.8.6.2  christos 
    179  1.8.6.2  christos 	v = *p;
    180  1.8.6.2  christos 	*p ^= mask;
    181  1.8.6.2  christos 
    182  1.8.6.2  christos 	return ((v & mask) != 0);
    183  1.8.6.2  christos }
    184  1.8.6.2  christos 
    185  1.8.6.2  christos static inline unsigned long
    186  1.8.6.2  christos __find_next_bit(const unsigned long *ptr, unsigned long nbits,
    187  1.8.6.2  christos     unsigned long startbit, unsigned long toggle)
    188  1.8.6.2  christos {
    189  1.8.6.2  christos 	const size_t bpl = (CHAR_BIT * sizeof(*ptr));
    190  1.8.6.2  christos 	const unsigned long *p = ptr + startbit/bpl;
    191  1.8.6.2  christos 	size_t n = howmany(nbits, bpl);
    192  1.8.6.2  christos 	unsigned long result;
    193  1.8.6.2  christos 	uint64_t word;
    194  1.8.6.2  christos 
    195  1.8.6.2  christos 	/*
    196  1.8.6.2  christos 	 * We use ffs64 because NetBSD doesn't have a handy ffsl that
    197  1.8.6.2  christos 	 * works on unsigned long.  This is a waste on 32-bit systems
    198  1.8.6.2  christos 	 * but I'd rather not maintain multiple copies of this -- the
    199  1.8.6.2  christos 	 * first version had enough bugs already.
    200  1.8.6.2  christos 	 */
    201  1.8.6.2  christos 
    202  1.8.6.2  christos 	/* Do we need to examine a partial starting word?  */
    203  1.8.6.2  christos 	if (startbit % bpl) {
    204  1.8.6.2  christos 		/* Toggle the bits and convert to 64 bits for ffs64.  */
    205  1.8.6.2  christos 		word = *p ^ toggle;
    206  1.8.6.2  christos 
    207  1.8.6.2  christos 		/* Clear the low startbit%bpl bits.  */
    208  1.8.6.2  christos 		word &= (~0UL << (startbit % bpl));
    209  1.8.6.2  christos 
    210  1.8.6.2  christos 		/* Are any of these bits set now?  */
    211  1.8.6.2  christos 		if (word)
    212  1.8.6.2  christos 			goto out;
    213  1.8.6.2  christos 
    214  1.8.6.2  christos 		/* Move past it.  */
    215  1.8.6.2  christos 		p++;
    216  1.8.6.2  christos 		n--;
    217  1.8.6.2  christos 	}
    218  1.8.6.2  christos 
    219  1.8.6.2  christos 	/* Find the first word with any bits set.  */
    220  1.8.6.2  christos 	for (; n --> 0; p++) {
    221  1.8.6.2  christos 		/* Toggle the bits and convert to 64 bits for ffs64. */
    222  1.8.6.2  christos 		word = *p ^ toggle;
    223  1.8.6.2  christos 
    224  1.8.6.2  christos 		/* Are any of these bits set now?  */
    225  1.8.6.2  christos 		if (word)
    226  1.8.6.2  christos 			goto out;
    227  1.8.6.2  christos 	}
    228  1.8.6.2  christos 
    229  1.8.6.2  christos 	/* Nada.  */
    230  1.8.6.2  christos 	return nbits;
    231  1.8.6.2  christos 
    232  1.8.6.2  christos out:
    233  1.8.6.2  christos 	/* Count how many words we've skipped.  */
    234  1.8.6.2  christos 	result = bpl*(p - ptr);
    235  1.8.6.2  christos 
    236  1.8.6.2  christos 	/* Find the first set bit in this word, zero-based.  */
    237  1.8.6.2  christos 	result += ffs64(word) - 1;
    238  1.8.6.2  christos 
    239  1.8.6.2  christos 	/* We may have overshot, so clamp down to at most nbits.  */
    240  1.8.6.2  christos 	return MIN(result, nbits);
    241  1.8.6.2  christos }
    242  1.8.6.2  christos 
    243  1.8.6.2  christos static inline unsigned long
    244  1.8.6.2  christos find_next_bit(const unsigned long *ptr, unsigned long nbits,
    245  1.8.6.2  christos     unsigned long startbit)
    246  1.8.6.2  christos {
    247  1.8.6.2  christos 	return __find_next_bit(ptr, nbits, startbit, 0);
    248  1.8.6.2  christos }
    249  1.8.6.2  christos 
    250  1.8.6.2  christos static inline unsigned long
    251  1.8.6.2  christos find_first_bit(const unsigned long *ptr, unsigned long nbits)
    252  1.8.6.2  christos {
    253  1.8.6.2  christos 	return find_next_bit(ptr, nbits, 0);
    254  1.8.6.2  christos }
    255  1.8.6.2  christos 
    256  1.8.6.2  christos static inline unsigned long
    257  1.8.6.2  christos find_next_zero_bit(const unsigned long *ptr, unsigned long nbits,
    258  1.8.6.2  christos     unsigned long startbit)
    259  1.8.6.2  christos {
    260  1.8.6.2  christos 	return __find_next_bit(ptr, nbits, startbit, ~0UL);
    261  1.8.6.2  christos }
    262  1.8.6.2  christos 
    263  1.8.6.2  christos static inline unsigned long
    264  1.8.6.2  christos find_first_zero_bit(const unsigned long *ptr, unsigned long nbits)
    265  1.8.6.2  christos {
    266  1.8.6.2  christos 	return find_next_zero_bit(ptr, nbits, 0);
    267  1.8.6.2  christos }
    268  1.8.6.2  christos 
    269  1.8.6.2  christos #define	for_each_set_bit(BIT, PTR, NBITS)				      \
    270  1.8.6.2  christos 	for ((BIT) = find_first_bit((PTR), (NBITS));			      \
    271  1.8.6.2  christos 	     (BIT) < (NBITS);						      \
    272  1.8.6.2  christos 	     (BIT) = find_next_bit((PTR), (NBITS), (BIT) + 1))
    273  1.8.6.2  christos 
    274  1.8.6.2  christos #endif  /* _LINUX_BITOPS_H_ */
    275