1.\" $NetBSD: bits.3,v 1.2 2006/09/03 00:04:02 wiz Exp $ 2.\" 3.\" Copyright (c) 2006 David Young. All rights reserved. 4.\" 5.\" Redistribution and use in source and binary forms, with or 6.\" without modification, are permitted provided that the following 7.\" conditions are met: 8.\" 1. Redistributions of source code must retain the above copyright 9.\" notice, this list of conditions and the following disclaimer. 10.\" 2. Redistributions in binary form must reproduce the above 11.\" copyright notice, this list of conditions and the following 12.\" disclaimer in the documentation and/or other materials 13.\" provided with the distribution. 14.\" 3. The name of David Young may not be used to endorse or promote 15.\" products derived from this software without specific prior 16.\" written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY DAVID YOUNG ``AS IS'' AND ANY 19.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 20.\" THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 21.\" PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID 22.\" YOUNG BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 23.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 24.\" TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 26.\" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29.\" POSSIBILITY OF SUCH DAMAGE. 30.\" 31.Dd July 9, 2006 32.Dt BITS 3 33.Os 34.Sh NAME 35.Nm __BIT , 36.Nm __BITS , 37.Nm __SHIFTIN , 38.Nm __SHIFTOUT , 39.Nm __SHIFTOUT_MASK 40.Nd "macros for preparing bitmasks and operating on bit fields" 41.Sh SYNOPSIS 42.In sys/cdefs.h 43.Ft uint32_t 44.Fn __BIT "n" 45.Ft uint32_t 46.Fn __BITS "m" "n" 47.Fn __SHIFTIN "v" "mask" 48.Fn __SHIFTOUT "v" "mask" 49.Fn __SHIFTOUT_MASK "mask" 50.Sh DESCRIPTION 51These macros prepare bitmasks, extract bitfields from words, and 52insert bitfields into words. 53A 54.Dq bitfield 55is a span of consecutive bits defined by a bitmask, where 1s select 56the bits in the bitfield. 57.Pp 58Use __BIT and __BITS to define bitmasks: 59.Pp 60.Bl -tag -width __BITS -offset indent 61.It Fn __BIT "n" 62Return a bitmask with bit m set, where the least significant bit is bit 0. 63.It Fn __BITS "m" "n" 64Return a bitmask with bits 65.Fa m 66through 67.Fa n , 68inclusive, set. 69It does not matter whether 70.Fa m No \*[Gt] Fa n 71or 72.Fa m No \*[Lt]= Fa n . 73The least significant bit is bit 0. 74.El 75.Pp 76.Fn __SHIFTIN , 77.Fn __SHIFTOUT , 78and 79.Fn __SHIFTOUT_MASK 80help read and write bitfields from words: 81.Pp 82.Bl -tag -width __SHIFTOUT_MASK -offset indent 83.It Fn __SHIFTIN "v" "mask" 84Left-shift bits 85.Fa v 86into the bitfield defined by 87.Fa mask , 88and return them. 89No side-effects. 90.It Fn __SHIFTOUT "v" "mask" 91Extract and return the bitfield selected by 92.Fa mask 93from 94.Fa v , 95right-shifting the bits so that the rightmost selected bit is at 96bit 0. 97No side-effects. 98.It Fn __SHIFTOUT_MASK "mask" 99Right-shift the bits in 100.Fa mask 101so that the rightmost non-zero bit is at bit 0. 102This is useful for finding the greatest unsigned value that a 103bitfield can hold. 104No side-effects. 105Note that 106.Fn __SHIFTOUT_MASK "m" = 107.Fn __SHIFTOUT "m" "m" . 108.El 109.Sh EXAMPLES 110.Bd -literal 111/* 112 * Register definitions taken from the RFMD RF3000 manual. 113 */ 114#define RF3000_GAINCTL 0x11 /* TX variable gain control */ 115#define RF3000_GAINCTL_TXVGC_MASK __BITS(7, 2) 116#define RF3000_GAINCTL_SCRAMBLER __BIT(1) 117 118/* 119 * Shift the transmit power into the transmit-power field of the 120 * gain-control register and write it to the baseband processor. 121 */ 122atw_rf3000_write(sc, RF3000_GAINCTL, 123 __SHIFTIN(txpower, RF3000_GAINCTL_TXVGC_MASK)); 124 125/* 126 * Register definitions taken from the ADMtek ADM8211 manual. 127 * 128 */ 129#define ATW_RXSTAT_OWN __BIT(31) /* 1: NIC may fill descriptor */ 130/* ... */ 131#define ATW_RXSTAT_DA1 __BIT(17) /* DA bit 1, admin'd address */ 132#define ATW_RXSTAT_DA0 __BIT(16) /* DA bit 0, group address */ 133#define ATW_RXSTAT_RXDR_MASK __BITS(15,12) /* RX data rate */ 134#define ATW_RXSTAT_FL_MASK __BITS(11,0) /* RX frame length, last 135 * descriptor only 136 */ 137 138/* Extract the frame length from the Rx descriptor's 139 * status field. 140 */ 141len = __SHIFTOUT(rxstat, ATW_RXSTAT_FL_MASK); 142.Ed 143.Sh HISTORY 144The 145.Nm bits 146macros first appeared in 147.Xr atw 4 , 148with different names and implementation. 149.Nm bits 150macros appeared with their current names and implementation in 151.Nx 4.0 . 152.Sh AUTHORS 153The 154.Nm bits 155macros were written by 156.An David Young Aq dyoung@NetBSD.org . 157.An Matt Thomas Aq matt@NetBSD.org 158suggested important improvements to the implementation, and 159contributed the macro names 160.Fn SHIFTIN 161and 162.Fn SHIFTOUT . 163.Sh BUGS 164.Fn __BIT 165and 166.Fn __BITS 167can only express 32-bit bitmasks. 168