1*a5d6f6fdSwiz.\" $NetBSD: bits.3,v 1.22 2022/01/22 09:22:41 wiz Exp $ 28cd106d3Sdyoung.\" 399289f93Sdyoung.\" Copyright (c) 2006, 2010 David Young. All rights reserved. 48cd106d3Sdyoung.\" 58cd106d3Sdyoung.\" Redistribution and use in source and binary forms, with or 68cd106d3Sdyoung.\" without modification, are permitted provided that the following 78cd106d3Sdyoung.\" conditions are met: 88cd106d3Sdyoung.\" 1. Redistributions of source code must retain the above copyright 98cd106d3Sdyoung.\" notice, this list of conditions and the following disclaimer. 108cd106d3Sdyoung.\" 2. Redistributions in binary form must reproduce the above 118cd106d3Sdyoung.\" copyright notice, this list of conditions and the following 128cd106d3Sdyoung.\" disclaimer in the documentation and/or other materials 138cd106d3Sdyoung.\" provided with the distribution. 148cd106d3Sdyoung.\" 158cd106d3Sdyoung.\" THIS SOFTWARE IS PROVIDED BY DAVID YOUNG ``AS IS'' AND ANY 168cd106d3Sdyoung.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 178cd106d3Sdyoung.\" THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 188cd106d3Sdyoung.\" PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID 198cd106d3Sdyoung.\" YOUNG BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 208cd106d3Sdyoung.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 218cd106d3Sdyoung.\" TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 228cd106d3Sdyoung.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 238cd106d3Sdyoung.\" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 248cd106d3Sdyoung.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 258cd106d3Sdyoung.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 268cd106d3Sdyoung.\" POSSIBILITY OF SUCH DAMAGE. 278cd106d3Sdyoung.\" 28a8418b3eSskrll.Dd January 22, 2022 298cd106d3Sdyoung.Dt BITS 3 308cd106d3Sdyoung.Os 318cd106d3Sdyoung.Sh NAME 328cd106d3Sdyoung.Nm __BIT , 338cd106d3Sdyoung.Nm __BITS , 34a8418b3eSskrll.Nm __MASK , 358cd106d3Sdyoung.Nm __SHIFTIN , 368cd106d3Sdyoung.Nm __SHIFTOUT , 378cd106d3Sdyoung.Nm __SHIFTOUT_MASK 388cd106d3Sdyoung.Nd "macros for preparing bitmasks and operating on bit fields" 398cd106d3Sdyoung.Sh SYNOPSIS 4099289f93Sdyoung.In sys/param.h 418cd106d3Sdyoung.In sys/cdefs.h 4231b71188Smsaitoh.Ft uintmax_t 438cd106d3Sdyoung.Fn __BIT "n" 4431b71188Smsaitoh.Ft uintmax_t 458cd106d3Sdyoung.Fn __BITS "m" "n" 46fa1e00efSskrll.Ft uintmax_t 47a8418b3eSskrll.Fn __MASK "n" 48a8418b3eSskrll.Ft uintmax_t 498cd106d3Sdyoung.Fn __SHIFTIN "v" "mask" 50fa1e00efSskrll.Ft uintmax_t 518cd106d3Sdyoung.Fn __SHIFTOUT "v" "mask" 52fa1e00efSskrll.Ft uintmax_t 538cd106d3Sdyoung.Fn __SHIFTOUT_MASK "mask" 548cd106d3Sdyoung.Sh DESCRIPTION 558cd106d3SdyoungThese macros prepare bitmasks, extract bitfields from words, and 568cd106d3Sdyounginsert bitfields into words. 578cd106d3SdyoungA 588cd106d3Sdyoung.Dq bitfield 598cd106d3Sdyoungis a span of consecutive bits defined by a bitmask, where 1s select 608cd106d3Sdyoungthe bits in the bitfield. 618cd106d3Sdyoung.Pp 626cf60012SjruohoUse 63a8418b3eSskrll.Fn __BIT , 64a8418b3eSskrll.Fn __BITS , 656cf60012Sjruohoand 66a8418b3eSskrll.Fn __MASK 676cf60012Sjruohoto define bitmasks: 688cd106d3Sdyoung.Bl -tag -width __BITS -offset indent 698cd106d3Sdyoung.It Fn __BIT "n" 708e2b504dSjymReturn a bitmask with bit 718e2b504dSjym.Fa n 728e2b504dSjymset, where the least significant bit is bit 0. 738cd106d3Sdyoung.It Fn __BITS "m" "n" 748cd106d3SdyoungReturn a bitmask with bits 758cd106d3Sdyoung.Fa m 768cd106d3Sdyoungthrough 778cd106d3Sdyoung.Fa n , 788cd106d3Sdyounginclusive, set. 798cd106d3SdyoungIt does not matter whether 8001869ca4Swiz.Fa m No > Fa n 816fb7165cSwizor 8201869ca4Swiz.Fa m No <= Fa n . 838cd106d3SdyoungThe least significant bit is bit 0. 84a8418b3eSskrll.It Fn __MASK "n" 85a8418b3eSskrllReturn a bitmask with the first 86a8418b3eSskrll.Fa n 87*a5d6f6fdSwizbits set. 88*a5d6f6fdSwizThat is, bits 0 through 89a8418b3eSskrll.Fa n 90a8418b3eSskrll- 1, inclusive, set. 918cd106d3Sdyoung.El 928cd106d3Sdyoung.Pp 938cd106d3Sdyoung.Fn __SHIFTIN , 946fb7165cSwiz.Fn __SHIFTOUT , 956fb7165cSwizand 968cd106d3Sdyoung.Fn __SHIFTOUT_MASK 978cd106d3Sdyounghelp read and write bitfields from words: 988cd106d3Sdyoung.Bl -tag -width __SHIFTOUT_MASK -offset indent 998cd106d3Sdyoung.It Fn __SHIFTIN "v" "mask" 1008cd106d3SdyoungLeft-shift bits 1018cd106d3Sdyoung.Fa v 1028cd106d3Sdyounginto the bitfield defined by 1038cd106d3Sdyoung.Fa mask , 1048cd106d3Sdyoungand return them. 1058cd106d3SdyoungNo side-effects. 1068cd106d3Sdyoung.It Fn __SHIFTOUT "v" "mask" 1078cd106d3SdyoungExtract and return the bitfield selected by 1088cd106d3Sdyoung.Fa mask 1098cd106d3Sdyoungfrom 1108cd106d3Sdyoung.Fa v , 1118cd106d3Sdyoungright-shifting the bits so that the rightmost selected bit is at 1128cd106d3Sdyoungbit 0. 1138cd106d3SdyoungNo side-effects. 1148cd106d3Sdyoung.It Fn __SHIFTOUT_MASK "mask" 1158cd106d3SdyoungRight-shift the bits in 1168cd106d3Sdyoung.Fa mask 1178cd106d3Sdyoungso that the rightmost non-zero bit is at bit 0. 1188cd106d3SdyoungThis is useful for finding the greatest unsigned value that a 1198cd106d3Sdyoungbitfield can hold. 1208cd106d3SdyoungNo side-effects. 1218cd106d3SdyoungNote that 122054e1e75Sjnemeth.Fn __SHIFTOUT_MASK "m" 123054e1e75Sjnemeth= 1248cd106d3Sdyoung.Fn __SHIFTOUT "m" "m" . 1258cd106d3Sdyoung.El 1268cd106d3Sdyoung.Sh EXAMPLES 1274328a42aSjruohoThe following example demonstrates basic usage of the 1284328a42aSjruoho.Nm bits 1294328a42aSjruohomacros: 1304328a42aSjruoho.Bd -literal -offset indent 1314328a42aSjruohouint32_t bits, mask, val; 1328cd106d3Sdyoung 1334328a42aSjruohobits = __BITS(2, 3); /* 00001100 */ 1344328a42aSjruohomask = __BIT(2) | __BIT(3); /* 00001100 */ 1358cd106d3Sdyoung 1364328a42aSjruohoval = __SHIFTIN(0x03, mask); /* 00001100 */ 1374328a42aSjruohoval = __SHIFTOUT(0xf, mask); /* 00000011 */ 1388cd106d3Sdyoung.Ed 139ec9cee68Sjruoho.Sh SEE ALSO 140e5c4609fSjruoho.Xr bitops 3 , 141ec9cee68Sjruoho.Xr cdefs 3 1428cd106d3Sdyoung.Sh HISTORY 1438cd106d3SdyoungThe 1448cd106d3Sdyoung.Nm bits 1458cd106d3Sdyoungmacros first appeared in 1466fb7165cSwiz.Xr atw 4 , 1478cd106d3Sdyoungwith different names and implementation. 148dd1725f2SjruohoIn their current form these macros appeared in 1498cd106d3Sdyoung.Nx 4.0 . 1508cd106d3Sdyoung.Sh AUTHORS 1518cd106d3SdyoungThe 1528cd106d3Sdyoung.Nm bits 1538cd106d3Sdyoungmacros were written by 154a5684d07Swiz.An David Young Aq Mt dyoung@NetBSD.org . 155a5684d07Swiz.An Matt Thomas Aq Mt matt@NetBSD.org 1568cd106d3Sdyoungsuggested important improvements to the implementation, and 1578cd106d3Sdyoungcontributed the macro names 1588cd106d3Sdyoung.Fn SHIFTIN 1598cd106d3Sdyoungand 1608cd106d3Sdyoung.Fn SHIFTOUT . 161