PER Firmware
Loading...
Searching...
No Matches
clamp.h File Reference

Utility header to clamp a value to a bounded range. More...

Go to the source code of this file.

Macros

#define DEFINE_CLAMP(type, name)
 
#define CLAMP(input, lower_bound, upper_bound)
 

Detailed Description

Utility header to clamp a value to a bounded range.

Typesafe, supporting ints and floats. Integer promotions also cover int8_t, uint8_t, int16_t, uint16_t

Author
Irving Wang (irvin.nosp@m.gw@p.nosp@m.urdue.nosp@m..edu)

Macro Definition Documentation

◆ CLAMP

#define CLAMP ( input,
lower_bound,
upper_bound )
Value:
_Generic((input) + (lower_bound) + (upper_bound), \
int: clamp_int, \
unsigned int: clamp_uint, \
long: clamp_long, \
unsigned long: clamp_ulong, \
float: clamp_float \
)(input, lower_bound, upper_bound)

◆ DEFINE_CLAMP

#define DEFINE_CLAMP ( type,
name )
Value:
[[gnu::always_inline]] \
static inline type clamp_##name(type input, type lower, type upper) { \
if (input < lower) return lower; \
if (input > upper) return upper; \
return input; \
}