1191762Simp/* 2191762Simp * Copyright (c) 2004, 2005 David Young. All rights reserved. 3191762Simp * 4191762Simp * Programmed for NetBSD by David Young. 5191762Simp * 6191762Simp * Redistribution and use in source and binary forms, with or without 7191762Simp * modification, are permitted provided that the following conditions 8191762Simp * are met: 9191762Simp * 1. Redistributions of source code must retain the above copyright 10191762Simp * notice, this list of conditions and the following disclaimer. 11191762Simp * 2. Redistributions in binary form must reproduce the above copyright 12191762Simp * notice, this list of conditions and the following disclaimer in the 13191762Simp * documentation and/or other materials provided with the distribution. 14191762Simp * 3. The name of David Young may not be used to endorse or promote 15191762Simp * products derived from this software without specific prior 16191762Simp * written permission. 17191762Simp * 18191762Simp * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY 19191762Simp * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 20191762Simp * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 21191762Simp * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David 22191762Simp * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 23191762Simp * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 24191762Simp * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25191762Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 26191762Simp * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27191762Simp * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28191762Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 29191762Simp * OF SUCH DAMAGE. 30191762Simp * 31191762Simp * $DragonFly: src/sys/dev/netif/bwi/bitops.h,v 1.1 2007/09/08 06:15:54 sephe Exp $ 32191762Simp * $FreeBSD$ 33191762Simp */ 34191762Simp 35191762Simp#ifndef _BITOPS_H 36191762Simp#define _BITOPS_H 37191762Simp 38191762Simp/* 39191762Simp * __BIT(n): Return a bitmask with bit m set, where the least 40191762Simp * significant bit is bit 0. 41191762Simp * 42191762Simp * __BITS(m, n): Return a bitmask with bits m through n, inclusive, 43191762Simp * set. It does not matter whether m>n or m<=n. The 44191762Simp * least significant bit is bit 0. 45191762Simp * 46191762Simp * A "bitfield" is a span of consecutive bits defined by a bitmask, 47191762Simp * where 1s select the bits in the bitfield. __SHIFTIN, __SHIFTOUT, 48191762Simp * and SHIFTOUT_MASK help read and write bitfields from device registers. 49191762Simp * 50191762Simp * __SHIFTIN(v, mask): Left-shift bits `v' into the bitfield 51191762Simp * defined by `mask', and return them. No 52191762Simp * side-effects. 53191762Simp * 54191762Simp * __SHIFTOUT(v, mask): Extract and return the bitfield selected 55191762Simp * by `mask' from `v', right-shifting the 56191762Simp * bits so that the rightmost selected bit 57191762Simp * is at bit 0. No side-effects. 58191762Simp * 59191762Simp * __SHIFTOUT_MASK(mask): Right-shift the bits in `mask' so that 60191762Simp * the rightmost non-zero bit is at bit 61191762Simp * 0. This is useful for finding the 62191762Simp * greatest unsigned value that a bitfield 63191762Simp * can hold. No side-effects. Note that 64191762Simp * SHIFTOUT_MASK(m) = SHIFTOUT(m, m). 65191762Simp */ 66191762Simp 67191762Simp/* __BIT(n): nth bit, where __BIT(0) == 0x1. */ 68191762Simp#define __BIT(__n) (((__n) == 32) ? 0 : ((uint32_t)1 << (__n))) 69191762Simp 70191762Simp/* __BITS(m, n): bits m through n, m < n. */ 71191762Simp#define __BITS(__m, __n) \ 72191762Simp ((__BIT(MAX((__m), (__n)) + 1) - 1) ^ (__BIT(MIN((__m), (__n))) - 1)) 73191762Simp 74191762Simp/* Find least significant bit that is set */ 75191762Simp#define __LOWEST_SET_BIT(__mask) ((((__mask) - 1) & (__mask)) ^ (__mask)) 76191762Simp 77191762Simp#define __SHIFTOUT(__x, __mask) (((__x) & (__mask)) / __LOWEST_SET_BIT(__mask)) 78191762Simp#define __SHIFTIN(__x, __mask) ((__x) * __LOWEST_SET_BIT(__mask)) 79191762Simp#define __SHIFTOUT_MASK(__mask) __SHIFTOUT((__mask), (__mask)) 80191762Simp 81191762Simp#endif /* !_BITOPS_H */ 82