Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

minmax: make generic MIN() and MAX() macros available everywhere

This just standardizes the use of MIN() and MAX() macros, with the very
traditional semantics. The goal is to use these for C constant
expressions and for top-level / static initializers, and so be able to
simplify the min()/max() macros.

These macro names were used by various kernel code - they are very
traditional, after all - and all such users have been fixed up, with a
few different approaches:

- trivial duplicated macro definitions have been removed

Note that 'trivial' here means that it's obviously kernel code that
already included all the major kernel headers, and thus gets the new
generic MIN/MAX macros automatically.

- non-trivial duplicated macro definitions are guarded with #ifndef

This is the "yes, they define their own versions, but no, the include
situation is not entirely obvious, and maybe they don't get the
generic version automatically" case.

- strange use case #1

A couple of drivers decided that the way they want to describe their
versioning is with

#define MAJ 1
#define MIN 2
#define DRV_VERSION __stringify(MAJ) "." __stringify(MIN)

which adds zero value and I just did my Alexander the Great
impersonation, and rewrote that pointless Gordian knot as

#define DRV_VERSION "1.2"

instead.

- strange use case #2

A couple of drivers thought that it's a good idea to have a random
'MIN' or 'MAX' define for a value or index into a table, rather than
the traditional macro that takes arguments.

These values were re-written as C enum's instead. The new
function-line macros only expand when followed by an open
parenthesis, and thus don't clash with enum use.

Happily, there weren't really all that many of these cases, and a lot of
users already had the pattern of using '#ifndef' guarding (or in one
case just using '#undef MIN') before defining their own private version
that does the same thing. I left such cases alone.

Cc: David Laight <David.Laight@aculab.com>
Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

