xref: /freebsd/sys/compat/linuxkpi/common/include/linux/minmax.h (revision e9ac41698b2f322d55ccf9da50a3596edb2c1800)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2010 iX Systems, Inc.
5  * Copyright (c) 2010 Panasas, Inc.
6  * Copyright (c) 2013-2015 Mellanox Technologies, Ltd.
7  * Copyright (c) 2014-2015 François Tigeot
8  * Copyright (c) 2015 Hans Petter Selasky <hselasky@FreeBSD.org>
9  * Copyright (c) 2016 Matt Macy <mmacy@FreeBSD.org>
10  * Copyright (c) 2023 Serenity Cyber Security, LLC.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef _LINUXKPI_LINUX_MINMAX_H_
35 #define	_LINUXKPI_LINUX_MINMAX_H_
36 
37 #include <linux/build_bug.h>
38 #include <linux/compiler.h>
39 #include <linux/types.h>
40 
41 #define	min(x, y)	((x) < (y) ? (x) : (y))
42 #define	max(x, y)	((x) > (y) ? (x) : (y))
43 
44 #define	min3(a, b, c)	min(a, min(b, c))
45 #define	max3(a, b, c)	max(a, max(b, c))
46 
47 #define min_not_zero(x, y) ({						\
48 	__typeof(x) __min1 = (x);					\
49 	__typeof(y) __min2 = (y);					\
50 	__min1 == 0 ? __min2 : ((__min2 == 0) ? __min1 : min(__min1, __min2));\
51 })
52 
53 #define	min_t(type, x, y) ({			\
54 	type __min1 = (x);			\
55 	type __min2 = (y);			\
56 	__min1 < __min2 ? __min1 : __min2; })
57 
58 #define	max_t(type, x, y) ({			\
59 	type __max1 = (x);			\
60 	type __max2 = (y);			\
61 	__max1 > __max2 ? __max1 : __max2; })
62 
63 #define	clamp_t(type, _x, min, max)	min_t(type, max_t(type, _x, min), max)
64 #define	clamp(x, lo, hi)		min(max(x, lo), hi)
65 #define	clamp_val(val, lo, hi)	clamp_t(typeof(val), val, lo, hi)
66 
67 /* Swap values of a and b */
68 #define swap(a, b) do {			\
69 	__typeof(a) _swap_tmp = a;	\
70 	a = b;				\
71 	b = _swap_tmp;			\
72 } while (0)
73 
74 #endif /* _LINUXKPI_LINUX_MINMAX_H_ */
75