1.\" $NetBSD: bits.3,v 1.7 2010/03/22 12:39:22 jruoho 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.\" 15.\" THIS SOFTWARE IS PROVIDED BY DAVID YOUNG ``AS IS'' AND ANY 16.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 17.\" THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 18.\" PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID 19.\" YOUNG BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 21.\" TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23.\" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26.\" POSSIBILITY OF SUCH DAMAGE. 27.\" 28.Dd July 9, 2006 29.Dt BITS 3 30.Os 31.Sh NAME 32.Nm __BIT , 33.Nm __BITS , 34.Nm __SHIFTIN , 35.Nm __SHIFTOUT , 36.Nm __SHIFTOUT_MASK 37.Nd "macros for preparing bitmasks and operating on bit fields" 38.Sh SYNOPSIS 39.In sys/cdefs.h 40.Ft uint32_t 41.Fn __BIT "n" 42.Ft uint32_t 43.Fn __BITS "m" "n" 44.Fn __SHIFTIN "v" "mask" 45.Fn __SHIFTOUT "v" "mask" 46.Fn __SHIFTOUT_MASK "mask" 47.Sh DESCRIPTION 48These macros prepare bitmasks, extract bitfields from words, and 49insert bitfields into words. 50A 51.Dq bitfield 52is a span of consecutive bits defined by a bitmask, where 1s select 53the bits in the bitfield. 54.Pp 55Use 56.Fn __BIT 57and 58.Fn __BITS 59to define bitmasks: 60.Pp 61.Bl -tag -width __BITS -offset indent 62.It Fn __BIT "n" 63Return a bitmask with bit m set, where the least significant bit is bit 0. 64.It Fn __BITS "m" "n" 65Return a bitmask with bits 66.Fa m 67through 68.Fa n , 69inclusive, set. 70It does not matter whether 71.Fa m No \*[Gt] Fa n 72or 73.Fa m No \*[Lt]= Fa n . 74The least significant bit is bit 0. 75.El 76.Pp 77.Fn __SHIFTIN , 78.Fn __SHIFTOUT , 79and 80.Fn __SHIFTOUT_MASK 81help read and write bitfields from words: 82.Pp 83.Bl -tag -width __SHIFTOUT_MASK -offset indent 84.It Fn __SHIFTIN "v" "mask" 85Left-shift bits 86.Fa v 87into the bitfield defined by 88.Fa mask , 89and return them. 90No side-effects. 91.It Fn __SHIFTOUT "v" "mask" 92Extract and return the bitfield selected by 93.Fa mask 94from 95.Fa v , 96right-shifting the bits so that the rightmost selected bit is at 97bit 0. 98No side-effects. 99.It Fn __SHIFTOUT_MASK "mask" 100Right-shift the bits in 101.Fa mask 102so that the rightmost non-zero bit is at bit 0. 103This is useful for finding the greatest unsigned value that a 104bitfield can hold. 105No side-effects. 106Note that 107.Fn __SHIFTOUT_MASK "m" 108= 109.Fn __SHIFTOUT "m" "m" . 110.El 111.Sh EXAMPLES 112.Bd -literal 113/* 114 * Register definitions taken from the RFMD RF3000 manual. 115 */ 116#define RF3000_GAINCTL 0x11 117#define RF3000_GAINCTL_TXVGC_MASK __BITS(7, 2) 118#define RF3000_GAINCTL_SCRAMBLER __BIT(1) 119 120/* 121 * Shift the transmit power into the transmit-power field of the 122 * gain-control register and write it to the baseband processor. 123 */ 124atw_rf3000_write(sc, RF3000_GAINCTL, 125 __SHIFTIN(txpower, RF3000_GAINCTL_TXVGC_MASK)); 126 127/* 128 * Register definitions taken from the ADMtek ADM8211 manual. 129 */ 130#define ATW_RXSTAT_OWN __BIT(31) 131 132/* ... */ 133 134#define ATW_RXSTAT_DA1 __BIT(17) 135#define ATW_RXSTAT_DA0 __BIT(16) 136#define ATW_RXSTAT_RXDR_MASK __BITS(15,12) 137#define ATW_RXSTAT_FL_MASK __BITS(11,0) 138 139/* 140 * Extract the frame length from the Rx descriptor's status field. 141 */ 142len = __SHIFTOUT(rxstat, ATW_RXSTAT_FL_MASK); 143.Ed 144.Sh HISTORY 145The 146.Nm bits 147macros first appeared in 148.Xr atw 4 , 149with different names and implementation. 150In their current form these macros appeared 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