xref: /freebsd/sys/dev/bwi/bitops.h (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2004, 2005 David Young.  All rights reserved.
5  *
6  * Programmed for NetBSD by David Young.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of David Young may not be used to endorse or promote
17  *    products derived from this software without specific prior
18  *    written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY
21  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL David
24  * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31  * OF SUCH DAMAGE.
32  *
33  * $DragonFly: src/sys/dev/netif/bwi/bitops.h,v 1.1 2007/09/08 06:15:54 sephe Exp $
34  * $FreeBSD$
35  */
36 
37 #ifndef _BITOPS_H
38 #define _BITOPS_H
39 
40 /*
41  * __BIT(n): Return a bitmask with bit m set, where the least
42  *           significant bit is bit 0.
43  *
44  * __BITS(m, n): Return a bitmask with bits m through n, inclusive,
45  *               set.  It does not matter whether m>n or m<=n.  The
46  *               least significant bit is bit 0.
47  *
48  * A "bitfield" is a span of consecutive bits defined by a bitmask,
49  * where 1s select the bits in the bitfield.  __SHIFTIN, __SHIFTOUT,
50  * and SHIFTOUT_MASK help read and write bitfields from device registers.
51  *
52  * __SHIFTIN(v, mask): Left-shift bits `v' into the bitfield
53  *                     defined by `mask', and return them.  No
54  *                     side-effects.
55  *
56  * __SHIFTOUT(v, mask): Extract and return the bitfield selected
57  *                      by `mask' from `v', right-shifting the
58  *                      bits so that the rightmost selected bit
59  *                      is at bit 0.  No side-effects.
60  *
61  * __SHIFTOUT_MASK(mask): Right-shift the bits in `mask' so that
62  *                        the rightmost non-zero bit is at bit
63  *                        0.  This is useful for finding the
64  *                        greatest unsigned value that a bitfield
65  *                        can hold.  No side-effects.  Note that
66  *                        SHIFTOUT_MASK(m) = SHIFTOUT(m, m).
67  */
68 
69 /* __BIT(n): nth bit, where __BIT(0) == 0x1. */
70 #define	__BIT(__n) (((__n) == 32) ? 0 : ((uint32_t)1 << (__n)))
71 
72 /* __BITS(m, n): bits m through n, m < n. */
73 #define	__BITS(__m, __n)	\
74 	((__BIT(MAX((__m), (__n)) + 1) - 1) ^ (__BIT(MIN((__m), (__n))) - 1))
75 
76 /* Find least significant bit that is set */
77 #define	__LOWEST_SET_BIT(__mask) ((((__mask) - 1) & (__mask)) ^ (__mask))
78 
79 #define	__SHIFTOUT(__x, __mask) (((__x) & (__mask)) / __LOWEST_SET_BIT(__mask))
80 #define	__SHIFTIN(__x, __mask) ((__x) * __LOWEST_SET_BIT(__mask))
81 #define	__SHIFTOUT_MASK(__mask) __SHIFTOUT((__mask), (__mask))
82 
83 #endif	/* !_BITOPS_H */
84