\documentclass[10pt,a4paper,english]{article}
\usepackage{babel}
\usepackage{ae}
\usepackage{aeguill}
\usepackage{shortvrb}
\usepackage[latin1]{inputenc}
\usepackage{tabularx}
\usepackage{longtable}
\setlength{\extrarowheight}{2pt}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{color}
\usepackage{multirow}
\usepackage{ifthen}
\usepackage[colorlinks=true,linkcolor=blue,urlcolor=blue]{hyperref}
\usepackage[DIV12]{typearea}
%% generator Docutils: http://docutils.sourceforge.net/
\newlength{\admonitionwidth}
\setlength{\admonitionwidth}{0.9\textwidth}
\newlength{\docinfowidth}
\setlength{\docinfowidth}{0.9\textwidth}
\newlength{\locallinewidth}
\newcommand{\optionlistlabel}[1]{\bf #1 \hfill}
\newenvironment{optionlist}[1]
{\begin{list}{}
  {\setlength{\labelwidth}{#1}
   \setlength{\rightmargin}{1cm}
   \setlength{\leftmargin}{\rightmargin}
   \addtolength{\leftmargin}{\labelwidth}
   \addtolength{\leftmargin}{\labelsep}
   \renewcommand{\makelabel}{\optionlistlabel}}
}{\end{list}}
\newlength{\lineblockindentation}
\setlength{\lineblockindentation}{2.5em}
\newenvironment{lineblock}[1]
{\begin{list}{}
  {\setlength{\partopsep}{\parskip}
   \addtolength{\partopsep}{\baselineskip}
   \topsep0pt\itemsep0.15\baselineskip\parsep0pt
   \leftmargin#1}
 \raggedright}
{\end{list}}
% begin: floats for footnotes tweaking.
\setlength{\floatsep}{0.5em}
\setlength{\textfloatsep}{\fill}
\addtolength{\textfloatsep}{3em}
\renewcommand{\textfraction}{0.5}
\renewcommand{\topfraction}{0.5}
\renewcommand{\bottomfraction}{0.5}
\setcounter{totalnumber}{50}
\setcounter{topnumber}{50}
\setcounter{bottomnumber}{50}
% end floats for footnotes
% some commands, that could be overwritten in the style file.
\newcommand{\rubric}[1]{\subsection*{~\hfill {\it #1} \hfill ~}}
\newcommand{\titlereference}[1]{\textsl{#1}}
% end of "some commands"
\title{Multi-word Bitmask Library}
\author{}
\date{}
\hypersetup{
pdftitle={Multi-word Bitmask Library}
}
\raggedbottom
\begin{document}
\maketitle


\setlength{\locallinewidth}{\linewidth}
% rst3: filename: libbitmask.tex 
\pagenumbering{roman}\large\parindent0pt\parskip10pt
This Bitmask library supports multi-word bitmask operations for applications
programmed in 'C'. It works in conjunction with recent Linux kernel support for
processor and memory placement on multiprocessor SMP and NUMA systems. The
\texttt{cpuset} library, being developed in parallel, depends on this bitmask
library.
\begin{quote}
\begin{description}
\item [Author:]
Paul Jackson


\item [Address:]
\href{mailto:pj@sgi.com}{pj@sgi.com}


\item [Date:]
23 Sept 2005


\item [Copyright:]
Copyright (c) 2004-2006 Silicon Graphics, Inc.  All rights reserved.


\end{description}
\end{quote}
\newpage\small\medskip
This document is written using the outline processor \href{http://webpages.charter.net/edreamleo/front.html}{Leo}, and version controlled
using \href{http://cssc.sourceforge.net/index.shtml}{CSSC}. It is rendered using \href{http://docutils.sourceforge.net}{Python Docutils} on \href{http://docutils.sourceforge.net/docs/rst/quickstart.html}{reStructuredText}
extracted from \href{http://webpages.charter.net/edreamleo/front.html}{Leo}, directly into both \href{http://www.w3.org/MarkUp}{html} and \href{http://www.latex-project.org}{LaTeX}.  The \href{http://www.latex-project.org}{LaTeX} is
converted into \href{http://www.adobe.com/products/acrobat}{pdf} using the \href{http://www.tug.org/applications/pdftex}{pdflatex} utility.  The html is converted into
plain text using the \href{http://lynx.isc.org}{lynx} utility.
\newpage\large\vspace*{1cm}\hypertarget{table-of-contents}{}
\pdfbookmark[0]{Table of Contents}{table-of-contents}
\subsubsection*{~\hfill Table of Contents\hfill ~}
\begin{list}{}{}
\item {} \href{\#what-are-bitmasks}{1~~~What are bitmasks?}

\item {} \href{\#ascii-string-representations}{2~~~Ascii string representations}

\item {} \href{\#calling-return-and-error-conventions}{3~~~Calling, return and error conventions}

\item {} \href{\#other-bits-always-zero}{4~~~Other bits always zero}

\item {} \href{\#internal-binary-representation}{5~~~Internal binary representation}

\item {} \href{\#comparing-these-bitmasks-with-the-linux-kernel}{6~~~Comparing these bitmasks with the Linux kernel}

\item {} \href{\#bitmask-library-functions}{7~~~Bitmask Library Functions}

\end{list}

\newpage \pagenumbering{arabic} \normalsize

%___________________________________________________________________________

\hypertarget{what-are-bitmasks}{}
\pdfbookmark[0]{1~~~What are bitmasks?}{what-are-bitmasks}
\section*{1~~~What are bitmasks?}

Bitmasks provide multi-word bit masks and operations thereon to do
such things as set and clear bits, intersect and union masks, query bits, and
display and parse masks.

The initial intended use for these bitmasks is to represent sets of
CPUs and Memory Nodes, when configuring large SMP and NUMA systems.
However there is little in the semantics of bitmasks that is specific
to this particular use, and bitmasks should be usable for
other purposes that had similar design requirements.

These bitmasks share the same underlying layout as the bitmasks used
by the Linux kernel to represent sets of CPUs and Memory Nodes. Unlike
the kernel bitmasks, these bitmasks use dynamically allocated memory
and are manipulated via a pointer. This enables a program to work
correctly on systems with various numbers of CPUs and Nodes, without
recompilation.

There is a related cpuset library which uses the bitmask type provided
here to represent sets of CPUs and Memory Nodes. The internal
representation (as an array of unsigned longs, in little endian order)
is directly compatible with the \texttt{sched{\_}setaffinity(2)} and
\texttt{sched{\_}getaffinity(2)} system calls (added in Linux 2.6).


%___________________________________________________________________________

\hypertarget{ascii-string-representations}{}
\pdfbookmark[0]{2~~~Ascii string representations}{ascii-string-representations}
\section*{2~~~Ascii string representations}

There are two ascii representations of these multi-word bitmasks,
and this library provides display and parsing routines to convert
both representations to and from the internal binary representation
of bitmasks.

The hex mask representation of a bitmask of size 64, with bits
1,5,6,11-13,17-19 set looks like:
\begin{quote}
\begin{quote}{\ttfamily \raggedright \noindent
00000000,000E3862
}\end{quote}
\end{quote}

and the decimal list representation for this same value looks like:
\begin{quote}
\begin{quote}{\ttfamily \raggedright \noindent
1,5,6,11-13,17-19
}\end{quote}
\end{quote}


%___________________________________________________________________________

\hypertarget{hex-mask}{}
\pdfbookmark[1]{2.1~~~Hex mask}{hex-mask}
\subsection*{2.1~~~Hex mask}

The hex mask representation of multi-word bit masks displays each
32-bit word in hex (zero filled), and for masks longer than
one word, uses a comma separator between words.  Words are
displayed in big-endian order most significant first.  And hex
digits within a word are also in big-endian order.

The number of 32-bit words displayed is the minimum number needed
to display all bits of the bitmask, based on the size of the
bitmask.

Examples of the hex word bitmask display format:
\begin{quote}
\begin{quote}{\ttfamily \raggedright \noindent
A~mask~with~just~bit~0~set~displays~as~"00000001".~\\
~\\
A~mask~with~just~bit~127~set~displays~as~"80000000,00000000,00000000,00000000".~\\
~\\
A~mask~with~just~bit~64~set~displays~as~"00000001,00000000,00000000".~\\
~\\
A~mask~with~bits~0,~1,~2,~4,~8,~16,~32~and~64~set~displays~as~\\
"00000001,00000001,00010117".~~The~first~"1"~is~for~bit~64,~the~second~\\
for~bit~32,~the~third~for~bit~16,~and~so~forth,~to~the~"7",~which~is~for~\\
bits~2,~1~and~0.~\\
~\\
A~mask~with~bits~32~through~39~set~displays~as~"000000ff,00000000".~\\
~\\
A~64~bit~bitmask~with~bits~1,~5,~6,~11-13,~and~17-19~set~displays~as~\\
"00000000,000E3862".
}\end{quote}
\end{quote}


%___________________________________________________________________________

\hypertarget{decimal-list}{}
\pdfbookmark[1]{2.2~~~Decimal list}{decimal-list}
\subsection*{2.2~~~Decimal list}

The decimal list representation of bitmasks represents
them as a list of numbers and ranges of numbers.

This format supports a space separated list of one or more comma
separated sequences of ascii decimal bit numbers and ranges,
optionally modified by a \titlereference{stride operator}.

Example of the decimal list bitmask display format:
\begin{quote}
\begin{quote}{\ttfamily \raggedright \noindent
0-4,9~~~~~~~~~~~{\#}~set~bits~0,~1,~2,~3,~4,~and~9
}\end{quote}
\end{quote}

The \titlereference{stride operator} is used to designate every N-th bit in a range
It is written as a colon \textbf{``:''} followed by the number N,
with no spaces on either side of the colon.

Examples of the \titlereference{stride operator}:
\begin{quote}
\begin{quote}{\ttfamily \raggedright \noindent
0-31:2~~~~~~~~~~{\#}~the~16~even~bits~0,~2,~4,~...~30~LINEBREAK~\\
1-31:2~~~~~~~~~~{\#}~the~16~odd~bits~1,~3,~5,~...~31~LINEBREAK~\\
0-31~~~~~~~~~~~~{\#}~all~32~bits~0,~1,~2,~...~31
}\end{quote}
\end{quote}


%___________________________________________________________________________

\hypertarget{calling-return-and-error-conventions}{}
\pdfbookmark[0]{3~~~Calling, return and error conventions}{calling-return-and-error-conventions}
\section*{3~~~Calling, return and error conventions}

As explained in more detail in the next section,
all bitmask operations treat all bits, outside of the originally
specified bit range from 0 to size-1, as if they were zero.

Most of the operations change the bitmask referenced
by the first argument, and return a pointer to that bitmask,
to allow convenient chaining of calls.  However, be careful
of such usage - it's really easy to code memory leaks this way.
Each \texttt{struct bitmask *} pointer obtained from a call to
\href{\#bitmask-alloc}{bitmask{\_}alloc} needs to be free'd with a call (exactly \emph{one} call)
to \href{\#bitmask-free}{bitmask{\_}free}.

Bit positions in bitmasks are zero based (not one based). The bit
positions in a bitmask of size n are numbered 0 through n-1.

The Boolean functions return 1 (True) or 0 (False).

All but the first \texttt{struct bitmask *} pointers passed to any of the
following operations are read-only, declared as: \texttt{const struct bitmask *}.

The unary operations, such as \href{\#bitmask-complement}{bitmask{\_}complement}, take two bitmask
arguments, for the result and the source, in that order.  The same
\texttt{struct bitmask *} pointer may be passed for both arguments, in
order to apply the operation in place.

The binary operations, such as \href{\#bitmask-and}{bitmask{\_}and}, take three bitmask
arguments, for the result and the two sources.  Either source may be
the same as the result.  Indeed, all three arguments may be the same
pointer (though it is not clear what purpose that would serve).

The shift operations zero fill, whether left or right shifting.

The range operations follow 'C' conventions in using closed
left, open right intervals.  That is, the range of bit positions
determined by the pair of integer arguments (i, j) includes
exactly all positions {\textgreater}= i and {\textless} j.

The \href{\#bitmask-next}{bitmask{\_}next} function returns the bitmask size if all bits are clear
above the requested position.

The \href{\#bitmask-first}{bitmask{\_}first} and \href{\#bitmask-last}{bitmask{\_}last} functions return the bitmask size if
all bits are clear in the bitmask.

Two masks are equal if they have the same set bits, regardless
of whether they have the same size.

Most of the operations or functions have no error return cases.
They are defined so as to have valid returns for all well formed
arguments.  Of course, if the arguments are not well formed, then
your application will probably exit with a Segmentation Violation.
This is 'C' after all.

The \href{\#bitmask-alloc}{bitmask{\_}alloc} function returns a zero pointer (NULL) and sets
\titlereference{errno} in the event that \texttt{malloc(3)} fails. See the \texttt{malloc(3)}
man page for possible values of \titlereference{errno} (\textbf{ENOMEM} being the most
likely).

The \href{\#bitmask-displayhex}{bitmask{\_}displayhex}, \href{\#bitmask-displaylist}{bitmask{\_}displaylist}, \href{\#bitmask-parsehex}{bitmask{\_}parsehex}, and
\href{\#bitmask-parselist}{bitmask{\_}parselist} routines have more complex error and return
conventions. See their detailed descriptions below.


%___________________________________________________________________________

\hypertarget{other-bits-always-zero}{}
\pdfbookmark[0]{4~~~Other bits always zero}{other-bits-always-zero}
\section*{4~~~Other bits always zero}

All bitmask operations treat all bits, outside of the originally
specified bit range from 0 to size-1, as if they were zero. Even bits
that might actually be present, due to the use of some multiple of
unsigned longs to represent the masks, are always zero, if they are
outside the specified number of bits in the mask.

The specified number of bits in a bitmask (its size) is established
in the \href{\#bitmask-alloc}{bitmask{\_}alloc} call, and \textbf{never} changed after that.
\begin{description}
%[visit_definition_list_item]
\item[Note:] %[visit_definition]

In particular, observe that the \href{\#bitmask-copy}{bitmask{\_}copy} function does \textbf{not}
change the size of the target bitmask.  Hence the result of copying a
large bitmask to a small one will often not be equal to the original
large bitmask - rather it will be shortened (to the smaller target size,
with bits above that size zero'd).

%[depart_definition]
%[depart_definition_list_item]
\end{description}

For example, if you invoke:
\begin{quote}
\begin{quote}{\ttfamily \raggedright \noindent
struct~bitmask~*bmp~=~bitmask{\_}alloc(17);~\\
bitmask{\_}setall(bmp);~\\
bitmask{\_}setbit(bmp,~21);
}\end{quote}
\end{quote}

then the calls:
\begin{quote}
\begin{quote}{\ttfamily \raggedright \noindent
{\{}~\\
~~~int~x~=~bitmask{\_}last(bmp);~\\
~~~int~y~=~bitmask{\_}isbitset(999);~\\
{\}}
}\end{quote}
\end{quote}

will set x to 16 (the 17 bits are numbered 0 to 16), not 21 or some
other higher number, and they will set y to 0.

Requests to set bits outside those in the range specified
in the initial \href{\#bitmask-alloc}{bitmask{\_}alloc} are ignored and do not
cause any error.

Requests to display or query bits outside those in the range
specified in the initial \href{\#bitmask-alloc}{bitmask{\_}alloc} always behave as
if those bits were present and zero.


%___________________________________________________________________________

\hypertarget{internal-binary-representation}{}
\pdfbookmark[0]{5~~~Internal binary representation}{internal-binary-representation}
\section*{5~~~Internal binary representation}

The 'C' code that uses bitmasks sees only a \texttt{struct bitmask *} opaque pointer.

Hidden within the implementation of bitmasks, a \texttt{struct bitmask} is simply:
\begin{quote}
\begin{quote}{\ttfamily \raggedright \noindent
struct~bitmask~{\{}~\\
~~~~~~~~unsigned~int~size;~~~~~~/*~size~in~bits~of~bitmask~*/~\\
~~~~~~~~unsigned~long~*maskp;~~~/*~array~of~unsigned~longs~*/~\\
{\}};
}\end{quote}
\end{quote}

This structure, and the variable length array of unsigned long words
to which it points are allocated using \texttt{malloc(3)} in the calls to
\href{\#bitmask-alloc}{bitmask{\_}alloc}, and deallocated using \texttt{free(3)} in the calls to
\href{\#bitmask-free}{bitmask{\_}free}.

The \texttt{maskp} array of unsigned longs is arranged the same as the
bitmask operands to the \texttt{sched{\_}setaffinity(2)} and
\texttt{sched{\_}getaffinity(2)} system calls (added in Linux 2.6). As of this
writing this is the same layout as is used by the kernels
\texttt{cpumask{\_}t} and \texttt{nodemask{\_}t} types and the task struct
\texttt{cpus{\_}allowed} and \texttt{mems{\_}allowed} fields.

This representation places multiple unsigned long words in little
endian order - low order word first. Within each unsigned long, bits
are addressed in 'natural C' order, as 1{\textless}{\textless}n, for n between 0 and 31 on
32 bit architectures, and between 0 and 63 on 64 bit architectures.

The implementation of this bitmask library reserves the right to
extend or change this structure and other details of this internal
representation.


%___________________________________________________________________________

\hypertarget{comparing-these-bitmasks-with-the-linux-kernel}{}
\pdfbookmark[0]{6~~~Comparing these bitmasks with the Linux kernel}{comparing-these-bitmasks-with-the-linux-kernel}
\section*{6~~~Comparing these bitmasks with the Linux kernel}

This section compares this bitmask library with the implementation
of bitmasks in the Linux kernel, as of version 2.6.

Users of this library don't actually need to understand these differences.
However users already familiar with kernel bitmasks may find this comparison
helpful.  And this comparison provides an interesting way to present a few of
the design tradeoffs that were made in creating this library.
\begin{quote}
\begin{itemize}
\item {} 
This library implementation and API is optimized for ease of porting,
ease of use and flexible runtime behaviour.

\item {} 
The Linux kernel bitmasks are optimized for optimum space and time
performance with compiled in fixed sizing of critical cpu and node
masks.

\item {} 
This library provides a larger, more complete and consistent set
of bitmask routines than the kernel bitmasks.

\item {} 
All calls are actual subroutine calls, not gcc inline functions or
macros.

\end{itemize}
\end{quote}


%___________________________________________________________________________

\hypertarget{dynamic-memory}{}
\pdfbookmark[1]{6.1~~~Dynamic Memory}{dynamic-memory}
\subsection*{6.1~~~Dynamic Memory}

The representation of bitmasks in the Linux kernel,
as of this writing, is essentially:
\begin{quote}
\begin{quote}{\ttfamily \raggedright \noindent
struct~{\{}~unsigned~long~bitmask{[}N];~{\}};
}\end{quote}
\end{quote}

None of this uses memory allocated dynamically at runtime.
Instead, all sizes are known at compile time, and the compiler,
along with some inline functions and macros, sizes each bitmask
to a hardcoded size, such as NR{\_}CPUS (number of CPUs which that
kernel will support).

The current representation of bitmasks in this library, as
noted in the previous section, is:
\begin{quote}
\begin{quote}{\ttfamily \raggedright \noindent
struct~bitmask~{\{}~\\
~~~~~~~~unsigned~int~size;~~~~~~/*~size~in~bits~of~bitmask~*/~\\
~~~~~~~~unsigned~long~*maskp;~~~/*~array~of~unsigned~longs~*/~\\
{\}};
}\end{quote}
\end{quote}

Both this structure, and the array \texttt{maskp} it references, are
dynamically allocated at runtime.

User programs, unlike a specific compilation of the kernel, usually avoid
hardcoding the number of CPUs and Memory Nodes which they support.
It is for this simple reason that this library uses dynamic memory
allocation and runtime sizing, instead of the static allocation and
compile time sizing used by the kernel's bitmask implementation.


%___________________________________________________________________________

\hypertarget{portable-c}{}
\pdfbookmark[1]{6.2~~~Portable C}{portable-c}
\subsection*{6.2~~~Portable C}

This library is implemented in Portable C, and presents an API that
can easily be used in any Portable C code. The implementing code is
kept simple, portable and easy to develop and maintain. The code is
not optimized for critical inner loop performance requirements.

The Linux kernel bitmasks make essential use of gcc extensions
in order to provide the compile time sizing and optimum performance
that is required for use in critical scheduler and allocator loops.


%___________________________________________________________________________

\hypertarget{larger-api}{}
\pdfbookmark[1]{6.3~~~Larger API}{larger-api}
\subsection*{6.3~~~Larger API}

In order to make it easy to code bitmask operations, and reduce the
costs of coding errors in the applications using these routines, this
library provides a larger, more complete and consistent set of bitmask
routines than the kernel bitmasks.

The kernel has some carefully optimized bitmasks routines for specific
architectures, which makes it a bit more difficult to keep their API
as straightforward and consistent as this library. And it avoids
providing routines that don't have an actual use in existing kernel
code.


%___________________________________________________________________________

\hypertarget{no-macros-or-access-to-bitmask-internals}{}
\pdfbookmark[1]{6.4~~~No macros or access to bitmask internals}{no-macros-or-access-to-bitmask-internals}
\subsection*{6.4~~~No macros or access to bitmask internals}

The implementation of this bitmask library uses no gcc inline
functions or preprocessor macro functions of \texttt{struct bitmask} in
the bitmask.h header file, and produces no code in the application
binary that knows the internals of \texttt{struct bitmask}.

Everything that looks like a function on bitmasks is a real function
call into the libmask.so library.  The \texttt{struct bitmask} structure is
declared in \texttt{bitmask.h} without its members defined, as simply:
\begin{quote}
\begin{quote}{\ttfamily \raggedright \noindent
struct~bitmask;
}\end{quote}
\end{quote}

The reason that there are no such macro or inline functions in the bitmask.h
header file is that without access to the internals of the bitmask
structure, they could not be compiled.

There are two reasons that the internals of the bitmask structure are
not accessible in the bitmask.h header.

One reason is to discourage code that looks inside a structure that is
intended to be opaque. This reduces the risk that some future change
to the implementation internals of this structure will adversely
impact existing application binaries using this API.

The other reason is to discourage bitmask structure assignment, which
reduces the risk of memory corruption bugs from misuse of this structure.

Code such as the following will not compile, but it if did, would
typically result in memory corruption.
\begin{quote}
\begin{quote}{\ttfamily \raggedright \noindent
{\#}include~<bitmask.h>~\\
~\\
struct~bitmask~*bmp1~=~bitmask{\_}alloc(32);~\\
struct~bitmask~*bmp2~=~bitmask{\_}alloc(32);~\\
~\\
*bmp2~=~*bmp1;~~~~~~~/*~1.~Doesn't~compile~-~if~it~did,~would~be~unsafe~assignment~*/~\\
bitmask{\_}free(bmp1);~~/*~2.~Free~it~once~*/~\\
bitmask{\_}free(bmp2);~~/*~3.~Free~it~twice:~corrupted~malloc~heap~*/
}\end{quote}
\end{quote}

At step {[}1], the dynamic memory allocated to bmp2 is lost (memory
leak) and the dynamic memory allocated to bmp1 is now referenced
twice. At step {[}2], the memory for bmp1 is free'd once. At step {[}3],
it is free'd again, resulting in a corrupt malloc heap, and likely an
obscure crash later in the program execution.

Assigning the pointers, and passing them as arguments, is acceptable,
so long as you are careful not to cause a memory leak by assigning to
a \titlereference{struct bitmask *} pointer that is currently referencing some other
dynamically allocated bitmask which should first be freed via that
pointer.
\begin{quote}
\begin{quote}{\ttfamily \raggedright \noindent
{\#}include~<bitmask.h>~\\
~\\
struct~bitmask~*bmp1;~\\
struct~bitmask~*bmp2~=~bitmask{\_}alloc(32);~\\
extern~void~f(struct~bitmask~*);~\\
bmp1~=~bmp2;~~~~~~~~~/*~ok~*/~\\
f(bmp1);~~~~~~~~~~~~~/*~ok~*/
}\end{quote}
\end{quote}

Both of the above reasons reflect the same basic design tradeoff to prefer
robust, portable code over aggressive extraction of performance.

The above choices also enable application binaries to continue working
correctly in the face of internal changes to the bitmask library,
without requiring the applications to be recompiled. This is quite
unlike the Linux kernel, which is routinely recompiled in its
entirety, as a single unit, anytime any part of it changes.


%___________________________________________________________________________

\hypertarget{bitmask-library-functions}{}
\pdfbookmark[0]{7~~~Bitmask Library Functions}{bitmask-library-functions}
\section*{7~~~Bitmask Library Functions}

The following inclusion and linkage provides access to the bitmask API
from 'C' code:
\begin{quote}
\begin{quote}{\ttfamily \raggedright \noindent
{\#}include~<bitmask.h>~\\
/*~link~with~-lbitmask~*/
}\end{quote}
\end{quote}

The following functions are supported in the 'C' bitmask API. In some
cases, 'C' equivalent code is shown, as if bitmasks were a single
unsigned long, even though they are packaged in a structure,
referenced by a pointer, and actually contain an array of perhaps
multiple unsigned longs.

None of these operations other than \href{\#bitmask-alloc}{bitmask{\_}alloc} allocate new bitmasks,
and none of them other than \href{\#bitmask-free}{bitmask{\_}free} free existing bitmasks.

None of these operations other than \href{\#bitmask-alloc}{bitmask{\_}alloc} set or change the
size of a bitmask.

The following functions are supported in the 'C' bitmask API:
\begin{itemize}
\item {} 
Allocate and free \texttt{struct bitmask *}
\begin{itemize}
\item {} 
\href{\#bitmask-alloc}{bitmask{\_}alloc} (n) - Allocate a new struct bitmask with a size of n bits

\item {} 
\href{\#bitmask-free}{bitmask{\_}free} (struct bitmask * bmp) - Free struct bitmask

\end{itemize}

\item {} 
Display and parse ascii string representations
\begin{itemize}
\item {} 
\href{\#bitmask-displayhex}{bitmask{\_}displayhex} (buf, len, bmp) - Write hex word representation of bmp to buf

\item {} 
\href{\#bitmask-displaylist}{bitmask{\_}displaylist} (buf, len, bmp) - Write decimal list representation of bmp to buf

\item {} 
\href{\#bitmask-parsehex}{bitmask{\_}parsehex} (buf, bmp) - Parse hex word representation in buf to bmp

\item {} 
\href{\#bitmask-parselist}{bitmask{\_}parselist} (buf, bmp) - Parse decimal list representation in buf to bmp

\end{itemize}

\item {} 
Basic initialization operations
\begin{itemize}
\item {} 
\href{\#bitmask-copy}{bitmask{\_}copy} (bmp1, bmp2) - Copy bmp2 to bmp1

\item {} 
\href{\#bitmask-setall}{bitmask{\_}setall} (bmp) - Set all bits in bitmask: bmp = {\textasciitilde}0

\item {} 
\href{\#bitmask-clearall}{bitmask{\_}clearall} (bmp) - Clear all bits in bitmask: bmp = 0

\end{itemize}

\item {} 
Interface aids for kernel sched{\_}{\{}set,get{\}}affinity system calls
\begin{itemize}
\item {} 
\href{\#bitmask-nbytes}{bitmask{\_}nbytes} (bmp) - Length in bytes of mask - use as second argument to these calls

\item {} 
\href{\#bitmask-mask}{bitmask{\_}mask} (bmp) - Direct pointer to bit mask  - use as third argument to these calls

\end{itemize}

\item {} 
Unary numeric queries
\begin{itemize}
\item {} 
\href{\#bitmask-nbits}{bitmask{\_}nbits} (bmp) - Size in bits of entire bitmask

\item {} 
\href{\#bitmask-weight}{bitmask{\_}weight} (bmp) - Hamming Weight: number of set bits

\end{itemize}

\item {} 
Unary Boolean queries
\begin{itemize}
\item {} 
\href{\#bitmask-isbitset}{bitmask{\_}isbitset} (bmp, i) - True if specified bit \textbf{i} is set

\item {} 
\href{\#bitmask-isbitclear}{bitmask{\_}isbitclear} (bmp, i) - True if specified bit \textbf{i} is clear

\item {} 
\href{\#bitmask-isallset}{bitmask{\_}isallset} (bmp) - True if all bits are set

\item {} 
\href{\#bitmask-isallclear}{bitmask{\_}isallclear} (bmp) - True if all bits are clear

\end{itemize}

\item {} 
Single bit operations
\begin{itemize}
\item {} 
\href{\#bitmask-setbit}{bitmask{\_}setbit} (bmp, i) - Set a single bit \textbf{i} in bitmask

\item {} 
\href{\#bitmask-clearbit}{bitmask{\_}clearbit} (bmp, i) - Clear a single bit \textbf{i} in bitmask

\end{itemize}

\item {} 
Binary Boolean operations: bmp1 op? bmp2
\begin{itemize}
\item {} 
\href{\#bitmask-equal}{bitmask{\_}equal} (bmp1, bmp2) - True if two bitmasks are equal

\item {} 
\href{\#bitmask-subset}{bitmask{\_}subset} (bmp1, bmp2) - True if first bitmask is subset of second

\item {} 
\href{\#bitmask-disjoint}{bitmask{\_}disjoint} (bmp1, bmp2) - True if two bitmasks don't overlap

\item {} 
\href{\#bitmask-intersects}{bitmask{\_}intersects} (bmp1, bmp2) - True if two bitmasks do overlap

\end{itemize}

\item {} 
Range operations
\begin{itemize}
\item {} 
\href{\#bitmask-setrange}{bitmask{\_}setrange} (bmp, i, j) - Set bits of bitmask in specified range {[}i, j)

\item {} 
\href{\#bitmask-clearrange}{bitmask{\_}clearrange} (bmp, i, j) - Clear bits of bitmask in specified range

\item {} 
\href{\#bitmask-keeprange}{bitmask{\_}keeprange} (bmp, i, j) - Clear all but specified range

\end{itemize}

\item {} 
Unary operations: bmp1 = op(bmp2)
\begin{itemize}
\item {} 
\href{\#bitmask-complement}{bitmask{\_}complement} (bmp1, bmp2) - Complement: bmp1 = {\textasciitilde}bmp2

\item {} 
\href{\#bitmask-shiftright}{bitmask{\_}shiftright} (bmp1, bmp2, n) - Right shift: bmp1 = bmp2 {\textgreater}{\textgreater} n

\item {} 
\href{\#bitmask-shiftleft}{bitmask{\_}shiftleft} (bmp1, bmp2, n) - Left shift: bmp1 = bmp2 {\textless}{\textless} n

\end{itemize}

\item {} 
Binary operations: bmp1 = bmp2 op bmp3
\begin{itemize}
\item {} 
\href{\#bitmask-and}{bitmask{\_}and} (bmp1, bmp2, bmp3) - Logical \titlereference{and} of two bitmasks: bmp1 = bmp2 {\&} bmp3

\item {} 
\href{\#bitmask-andnot}{bitmask{\_}andnot} (bmp1, bmp2, bmp3) - Logical \titlereference{andnot} of two bitmasks: bmp1 = bmp2 {\&} {\textasciitilde}bmp3

\item {} 
\href{\#bitmask-or}{bitmask{\_}or} (bmp1, bmp2, bmp3) - Logical \titlereference{or} of two bitmasks: bmp1 = bmp2 {\textbar} bmp3

\item {} 
\href{\#bitmask-eor}{bitmask{\_}eor} (bmp1, bmp2, bmp3) - Logical \titlereference{eor} of two bitmasks: bmp1 = bmp2 {\textasciicircum} bmp3

\end{itemize}

\item {} 
Iteration operators
\begin{itemize}
\item {} 
\href{\#bitmask-first}{bitmask{\_}first} (bmp) - Number of lowest set bit (min)

\item {} 
\href{\#bitmask-next}{bitmask{\_}next} (bmp, i) - Number of next set bit above given bit \textbf{i}

\item {} 
\href{\#bitmask-rel-to-abs-pos}{bitmask{\_}rel{\_}to{\_}abs{\_}pos} (bmp, n) - Absolute position of nth set bit

\item {} 
\href{\#bitmask-abs-to-rel-pos}{bitmask{\_}abs{\_}to{\_}rel{\_}pos} (bmp, n) - Relative position amongst set bits of bit n

\item {} 
\href{\#bitmask-last}{bitmask{\_}last} (bmp) - Number of highest set bit (max)

\end{itemize}

\end{itemize}


%___________________________________________________________________________

\hypertarget{bitmask-alloc}{}
\pdfbookmark[1]{7.1~~~bitmask{\_}alloc}{bitmask-alloc}
\subsection*{7.1~~~bitmask{\_}alloc}


%___________________________________________________________________________

\hypertarget{struct-bitmask-bitmask-alloc-unsigned-int-nbits}{}
\pdfbookmark[2]{struct bitmask *bitmask{\_}alloc(unsigned int nbits);}{struct-bitmask-bitmask-alloc-unsigned-int-nbits}
\subsubsection*{\texttt{struct bitmask *bitmask{\_}alloc(unsigned int nbits);}}
\begin{quote}

Allocate a new struct bitmask with a size of \texttt{nbits} bits.

This is the only bitmask function that creates bitmasks.

Each \texttt{struct bitmask *} pointer obtained from a call to
\href{\#bitmask-alloc}{bitmask{\_}alloc} needs to be free'd with a call (exactly \emph{one}
call) to \href{\#bitmask-free}{bitmask{\_}free}.

The \href{\#bitmask-alloc}{bitmask{\_}alloc} function uses the underlying \texttt{malloc(3)}
routine to obtain memory. It returns a zero pointer (NULL) and
sets \titlereference{errno} in the event that \texttt{malloc(3)} fails. See the
\texttt{malloc(3)} man page for possible values of \titlereference{errno} (\textbf{ENOMEM}
being the most likely).

The size of a bitmask, as specified in the \titlereference{bitmask{\_}alloc} call
that created it, is \emph{never} changed by subsequent bitmask
operations. Bits at positions outside the range zero to nbits-1 are
always zero. Attempts to modify bits at such positions are always
ignored, doing nothing, successfully.

For portable code, when allocating a bitmask to handle the CPUs or Memory
Nodes on a system, the number of CPUs or Nodes should not be hardcoded, but
obtained dynamically from the system.  The routines \texttt{cpuset{\_}cpus{\_}nbits()}
and \texttt{cpuset{\_}mems{\_}nbits()} in the related \titlereference{libcpuset} library provide the
maximum number of CPUs or Memory Nodes that the operating system was
compiled to support.  Use these values to size CPU and Memory Node bitmasks
for calls into the \titlereference{libcpuset} library.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-free}{}
\pdfbookmark[1]{7.2~~~bitmask{\_}free}{bitmask-free}
\subsection*{7.2~~~bitmask{\_}free}


%___________________________________________________________________________

\hypertarget{void-bitmask-free-struct-bitmask-bmp}{}
\pdfbookmark[2]{void bitmask{\_}free(struct bitmask *bmp);}{void-bitmask-free-struct-bitmask-bmp}
\subsubsection*{\texttt{void bitmask{\_}free(struct bitmask *bmp);}}
\begin{quote}

Free a bitmask struct.

This call frees the memory assigned to a bitmask. It is the only
function that frees bitmasks. The \titlereference{struct bitmask *`} pointer must
have been returned by a previous call to \href{\#bitmask-alloc}{bitmask{\_}alloc}.  The
memory is not cleared. If \titlereference{bmp} is NULL, no operation is
performed.

Each \texttt{struct bitmask *} pointer obtained from a call to
\href{\#bitmask-alloc}{bitmask{\_}alloc} needs to be free'd with a call (exactly \emph{one}
call) to \href{\#bitmask-free}{bitmask{\_}free}.

\href{\#bitmask-free}{bitmask{\_}free} returns no value.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-displayhex}{}
\pdfbookmark[1]{7.3~~~bitmask{\_}displayhex}{bitmask-displayhex}
\subsection*{7.3~~~bitmask{\_}displayhex}


%___________________________________________________________________________

\hypertarget{int-bitmask-displayhex-char-buf-int-len-const-struct-bitmask-bmp}{}
\pdfbookmark[2]{int bitmask{\_}displayhex(char *buf, int len, const struct bitmask *bmp);}{int-bitmask-displayhex-char-buf-int-len-const-struct-bitmask-bmp}
\subsubsection*{\texttt{int bitmask{\_}displayhex(char *buf, int len, const struct bitmask *bmp);}}
\begin{quote}

Write hex mask representation of bmp to buf.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-displaylist}{}
\pdfbookmark[1]{7.4~~~bitmask{\_}displaylist}{bitmask-displaylist}
\subsection*{7.4~~~bitmask{\_}displaylist}


%___________________________________________________________________________

\hypertarget{int-bitmask-displaylist-char-buf-int-len-const-struct-bitmask-bmp}{}
\pdfbookmark[2]{int bitmask{\_}displaylist(char *buf, int len, const struct bitmask *bmp);}{int-bitmask-displaylist-char-buf-int-len-const-struct-bitmask-bmp}
\subsubsection*{\texttt{int bitmask{\_}displaylist(char *buf, int len, const struct bitmask *bmp);}}
\begin{quote}

Write decimal list representation of bmp to buf.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-parsehex}{}
\pdfbookmark[1]{7.5~~~bitmask{\_}parsehex}{bitmask-parsehex}
\subsection*{7.5~~~bitmask{\_}parsehex}


%___________________________________________________________________________

\hypertarget{int-bitmask-parsehex-const-char-buf-struct-bitmask-bmp}{}
\pdfbookmark[2]{int bitmask{\_}parsehex(const char *buf, struct bitmask *bmp);}{int-bitmask-parsehex-const-char-buf-struct-bitmask-bmp}
\subsubsection*{\texttt{int bitmask{\_}parsehex(const char *buf, struct bitmask *bmp);}}
\begin{quote}

Parse hex mask representation in buf to bmp.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-parselist}{}
\pdfbookmark[1]{7.6~~~bitmask{\_}parselist}{bitmask-parselist}
\subsection*{7.6~~~bitmask{\_}parselist}


%___________________________________________________________________________

\hypertarget{int-bitmask-parselist-const-char-buf-struct-bitmask-bmp}{}
\pdfbookmark[2]{int bitmask{\_}parselist(const char *buf, struct bitmask *bmp);}{int-bitmask-parselist-const-char-buf-struct-bitmask-bmp}
\subsubsection*{\texttt{int bitmask{\_}parselist(const char *buf, struct bitmask *bmp);}}
\begin{quote}

Parse decimal list representation in buf to bmp.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-copy}{}
\pdfbookmark[1]{7.7~~~bitmask{\_}copy}{bitmask-copy}
\subsection*{7.7~~~bitmask{\_}copy}


%___________________________________________________________________________

\hypertarget{struct-bitmask-bitmask-copy-struct-bitmask-bmp1-const-bitmask-bmp2}{}
\pdfbookmark[2]{struct bitmask *bitmask{\_}copy(struct bitmask *bmp1, const bitmask *bmp2);}{struct-bitmask-bitmask-copy-struct-bitmask-bmp1-const-bitmask-bmp2}
\subsubsection*{\texttt{struct bitmask *bitmask{\_}copy(struct bitmask *bmp1, const bitmask *bmp2);}}
\begin{quote}

Copy the value of bitmask bmp2 to bitmask bmp1.  If the size of bmp1 is
smaller than bmp2, then bits set in bmp2 beyond what fit in bmp1 are
lost in the bmp1 copy.  If the size of bmp1 is larger than bmp2,then
bits set in bmp1 beyond what bmp2 specifies are cleared.  The target
bitmask bmp1 is not resized in any case.

Returns the pointer bmp1.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-setall}{}
\pdfbookmark[1]{7.8~~~bitmask{\_}setall}{bitmask-setall}
\subsection*{7.8~~~bitmask{\_}setall}


%___________________________________________________________________________

\hypertarget{struct-bitmask-bitmask-setall-struct-bitmask-bmp}{}
\pdfbookmark[2]{struct bitmask *bitmask{\_}setall(struct bitmask *bmp);}{struct-bitmask-bitmask-setall-struct-bitmask-bmp}
\subsubsection*{\texttt{struct bitmask *bitmask{\_}setall(struct bitmask *bmp);}}
\begin{quote}

Sets all bits in bitmask bmp.

Returns the pointer bmp.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-clearall}{}
\pdfbookmark[1]{7.9~~~bitmask{\_}clearall}{bitmask-clearall}
\subsection*{7.9~~~bitmask{\_}clearall}


%___________________________________________________________________________

\hypertarget{struct-bitmask-bitmask-clearall-struct-bitmask-bmp}{}
\pdfbookmark[2]{struct bitmask *bitmask{\_}clearall(struct bitmask *bmp);}{struct-bitmask-bitmask-clearall-struct-bitmask-bmp}
\subsubsection*{\texttt{struct bitmask *bitmask{\_}clearall(struct bitmask *bmp);}}
\begin{quote}

Clears all bits in bitmask bmp.

Returns the pointer bmp.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-nbytes}{}
\pdfbookmark[1]{7.10~~~bitmask{\_}nbytes}{bitmask-nbytes}
\subsection*{7.10~~~bitmask{\_}nbytes}


%___________________________________________________________________________

\hypertarget{unsigned-int-bitmask-nbytes-struct-bitmask-bmp}{}
\pdfbookmark[2]{unsigned int bitmask{\_}nbytes(struct bitmask *bmp);}{unsigned-int-bitmask-nbytes-struct-bitmask-bmp}
\subsubsection*{\texttt{unsigned int bitmask{\_}nbytes(struct bitmask *bmp);}}
\begin{quote}

Returns the length in bytes of a bitmask.

This is useful as the second argument to systems calls (new in
Linux 2.6) \texttt{sched{\_}setaffinity(2)} and \texttt{sched{\_}getaffinity(2)}

Example:
\begin{quote}
\begin{quote}{\ttfamily \raggedright \noindent
/*~Bind~current~process~to~the~3rd~CPU~(number~2)~of~a~possible~64~CPUs~*/~\\
~\\
struct~bitmask~*bmp~=~bitmask{\_}alloc(64);~\\
bitmask{\_}setbit(2);~\\
if~(sched{\_}setaffinity(0,~bitmask{\_}nbytes(bmp),~bitmask{\_}mask(bmp))~<~0)~\\
~~~~~~~~...~handle~error~...~\\
bitmask{\_}free(bmp);
}\end{quote}
\end{quote}
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-mask}{}
\pdfbookmark[1]{7.11~~~bitmask{\_}mask}{bitmask-mask}
\subsection*{7.11~~~bitmask{\_}mask}


%___________________________________________________________________________

\hypertarget{unsigned-long-bitmask-mask-struct-bitmask-bmp}{}
\pdfbookmark[2]{unsigned long *bitmask{\_}mask(struct bitmask *bmp);}{unsigned-long-bitmask-mask-struct-bitmask-bmp}
\subsubsection*{\texttt{unsigned long *bitmask{\_}mask(struct bitmask *bmp);}}
\begin{quote}

Returns a direct pointer to the unsigned long mask array of a bitmask.

This is useful as the third argument to systems calls (new in
Linux 2.6) \texttt{sched{\_}setaffinity(2)} and \texttt{sched{\_}getaffinity(2)}

See also the example for \href{\#bitmask-nbytes}{bitmask{\_}nbytes}, above.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-nbits}{}
\pdfbookmark[1]{7.12~~~bitmask{\_}nbits}{bitmask-nbits}
\subsection*{7.12~~~bitmask{\_}nbits}


%___________________________________________________________________________

\hypertarget{unsigned-int-bitmask-nbits-const-struct-bitmask-bmp}{}
\pdfbookmark[2]{unsigned int bitmask{\_}nbits(const struct bitmask *bmp);}{unsigned-int-bitmask-nbits-const-struct-bitmask-bmp}
\subsubsection*{\texttt{unsigned int bitmask{\_}nbits(const struct bitmask *bmp);}}
\begin{quote}

Size in bits of entire bitmask.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-weight}{}
\pdfbookmark[1]{7.13~~~bitmask{\_}weight}{bitmask-weight}
\subsection*{7.13~~~bitmask{\_}weight}


%___________________________________________________________________________

\hypertarget{unsigned-int-bitmask-weight-const-struct-bitmask-bmp}{}
\pdfbookmark[2]{unsigned int bitmask{\_}weight(const struct bitmask *bmp);}{unsigned-int-bitmask-weight-const-struct-bitmask-bmp}
\subsubsection*{\texttt{unsigned int bitmask{\_}weight(const struct bitmask *bmp);}}
\begin{quote}

Hamming Weight: number of set bits.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-isbitset}{}
\pdfbookmark[1]{7.14~~~bitmask{\_}isbitset}{bitmask-isbitset}
\subsection*{7.14~~~bitmask{\_}isbitset}


%___________________________________________________________________________

\hypertarget{int-bitmask-isbitset-const-struct-bitmask-bmp-unsigned-int-i}{}
\pdfbookmark[2]{int bitmask{\_}isbitset(const struct bitmask *bmp, unsigned int i);}{int-bitmask-isbitset-const-struct-bitmask-bmp-unsigned-int-i}
\subsubsection*{\texttt{int bitmask{\_}isbitset(const struct bitmask *bmp, unsigned int i);}}
\begin{quote}

True if specified bit i is set.  Always false if i {\textgreater}= bitmask{\_}nbits{\_}().
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-isbitclear}{}
\pdfbookmark[1]{7.15~~~bitmask{\_}isbitclear}{bitmask-isbitclear}
\subsection*{7.15~~~bitmask{\_}isbitclear}


%___________________________________________________________________________

\hypertarget{int-bitmask-isbitclear-const-struct-bitmask-bmp-unsigned-int-i}{}
\pdfbookmark[2]{int bitmask{\_}isbitclear(const struct bitmask *bmp, unsigned int i);}{int-bitmask-isbitclear-const-struct-bitmask-bmp-unsigned-int-i}
\subsubsection*{\texttt{int bitmask{\_}isbitclear(const struct bitmask *bmp, unsigned int i);}}
\begin{quote}

True if specified bit i is clear.  Always true if i {\textgreater}= bitmask{\_}nbits{\_}().
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-isallset}{}
\pdfbookmark[1]{7.16~~~bitmask{\_}isallset}{bitmask-isallset}
\subsection*{7.16~~~bitmask{\_}isallset}


%___________________________________________________________________________

\hypertarget{int-bitmask-isallset-const-struct-bitmask-bmp}{}
\pdfbookmark[2]{int bitmask{\_}isallset(const struct bitmask *bmp);}{int-bitmask-isallset-const-struct-bitmask-bmp}
\subsubsection*{\texttt{int bitmask{\_}isallset(const struct bitmask *bmp);}}
\begin{quote}

True if all bits from positions 0 to bitmask{\_}nbits{\_}() - 1 are set.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-isallclear}{}
\pdfbookmark[1]{7.17~~~bitmask{\_}isallclear}{bitmask-isallclear}
\subsection*{7.17~~~bitmask{\_}isallclear}


%___________________________________________________________________________

\hypertarget{int-bitmask-isallclear-const-struct-bitmask-bmp}{}
\pdfbookmark[2]{int bitmask{\_}isallclear(const struct bitmask *bmp);}{int-bitmask-isallclear-const-struct-bitmask-bmp}
\subsubsection*{\texttt{int bitmask{\_}isallclear(const struct bitmask *bmp);}}
\begin{quote}

True if all bits from positions 0 to bitmask{\_}nbits{\_}() - 1 are clear.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-setbit}{}
\pdfbookmark[1]{7.18~~~bitmask{\_}setbit}{bitmask-setbit}
\subsection*{7.18~~~bitmask{\_}setbit}


%___________________________________________________________________________

\hypertarget{struct-bitmask-bitmask-setbit-struct-bitmask-bmp-unsigned-int-i}{}
\pdfbookmark[2]{struct bitmask *bitmask{\_}setbit(struct bitmask *bmp, unsigned int i);}{struct-bitmask-bitmask-setbit-struct-bitmask-bmp-unsigned-int-i}
\subsubsection*{\texttt{struct bitmask *bitmask{\_}setbit(struct bitmask *bmp, unsigned int i);}}
\begin{quote}

Set a single bit i in bitmask.  Does nothing successfully
if i {\textgreater}= bitmask{\_}nbits{\_}().  Returns the \texttt{bmp} pointer passed in.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-clearbit}{}
\pdfbookmark[1]{7.19~~~bitmask{\_}clearbit}{bitmask-clearbit}
\subsection*{7.19~~~bitmask{\_}clearbit}


%___________________________________________________________________________

\hypertarget{struct-bitmask-bitmask-clearbit-struct-bitmask-bmp-unsigned-int-i}{}
\pdfbookmark[2]{struct bitmask *bitmask{\_}clearbit(struct bitmask *bmp, unsigned int i);}{struct-bitmask-bitmask-clearbit-struct-bitmask-bmp-unsigned-int-i}
\subsubsection*{\texttt{struct bitmask *bitmask{\_}clearbit(struct bitmask *bmp, unsigned int i);}}
\begin{quote}

Clear a single bit i in bitmask.  Does nothing successfully
if i {\textgreater}= bitmask{\_}nbits{\_}().  Returns the \texttt{bmp} pointer passed in.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-equal}{}
\pdfbookmark[1]{7.20~~~bitmask{\_}equal}{bitmask-equal}
\subsection*{7.20~~~bitmask{\_}equal}


%___________________________________________________________________________

\hypertarget{int-bitmask-equal-const-struct-bitmask-bmp1-const-bitmask-bmp2}{}
\pdfbookmark[2]{int bitmask{\_}equal(const struct bitmask *bmp1, const bitmask *bmp2);}{int-bitmask-equal-const-struct-bitmask-bmp1-const-bitmask-bmp2}
\subsubsection*{\texttt{int bitmask{\_}equal(const struct bitmask *bmp1, const bitmask *bmp2);}}
\begin{quote}

True if two bitmasks are equal.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-subset}{}
\pdfbookmark[1]{7.21~~~bitmask{\_}subset}{bitmask-subset}
\subsection*{7.21~~~bitmask{\_}subset}


%___________________________________________________________________________

\hypertarget{int-bitmask-subset-const-struct-bitmask-bmp1-const-bitmask-bmp2}{}
\pdfbookmark[2]{int bitmask{\_}subset(const struct bitmask *bmp1, const bitmask *bmp2);}{int-bitmask-subset-const-struct-bitmask-bmp1-const-bitmask-bmp2}
\subsubsection*{\texttt{int bitmask{\_}subset(const struct bitmask *bmp1, const bitmask *bmp2);}}
\begin{quote}

True if first bitmask is subset of second.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-disjoint}{}
\pdfbookmark[1]{7.22~~~bitmask{\_}disjoint}{bitmask-disjoint}
\subsection*{7.22~~~bitmask{\_}disjoint}


%___________________________________________________________________________

\hypertarget{int-bitmask-disjoint-const-struct-bitmask-bmp1-const-bitmask-bmp2}{}
\pdfbookmark[2]{int bitmask{\_}disjoint(const struct bitmask *bmp1, const bitmask *bmp2);}{int-bitmask-disjoint-const-struct-bitmask-bmp1-const-bitmask-bmp2}
\subsubsection*{\texttt{int bitmask{\_}disjoint(const struct bitmask *bmp1, const bitmask *bmp2);}}
\begin{quote}

True if two bitmasks don't overlap.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-intersects}{}
\pdfbookmark[1]{7.23~~~bitmask{\_}intersects}{bitmask-intersects}
\subsection*{7.23~~~bitmask{\_}intersects}


%___________________________________________________________________________

\hypertarget{int-bitmask-intersects-const-struct-bitmask-bmp1-const-bitmask-bmp2}{}
\pdfbookmark[2]{int bitmask{\_}intersects(const struct bitmask *bmp1, const bitmask *bmp2);}{int-bitmask-intersects-const-struct-bitmask-bmp1-const-bitmask-bmp2}
\subsubsection*{\texttt{int bitmask{\_}intersects(const struct bitmask *bmp1, const bitmask *bmp2);}}
\begin{quote}

True if two bitmasks do overlap.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-setrange}{}
\pdfbookmark[1]{7.24~~~bitmask{\_}setrange}{bitmask-setrange}
\subsection*{7.24~~~bitmask{\_}setrange}


%___________________________________________________________________________

\hypertarget{struct-bitmask-bitmask-setrange-struct-bitmask-bmp-unsigned-int-i-unsigned-int-j}{}
\pdfbookmark[2]{struct bitmask *bitmask{\_}setrange(struct bitmask *bmp, unsigned int i, unsigned int j);}{struct-bitmask-bitmask-setrange-struct-bitmask-bmp-unsigned-int-i-unsigned-int-j}
\subsubsection*{\texttt{struct bitmask *bitmask{\_}setrange(struct bitmask *bmp, unsigned int i, unsigned int j);}}
\begin{quote}

Set bits of bitmask in specified range {[}i, j).
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-clearrange}{}
\pdfbookmark[1]{7.25~~~bitmask{\_}clearrange}{bitmask-clearrange}
\subsection*{7.25~~~bitmask{\_}clearrange}


%___________________________________________________________________________

\hypertarget{struct-bitmask-bitmask-clearrange-struct-bitmask-bmp-unsigned-int-i-unsigned-int-j}{}
\pdfbookmark[2]{struct bitmask *bitmask{\_}clearrange(struct bitmask *bmp, unsigned int i, unsigned int j);}{struct-bitmask-bitmask-clearrange-struct-bitmask-bmp-unsigned-int-i-unsigned-int-j}
\subsubsection*{\texttt{struct bitmask *bitmask{\_}clearrange(struct bitmask *bmp, unsigned int i, unsigned int j);}}
\begin{quote}

Clear bits of bitmask in specified range.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-keeprange}{}
\pdfbookmark[1]{7.26~~~bitmask{\_}keeprange}{bitmask-keeprange}
\subsection*{7.26~~~bitmask{\_}keeprange}


%___________________________________________________________________________

\hypertarget{struct-bitmask-bitmask-keeprange-struct-bitmask-bmp-unsigned-int-i-unsigned-int-j}{}
\pdfbookmark[2]{struct bitmask *bitmask{\_}keeprange(struct bitmask *bmp, unsigned int i, unsigned int j);}{struct-bitmask-bitmask-keeprange-struct-bitmask-bmp-unsigned-int-i-unsigned-int-j}
\subsubsection*{\texttt{struct bitmask *bitmask{\_}keeprange(struct bitmask *bmp, unsigned int i, unsigned int j);}}
\begin{quote}

Clear all but specified range.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-complement}{}
\pdfbookmark[1]{7.27~~~bitmask{\_}complement}{bitmask-complement}
\subsection*{7.27~~~bitmask{\_}complement}


%___________________________________________________________________________

\hypertarget{struct-bitmask-bitmask-complement-struct-bitmask-bmp1-const-bitmask-bmp2}{}
\pdfbookmark[2]{struct bitmask *bitmask{\_}complement(struct bitmask *bmp1, const bitmask *bmp2);}{struct-bitmask-bitmask-complement-struct-bitmask-bmp1-const-bitmask-bmp2}
\subsubsection*{\texttt{struct bitmask *bitmask{\_}complement(struct bitmask *bmp1, const bitmask *bmp2);}}
\begin{quote}

Complement: bmp1 = {\textasciitilde}bmp2.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-shiftright}{}
\pdfbookmark[1]{7.28~~~bitmask{\_}shiftright}{bitmask-shiftright}
\subsection*{7.28~~~bitmask{\_}shiftright}


%___________________________________________________________________________

\hypertarget{struct-bitmask-bitmask-shiftright-struct-bitmask-bmp1-const-bitmask-bmp2-unsigned-int-n}{}
\pdfbookmark[2]{struct bitmask *bitmask{\_}shiftright(struct bitmask *bmp1, const bitmask *bmp2, unsigned int n);}{struct-bitmask-bitmask-shiftright-struct-bitmask-bmp1-const-bitmask-bmp2-unsigned-int-n}
\subsubsection*{\texttt{struct bitmask *bitmask{\_}shiftright(struct bitmask *bmp1, const bitmask *bmp2, unsigned int n);}}
\begin{quote}

Right shift: bmp1 = bmp2 {\textgreater}{\textgreater} n.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-shiftleft}{}
\pdfbookmark[1]{7.29~~~bitmask{\_}shiftleft}{bitmask-shiftleft}
\subsection*{7.29~~~bitmask{\_}shiftleft}


%___________________________________________________________________________

\hypertarget{struct-bitmask-bitmask-shiftleft-struct-bitmask-bmp1-const-bitmask-bmp2-unsigned-int-n}{}
\pdfbookmark[2]{struct bitmask *bitmask{\_}shiftleft(struct bitmask *bmp1, const bitmask *bmp2, unsigned int n);}{struct-bitmask-bitmask-shiftleft-struct-bitmask-bmp1-const-bitmask-bmp2-unsigned-int-n}
\subsubsection*{\texttt{struct bitmask *bitmask{\_}shiftleft(struct bitmask *bmp1, const bitmask *bmp2, unsigned int n);}}
\begin{quote}

Left shift: bmp1 = bmp2 {\textless}{\textless} n.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-and}{}
\pdfbookmark[1]{7.30~~~bitmask{\_}and}{bitmask-and}
\subsection*{7.30~~~bitmask{\_}and}


%___________________________________________________________________________

\hypertarget{struct-bitmask-bitmask-and-struct-bitmask-bmp1-const-bitmask-bmp2-const-bitmask-bmp3}{}
\pdfbookmark[2]{struct bitmask *bitmask{\_}and(struct bitmask *bmp1, const bitmask *bmp2, const bitmask *bmp3);}{struct-bitmask-bitmask-and-struct-bitmask-bmp1-const-bitmask-bmp2-const-bitmask-bmp3}
\subsubsection*{\texttt{struct bitmask *bitmask{\_}and(struct bitmask *bmp1, const bitmask *bmp2, const bitmask *bmp3);}}
\begin{quote}

Logical \titlereference{and} of two bitmasks: bmp1 = bmp2 {\&} bmp3.

The bits that are set in the result bitmask, bmp1, are the
intersection of the bits that are set in the source bitmasks
bmp2 and bmp3.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-andnot}{}
\pdfbookmark[1]{7.31~~~bitmask{\_}andnot}{bitmask-andnot}
\subsection*{7.31~~~bitmask{\_}andnot}


%___________________________________________________________________________

\hypertarget{struct-bitmask-bitmask-andnot-struct-bitmask-bmp1-const-bitmask-bmp2-const-bitmask-bmp3}{}
\pdfbookmark[2]{struct bitmask *bitmask{\_}andnot(struct bitmask *bmp1, const bitmask *bmp2, const bitmask *bmp3);}{struct-bitmask-bitmask-andnot-struct-bitmask-bmp1-const-bitmask-bmp2-const-bitmask-bmp3}
\subsubsection*{\texttt{struct bitmask *bitmask{\_}andnot(struct bitmask *bmp1, const bitmask *bmp2, const bitmask *bmp3);}}
\begin{quote}

Logical \titlereference{andnot} of two bitmasks: bmp1 = bmp2 {\&} {\textasciitilde}bmp3.

The bits that are set in the result bitmask, bmp1, are the
the bits that are set in the source bitmask bmp2 but not in
bmp3.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-or}{}
\pdfbookmark[1]{7.32~~~bitmask{\_}or}{bitmask-or}
\subsection*{7.32~~~bitmask{\_}or}


%___________________________________________________________________________

\hypertarget{struct-bitmask-bitmask-or-struct-bitmask-bmp1-const-bitmask-bmp2-const-bitmask-bmp3}{}
\pdfbookmark[2]{struct bitmask *bitmask{\_}or(struct bitmask *bmp1, const bitmask *bmp2, const bitmask *bmp3);}{struct-bitmask-bitmask-or-struct-bitmask-bmp1-const-bitmask-bmp2-const-bitmask-bmp3}
\subsubsection*{\texttt{struct bitmask *bitmask{\_}or(struct bitmask *bmp1, const bitmask *bmp2, const bitmask *bmp3);}}
\begin{quote}

Logical \titlereference{or} of two bitmasks: bmp1 = bmp2 {\textbar} bmp3.

The bits that are set in the result bitmask, bmp1, are the
union of the bits that are set in either source bitmasks
bmp2 or bmp3.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-eor}{}
\pdfbookmark[1]{7.33~~~bitmask{\_}eor}{bitmask-eor}
\subsection*{7.33~~~bitmask{\_}eor}


%___________________________________________________________________________

\hypertarget{struct-bitmask-bitmask-eor-struct-bitmask-bmp1-const-bitmask-bmp2-const-bitmask-bmp3}{}
\pdfbookmark[2]{struct bitmask *bitmask{\_}eor(struct bitmask *bmp1, const bitmask *bmp2, const bitmask *bmp3);}{struct-bitmask-bitmask-eor-struct-bitmask-bmp1-const-bitmask-bmp2-const-bitmask-bmp3}
\subsubsection*{\texttt{struct bitmask *bitmask{\_}eor(struct bitmask *bmp1, const bitmask *bmp2, const bitmask *bmp3);}}
\begin{quote}

Logical \titlereference{eor} of two bitmasks: bmp1 = bmp2 {\textasciicircum} bmp3.

The bits that are set in the result bitmask, bmp1, are the
symmetric difference (in one or the other but not both) of
the bits that are set in the source bitmasks bmp2 or bmp3.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-first}{}
\pdfbookmark[1]{7.34~~~bitmask{\_}first}{bitmask-first}
\subsection*{7.34~~~bitmask{\_}first}


%___________________________________________________________________________

\hypertarget{int-bitmask-first-const-struct-bitmask-bmp}{}
\pdfbookmark[2]{int bitmask{\_}first(const struct bitmask *bmp);}{int-bitmask-first-const-struct-bitmask-bmp}
\subsubsection*{\texttt{int bitmask{\_}first(const struct bitmask *bmp);}}
\begin{quote}

Number of lowest set bit (min).
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-next}{}
\pdfbookmark[1]{7.35~~~bitmask{\_}next}{bitmask-next}
\subsection*{7.35~~~bitmask{\_}next}


%___________________________________________________________________________

\hypertarget{unsigned-int-bitmask-next-const-struct-bitmask-bmp-unsigned-int-i}{}
\pdfbookmark[2]{unsigned int bitmask{\_}next(const struct bitmask *bmp, unsigned int i);}{unsigned-int-bitmask-next-const-struct-bitmask-bmp-unsigned-int-i}
\subsubsection*{\texttt{unsigned int bitmask{\_}next(const struct bitmask *bmp, unsigned int i);}}
\begin{quote}

Number of next set bit above given bit i.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-rel-to-abs-pos}{}
\pdfbookmark[1]{7.36~~~bitmask{\_}rel{\_}to{\_}abs{\_}pos}{bitmask-rel-to-abs-pos}
\subsection*{7.36~~~bitmask{\_}rel{\_}to{\_}abs{\_}pos}


%___________________________________________________________________________

\hypertarget{unsigned-int-bitmask-rel-to-abs-pos-const-struct-bitmask-bmp-unsigned-int-n}{}
\pdfbookmark[2]{unsigned int bitmask{\_}rel{\_}to{\_}abs{\_}pos(const struct bitmask *bmp, unsigned int n);}{unsigned-int-bitmask-rel-to-abs-pos-const-struct-bitmask-bmp-unsigned-int-n}
\subsubsection*{\texttt{unsigned int bitmask{\_}rel{\_}to{\_}abs{\_}pos(const struct bitmask *bmp, unsigned int n);}}
\begin{quote}

Return the number of the nth set bit. Calling bitmask{\_}rel{\_}to{\_}abs{\_}pos(bmp, 0) is
equivalent to calling bitmask{\_}first(bmp). Calling bitmask{\_}rel{\_}to{\_}abs{\_}pos(bmp,
bitmask{\_}weight(bmp) - 1) is equivalent to calling bitmask{\_}last(bmp).
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-abs-to-rel-pos}{}
\pdfbookmark[1]{7.37~~~bitmask{\_}abs{\_}to{\_}rel{\_}pos}{bitmask-abs-to-rel-pos}
\subsection*{7.37~~~bitmask{\_}abs{\_}to{\_}rel{\_}pos}


%___________________________________________________________________________

\hypertarget{unsigned-int-bitmask-abs-to-rel-pos-const-struct-bitmask-bmp-unsigned-int-n}{}
\pdfbookmark[2]{unsigned int bitmask{\_}abs{\_}to{\_}rel{\_}pos(const struct bitmask *bmp, unsigned int n);}{unsigned-int-bitmask-abs-to-rel-pos-const-struct-bitmask-bmp-unsigned-int-n}
\subsubsection*{\texttt{unsigned int bitmask{\_}abs{\_}to{\_}rel{\_}pos(const struct bitmask *bmp, unsigned int n);}}
\begin{quote}

Return the relative bit position, amongst just the set bits, of the nth
bit, if the nth bit is set.  If the nth bit is not set, return bitmask{\_}nbytes.
For the bit positions that are set, bitmask{\_}abs{\_}to{\_}rel{\_}pos is the inverse
of bitmask{\_}rel{\_}to{\_}abs{\_}pos.
\end{quote}


%___________________________________________________________________________

\hypertarget{bitmask-last}{}
\pdfbookmark[1]{7.38~~~bitmask{\_}last}{bitmask-last}
\subsection*{7.38~~~bitmask{\_}last}


%___________________________________________________________________________

\hypertarget{unsigned-int-bitmask-last-const-struct-bitmask-bmp}{}
\pdfbookmark[2]{unsigned int bitmask{\_}last(const struct bitmask *bmp);}{unsigned-int-bitmask-last-const-struct-bitmask-bmp}
\subsubsection*{\texttt{unsigned int bitmask{\_}last(const struct bitmask *bmp);}}
\begin{quote}

Number of highest set bit (max).
\end{quote}

\end{document}
