GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
Macros
gp_clamp.h File Reference

Fast value clamping. More...

#include <core/gp_compiler.h>

Go to the source code of this file.

Macros

#define GP_CLAMP(val, min, max)
 Clamps a value.
 
#define GP_CLAMP_DOWN(val, max)
 Clamps down a value.
 
#define GP_SAT_ADD(val, add, max)
 Saturated addition.
 

Detailed Description

Fast value clamping.

Definition in file gp_clamp.h.

Macro Definition Documentation

◆ GP_CLAMP

#define GP_CLAMP (   val,
  min,
  max 
)
Value:
({ \
(GP_IS_CONSTANT(min) && GP_IS_CONSTANT(max) && \
min == 0 && max == 255) ? \
GP_CLAMP_INT_0_255(val) : \
GP_CLAMP_GENERIC(val, min, max); \
})
#define GP_IS_CONSTANT(x)
Expands to __buildin_constant_p() if supported by the compiler.
Definition gp_compiler.h:38

Clamps a value.

The resulting value will fit into [min, max] interval.

Parameters
valA value to be clamped.
minA lower limit.
maxAn upper limit.
Returns
A clamped value.

Definition at line 41 of file gp_clamp.h.

◆ GP_CLAMP_DOWN

#define GP_CLAMP_DOWN (   val,
  max 
)
Value:
({ \
typeof(val) gp_val__ = (val); \
typeof(val) gp_max__ = (max); \
gp_val__ > gp_max__ ? gp_max__ : gp_val__; \
})

Clamps down a value.

Parameters
valA value to be clamped.
maxAn uppper limit.
Returns
A clamped down value.

Definition at line 56 of file gp_clamp.h.

◆ GP_SAT_ADD

#define GP_SAT_ADD (   val,
  add,
  max 
)
Value:
({ \
typeof(val) gp_val__ = (val); \
typeof(add) gp_add__ = (add); \
typeof(val) gp_max__ = (max); \
typeof(val) gp_ret__; \
typeof(val) gp_add_neg__ = -gp_add__; \
if (gp_add__ > 0) \
gp_ret__ = gp_val__ + gp_add__ > gp_max__ ? gp_max__ : gp_val__ + gp_add__; \
else \
gp_ret__ = gp_val__ < gp_add_neg__ ? 0 : gp_val__ + gp_add__; \
gp_ret__; \
})

Saturated addition.

The resulting value will fit into the [0, max] interval.

Parameters
valA positive value to add to.
addA value to be added may be negative.
maxAn upper limit for the addition.
Returns
A saturated addition.

Definition at line 73 of file gp_clamp.h.