From: Stephen Hemminger Date: Tue, 7 Nov 2017 02:15:34 +0000 (+0900) Subject: netem: use fixed rather than floating point for scaling X-Git-Tag: v4.16.0~143^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e4beb5278728125999d2d90c0edbb178217ff582;p=thirdparty%2Fiproute2.git netem: use fixed rather than floating point for scaling Don't need to do floating point math to compute scaled random. Signed-off-by: Stephen Hemminger --- diff --git a/tc/q_netem.c b/tc/q_netem.c index cdaddce9d..b97c351e0 100644 --- a/tc/q_netem.c +++ b/tc/q_netem.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -53,15 +54,12 @@ static void explain1(const char *arg) */ #define MAX_DIST (16*1024) -static const double max_percent_value = 0xffffffff; - /* scaled value used to percent of maximum. */ static void set_percent(__u32 *percent, double per) { - *percent = (unsigned int) rint(per * max_percent_value); + *percent = rint(per * UINT32_MAX); } - /* Parse either a fraction '.3' or percent '30% * return: 0 = ok, -1 = error, 1 = out of range */ @@ -89,7 +87,7 @@ static int get_percent(__u32 *percent, const char *str) static void print_percent(char *buf, int len, __u32 per) { - snprintf(buf, len, "%g%%", 100. * (double) per / max_percent_value); + snprintf(buf, len, "%g%%", (100. * per) / UINT32_MAX); } static char *sprint_percent(__u32 per, char *buf) @@ -323,7 +321,7 @@ static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv, /* netem option is "1-h" but kernel * expects "h". */ - gemodel.h = max_percent_value - gemodel.h; + gemodel.h = UINT32_MAX - gemodel.h; if (!NEXT_IS_NUMBER()) continue; @@ -630,7 +628,7 @@ static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) fprintf(f, " loss gemodel p %s", sprint_percent(gemodel->p, b1)); fprintf(f, " r %s", sprint_percent(gemodel->r, b1)); - fprintf(f, " 1-h %s", sprint_percent(max_percent_value - + fprintf(f, " 1-h %s", sprint_percent(UINT32_MAX - gemodel->h, b1)); fprintf(f, " 1-k %s", sprint_percent(gemodel->k1, b1)); }