]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/percent-util.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 int parse_percent_unbounded(const char *p
);
10 int parse_percent(const char *p
);
12 int parse_permille_unbounded(const char *p
);
13 int parse_permille(const char *p
);
15 int parse_permyriad_unbounded(const char *p
);
16 int parse_permyriad(const char *p
);
18 /* Some macro-like helpers that convert a percent/permille/permyriad value (as parsed by parse_percent()) to
19 * a value relative to 100% == 2^32-1. Rounds to closest. */
20 static inline uint32_t UINT32_SCALE_FROM_PERCENT(int percent
) {
21 assert_cc(INT_MAX
<= UINT32_MAX
);
23 return (uint32_t) (((uint64_t) CLAMP(percent
, 0, 100) * UINT32_MAX
+ 50) / 100U);
26 static inline uint32_t UINT32_SCALE_FROM_PERMILLE(int permille
) {
27 return (uint32_t) (((uint64_t) CLAMP(permille
, 0, 1000) * UINT32_MAX
+ 500) / 1000U);
30 static inline uint32_t UINT32_SCALE_FROM_PERMYRIAD(int permyriad
) {
31 return (uint32_t) (((uint64_t) CLAMP(permyriad
, 0, 10000) * UINT32_MAX
+ 5000) / 10000U);
34 static inline int UINT32_SCALE_TO_PERCENT(uint32_t scale
) {
37 u
= (uint32_t) ((((uint64_t) scale
) * 100U + UINT32_MAX
/2) / UINT32_MAX
);
44 static inline int UINT32_SCALE_TO_PERMILLE(uint32_t scale
) {
47 u
= (uint32_t) ((((uint64_t) scale
) * 1000U + UINT32_MAX
/2) / UINT32_MAX
);
54 static inline int UINT32_SCALE_TO_PERMYRIAD(uint32_t scale
) {
57 u
= (uint32_t) ((((uint64_t) scale
) * 10000U + UINT32_MAX
/2) / UINT32_MAX
);
64 #define PERMYRIAD_AS_PERCENT_FORMAT_STR "%i.%02i%%"
65 #define PERMYRIAD_AS_PERCENT_FORMAT_VAL(x) ((x)/100), ((x)%100)