xref: /netbsd-src/share/man/man3/ilog2.3 (revision 17baa91bd87205efc5334c9d62e53cf2fe231545)
1.\" $NetBSD: ilog2.3,v 1.6 2021/11/05 22:39:35 riastradh Exp $ $
2.\"
3.\" Copyright (c) 2010 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Jukka Ruohonen.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28.\" POSSIBILITY OF SUCH DAMAGE.
29.\"
30.Dd November 5, 2021
31.Dt ILOG2 3
32.Os
33.Sh NAME
34.Nm ilog2
35.Nd integer base-2 logarithm
36.Sh SYNOPSIS
37.In sys/bitops.h
38.Ft int
39.Fn ilog2 "size x"
40.Sh DESCRIPTION
41For positive
42.Fa x ,
43the
44.Fn ilog2
45macro returns the integer part of the base-2 logarithm of
46.Fa x ;
47that is,
48.Fo floor
49.Fn log_2 x
50.Fc .
51If
52.Fa n Li = Fn ilog2 x ,
53then
54.Fa n
55is the largest integer such that
56.No 2** Ns Fa n Li <= Fa x ;
57in other words,
58.Fn ilog2
59returns the largest integer to which 2 can be raised to obtain a value
60at most
61.Fa x .
62.Pp
63The type of the input parameter must be unsigned.
64.Pp
65If
66.Fa x
67is a constant expression, then so is
68.Fn ilog2 x .
69.Sh ERRORS
70.Fn ilog2
71returns \-1 when
72.Fa x
73is zero.
74.Sh SEE ALSO
75.Xr bitops 3 ,
76.Xr ffs 3 ,
77.Xr ilogb 3 ,
78.Xr log2 3 ,
79.Xr imax 9 ,
80.Xr powerof2 9
81.Sh HISTORY
82The
83.Fn ilog2
84macro first appeared in
85.Nx 5.0 .
86