Skip to content

Commit a620166

Browse files
committed
Implement _PyLong_NumBits
1 parent 932d2af commit a620166

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_long.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,3 +471,16 @@ class TestPyLong(CPyExtTestCase):
471471
arguments=["const char* bytes", "Py_ssize_t size", "int little_endian", "int is_signed"],
472472
cmpfunc=unhandled_error_compare,
473473
)
474+
475+
test__PyLong_NumBits = CPyExtFunction(
476+
lambda args: args[0].bit_length(),
477+
lambda: (
478+
(1,),
479+
(1230948701328090743,),
480+
(-1230948701328090743,),
481+
),
482+
resultspec="n",
483+
argspec="O",
484+
arguments=["PyObject* obj"],
485+
cmpfunc=unhandled_error_compare,
486+
)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextLongBuiltins.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,4 +483,13 @@ static Object convert(Object charPtr, long size, int littleEndian, int signed,
483483
return fromByteArray.execute(inliningTarget, bytes, littleEndian != 0, signed != 0);
484484
}
485485
}
486+
487+
@CApiBuiltin(ret = SIZE_T, args = {PyObject}, call = Direct)
488+
abstract static class _PyLong_NumBits extends CApiUnaryBuiltinNode {
489+
@Specialization
490+
static long numBits(Object obj,
491+
@Cached IntBuiltins.BitLengthNode bitLengthNode) {
492+
return bitLengthNode.execute(obj);
493+
}
494+
}
486495
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/CApiFunction.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,6 @@ public final class CApiFunction {
10761076
@CApiBuiltin(name = "_PyLong_GCD", ret = PyObject, args = {PyObject, PyObject}, call = NotImplemented)
10771077
@CApiBuiltin(name = "_PyLong_Lshift", ret = PyObject, args = {PyObject, SIZE_T}, call = NotImplemented)
10781078
@CApiBuiltin(name = "_PyLong_New", ret = PyLongObject, args = {Py_ssize_t}, call = NotImplemented)
1079-
@CApiBuiltin(name = "_PyLong_NumBits", ret = SIZE_T, args = {PyObject}, call = NotImplemented)
10801079
@CApiBuiltin(name = "_PyLong_Rshift", ret = PyObject, args = {PyObject, SIZE_T}, call = NotImplemented)
10811080
@CApiBuiltin(name = "_PyLong_Size_t_Converter", ret = Int, args = {PyObject, Pointer}, call = NotImplemented)
10821081
@CApiBuiltin(name = "_PyLong_UnsignedInt_Converter", ret = Int, args = {PyObject, Pointer}, call = NotImplemented)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/IntBuiltins.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3141,7 +3141,9 @@ static int bitCount(PInt i) {
31413141
@Builtin(name = "bit_length", minNumOfPositionalArgs = 1)
31423142
@GenerateNodeFactory
31433143
@TypeSystemReference(PythonArithmeticTypes.class)
3144-
abstract static class BitLengthNode extends PythonBuiltinNode {
3144+
public abstract static class BitLengthNode extends PythonUnaryBuiltinNode {
3145+
public abstract int execute(Object argument);
3146+
31453147
@Specialization
31463148
static int bitLength(int argument) {
31473149
return Integer.SIZE - Integer.numberOfLeadingZeros(Math.abs(argument));

0 commit comments

Comments
 (0)