From f47253c5c70339d23e48cb2e15e1626ffc79c690 Mon Sep 17 00:00:00 2001 From: Jose Fonseca Date: Fri, 11 Nov 2022 15:55:11 +0000 Subject: [PATCH] draw,util: Refactor draw_overflow_uadd into util. So it can be used outside draw. Also drop the overflow_value parameter, as it wasn't meaningfully useful. Reviewed-by: Brian Paul Reviewed-by: Roland Scheidegger Part-of: --- src/gallium/auxiliary/draw/draw_private.h | 17 ----------------- src/gallium/auxiliary/draw/draw_pt.c | 6 +----- src/gallium/auxiliary/draw/draw_pt_vsplit.c | 5 +---- src/util/u_math.h | 14 ++++++++++++++ 4 files changed, 16 insertions(+), 26 deletions(-) diff --git a/src/gallium/auxiliary/draw/draw_private.h b/src/gallium/auxiliary/draw/draw_private.h index 6b3de1d40cc..f3bcff49077 100644 --- a/src/gallium/auxiliary/draw/draw_private.h +++ b/src/gallium/auxiliary/draw/draw_private.h @@ -588,21 +588,4 @@ draw_clamp_viewport_idx(int idx) return ((PIPE_MAX_VIEWPORTS > idx && idx >= 0) ? idx : 0); } - -/** - * Adds two unsigned integers and if the addition - * overflows then it returns the value from - * the overflow_value variable. - */ -static inline unsigned -draw_overflow_uadd(unsigned a, unsigned b, - unsigned overflow_value) -{ - unsigned res = a + b; - if (res < a) { - res = overflow_value; - } - return res; -} - #endif /* DRAW_PRIVATE_H */ diff --git a/src/gallium/auxiliary/draw/draw_pt.c b/src/gallium/auxiliary/draw/draw_pt.c index 9d716732d59..676f5263a7a 100644 --- a/src/gallium/auxiliary/draw/draw_pt.c +++ b/src/gallium/auxiliary/draw/draw_pt.c @@ -360,13 +360,9 @@ prim_restart_loop(struct draw_context *draw, struct pipe_draw_start_count_bias cur = *draw_info; cur.count = 0; - /* The largest index within a loop using the i variable as the index. - * Used for overflow detection */ - const unsigned MAX_LOOP_IDX = 0xffffffff; - for (unsigned j = 0; j < draw_info->count; j++) { unsigned index = 0; - unsigned i = draw_overflow_uadd(draw_info->start, j, MAX_LOOP_IDX); + unsigned i = util_clamped_uadd(draw_info->start, j); if (i < elt_max) { switch (draw->pt.user.eltSize) { case 1: diff --git a/src/gallium/auxiliary/draw/draw_pt_vsplit.c b/src/gallium/auxiliary/draw/draw_pt_vsplit.c index 0455f40dff7..42f01536f5e 100644 --- a/src/gallium/auxiliary/draw/draw_pt_vsplit.c +++ b/src/gallium/auxiliary/draw/draw_pt_vsplit.c @@ -33,9 +33,6 @@ #define SEGMENT_SIZE 1024 #define MAP_SIZE 256 -/* The largest possible index within an index buffer */ -#define MAX_ELT_IDX 0xffffffff - struct vsplit_frontend { struct draw_pt_front_end base; struct draw_context *draw; @@ -116,7 +113,7 @@ vsplit_add_cache(struct vsplit_frontend *vsplit, unsigned fetch) static inline unsigned vsplit_get_base_idx(unsigned start, unsigned fetch) { - return draw_overflow_uadd(start, fetch, MAX_ELT_IDX); + return util_clamped_uadd(start, fetch); } diff --git a/src/util/u_math.h b/src/util/u_math.h index 21037cb3a46..75b9bbbc70e 100644 --- a/src/util/u_math.h +++ b/src/util/u_math.h @@ -809,6 +809,20 @@ util_quantize_lod_bias(float lod) return roundf(lod * 256) / 256; } +/** + * Adds two unsigned integers and if the addition + * overflows then clamp it to ~0U. + */ +static inline unsigned +util_clamped_uadd(unsigned a, unsigned b) +{ + unsigned res = a + b; + if (res < a) { + res = ~0U; + } + return res; +} + #ifdef __cplusplus } #endif