roundup.9 revision 1.9
$NetBSD: roundup.9,v 1.9 2019/10/02 08:21:08 rin Exp $

Copyright (c) 2010 The NetBSD Foundation, Inc.
All rights reserved.

This code is derived from software contributed to The NetBSD Foundation
by Jukka Ruohonen.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

.Dd October 2, 2019 .Dt ROUNDUP 9 .Os .Sh NAME .Nm roundup .Nd macros for counting and rounding .Sh SYNOPSIS n sys/param.h .Ft size .Fn howmany "x" "size" .Ft size .Fn roundup "x" "size" .Ft size .Fn rounddown "x" "size" .Ft size .Fn roundup2 "x" "size" .Ft size .Fn rounddown2 "x" "size" .Ft int .Fn powerof2 "x" .Sh DESCRIPTION The .Fn roundup and .Fn rounddown macros return an integer from rounding .Fa x up and down, respectively, to the next .Fa size . The .Fn howmany macro in turn reveals how many times .Fa size fits into .Fa x , rounding the residual up.

p The .Fn roundup2 and .Fn rounddown2 macros also round up and down, respectively, but with the assumption that .Fa size is a power of two. If .Fa x is indeed a power of two, .Fn powerof2 return 1. .Sh RETURN VALUES The return value is an integer from the respective operation. If .Fa x is 0, all macros except .Fn powerof2 return 0. The behavior is undefined if .Fa size is 0. .Sh EXAMPLES The following example rounds the variable .Va rx to a 32-bit boundary: d -literal -offset indent uint16_t rx; ... rx = roundup2(rx, sizeof(uint32_t)); .Ed .Sh SEE ALSO .Xr ilog2 3 , .Xr param 3 , .Xr imax 9 .Sh CAVEATS All described macros make no assumptions about the type of the parameters. These are implicitly assumed to be unsigned integers.