XenevaOS
Loading...
Searching...
No Matches
minmax.h
Go to the documentation of this file.
1#ifndef __LINUX_MINMAX_H__
2#define __LINUX_MINMAX_H__
3
4/*
5 * DCL <linux/minmax.h> -- min/max/min_t/max_t/clamp/ARRAY_SIZE.
6 *
7 * Every definition here is guarded *and* token-identical to the copies already
8 * in <linux/kernel.h> and <linux/mm.h>, because those two define min() and
9 * ARRAY_SIZE() themselves (mm.h guards, kernel.h does not). Two headers
10 * spelling the same macro differently is a -Wmacro-redefined warning at best
11 * and, for ARRAY_SIZE, a mismatch in parameter names that the preprocessor
12 * counts as a real difference. Matching exactly means whichever header loads
13 * first wins silently -- which is what the tty_buffer.c include list, with
14 * both <linux/minmax.h> and <linux/mm.h> reachable, needs.
15 *
16 * min_t()/max_t() do the cast *before* the comparison; that is the whole
17 * point, and it is what tty_buffer.c:315 relies on when it writes
18 * `min_t(size_t, size - copied, TTY_BUFFER_PAGE)` -- without the cast, an
19 * int and a size_t would be compared under the usual arithmetic conversions
20 * and a negative difference would wrap to a huge unsigned value.
21 */
22
23#ifndef min
24#define min(a, b) (((a) < (b)) ? (a) : (b))
25#endif
26
27#ifndef max
28#define max(a, b) (((a) > (b)) ? (a) : (b))
29#endif
30
31#ifndef min_t
32#define min_t(type, a, b) ((type)(a) < (type)(b) ? (type)(a) : (type)(b))
33#endif
34
35#ifndef max_t
36#define max_t(type, a, b) ((type)(a) > (type)(b) ? (type)(a) : (type)(b))
37#endif
38
39/*
40 * MAX_T/MIN_T -- mainline's typed spelling of the two above, defined in terms
41 * of them rather than repeated, so the cast-before-compare property is not a
42 * second thing to get right. Their only caller is drm_color_mgmt.c:539's
43 * `MAX_T(int, DRM_COLOR_ENCODING_MAX, ret)`, which clamps a driver's
44 * get_color_encoding() answer into the connector's enum range -- the cast
45 * matters because the return is a signed int and one arm of the comparison is
46 * an enum constant, and a negative answer must stay negative rather than
47 * wrap past the enum bound.
48 */
49#ifndef MAX_T
50#define MAX_T(type, a, b) max_t(type, a, b)
51#endif
52
53#ifndef MIN_T
54#define MIN_T(type, a, b) min_t(type, a, b)
55#endif
56
57/*
58 * swap(a, b) -- exchange two lvalues in place, mainline's spelling
59 * (minmax.h:307). The local is declared with typeof(a) rather than a fixed
60 * type so it keeps whatever width and signedness the first argument has --
61 * drm_crtc.c:688 swaps two `int` display dimensions before the clipping
62 * arithmetic (a rotated primary plane is checked in the axes the framebuffer
63 * is sampled on, not the ones the mode is stated in), while vkms_formats.c
64 * swaps __u16 pixel components at :267 and u64 fixed-point matrix entries at
65 * :867-869. A typed helper would force a cast at two of the three.
66 *
67 * do/while(0) rather than a bare comma expression, because swap() is a
68 * statement and mainline spells it that way: `if (rotated) swap(a, b);
69 * else ...` has to stay one statement. Same shape as assign_bit() in
70 * bitops.h and WRITE_ONCE() in kernel.h.
71 */
72#ifndef swap
73#define swap(a, b) \
74 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
75#endif
76
77#ifndef clamp
78#define clamp(val, lo, hi) ((val) < (lo) ? (lo) : ((val) > (hi) ? (hi) : (val)))
79#endif
80
81/*
82 * clamp_val -- mainline's spelling here is
83 * `__careful_clamp(typeof(val), val, lo, hi)`, which typechecks the three
84 * arguments through __types_ok3()/BUILD_BUG_ON_MSG; DCL has none of that
85 * apparatus, and both callers pass a lo/hi pair already in the type they
86 * mean: drm_color_mgmt.c:144 clamps an s64 magnitude against literals, and
87 * vkms_composer.c:231-234 clamps four __u16 pixel components against 0 and
88 * 0xffff. So this is the plain comparison, and the name is kept only because
89 * the callers are mainline's.
90 */
91#ifndef clamp_val
92#define clamp_val(val, lo, hi) clamp((val), (lo), (hi))
93#endif
94
95#ifndef ARRAY_SIZE
96#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
97#endif
98
99#endif /* __LINUX_MINMAX_H__ */