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 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef _LINUXKPI_LINUX_MINMAX_H_ 34 #define _LINUXKPI_LINUX_MINMAX_H_ 35 36 #include <linux/build_bug.h> 37 #include <linux/compiler.h> 38 #include <linux/types.h> 39 40 #define min(x, y) ((x) < (y) ? (x) : (y)) 41 #define max(x, y) ((x) > (y) ? (x) : (y)) 42 43 #define min3(a, b, c) min(a, min(b, c)) 44 #define max3(a, b, c) max(a, max(b, c)) 45 46 #define min_t(type, x, y) ({ \ 47 type __min1 = (x); \ 48 type __min2 = (y); \ 49 __min1 < __min2 ? __min1 : __min2; }) 50 51 #define max_t(type, x, y) ({ \ 52 type __max1 = (x); \ 53 type __max2 = (y); \ 54 __max1 > __max2 ? __max1 : __max2; }) 55 56 #define clamp_t(type, _x, min, max) min_t(type, max_t(type, _x, min), max) 57 #define clamp(x, lo, hi) min(max(x, lo), hi) 58 #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) 59 60 /* Swap values of a and b */ 61 #define swap(a, b) do { \ 62 __typeof(a) _swap_tmp = a; \ 63 a = b; \ 64 b = _swap_tmp; \ 65 } while (0) 66 67 #endif /* _LINUXKPI_LINUX_MINMAX_H_ */ 68