Skip to content

Commit b292561

Browse files
Try other code. Add a comment.
1 parent a584478 commit b292561

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

Include/pymacro.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,13 @@
115115
#define Py_MAX(x, y) (((x) > (y)) ? (x) : (y))
116116

117117
/* Absolute value of the number x */
118-
#define _Py_ABS_CAST(T,x) ((x) >= 0 ? ((T) (x)) : (- (((T) ((x) + 1)) - 1)))
119118
#define Py_ABS(x) ((x) < 0 ? -(x) : (x))
119+
/* Safer implementation that avoids an undefined behavior for the minimal
120+
value of the signed integer type if its absolute value is larger than
121+
the maximal value of the signed integer type (in the two's complement
122+
representations, which is common).
123+
*/
124+
#define _Py_ABS_CAST(T, x) ((x) >= 0 ? ((T) (x)) : ((T) (((T) -((x) + 1)) + 1u)))
120125

121126
#define _Py_XSTRINGIFY(x) #x
122127

0 commit comments

Comments
 (0)