Skip to content

Conversation

@serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Nov 14, 2025

Python/marshal.c Outdated
Comment on lines 313 to 315
uint64_t abs_value = long_export.value < -INT64_MAX
? (uint64_t)INT64_MAX + (uint64_t)-(long_export.value + INT64_MAX)
: (uint64_t)Py_ABS(long_export.value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we assume two's complement representation of integers and use simpler workaround? As this fix span several places, I would prefer if it could be refactored to a separate macro.

I.e. something like:

#define My_ABS(x, MAX) \
    ((x) < 0 ? ((x) >= -MAX ? -(x) : (U##MAX >> 1) + 1) : (x))

Nowadays two's complement is only case permitted by the C23 and is a de-facto standard. Do you have some system in mind on which we should care? I'm pretty sure all Tier 1-3 platforms fit to this picture.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All Tier 1-3 platforms perhaps fine with the current code. We change the code because we cannot be sure that it work on all exotic platforms and that future versions of the C compiler will not interpret an undefined behavior in interesting way.

The current gcc does not generate additions and substractions for this PR. It generates something more smart, although not so smart as for Py_ABS(). Although for your proposition it generates more complex code.

Details

#include <limits.h>

#define Py_ABS(x) ((x) < 0 ? -(x) : (x))
#define My_ABS(x, MAX) \
    ((x) < 0 ? ((x) >= -MAX ? -(x) : (U##MAX >> 1) + 1) : (x))

unsigned int intabs0(int x) {
    return (unsigned int)Py_ABS(x);
}

unsigned int intabs(int x) {
    return x < -INT_MAX
        ? (unsigned int)INT_MAX + (unsigned int)-(x + INT_MAX)
        : (unsigned int)Py_ABS(x);
}

unsigned int intabs2(int x) {
    return My_ABS(x, INT_MAX);
}

unsigned long longabs0(long x) {
    return (unsigned long)Py_ABS(x);
}

unsigned long longabs(long x) {
    return x < -LONG_MAX
        ? (unsigned long)LONG_MAX + (unsigned long)-(x + LONG_MAX)
        : (unsigned long)Py_ABS(x);
}

unsigned long longabs2(long x) {
    return My_ABS(x, LONG_MAX);
}

Anyway, the performance of this code is not critical (if there is any difference).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All Tier 1-3 platforms perhaps fine with the current code.

(Yes, and I suspect tests might be redundant in fact.)

Although for your proposition it generates more complex code.

A different version:

#define __GMP_CAST(type, expr) ((type) (expr))
#define NEG_CAST(T,x) (- (__GMP_CAST (T, (x) + 1) - 1))
#define ABS_CAST(T,x) ((x) >= 0 ? __GMP_CAST (T, x) : NEG_CAST (T, x))

I found same approach in the GNU GMP, so just copied NIH code here.

Anyway, the performance of this code is not critical (if there is any difference).

Your solution looks ok for me. But in any case we should factor it to some macro.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this version gcc generates exactly the same code as for the current code. But it is now free from undefined behavior.

@skirpichev skirpichev self-requested a review November 15, 2025 02:00
Copy link
Contributor

@skirpichev skirpichev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

This version triggers a warning on M$ compiler, but this is probably ok.

@skirpichev skirpichev requested a review from picnixz November 17, 2025 23:01
#define Py_MAX(x, y) (((x) > (y)) ? (x) : (y))

/* Absolute value of the number x */
#define _Py_ABS_CAST(T,x) ((x) >= 0 ? ((T) (x)) : (- (((T) ((x) + 1)) - 1)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#define _Py_ABS_CAST(T,x) ((x) >= 0 ? ((T) (x)) : (- (((T) ((x) + 1)) - 1)))
#define _Py_ABS_CAST(T, x) ((x) >= 0 ? ((T) (x)) : (- (((T) ((x) + 1)) - 1)))

Copy link
Member

@picnixz picnixz Nov 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For posterity:

_Py_ABS_CAST(uint8_t, (int8_t)-128) == 128

The (T)((x) + 1) - 1 is:

(
 (
   (uint8_t) (
     (-128) + 1  // -127 (still int8_t)
   ) // 129 (2's complement on 8 bits)
 ) - 1 // 128 (as an uint8_t)
)

Since $-128\bmod{256} = 128$, we are good. For another number say -5 we have:

(
 (
   (uint8_t) (
     (-5) + 1  // -4 (still int8_t)
   ) // 252 (2's complement on 8 bits)
 ) - 1 // 251 (as an uint8_t)
)

And now $-251 \bmod{256} = 5$ and we're good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants