]> git.ipfire.org Git - thirdparty/systemd.git/commit - src/basic/macro.h
macro: add DIV_ROUND_UP()
authorDavid Herrmann <dh.herrmann@gmail.com>
Mon, 29 Dec 2014 16:51:36 +0000 (17:51 +0100)
committerDavid Herrmann <dh.herrmann@gmail.com>
Tue, 30 Dec 2014 00:39:01 +0000 (01:39 +0100)
commit180a60bc879ab0554297bc08a7a0b9274b119b55
tree842c1afa088c3c26ce98856bcefde420dc6257c2
parent5ef378c1c5ba1a29a461e77685765da81163c853
macro: add DIV_ROUND_UP()

This macro calculates A / B but rounds up instead of down. We explicitly
do *NOT* use:
        (A + B - 1) / A
as it suffers from an integer overflow, even though the passed values are
properly tested against overflow. Our test-cases show this behavior.

Instead, we use:
        A / B + !!(A % B)

Note that on "Real CPUs" this does *NOT* result in two divisions. Instead,
instructions like idivl@x86 provide both, the quotient and the remainder.
Therefore, both algorithms should perform equally well (I didn't verify
this, though).
src/shared/macro.h
src/test/test-util.c