summaryrefslogtreecommitdiffstats
path: root/src/commonlib/include/commonlib/clamp.h
blob: e01a107ed47cb2fbeb551c6abd25c47f6dc4222d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* SPDX-License-Identifier: GPL-2.0-only */

#ifndef COMMONLIB_CLAMP_H
#define COMMONLIB_CLAMP_H

#include <stdint.h>

/*
 * Clamp a value, so that it is between a lower and an upper bound.
 */
static inline u32 clamp_u32(const u32 min, const u32 val, const u32 max)
{
	if (val > max)
		return max;

	if (val < min)
		return min;

	return val;
}

#endif /* COMMONLIB_CLAMP_H */