Skip to content

Commit df24c4f

Browse files
Use more efficient implementation.
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
1 parent 887853d commit df24c4f

File tree

3 files changed

+3
-6
lines changed

3 files changed

+3
-6
lines changed

Include/pymacro.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
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)))
118119
#define Py_ABS(x) ((x) < 0 ? -(x) : (x))
119120

120121
#define _Py_XSTRINGIFY(x) #x

Python/marshal.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,7 @@ w_PyLong(const PyLongObject *ob, char flag, WFILE *p)
310310
}
311311
if (!long_export.digits) {
312312
int8_t sign = long_export.value < 0 ? -1 : 1;
313-
uint64_t abs_value = long_export.value < -INT64_MAX
314-
? (uint64_t)INT64_MAX + (uint64_t)-(long_export.value + INT64_MAX)
315-
: (uint64_t)Py_ABS(long_export.value);
313+
uint64_t abs_value = _Py_ABS_CAST(uint64_t, long_export.value);
316314
uint64_t d = abs_value;
317315
long l = 0;
318316

Python/pystrhex.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ static PyObject *_Py_strhex_impl(const char* argbuf, const Py_ssize_t arglen,
4242
else {
4343
bytes_per_sep_group = 0;
4444
}
45-
unsigned int abs_bytes_per_sep = (bytes_per_sep_group < -INT_MAX)
46-
? (unsigned int)INT_MAX + (unsigned int)-(bytes_per_sep_group + INT_MAX)
47-
: (unsigned int)Py_ABS(bytes_per_sep_group);
45+
unsigned int abs_bytes_per_sep = _Py_ABS_CAST(unsigned int, bytes_per_sep_group);
4846
Py_ssize_t resultlen = 0;
4947
if (bytes_per_sep_group && arglen > 0) {
5048
/* How many sep characters we'll be inserting. */

0 commit comments

Comments
 (0)