![]() |
|
|||
0001 /* SPDX-License-Identifier: GPL-2.0 */ 0002 #ifndef _LINUX_MINMAX_H 0003 #define _LINUX_MINMAX_H 0004 0005 #include <linux/const.h> 0006 0007 /* 0008 * min()/max()/clamp() macros must accomplish three things: 0009 * 0010 * - avoid multiple evaluations of the arguments (so side-effects like 0011 * "x++" happen only once) when non-constant. 0012 * - perform strict type-checking (to generate warnings instead of 0013 * nasty runtime surprises). See the "unnecessary" pointer comparison 0014 * in __typecheck(). 0015 * - retain result as a constant expressions when called with only 0016 * constant expressions (to avoid tripping VLA warnings in stack 0017 * allocation usage). 0018 */ 0019 #define __typecheck(x, y) \ 0020 (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1))) 0021 0022 #define __no_side_effects(x, y) \ 0023 (__is_constexpr(x) && __is_constexpr(y)) 0024 0025 #define __safe_cmp(x, y) \ 0026 (__typecheck(x, y) && __no_side_effects(x, y)) 0027 0028 #define __cmp(x, y, op) ((x) op (y) ? (x) : (y)) 0029 0030 #define __cmp_once(x, y, unique_x, unique_y, op) ({ \ 0031 typeof(x) unique_x = (x); \ 0032 typeof(y) unique_y = (y); \ 0033 __cmp(unique_x, unique_y, op); }) 0034 0035 #define __careful_cmp(x, y, op) \ 0036 __builtin_choose_expr(__safe_cmp(x, y), \ 0037 __cmp(x, y, op), \ 0038 __cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op)) 0039 0040 /** 0041 * min - return minimum of two values of the same or compatible types 0042 * @x: first value 0043 * @y: second value 0044 */ 0045 #define min(x, y) __careful_cmp(x, y, <) 0046 0047 /** 0048 * max - return maximum of two values of the same or compatible types 0049 * @x: first value 0050 * @y: second value 0051 */ 0052 #define max(x, y) __careful_cmp(x, y, >) 0053 0054 /** 0055 * min3 - return minimum of three values 0056 * @x: first value 0057 * @y: second value 0058 * @z: third value 0059 */ 0060 #define min3(x, y, z) min((typeof(x))min(x, y), z) 0061 0062 /** 0063 * max3 - return maximum of three values 0064 * @x: first value 0065 * @y: second value 0066 * @z: third value 0067 */ 0068 #define max3(x, y, z) max((typeof(x))max(x, y), z) 0069 0070 /** 0071 * min_not_zero - return the minimum that is _not_ zero, unless both are zero 0072 * @x: value1 0073 * @y: value2 0074 */ 0075 #define min_not_zero(x, y) ({ \ 0076 typeof(x) __x = (x); \ 0077 typeof(y) __y = (y); \ 0078 __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); }) 0079 0080 /** 0081 * clamp - return a value clamped to a given range with strict typechecking 0082 * @val: current value 0083 * @lo: lowest allowable value 0084 * @hi: highest allowable value 0085 * 0086 * This macro does strict typechecking of @lo/@hi to make sure they are of the 0087 * same type as @val. See the unnecessary pointer comparisons. 0088 */ 0089 #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi) 0090 0091 /* 0092 * ..and if you can't take the strict 0093 * types, you can specify one yourself. 0094 * 0095 * Or not use min/max/clamp at all, of course. 0096 */ 0097 0098 /** 0099 * min_t - return minimum of two values, using the specified type 0100 * @type: data type to use 0101 * @x: first value 0102 * @y: second value 0103 */ 0104 #define min_t(type, x, y) __careful_cmp((type)(x), (type)(y), <) 0105 0106 /** 0107 * max_t - return maximum of two values, using the specified type 0108 * @type: data type to use 0109 * @x: first value 0110 * @y: second value 0111 */ 0112 #define max_t(type, x, y) __careful_cmp((type)(x), (type)(y), >) 0113 0114 /** 0115 * clamp_t - return a value clamped to a given range using a given type 0116 * @type: the type of variable to use 0117 * @val: current value 0118 * @lo: minimum allowable value 0119 * @hi: maximum allowable value 0120 * 0121 * This macro does no typechecking and uses temporary variables of type 0122 * @type to make all the comparisons. 0123 */ 0124 #define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi) 0125 0126 /** 0127 * clamp_val - return a value clamped to a given range using val's type 0128 * @val: current value 0129 * @lo: minimum allowable value 0130 * @hi: maximum allowable value 0131 * 0132 * This macro does no typechecking and uses temporary variables of whatever 0133 * type the input argument @val is. This is useful when @val is an unsigned 0134 * type and @lo and @hi are literals that will otherwise be assigned a signed 0135 * integer type. 0136 */ 0137 #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) 0138 0139 /** 0140 * swap - swap values of @a and @b 0141 * @a: first value 0142 * @b: second value 0143 */ 0144 #define swap(a, b) \ 0145 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0) 0146 0147 #endif /* _LINUX_MINMAX_H */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |