|
107 | 107 | # endif |
108 | 108 | #endif |
109 | 109 |
|
| 110 | +#if ((defined(__GNUC__) || defined(__clang__)) \ |
| 111 | + && defined(_Py_TYPEOF) && !defined(__cplusplus)) |
| 112 | + // Implement Py_MIN(), Py_MAX() and Py_ABS() using _Py_TYPEOF() and |
| 113 | + // statement expression to only evaluate each argument only once. |
| 114 | + // It cannot be used in C++: ISO C++ forbids braced-groups within |
| 115 | + // expressions. Statement expression is a GNU extension. Use __extension__ |
| 116 | + // to avoid compiler warning in pedantic mode. |
110 | 117 |
|
111 | | -/* Minimum value between x and y */ |
112 | | -#define Py_MIN(x, y) (((x) > (y)) ? (y) : (x)) |
113 | | - |
114 | | -/* Maximum value between x and y */ |
115 | | -#define Py_MAX(x, y) (((x) > (y)) ? (x) : (y)) |
| 118 | + /* Minimum value between x and y */ |
| 119 | +# define Py_MIN(x, y) \ |
| 120 | + __extension__ \ |
| 121 | + ({ _Py_TYPEOF (x) _x = (x); \ |
| 122 | + _Py_TYPEOF (y) _y = (y); \ |
| 123 | + _x < _y ? _x : _y; }) |
| 124 | + /* Maximum value between x and y */ |
| 125 | +# define Py_MAX(x, y) \ |
| 126 | + __extension__ \ |
| 127 | + ({ _Py_TYPEOF (x) _x = (x); \ |
| 128 | + _Py_TYPEOF (y) _y = (y); \ |
| 129 | + _x > _y ? _x : _y; }) |
| 130 | + /* Absolute value of the number x */ |
| 131 | +# define Py_ABS(x) \ |
| 132 | + __extension__ \ |
| 133 | + ({ _Py_TYPEOF (x) _x = (x); \ |
| 134 | + _x < 0 ? -_x : _x; }) |
| 135 | +#else |
| 136 | + /* Minimum value between x and y */ |
| 137 | +# define Py_MIN(x, y) (((x) > (y)) ? (y) : (x)) |
| 138 | + /* Maximum value between x and y */ |
| 139 | +# define Py_MAX(x, y) (((x) > (y)) ? (x) : (y)) |
| 140 | + /* Absolute value of the number x */ |
| 141 | +# define Py_ABS(x) ((x) < 0 ? -(x) : (x)) |
| 142 | +#endif |
116 | 143 |
|
117 | | -/* Absolute value of the number x */ |
118 | | -#define Py_ABS(x) ((x) < 0 ? -(x) : (x)) |
119 | 144 | /* Safer implementation that avoids an undefined behavior for the minimal |
120 | 145 | value of the signed integer type if its absolute value is larger than |
121 | 146 | the maximal value of the signed integer type (in the two's complement |
|
0 commit comments