+43 -38
+2
arch/um/drivers/mconsole_user.c
··· 71 71 return NULL; 72 72 } 73 73 74 + #ifndef MIN 74 75 #define MIN(a,b) ((a)<(b) ? (a):(b)) 76 + #endif 75 77 76 78 #define STRINGX(x) #x 77 79 #define STRING(x) STRINGX(x)
-1
drivers/edac/skx_common.h
··· 45 45 #define I10NM_NUM_CHANNELS MAX(I10NM_NUM_DDR_CHANNELS, I10NM_NUM_HBM_CHANNELS) 46 46 #define I10NM_NUM_DIMMS MAX(I10NM_NUM_DDR_DIMMS, I10NM_NUM_HBM_DIMMS) 47 47 48 - #define MAX(a, b) ((a) > (b) ? (a) : (b)) 49 48 #define NUM_IMC MAX(SKX_NUM_IMC, I10NM_NUM_IMC) 50 49 #define NUM_CHANNELS MAX(SKX_NUM_CHANNELS, I10NM_NUM_CHANNELS) 51 50 #define NUM_DIMMS MAX(SKX_NUM_DIMMS, I10NM_NUM_DIMMS)
+2
drivers/gpu/drm/amd/display/dc/core/dc_stream.c
··· 35 35 #include "dc_stream_priv.h" 36 36 37 37 #define DC_LOGGER dc->ctx->logger 38 + #ifndef MIN 38 39 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) 39 40 #define MAX(x, y) ((x > y) ? x : y) 41 + #endif 40 42 41 43 /******************************************************************************* 42 44 * Private functions
+2
drivers/gpu/drm/amd/display/modules/hdcp/hdcp_ddc.c
··· 25 25 26 26 #include "hdcp.h" 27 27 28 + #ifndef MIN 28 29 #define MIN(a, b) ((a) < (b) ? (a) : (b)) 30 + #endif 29 31 #define HDCP_I2C_ADDR 0x3a /* 0x74 >> 1*/ 30 32 #define KSV_READ_SIZE 0xf /* 0x6803b - 0x6802c */ 31 33 #define HDCP_MAX_AUX_TRANSACTION_SIZE 16
+10 -4
drivers/gpu/drm/amd/pm/powerplay/hwmgr/ppevvmath.h
··· 22 22 */ 23 23 #include <asm/div64.h> 24 24 25 - #define SHIFT_AMOUNT 16 /* We multiply all original integers with 2^SHIFT_AMOUNT to get the fInt representation */ 25 + enum ppevvmath_constants { 26 + /* We multiply all original integers with 2^SHIFT_AMOUNT to get the fInt representation */ 27 + SHIFT_AMOUNT = 16, 26 28 27 - #define PRECISION 5 /* Change this value to change the number of decimal places in the final output - 5 is a good default */ 29 + /* Change this value to change the number of decimal places in the final output - 5 is a good default */ 30 + PRECISION = 5, 28 31 29 - #define SHIFTED_2 (2 << SHIFT_AMOUNT) 30 - #define MAX (1 << (SHIFT_AMOUNT - 1)) - 1 /* 32767 - Might change in the future */ 32 + SHIFTED_2 = (2 << SHIFT_AMOUNT), 33 + 34 + /* 32767 - Might change in the future */ 35 + MAX = (1 << (SHIFT_AMOUNT - 1)) - 1, 36 + }; 31 37 32 38 /* ------------------------------------------------------------------------------- 33 39 * NEW TYPE - fINT
+2
drivers/gpu/drm/radeon/evergreen_cs.c
··· 33 33 #include "evergreen_reg_safe.h" 34 34 #include "cayman_reg_safe.h" 35 35 36 + #ifndef MIN 36 37 #define MAX(a, b) (((a) > (b)) ? (a) : (b)) 37 38 #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 39 + #endif 38 40 39 41 #define REG_SAFE_BM_SIZE ARRAY_SIZE(evergreen_reg_safe_bm) 40 42
+12 -12
drivers/hwmon/adt7475.c
··· 22 22 #include <linux/util_macros.h> 23 23 24 24 /* Indexes for the sysfs hooks */ 25 - 26 - #define INPUT 0 27 - #define MIN 1 28 - #define MAX 2 29 - #define CONTROL 3 30 - #define OFFSET 3 31 - #define AUTOMIN 4 32 - #define THERM 5 33 - #define HYSTERSIS 6 34 - 25 + enum adt_sysfs_id { 26 + INPUT = 0, 27 + MIN = 1, 28 + MAX = 2, 29 + CONTROL = 3, 30 + OFFSET = 3, // Dup 31 + AUTOMIN = 4, 32 + THERM = 5, 33 + HYSTERSIS = 6, 35 34 /* 36 35 * These are unique identifiers for the sysfs functions - unlike the 37 36 * numbers above, these are not also indexes into an array 38 37 */ 38 + ALARM = 9, 39 + FAULT = 10, 40 + }; 39 41 40 - #define ALARM 9 41 - #define FAULT 10 42 42 43 43 /* 7475 Common Registers */ 44 44
+3
drivers/media/dvb-frontends/stv0367_priv.h
··· 25 25 #endif 26 26 27 27 /* MACRO definitions */ 28 + #ifndef MIN 28 29 #define MAX(X, Y) ((X) >= (Y) ? (X) : (Y)) 29 30 #define MIN(X, Y) ((X) <= (Y) ? (X) : (Y)) 31 + #endif 32 + 30 33 #define INRANGE(X, Y, Z) \ 31 34 ((((X) <= (Y)) && ((Y) <= (Z))) || \ 32 35 (((Z) <= (Y)) && ((Y) <= (X))) ? 1 : 0)
+1 -3
drivers/net/fjes/fjes_main.c
··· 14 14 #include "fjes.h" 15 15 #include "fjes_trace.h" 16 16 17 - #define MAJ 1 18 - #define MIN 2 19 - #define DRV_VERSION __stringify(MAJ) "." __stringify(MIN) 17 + #define DRV_VERSION "1.2" 20 18 #define DRV_NAME "fjes" 21 19 char fjes_driver_name[] = DRV_NAME; 22 20 char fjes_driver_version[] = DRV_VERSION;
-2
drivers/nfc/pn544/i2c.c
··· 126 126 #define PN544_FW_CMD_RESULT_COMMAND_REJECTED 0xE0 127 127 #define PN544_FW_CMD_RESULT_CHUNK_ERROR 0xE6 128 128 129 - #define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) 130 - 131 129 #define PN544_FW_WRITE_BUFFER_MAX_LEN 0x9f7 132 130 #define PN544_FW_I2C_MAX_PAYLOAD PN544_HCI_I2C_LLC_MAX_SIZE 133 131 #define PN544_FW_I2C_WRITE_FRAME_HEADER_LEN 8
-1
drivers/platform/x86/sony-laptop.c
··· 757 757 return result; 758 758 } 759 759 760 - #define MIN(a, b) (a > b ? b : a) 761 760 static int sony_nc_buffer_call(acpi_handle handle, char *name, u64 *value, 762 761 void *buffer, size_t buflen) 763 762 {
+1 -5
drivers/scsi/isci/init.c
··· 65 65 #include "task.h" 66 66 #include "probe_roms.h" 67 67 68 - #define MAJ 1 69 - #define MIN 2 70 - #define BUILD 0 71 - #define DRV_VERSION __stringify(MAJ) "." __stringify(MIN) "." \ 72 - __stringify(BUILD) 68 + #define DRV_VERSION "1.2.0" 73 69 74 70 MODULE_VERSION(DRV_VERSION); 75 71
-5
drivers/staging/media/atomisp/pci/hive_isp_css_include/math_support.h
··· 22 22 /* force a value to a lower even value */ 23 23 #define EVEN_FLOOR(x) ((x) & ~1) 24 24 25 - /* for preprocessor and array sizing use MIN and MAX 26 - otherwise use min and max */ 27 - #define MAX(a, b) (((a) > (b)) ? (a) : (b)) 28 - #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 29 - 30 25 #define CEIL_DIV(a, b) (((b) != 0) ? ((a) + (b) - 1) / (b) : 0) 31 26 #define CEIL_MUL(a, b) (CEIL_DIV(a, b) * (b)) 32 27 #define CEIL_MUL2(a, b) (((a) + (b) - 1) & ~((b) - 1))
+2
include/linux/minmax.h
··· 277 277 * Use these carefully: no type checking, and uses the arguments 278 278 * multiple times. Use for obvious constants only. 279 279 */ 280 + #define MIN(a,b) __cmp(min,a,b) 281 + #define MAX(a,b) __cmp(max,a,b) 280 282 #define MIN_T(type,a,b) __cmp(min,(type)(a),(type)(b)) 281 283 #define MAX_T(type,a,b) __cmp(max,(type)(a),(type)(b)) 282 284
-2
kernel/trace/preemptirq_delay_test.c
··· 34 34 35 35 static struct completion done; 36 36 37 - #define MIN(x, y) ((x) < (y) ? (x) : (y)) 38 - 39 37 static void busy_wait(ulong time) 40 38 { 41 39 u64 start, end;
-1
lib/btree.c
··· 43 43 #include <linux/slab.h> 44 44 #include <linux/module.h> 45 45 46 - #define MAX(a, b) ((a) > (b) ? (a) : (b)) 47 46 #define NODESIZE MAX(L1_CACHE_BYTES, 128) 48 47 49 48 struct btree_geo {
+2
lib/decompress_unlzma.c
··· 37 37 38 38 #include <linux/decompress/mm.h> 39 39 40 + #ifndef MIN 40 41 #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 42 + #endif 41 43 42 44 static long long INIT read_int(unsigned char *ptr, int size) 43 45 {
-2
mm/zsmalloc.c
··· 120 120 #define CLASS_BITS 8 121 121 #define MAGIC_VAL_BITS 8 122 122 123 - #define MAX(a, b) ((a) >= (b) ? (a) : (b)) 124 - 125 123 #define ZS_MAX_PAGES_PER_ZSPAGE (_AC(CONFIG_ZSMALLOC_CHAIN_SIZE, UL)) 126 124 127 125 /* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
+2
tools/testing/selftests/mm/mremap_test.c
··· 22 22 #define VALIDATION_DEFAULT_THRESHOLD 4 /* 4MB */ 23 23 #define VALIDATION_NO_THRESHOLD 0 /* Verify the entire region */ 24 24 25 + #ifndef MIN 25 26 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) 26 27 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) 28 + #endif 27 29 #define SIZE_MB(m) ((size_t)m * (1024 * 1024)) 28 30 #define SIZE_KB(k) ((size_t)k * 1024) 29 31
+2
tools/testing/selftests/seccomp/seccomp_bpf.c
··· 60 60 #define SKIP(s, ...) XFAIL(s, ##__VA_ARGS__) 61 61 #endif 62 62 63 + #ifndef MIN 63 64 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) 65 + #endif 64 66 65 67 #ifndef PR_SET_PTRACER 66 68 # define PR_SET_PTRACER 0x59616d61