PER Firmware
Loading...
Searching...
No Matches
clamp.h
Go to the documentation of this file.
1#ifndef CLAMP_H
2#define CLAMP_H
3
14#define DEFINE_CLAMP(type, name) \
15[[gnu::always_inline]] \
16static inline type clamp_##name(type input, type lower, type upper) { \
17 if (input < lower) return lower; \
18 if (input > upper) return upper; \
19 return input; \
20}
21
22DEFINE_CLAMP(int, int)
23DEFINE_CLAMP(unsigned int, uint)
24DEFINE_CLAMP(long, long)
25DEFINE_CLAMP(unsigned long, ulong)
26DEFINE_CLAMP(float, float)
27
28#define CLAMP(input, lower_bound, upper_bound) \
29 _Generic((input) + (lower_bound) + (upper_bound), \
30 int: clamp_int, \
31 unsigned int: clamp_uint, \
32 long: clamp_long, \
33 unsigned long: clamp_ulong, \
34 float: clamp_float \
35)(input, lower_bound, upper_bound)
36
37
38#endif // CLAMP_H