Commit fabeb4f1 authored by Niklas Haas's avatar Niklas Haas
Browse files

utils: add PL_CLAMP helper

I noticed that a couple of usages of PL_MIN(PL_MAX and vice versa
involve a wasteful double evaluation. Plus, the argument order of
PL_CLAMP is way easier to remember.
parent c8010c4e
......@@ -131,6 +131,7 @@ static inline float *pl_transpose(int dim, float *out, const float *in)
// Helper functions for some common numeric operations (careful: double-eval)
#define PL_MAX(x, y) ((x) > (y) ? (x) : (y))
#define PL_MIN(x, y) ((x) < (y) ? (x) : (y))
#define PL_CLAMP(x, l, h) ((x) < (l) ? (l) : (x) > (h) ? (h) : (x))
#define PL_CMP(a, b) ((a) < (b) ? -1 : (a) > (b) ? 1 : 0)
#define PL_DEF(x, d) ((x) ? (x) : (d))
#define PL_SQUARE(x) ((x) * (x))
......
......@@ -496,7 +496,7 @@ static int ccStrPrintDouble( char *str, int bufsize, int decimals, double value
for( index = 0 ; index < decimals ; index++ )
{
frac = (int32_t)( value * muldec ) - accumsub;
frac = PL_MIN(PL_MAX(frac, 0), 9); // FIXME: why is this needed?
frac = PL_CLAMP(frac, 0, 9); // FIXME: why is this needed?
str[index] = '0' + (char)frac;
accumsub += frac;
accumsub = ( accumsub << 3 ) + ( accumsub << 1 );
......
......@@ -1434,10 +1434,10 @@ void pl_pass_run(const struct pl_gpu *gpu, const struct pl_pass_run_params *para
// Constrain the scissors to the target dimension (to sanitize the
// underlying graphics API calls)
sc->x0 = PL_MAX(0, PL_MIN(target->params.w, sc->x0));
sc->y0 = PL_MAX(0, PL_MIN(target->params.h, sc->y0));
sc->x1 = PL_MAX(0, PL_MIN(target->params.w, sc->x1));
sc->y1 = PL_MAX(0, PL_MIN(target->params.h, sc->y1));
sc->x0 = PL_CLAMP(sc->x0, 0, target->params.w);
sc->y0 = PL_CLAMP(sc->y0, 0, target->params.h);
sc->x1 = PL_CLAMP(sc->x1, 0, target->params.w);
sc->y1 = PL_CLAMP(sc->y1, 0, target->params.h);
// Scissors wholly outside target -> silently drop pass (also needed
// to ensure we don't cause UB by specifying invalid scissors)
......
......@@ -335,7 +335,7 @@ static void generate_grain_y(float out[GRAIN_HEIGHT_LUT][GRAIN_WIDTH_LUT],
}
int16_t grain = buf[y][x] + round2(sum, data->ar_coeff_shift);
grain = PL_MAX(scale.grain_min, PL_MIN(scale.grain_max, grain));
grain = PL_CLAMP(grain, scale.grain_min, scale.grain_max);
buf[y][x] = grain;
}
}
......@@ -414,7 +414,7 @@ static void generate_grain_uv(float *out, int16_t buf[GRAIN_HEIGHT][GRAIN_WIDTH]
}
int16_t grain = buf[y][x] + round2(sum, data->ar_coeff_shift);
grain = PL_MAX(scale.grain_min, PL_MIN(scale.grain_max, grain));
grain = PL_CLAMP(grain, scale.grain_min, scale.grain_max);
buf[y][x] = grain;
}
}
......
......@@ -469,8 +469,8 @@ static bool update_swapchain_info(struct priv *p, VkSwapchainCreateInfoKHR *info
}
// Clamp the extent based on the supported limits
w = PL_MIN(PL_MAX(w, caps.minImageExtent.width), caps.maxImageExtent.width);
h = PL_MIN(PL_MAX(h, caps.minImageExtent.height), caps.maxImageExtent.height);
w = PL_CLAMP(w, caps.minImageExtent.width, caps.maxImageExtent.width);
h = PL_CLAMP(h, caps.minImageExtent.height, caps.maxImageExtent.height);
info->imageExtent = (VkExtent2D) { w, h };
// We just request whatever makes sense, and let the pl_vk decide what
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment