Skip to content

Commit 1a251f5

Browse files
committed
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 <[email protected]> Cc: Lorenzo Stoakes <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 8400291 commit 1a251f5

File tree

20 files changed

+43
-38
lines changed

20 files changed

+43
-38
lines changed

arch/um/drivers/mconsole_user.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ static struct mconsole_command *mconsole_parse(struct mc_request *req)
7171
return NULL;
7272
}
7373

74+
#ifndef MIN
7475
#define MIN(a,b) ((a)<(b) ? (a):(b))
76+
#endif
7577

7678
#define STRINGX(x) #x
7779
#define STRING(x) STRINGX(x)

drivers/edac/skx_common.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
#define I10NM_NUM_CHANNELS MAX(I10NM_NUM_DDR_CHANNELS, I10NM_NUM_HBM_CHANNELS)
4646
#define I10NM_NUM_DIMMS MAX(I10NM_NUM_DDR_DIMMS, I10NM_NUM_HBM_DIMMS)
4747

48-
#define MAX(a, b) ((a) > (b) ? (a) : (b))
4948
#define NUM_IMC MAX(SKX_NUM_IMC, I10NM_NUM_IMC)
5049
#define NUM_CHANNELS MAX(SKX_NUM_CHANNELS, I10NM_NUM_CHANNELS)
5150
#define NUM_DIMMS MAX(SKX_NUM_DIMMS, I10NM_NUM_DIMMS)

drivers/gpu/drm/amd/display/dc/core/dc_stream.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@
3535
#include "dc_stream_priv.h"
3636

3737
#define DC_LOGGER dc->ctx->logger
38+
#ifndef MIN
3839
#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
3940
#define MAX(x, y) ((x > y) ? x : y)
41+
#endif
4042

4143
/*******************************************************************************
4244
* Private functions

drivers/gpu/drm/amd/display/modules/hdcp/hdcp_ddc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525

2626
#include "hdcp.h"
2727

28+
#ifndef MIN
2829
#define MIN(a, b) ((a) < (b) ? (a) : (b))
30+
#endif
2931
#define HDCP_I2C_ADDR 0x3a /* 0x74 >> 1*/
3032
#define KSV_READ_SIZE 0xf /* 0x6803b - 0x6802c */
3133
#define HDCP_MAX_AUX_TRANSACTION_SIZE 16

drivers/gpu/drm/amd/pm/powerplay/hwmgr/ppevvmath.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,18 @@
2222
*/
2323
#include <asm/div64.h>
2424

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,
2628

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,
2831

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+
};
3137

3238
/* -------------------------------------------------------------------------------
3339
* NEW TYPE - fINT

drivers/gpu/drm/radeon/evergreen_cs.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@
3333
#include "evergreen_reg_safe.h"
3434
#include "cayman_reg_safe.h"
3535

36+
#ifndef MIN
3637
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
3738
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
39+
#endif
3840

3941
#define REG_SAFE_BM_SIZE ARRAY_SIZE(evergreen_reg_safe_bm)
4042

drivers/hwmon/adt7475.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@
2222
#include <linux/util_macros.h>
2323

2424
/* 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,
3534
/*
3635
* These are unique identifiers for the sysfs functions - unlike the
3736
* numbers above, these are not also indexes into an array
3837
*/
38+
ALARM = 9,
39+
FAULT = 10,
40+
};
3941

40-
#define ALARM 9
41-
#define FAULT 10
4242

4343
/* 7475 Common Registers */
4444

drivers/media/dvb-frontends/stv0367_priv.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525
#endif
2626

2727
/* MACRO definitions */
28+
#ifndef MIN
2829
#define MAX(X, Y) ((X) >= (Y) ? (X) : (Y))
2930
#define MIN(X, Y) ((X) <= (Y) ? (X) : (Y))
31+
#endif
32+
3033
#define INRANGE(X, Y, Z) \
3134
((((X) <= (Y)) && ((Y) <= (Z))) || \
3235
(((Z) <= (Y)) && ((Y) <= (X))) ? 1 : 0)

drivers/net/fjes/fjes_main.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
#include "fjes.h"
1515
#include "fjes_trace.h"
1616

17-
#define MAJ 1
18-
#define MIN 2
19-
#define DRV_VERSION __stringify(MAJ) "." __stringify(MIN)
17+
#define DRV_VERSION "1.2"
2018
#define DRV_NAME "fjes"
2119
char fjes_driver_name[] = DRV_NAME;
2220
char fjes_driver_version[] = DRV_VERSION;

drivers/nfc/pn544/i2c.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,6 @@ struct pn544_i2c_fw_secure_blob {
126126
#define PN544_FW_CMD_RESULT_COMMAND_REJECTED 0xE0
127127
#define PN544_FW_CMD_RESULT_CHUNK_ERROR 0xE6
128128

129-
#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
130-
131129
#define PN544_FW_WRITE_BUFFER_MAX_LEN 0x9f7
132130
#define PN544_FW_I2C_MAX_PAYLOAD PN544_HCI_I2C_LLC_MAX_SIZE
133131
#define PN544_FW_I2C_WRITE_FRAME_HEADER_LEN 8

0 commit comments

Comments
 (0)