diff options
author | Marek Olšák <[email protected]> | 2015-08-02 18:00:57 +0200 |
---|---|---|
committer | Marek Olšák <[email protected]> | 2015-08-06 20:44:36 +0200 |
commit | cbad30344d6e0b1ccc9fc8d5a8e6560e97dd9188 (patch) | |
tree | 4166c64d7cf92137e32d3b02235d553940dce0e2 /src/gallium/auxiliary/hud | |
parent | 97a65d90fe88e6b4b4a42d866b23e73ce72f6dc2 (diff) |
gallium/hud: replace assertions with clamping the unit index
Reviewed-by: Samuel Pitoiset <[email protected]>
Reviewed-by: Brian Paul <[email protected]>
Reviewed-by: Michel Dänzer <[email protected]>
Diffstat (limited to 'src/gallium/auxiliary/hud')
-rw-r--r-- | src/gallium/auxiliary/hud/hud_context.c | 42 |
1 files 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 |