use type and context safe generic macros for CLAMP/q_min/q_max
parent
ee5d47e1c5
commit
a8422bd857
|
@ -23,15 +23,46 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|||
#ifndef spingle__common_h
|
||||
#define spingle__common_h
|
||||
|
||||
// comndef.h -- general definitions
|
||||
|
||||
#undef min
|
||||
#undef max
|
||||
#define q_min(a, b) (((a) < (b)) ? (a) : (b))
|
||||
#define q_max(a, b) (((a) > (b)) ? (a) : (b))
|
||||
#define CLAMP(_minval, x, _maxval) \
|
||||
((x) < (_minval) ? (_minval) : \
|
||||
(x) > (_maxval) ? (_maxval) : (x))
|
||||
#define q_minfunc(typ, name) \
|
||||
static inline typ name(typ a, typ b) {return a < b ? a : b;}
|
||||
|
||||
#define q_maxfunc(typ, name) \
|
||||
static inline typ name(typ a, typ b) {return a > b ? a : b;}
|
||||
|
||||
#define CLAMPfunc(typ, name) \
|
||||
static inline typ name(typ min, typ x, typ max) \
|
||||
{ \
|
||||
return x < min ? min : x > max ? max : x; \
|
||||
}
|
||||
|
||||
#define GenericTypes(func, name) \
|
||||
func(char, name##chr) \
|
||||
func(byte, name##byt) \
|
||||
func(int16_t, name##i16) \
|
||||
func(int32_t, name##i32) \
|
||||
func(int64_t, name##i64) \
|
||||
func(uint16_t, name##u16) \
|
||||
func(uint32_t, name##u32) \
|
||||
func(uint64_t, name##u64) \
|
||||
func(float, name##f) \
|
||||
func(double, name##d)
|
||||
|
||||
#define GenericDecl(typ, name) , typ: name
|
||||
|
||||
GenericTypes(q_minfunc, q_min)
|
||||
GenericTypes(q_maxfunc, q_max)
|
||||
GenericTypes(CLAMPfunc, CLAMP)
|
||||
|
||||
#define CLAMP(minval, x, maxval) \
|
||||
_Generic((x) GenericTypes(GenericDecl, CLAMP))(minval, x, maxval)
|
||||
|
||||
#define q_min(a, b) _Generic((a) GenericTypes(GenericDecl, q_min))(a, b)
|
||||
#define q_max(a, b) _Generic((a) GenericTypes(GenericDecl, q_max))(a, b)
|
||||
|
||||
#undef q_minfunc
|
||||
#undef q_maxfunc
|
||||
#undef CLAMPfunc
|
||||
|
||||
typedef struct sizebuf_s
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue