/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 2004, 2005 David Young. All rights reserved.4*5* Programmed for NetBSD by David Young.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15* 3. The name of David Young may not be used to endorse or promote16* products derived from this software without specific prior17* written permission.18*19* THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY20* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A22* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David23* Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED25* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,26* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND27* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,28* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY30* OF SUCH DAMAGE.31*32* $DragonFly: src/sys/dev/netif/bwi/bitops.h,v 1.1 2007/09/08 06:15:54 sephe Exp $33*/3435#ifndef _BITOPS_H36#define _BITOPS_H3738/*39* __BIT(n): Return a bitmask with bit m set, where the least40* significant bit is bit 0.41*42* __BITS(m, n): Return a bitmask with bits m through n, inclusive,43* set. It does not matter whether m>n or m<=n. The44* least significant bit is bit 0.45*46* A "bitfield" is a span of consecutive bits defined by a bitmask,47* where 1s select the bits in the bitfield. __SHIFTIN, __SHIFTOUT,48* and SHIFTOUT_MASK help read and write bitfields from device registers.49*50* __SHIFTIN(v, mask): Left-shift bits `v' into the bitfield51* defined by `mask', and return them. No52* side-effects.53*54* __SHIFTOUT(v, mask): Extract and return the bitfield selected55* by `mask' from `v', right-shifting the56* bits so that the rightmost selected bit57* is at bit 0. No side-effects.58*59* __SHIFTOUT_MASK(mask): Right-shift the bits in `mask' so that60* the rightmost non-zero bit is at bit61* 0. This is useful for finding the62* greatest unsigned value that a bitfield63* can hold. No side-effects. Note that64* SHIFTOUT_MASK(m) = SHIFTOUT(m, m).65*/6667/* __BIT(n): nth bit, where __BIT(0) == 0x1. */68#define __BIT(__n) (((__n) == 32) ? 0 : ((uint32_t)1 << (__n)))6970/* __BITS(m, n): bits m through n, m < n. */71#define __BITS(__m, __n) \72((__BIT(MAX((__m), (__n)) + 1) - 1) ^ (__BIT(MIN((__m), (__n))) - 1))7374/* Find least significant bit that is set */75#define __LOWEST_SET_BIT(__mask) ((((__mask) - 1) & (__mask)) ^ (__mask))7677#define __SHIFTOUT(__x, __mask) (((__x) & (__mask)) / __LOWEST_SET_BIT(__mask))78#define __SHIFTIN(__x, __mask) ((__x) * __LOWEST_SET_BIT(__mask))79#define __SHIFTOUT_MASK(__mask) __SHIFTOUT((__mask), (__mask))8081#endif /* !_BITOPS_H */828384