Skip to content

Commit

Permalink
minmax: make generic MIN() and MAX() macros available
Browse files Browse the repository at this point in the history
 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 freebsd#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 freebsd#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]>
  • Loading branch information
torvalds authored and lutzbichler committed Nov 8, 2024
1 parent 7c58b97 commit ad86a2c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion drivers/gpu/drm/amd/display/dc/core/dc_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include "dc_stream_priv.h"

#define DC_LOGGER dc->ctx->logger
#ifdef __linux__
#ifndef MIN
#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
#define MAX(x, y) ((x > y) ? x : y)
#endif
Expand Down
6 changes: 2 additions & 4 deletions drivers/gpu/drm/amd/display/modules/hdcp/hdcp_ddc.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@

#include "hdcp.h"

#ifdef __FreeBSD__
#undef MIN
#endif

#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
#define HDCP_I2C_ADDR 0x3a /* 0x74 >> 1*/
#define KSV_READ_SIZE 0xf /* 0x6803b - 0x6802c */
#define HDCP_MAX_AUX_TRANSACTION_SIZE 16
Expand Down
14 changes: 10 additions & 4 deletions drivers/gpu/drm/amd/pm/powerplay/hwmgr/ppevvmath.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@
#undef MAX
#endif

#define SHIFT_AMOUNT 16 /* We multiply all original integers with 2^SHIFT_AMOUNT to get the fInt representation */
enum ppevvmath_constants {
/* We multiply all original integers with 2^SHIFT_AMOUNT to get the fInt representation */
SHIFT_AMOUNT = 16,

#define PRECISION 5 /* Change this value to change the number of decimal places in the final output - 5 is a good default */
/* Change this value to change the number of decimal places in the final output - 5 is a good default */
PRECISION = 5,

#define SHIFTED_2 (2 << SHIFT_AMOUNT)
#define MAX (1 << (SHIFT_AMOUNT - 1)) - 1 /* 32767 - Might change in the future */
SHIFTED_2 = (2 << SHIFT_AMOUNT),

/* 32767 - Might change in the future */
MAX = (1 << (SHIFT_AMOUNT - 1)) - 1,
};

/* -------------------------------------------------------------------------------
* NEW TYPE - fINT
Expand Down
2 changes: 1 addition & 1 deletion drivers/gpu/drm/radeon/evergreen_cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "evergreen_reg_safe.h"
#include "cayman_reg_safe.h"

#ifdef __linux__
#ifndef MIN
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
Expand Down

0 comments on commit ad86a2c

Please sign in to comment.