summaryrefslogtreecommitdiffstats
path: root/ccan/ccan/minmax/minmax.h
blob: d111d1bc3809ae3e81078c760ef4eadbcbc62b62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* CC0 (Public domain) - see LICENSE file for details */
#ifndef CCAN_MINMAX_H
#define CCAN_MINMAX_H

#include "config.h"

#include <ccan/build_assert/build_assert.h>

#if !HAVE_STATEMENT_EXPR || !HAVE_TYPEOF
/*
 * Without these, there's no way to avoid unsafe double evaluation of
 * the arguments
 */
#error Sorry, minmax module requires statement expressions and typeof
#endif

#if HAVE_BUILTIN_TYPES_COMPATIBLE_P
#define MINMAX_ASSERT_COMPATIBLE(a, b) \
	BUILD_ASSERT(__builtin_types_compatible_p(a, b))
#else
#define MINMAX_ASSERT_COMPATIBLE(a, b) \
	do { } while (0)
#endif

#define min(a, b) \
	({ \
		typeof(a) _a = (a); \
		typeof(b) _b = (b); \
		MINMAX_ASSERT_COMPATIBLE(typeof(_a), typeof(_b)); \
		_a < _b ? _a : _b; \
	})

#define max(a, b) \
	({ \
		typeof(a) _a = (a); \
		typeof(b) _b = (b); \
		MINMAX_ASSERT_COMPATIBLE(typeof(_a), typeof(_b)); \
		_a > _b ? _a : _b; \
	})

#define clamp(v, f, c)	(max(min((v), (c)), (f)))


#define min_t(t, a, b) \
	({ \
		t _ta = (a); \
		t _tb = (b); \
		min(_ta, _tb); \
	})
#define max_t(t, a, b) \
	({ \
		t _ta = (a); \
		t _tb = (b); \
		max(_ta, _tb); \
	})

#define clamp_t(t, v, f, c) \
	({ \
		t _tv = (v); \
		t _tf = (f); \
		t _tc = (c); \
		clamp(_tv, _tf, _tc); \
	})

#endif /* CCAN_MINMAX_H */