ROUNDUP(9)

πŸ“¦
version 11.0 Β· 2 versions
, in nixpkgs unstable

✨ ROUNDUP(9) ✨

okay bestie, welcome to ROUNDUP(9) from the Kernel Developer's Manual β€” this is the one about macros that count and round, and honestly it's giving math-girlie main character energy πŸ’…


🌸 NAME 🌸

roundup β€” macros for counting and rounding. no cap, that's literally it.


πŸŽ€ SYNOPSIS πŸŽ€

#include <sys/param.h>

size
howmany(x, size);

size
roundup(x, size);

size
rounddown(x, size);

size
roundup2(x, size);

size
rounddown2(x, size);

int
powerof2(x);

πŸ’– DESCRIPTION πŸ’–

so the roundup() and rounddown() macros return an integer from rounding x up and down, respectively, to the next size. and the howmany() macro? it reveals how many times size fits into x, rounding the residual up. periodt.

the roundup2() and rounddown2() macros also round up and down, respectively, but with the assumption that size is a power of two. and if x is indeed a power of two, powerof2() returns 1. slay.


πŸ¦‹ RETURN VALUES πŸ¦‹

the return value is an integer from the respective operation. if x is 0, all macros except powerof2() return 0. and the behavior is undefined if size is 0 β€” like, don't do that, bestie 😭


🌈 EXAMPLES 🌈

the following example rounds the variable rx to a 32-bit boundary:

uint16_t rx;

...

rx = roundup2(rx, sizeof(uint32_t));

iconic behavior, honestly ✨


🫢 SEE ALSO 🫢


πŸ’€ CAVEATS πŸ’€

all described macros make no assumptions about the type of the parameters. these are implicitly assumed to be unsigned integers. so like, vibe check your types before you send it, fr fr 🧚


this iconic little page is from Debian, dated October 2, 2019 β€” and that's the whole manual, bestie. you're welcome πŸ’œ

πŸ“¦ where this page comes from