1.\" $NetBSD: roundup.9,v 1.9 2019/10/02 08:21:08 rin 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 October 2, 2019 31.Dt ROUNDUP 9 32.Os 33.Sh NAME 34.Nm roundup 35.Nd macros for counting and rounding 36.Sh SYNOPSIS 37.In sys/param.h 38.Ft size 39.Fn howmany "x" "size" 40.Ft size 41.Fn roundup "x" "size" 42.Ft size 43.Fn rounddown "x" "size" 44.Ft size 45.Fn roundup2 "x" "size" 46.Ft size 47.Fn rounddown2 "x" "size" 48.Ft int 49.Fn powerof2 "x" 50.Sh DESCRIPTION 51The 52.Fn roundup 53and 54.Fn rounddown 55macros return an integer from rounding 56.Fa x 57up and down, respectively, to the next 58.Fa size . 59The 60.Fn howmany 61macro in turn reveals how many times 62.Fa size 63fits into 64.Fa x , 65rounding the residual up. 66.Pp 67The 68.Fn roundup2 69and 70.Fn rounddown2 71macros also round up and down, respectively, but with the assumption that 72.Fa size 73is a power of two. 74If 75.Fa x 76is indeed a power of two, 77.Fn powerof2 78return 1. 79.Sh RETURN VALUES 80The return value is an integer from the respective operation. 81If 82.Fa x 83is 0, all macros except 84.Fn powerof2 85return 0. 86The behavior is undefined if 87.Fa size 88is 0. 89.Sh EXAMPLES 90The following example rounds the variable 91.Va rx 92to a 32-bit boundary: 93.Bd -literal -offset indent 94uint16_t rx; 95 96\&... 97 98rx = roundup2(rx, sizeof(uint32_t)); 99.Ed 100.Sh SEE ALSO 101.Xr ilog2 3 , 102.Xr param 3 , 103.Xr imax 9 104.Sh CAVEATS 105All described macros make no assumptions about the type of the parameters. 106These are implicitly assumed to be unsigned integers. 107