]> git.ipfire.org Git - thirdparty/systemd.git/commit
util: add some helpers for converting percent/permille/permyriad to parts of 2^32-1
authorLennart Poettering <lennart@poettering.net>
Wed, 17 Feb 2021 15:53:11 +0000 (16:53 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 18 Feb 2021 21:36:34 +0000 (22:36 +0100)
commit3b6e71ad0351c5ed2dd8382a63ab3581fa2d6174
treeb231e01ef6f3b58582cd0ff374f22b129a8a5235
parent38d0c270063c9d7379b45b80b20b179a713edc6e
util: add some helpers for converting percent/permille/permyriad to parts of 2^32-1

At various places we accept values scaled to the range 0…2^32-1 which
are exposed to the user as percentages/permille/permyriad. Let's add
some helper macros (actually: typesafe macro-like functions) that help
with converting our internal encoding to the external encodings.

benefits: some of the previous code rounded up, some down. let's always
round to nearest, to ensure that our conversions are reversible. Also,
check for overflows correctly.

This also adds a test that makes sure that for the full
percent/permille/permyriad ranges we can convert forth and back without
loss of accuracy.
src/basic/percent-util.h
src/test/test-percent-util.c