Skip to content

[mypyc] Add primitive for abs(int) - #22363

Merged
p-sawicki merged 1 commit into
python:masterfrom
Pixeless001:dev
Nov 4, 2026
Merged

p-sawicki merged 1 commit into
python:masterfrom
Pixeless001:dev

Conversation

@Pixeless001

Copy link
Copy Markdown
Contributor

Fixes mypyc/mypyc#1094.

abs() on an int used to box it, go through PyNumber_Absolute and unbox the result. This adds
CPyTagged_Absolute, modeled on CPyTagged_Negate: short ints are handled inline, and long ints
fall back to PyNumber_Absolute. So does -2**62, since its absolute value doesn't fit in a short int.

A loop summing abs(i) over 2M ints went from 138 ms to 18 ms for me (Python 3.14 on Windows,
median of 9 runs).

@p-sawicki p-sawicki left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

looks good, thanks!

@p-sawicki
p-sawicki merged commit 1231cf1 into python:master Nov 4, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add primitive for abs(integer)

2 participants