From: Marek Olšák Date: Sun, 2 Aug 2015 16:00:57 +0000 (+0200) Subject: gallium/hud: replace assertions with clamping the unit index X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=cbad30344d6e0b1ccc9fc8d5a8e6560e97dd9188;p=mesa.git gallium/hud: replace assertions with clamping the unit index Reviewed-by: Samuel Pitoiset Reviewed-by: Brian Paul Reviewed-by: Michel Dänzer --- diff --git a/src/gallium/auxiliary/hud/hud_context.c b/src/gallium/auxiliary/hud/hud_context.c index decc055b21a..d3ad271f515 100644 --- a/src/gallium/auxiliary/hud/hud_context.c +++ b/src/gallium/auxiliary/hud/hud_context.c @@ -242,43 +242,47 @@ number_to_human_readable(uint64_t num, enum pipe_driver_query_type type, {" us", " ms", " s"}; /* based on microseconds */ static const char *hz_units[] = {" Hz", " KHz", " MHz", " GHz"}; - const char *suffix; + static const char *percent_units[] = {"%"}; + + const char **units; + unsigned max_unit; double divisor = (type == PIPE_DRIVER_QUERY_TYPE_BYTES) ? 1024 : 1000; - int unit = 0; + unsigned unit = 0; double d = num; - while (d > divisor) { - d /= divisor; - unit++; - } - switch (type) { case PIPE_DRIVER_QUERY_TYPE_MICROSECONDS: - assert(unit < ARRAY_SIZE(time_units)); - suffix = time_units[unit]; + max_unit = ARRAY_SIZE(time_units)-1; + units = time_units; break; case PIPE_DRIVER_QUERY_TYPE_PERCENTAGE: - suffix = "%"; + max_unit = ARRAY_SIZE(percent_units)-1; + units = percent_units; break; case PIPE_DRIVER_QUERY_TYPE_BYTES: - assert(unit < ARRAY_SIZE(byte_units)); - suffix = byte_units[unit]; + max_unit = ARRAY_SIZE(byte_units)-1; + units = byte_units; break; case PIPE_DRIVER_QUERY_TYPE_HZ: - assert(unit < ARRAY_SIZE(hz_units)); - suffix = hz_units[unit]; + max_unit = ARRAY_SIZE(hz_units)-1; + units = hz_units; break; default: - assert(unit < ARRAY_SIZE(metric_units)); - suffix = metric_units[unit]; + max_unit = ARRAY_SIZE(metric_units)-1; + units = metric_units; + } + + while (d > divisor && unit < max_unit) { + d /= divisor; + unit++; } if (d >= 100 || d == (int)d) - sprintf(out, "%.0f%s", d, suffix); + sprintf(out, "%.0f%s", d, units[unit]); else if (d >= 10 || d*10 == (int)(d*10)) - sprintf(out, "%.1f%s", d, suffix); + sprintf(out, "%.1f%s", d, units[unit]); else - sprintf(out, "%.2f%s", d, suffix); + sprintf(out, "%.2f%s", d, units[unit]); } static void