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