-
-
Notifications
You must be signed in to change notification settings - Fork 32k
GH-101291: Rearrange the size bits in PyLongObject #102464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
0ec07e4
292b9d0
5c54894
029aaa4
b56e6da
91269fc
c48e825
449c0e2
c5ba601
4b3a3e8
9ef9d2c
9c408c1
548d656
3e3fefd
391fb51
df8c7d3
bc14fa6
54c6f1b
ce6bfb2
4c1956b
301158b
1aa1891
bf2a9af
169f521
90f9072
f143443
a0d661e
145a2e4
638a98f
7f5acc0
b06bb6f
a19b0a7
87f49b2
f764aa8
9843ac0
d6cb917
469d26f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,9 +112,7 @@ PyAPI_FUNC(char*) _PyLong_FormatBytesWriter( | |
/* Return 1 if the argument is positive single digit int */ | ||
static inline int | ||
_PyLong_IsNonNegativeSingleDigit(const PyLongObject* op) { | ||
/* For a positive single digit int, the value of Py_SIZE(sub) is 0 or 1. | ||
|
||
We perform a fast check using a single comparison by casting from int | ||
/* We perform a fast check using a single comparison by casting from int | ||
to uint which casts negative numbers to large positive numbers. | ||
For details see Section 14.2 "Bounds Checking" in the Agner Fog | ||
optimization manual found at: | ||
|
@@ -124,51 +122,51 @@ _PyLong_IsNonNegativeSingleDigit(const PyLongObject* op) { | |
compiler options of GCC and clang | ||
*/ | ||
assert(PyLong_Check(op)); | ||
Py_ssize_t signed_size = Py_SIZE(op); | ||
Py_ssize_t signed_size = op->long_value.ob_size; | ||
return ((size_t)signed_size) <= 1; | ||
} | ||
|
||
|
||
static inline int | ||
_PyLong_IsSingleDigit(const PyLongObject* op) { | ||
assert(PyLong_Check(op)); | ||
Py_ssize_t signed_size = Py_SIZE(op); | ||
Py_ssize_t signed_size = op->long_value.ob_size; | ||
return ((size_t)(signed_size+1)) <= 2; | ||
} | ||
|
||
static inline Py_ssize_t | ||
_PyLong_SingleDigitValue(const PyLongObject *op) | ||
{ | ||
assert(PyLong_Check(op)); | ||
assert(Py_SIZE(op) >= -1 && Py_SIZE(op) <= 1); | ||
return Py_SIZE(op) * op->long_value.ob_digit[0]; | ||
assert(op->long_value.ob_size >= -1 && op->long_value.ob_size <= 1); | ||
return op->long_value.ob_size * op->long_value.ob_digit[0]; | ||
} | ||
|
||
static inline bool | ||
_PyLong_IsPositive(const PyLongObject *op) | ||
{ | ||
return Py_SIZE(op) > 0; | ||
return op->long_value.ob_size > 0; | ||
} | ||
|
||
static inline bool | ||
_PyLong_IsNegative(const PyLongObject *op) | ||
{ | ||
return Py_SIZE(op) < 0; | ||
return op->long_value.ob_size < 0; | ||
} | ||
|
||
static inline Py_ssize_t | ||
_PyLong_DigitCount(const PyLongObject *op) | ||
{ | ||
assert(PyLong_Check(op)); | ||
return Py_ABS(Py_SIZE(op)); | ||
return Py_ABS(op->long_value.ob_size); | ||
} | ||
|
||
/* Equivalent to _PyLong_DigitCount(op) * _PyLong_NonZeroSign(op) */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I take it this is for algorithms where the old "signed size" representation worked well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is for code that uses the "signed size" representation. |
||
static inline Py_ssize_t | ||
_PyLong_SignedDigitCount(const PyLongObject *op) | ||
{ | ||
assert(PyLong_Check(op)); | ||
return Py_SIZE(op); | ||
return op->long_value.ob_size; | ||
} | ||
|
||
/* Like _PyLong_DigitCount but asserts that op is non-negative */ | ||
|
@@ -177,28 +175,28 @@ _PyLong_UnsignedDigitCount(const PyLongObject *op) | |
{ | ||
assert(PyLong_Check(op)); | ||
assert(!_PyLong_IsNegative(op)); | ||
return Py_SIZE(op); | ||
return op->long_value.ob_size; | ||
} | ||
|
||
static inline bool | ||
_PyLong_IsZero(const PyLongObject *op) | ||
{ | ||
return Py_SIZE(op) == 0; | ||
return op->long_value.ob_size == 0; | ||
} | ||
|
||
static inline int | ||
_PyLong_NonZeroSign(const PyLongObject *op) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The name confused me -- why not just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because it shouldn't be called with It probably should be called on compact ints either. I'll check if it is, and rename it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've renamed it to |
||
{ | ||
assert(PyLong_Check(op)); | ||
assert(!_PyLong_IsZero(op)); | ||
return ((Py_SIZE(op) > 0) << 1) - 1; | ||
return ((op->long_value.ob_size > 0) << 1) - 1; | ||
} | ||
|
||
/* Do a and b have the same sign? Zero counts as positive. */ | ||
static inline int | ||
_PyLong_SameSign(const PyLongObject *a, const PyLongObject *b) | ||
{ | ||
return (Py_SIZE(a) ^ Py_SIZE(b)) >= 0; | ||
return (a->long_value.ob_size ^ b->long_value.ob_size) >= 0; | ||
} | ||
|
||
static inline void | ||
|
@@ -214,6 +212,20 @@ _PyLong_FlipSign(PyLongObject *op) { | |
op->long_value.ob_size = -op->long_value.ob_size; | ||
} | ||
|
||
#define _PyLong_DIGIT_INIT(val) \ | ||
{ \ | ||
.ob_base = _PyObject_IMMORTAL_INIT(&PyLong_Type), \ | ||
.long_value = { \ | ||
.ob_size = (val) == 0 ? 0 : ((val) < 0 ? -1 : 1), \ | ||
{ ((val) >= 0 ? (val) : -(val)) }, \ | ||
} \ | ||
} | ||
|
||
#define TAG_FROM_SIGN_AND_SIZE(neg, size) (neg ? -(size) : (size)) | ||
|
||
#define _PyLong_FALSE_TAG 0 | ||
#define _PyLong_TRUE_TAG 1 | ||
|
||
|
||
#ifdef __cplusplus | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -32,6 +32,8 @@ Copyright (C) 1994 Steen Lumholt. | |||||||||
# include "pycore_fileutils.h" // _Py_stat() | ||||||||||
#endif | ||||||||||
|
||||||||||
#include "pycore_long.h" | ||||||||||
|
||||||||||
#ifdef MS_WINDOWS | ||||||||||
#include <windows.h> | ||||||||||
#endif | ||||||||||
|
@@ -887,8 +889,8 @@ asBignumObj(PyObject *value) | |||||||||
PyObject *hexstr; | ||||||||||
const char *hexchars; | ||||||||||
mp_int bigValue; | ||||||||||
|
||||||||||
neg = Py_SIZE(value) < 0; | ||||||||||
assert(PyLong_Check(value)); | ||||||||||
neg = _PyLong_IsNegative((PyLongObject *)value); | ||||||||||
Comment on lines
+891
to
+892
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please put the blank line back.
Suggested change
|
||||||||||
hexstr = _PyLong_Format(value, 16); | ||||||||||
if (hexstr == NULL) | ||||||||||
return NULL; | ||||||||||
|
@@ -1960,7 +1962,7 @@ _tkinter_tkapp_getboolean(TkappObject *self, PyObject *arg) | |||||||||
int v; | ||||||||||
|
||||||||||
if (PyLong_Check(arg)) { /* int or bool */ | ||||||||||
return PyBool_FromLong(Py_SIZE(arg) != 0); | ||||||||||
return PyBool_FromLong(!_PyLong_IsZero((PyLongObject *)arg)); | ||||||||||
} | ||||||||||
|
||||||||||
if (PyTclObject_Check(arg)) { | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
#include "Python.h" | ||
#include "pycore_ast.h" // _PyAST_GetDocString() | ||
#include "pycore_compile.h" // _PyASTOptimizeState | ||
#include "pycore_long.h" // _PyLong | ||
#include "pycore_pystate.h" // _PyThreadState_GET() | ||
#include "pycore_format.h" // F_LJUST | ||
|
||
|
@@ -152,7 +153,9 @@ check_complexity(PyObject *obj, Py_ssize_t limit) | |
static PyObject * | ||
safe_multiply(PyObject *v, PyObject *w) | ||
{ | ||
if (PyLong_Check(v) && PyLong_Check(w) && Py_SIZE(v) && Py_SIZE(w)) { | ||
if (PyLong_Check(v) && PyLong_Check(w) && | ||
!_PyLong_IsZero((PyLongObject *)v) && !_PyLong_IsZero((PyLongObject *)w) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we need another convenience macro IsNonZero. |
||
) { | ||
size_t vbits = _PyLong_NumBits(v); | ||
size_t wbits = _PyLong_NumBits(w); | ||
if (vbits == (size_t)-1 || wbits == (size_t)-1) { | ||
|
@@ -198,7 +201,9 @@ safe_multiply(PyObject *v, PyObject *w) | |
static PyObject * | ||
safe_power(PyObject *v, PyObject *w) | ||
{ | ||
if (PyLong_Check(v) && PyLong_Check(w) && Py_SIZE(v) && Py_SIZE(w) > 0) { | ||
if (PyLong_Check(v) && PyLong_Check(w) && | ||
!_PyLong_IsZero((PyLongObject *)v) && _PyLong_IsPositive((PyLongObject *)w) | ||
) { | ||
size_t vbits = _PyLong_NumBits(v); | ||
size_t wbits = PyLong_AsSize_t(w); | ||
if (vbits == (size_t)-1 || wbits == (size_t)-1) { | ||
|
@@ -215,7 +220,9 @@ safe_power(PyObject *v, PyObject *w) | |
static PyObject * | ||
safe_lshift(PyObject *v, PyObject *w) | ||
{ | ||
if (PyLong_Check(v) && PyLong_Check(w) && Py_SIZE(v) && Py_SIZE(w)) { | ||
if (PyLong_Check(v) && PyLong_Check(w) && | ||
!_PyLong_IsZero((PyLongObject *)v) && !_PyLong_IsZero((PyLongObject *)w) | ||
) { | ||
size_t vbits = _PyLong_NumBits(v); | ||
size_t wbits = PyLong_AsSize_t(w); | ||
if (vbits == (size_t)-1 || wbits == (size_t)-1) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You didn't update this comment that documents
_longobject
, it's still talking aboutob_size
andPyVarObject