From cbad30344d6e0b1ccc9fc8d5a8e6560e97dd9188 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marek=20Ol=C5=A1=C3=A1k?= Date: Sun, 2 Aug 2015 18:00:57 +0200 Subject: [PATCH] gallium/hud: replace assertions with clamping the unit index MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Reviewed-by: Samuel Pitoiset Reviewed-by: Brian Paul Reviewed-by: Michel Dänzer --- src/gallium/auxiliary/hud/hud_context.c | 42 ++++++++++++++----------- 1 file changed, 23 insertions(+), 19 deletions(-) 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 -- 2.30.2