Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b3350a

Browse files
committed
ENH: Object loop for bit_count
[2/2] `popcount` object loop changes
1 parent 8bcabf1 commit 7b3350a

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

numpy/__init__.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3537,7 +3537,7 @@ arcsinh: _UFunc_Nin1_Nout1[L['arcsinh'], L[8], None]
35373537
arctan2: _UFunc_Nin2_Nout1[L['arctan2'], L[5], None]
35383538
arctan: _UFunc_Nin1_Nout1[L['arctan'], L[8], None]
35393539
arctanh: _UFunc_Nin1_Nout1[L['arctanh'], L[8], None]
3540-
bit_count: _UFunc_Nin1_Nout1[L['bit_count'], L[10], None]
3540+
bit_count: _UFunc_Nin1_Nout1[L['bit_count'], L[11], None]
35413541
bitwise_and: _UFunc_Nin2_Nout1[L['bitwise_and'], L[12], L[-1]]
35423542
bitwise_not: _UFunc_Nin1_Nout1[L['invert'], L[12], None]
35433543
bitwise_or: _UFunc_Nin2_Nout1[L['bitwise_or'], L[12], L[0]]

numpy/core/code_generators/generate_umath.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,7 @@ def english_upper(s):
960960
docstrings.get('numpy.core.umath.bit_count'),
961961
None,
962962
TD(ints),
963+
TD('O', f='npy_ObjectPopCount'),
963964
),
964965
'matmul' :
965966
Ufunc(2, 1, None,

numpy/core/src/umath/funcs.inc.src

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,38 @@ npy_ObjectClip(PyObject *arr, PyObject *min, PyObject *max) {
267267
return o;
268268
}
269269

270+
static PyObject *
271+
npy_ObjectPopCount(PyObject *obj) {
272+
PyObject *result;
273+
274+
/* Try to use inbuilt popcount if available */
275+
{
276+
static PyObject *builtin_popcount_func = NULL;
277+
278+
builtin_popcount_func = PyObject_GetAttrString(obj, "bit_count");
279+
if (builtin_popcount_func != NULL) {
280+
result = PyObject_CallFunction(builtin_popcount_func, "O", obj);
281+
return result;
282+
}
283+
/* silence errors, and fall back on pure-python `bit_count` */
284+
PyErr_Clear();
285+
}
286+
287+
/* Otherwise, use our internal one, written in python */
288+
{
289+
static PyObject *internal_popcount_func = NULL;
290+
291+
npy_cache_import("numpy.core._internal", "_bit_count", &internal_popcount_func);
292+
if (internal_popcount_func == NULL) {
293+
return NULL;
294+
}
295+
296+
result = PyObject_CallFunction(internal_popcount_func, "O", obj);
297+
298+
return result;
299+
}
300+
}
301+
270302
/*
271303
*****************************************************************************
272304
** COMPLEX FUNCTIONS **

0 commit comments

Comments
 (0)