xref: /freebsd/sys/dev/bwi/bitops.h (revision 2ff63af9b88c7413b7d71715b5532625752a248e)
1*718cf2ccSPedro F. Giffuni /*-
2*718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*718cf2ccSPedro F. Giffuni  *
412e36acbSWarner Losh  * Copyright (c) 2004, 2005 David Young.  All rights reserved.
512e36acbSWarner Losh  *
612e36acbSWarner Losh  * Programmed for NetBSD by David Young.
712e36acbSWarner Losh  *
812e36acbSWarner Losh  * Redistribution and use in source and binary forms, with or without
912e36acbSWarner Losh  * modification, are permitted provided that the following conditions
1012e36acbSWarner Losh  * are met:
1112e36acbSWarner Losh  * 1. Redistributions of source code must retain the above copyright
1212e36acbSWarner Losh  *    notice, this list of conditions and the following disclaimer.
1312e36acbSWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
1412e36acbSWarner Losh  *    notice, this list of conditions and the following disclaimer in the
1512e36acbSWarner Losh  *    documentation and/or other materials provided with the distribution.
1612e36acbSWarner Losh  * 3. The name of David Young may not be used to endorse or promote
1712e36acbSWarner Losh  *    products derived from this software without specific prior
1812e36acbSWarner Losh  *    written permission.
1912e36acbSWarner Losh  *
2012e36acbSWarner Losh  * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY
2112e36acbSWarner Losh  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
2212e36acbSWarner Losh  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
2312e36acbSWarner Losh  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL David
2412e36acbSWarner Losh  * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2512e36acbSWarner Losh  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
2612e36acbSWarner Losh  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2712e36acbSWarner Losh  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
2812e36acbSWarner Losh  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2912e36acbSWarner Losh  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3012e36acbSWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
3112e36acbSWarner Losh  * OF SUCH DAMAGE.
3212e36acbSWarner Losh  *
3312e36acbSWarner Losh  * $DragonFly: src/sys/dev/netif/bwi/bitops.h,v 1.1 2007/09/08 06:15:54 sephe Exp $
3412e36acbSWarner Losh  */
3512e36acbSWarner Losh 
3612e36acbSWarner Losh #ifndef _BITOPS_H
3712e36acbSWarner Losh #define _BITOPS_H
3812e36acbSWarner Losh 
3912e36acbSWarner Losh /*
4012e36acbSWarner Losh  * __BIT(n): Return a bitmask with bit m set, where the least
4112e36acbSWarner Losh  *           significant bit is bit 0.
4212e36acbSWarner Losh  *
4312e36acbSWarner Losh  * __BITS(m, n): Return a bitmask with bits m through n, inclusive,
4412e36acbSWarner Losh  *               set.  It does not matter whether m>n or m<=n.  The
4512e36acbSWarner Losh  *               least significant bit is bit 0.
4612e36acbSWarner Losh  *
4712e36acbSWarner Losh  * A "bitfield" is a span of consecutive bits defined by a bitmask,
4812e36acbSWarner Losh  * where 1s select the bits in the bitfield.  __SHIFTIN, __SHIFTOUT,
4912e36acbSWarner Losh  * and SHIFTOUT_MASK help read and write bitfields from device registers.
5012e36acbSWarner Losh  *
5112e36acbSWarner Losh  * __SHIFTIN(v, mask): Left-shift bits `v' into the bitfield
5212e36acbSWarner Losh  *                     defined by `mask', and return them.  No
5312e36acbSWarner Losh  *                     side-effects.
5412e36acbSWarner Losh  *
5512e36acbSWarner Losh  * __SHIFTOUT(v, mask): Extract and return the bitfield selected
5612e36acbSWarner Losh  *                      by `mask' from `v', right-shifting the
5712e36acbSWarner Losh  *                      bits so that the rightmost selected bit
5812e36acbSWarner Losh  *                      is at bit 0.  No side-effects.
5912e36acbSWarner Losh  *
6012e36acbSWarner Losh  * __SHIFTOUT_MASK(mask): Right-shift the bits in `mask' so that
6112e36acbSWarner Losh  *                        the rightmost non-zero bit is at bit
6212e36acbSWarner Losh  *                        0.  This is useful for finding the
6312e36acbSWarner Losh  *                        greatest unsigned value that a bitfield
6412e36acbSWarner Losh  *                        can hold.  No side-effects.  Note that
6512e36acbSWarner Losh  *                        SHIFTOUT_MASK(m) = SHIFTOUT(m, m).
6612e36acbSWarner Losh  */
6712e36acbSWarner Losh 
6812e36acbSWarner Losh /* __BIT(n): nth bit, where __BIT(0) == 0x1. */
6912e36acbSWarner Losh #define	__BIT(__n) (((__n) == 32) ? 0 : ((uint32_t)1 << (__n)))
7012e36acbSWarner Losh 
7112e36acbSWarner Losh /* __BITS(m, n): bits m through n, m < n. */
7212e36acbSWarner Losh #define	__BITS(__m, __n)	\
7312e36acbSWarner Losh 	((__BIT(MAX((__m), (__n)) + 1) - 1) ^ (__BIT(MIN((__m), (__n))) - 1))
7412e36acbSWarner Losh 
7512e36acbSWarner Losh /* Find least significant bit that is set */
7612e36acbSWarner Losh #define	__LOWEST_SET_BIT(__mask) ((((__mask) - 1) & (__mask)) ^ (__mask))
7712e36acbSWarner Losh 
7812e36acbSWarner Losh #define	__SHIFTOUT(__x, __mask) (((__x) & (__mask)) / __LOWEST_SET_BIT(__mask))
7912e36acbSWarner Losh #define	__SHIFTIN(__x, __mask) ((__x) * __LOWEST_SET_BIT(__mask))
8012e36acbSWarner Losh #define	__SHIFTOUT_MASK(__mask) __SHIFTOUT((__mask), (__mask))
8112e36acbSWarner Losh 
8212e36acbSWarner Losh #endif	/* !_BITOPS_H */
83