Skip to content

Commit daf9555

Browse files
committed
MAINT: Various linting and minor fixes:
1. Fixed passing all args to _internals umath bitcount. Note: We use kwargs here that might hinder performance 2. Fixed linting errors. 3. Improved verbosity of logs 4. Made a generic TO_BITS_LEN macro to accomdate more length based functions in future
1 parent a5d88e1 commit daf9555

File tree

5 files changed

+28
-26
lines changed

5 files changed

+28
-26
lines changed

numpy/core/_internal.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def _usefields(adict, align):
7878
"offsets": offsets,
7979
"titles": titles}, align)
8080

81+
8182
_INT_SHIFT = 30
8283
_MASK = (2 ** _INT_SHIFT)
8384

@@ -89,7 +90,7 @@ def _get_ob_digit_array(num):
8990
num_list = []
9091
while t != 0:
9192
# Get remainder from division
92-
small_int = t % _MASK # more efficient bitwise analogue: (t & (MASK-1))
93+
small_int = t % _MASK
9394
num_list.append(small_int)
9495

9596
# Get integral part of the division (floor division)
@@ -816,9 +817,10 @@ def _popcount64(a):
816817

817818
# Refer to npy_math_internal.h.src for more details.
818819
a = abs(a)
819-
a -= ((a >> 1) & 0x5555555555555555)
820+
a = a - ((a >> 1) & 0x5555555555555555)
820821
a = (a & 0x3333333333333333) + (a >> 2 & 0x3333333333333333)
821-
return (((a + (a >> 4)) & 0xf0f0f0f0f0f0f0f) * 0x101010101010101 >> 56) & 0xff
822+
a = (a + (a >> 4)) & 0xf0f0f0f0f0f0f0f
823+
return (a * 0x101010101010101 >> 56) & 0xff
822824

823825
def _bit_count(a):
824826
""" Computes the number of 1-bits in a (Python Integer) """

numpy/core/_methods.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ def _prod(a, axis=None, dtype=None, out=None, keepdims=False,
5252
initial=_NoValue, where=True):
5353
return umr_prod(a, axis, dtype, out, keepdims, initial, where)
5454

55-
def _bit_count(a, out=None, where=True, casting='same_kind',
56-
order='K', dtype=None, subok=True):
57-
return umr_bit_count(a, dtype)
58-
5955
def _any(a, axis=None, dtype=None, out=None, keepdims=False, *, where=True):
6056
# Parsing keyword arguments is currently fairly slow, so avoid it for now
6157
if where is True:
@@ -293,3 +289,8 @@ def _dump(self, file, protocol=2):
293289

294290
def _dumps(self, protocol=2):
295291
return pickle.dumps(self, protocol=protocol)
292+
293+
def _bit_count(a, out=None, *, where=True, casting='same_kind',
294+
order='K', dtype=None, subok=True):
295+
return umr_bit_count(a, out, where=where, casting=casting,
296+
order=order, dtype=dtype, subok=subok)

numpy/core/include/numpy/npy_math.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ NPY_INLINE static float __npy_nzerof(void)
113113

114114
/* Magic binary numbers used by popcount
115115
* For type T, the magic numbers are computed as follows:
116-
* Magic[0]: 0b101010101010101... = (T)~(T)0/3
117-
* Magic[1]: 0b11001100110011... = (T)~(T)0/15 * 3
118-
* Magic[2]: 0b111100001111... = (T)~(T)0/255 * 15
119-
* Magic[3]: 0b100000001... = (T)~(T)0/255
116+
* Magic[0]: 0b01 01 01 01 01 01... = (T)~(T)0/3
117+
* Magic[1]: 0b0011 0011 0011... = (T)~(T)0/15 * 3
118+
* Magic[2]: 0b00001111 00001111... = (T)~(T)0/255 * 15
119+
* Magic[3]: 0b00000001 00000001... = (T)~(T)0/255
120120
*/
121121
static const npy_uint8 MAGIC8[] = {0x55, 0x33, 0x0F, 0x01};
122122
static const npy_uint16 MAGIC16[] = {0x5555, 0x3333, 0x0F0F, 0x0101};

numpy/core/src/npymath/npy_math_internal.h.src

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -854,15 +854,13 @@ npy_rshift@u@@c@(npy_@u@@type@ a, npy_@u@@type@ b)
854854
* #STYPE = BYTE, SHORT, INT, LONG, LONGLONG#
855855
* #c = hh, h, , l, ll#
856856
*/
857-
#undef MAGIC_ARRAY
858-
#undef WIN_POPCOUNT
857+
#undef TO_BITS_LEN
859858
#if 0
860859
/**begin repeat1
861860
* #len = 8, 16, 32, 64#
862861
*/
863862
#elif NPY_BITSOF_@STYPE@ == @len@
864-
#define MAGIC_ARRAY MAGIC@len@
865-
#define WIN_POPCOUNT __popcnt@len@
863+
#define TO_BITS_LEN(X) X##@len@
866864
/**end repeat1**/
867865
#endif
868866

@@ -872,22 +870,22 @@ npy_popcountu@c@(npy_@type@ a)
872870
#if ((defined(__clang__) || defined(__GNUC__))) && NPY_BITSOF_@STYPE@ >= 32
873871
return __builtin_popcount@c@(a);
874872
#elif defined(_MSC_VER) && NPY_BITSOF_@STYPE@ >= 16
875-
return WIN_POPCOUNT(a);
873+
return TO_BITS_LEN(__popcnt)(a);
876874
#endif
877875

878876
/* Counting bits set, in parallel
879877
* Based on: http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
880878
*
881879
* Generic Algorithm for type T:
882-
* v = v - ((v >> 1) & (T)~(T)0/3);
883-
* v = (v & (T)~(T)0/15*3) + ((v >> 2) & (T)~(T)0/15*3);
884-
* v = (v + (v >> 4)) & (T)~(T)0/255*15;
885-
* c = (T)(v * ((T)~(T)0/255)) >> (sizeof(T) - 1) * CHAR_BIT;
880+
* a = a - ((a >> 1) & (T)~(T)0/3);
881+
* a = (a & (T)~(T)0/15*3) + ((a >> 2) & (T)~(T)0/15*3);
882+
* a = (a + (a >> 4)) & (T)~(T)0/255*15;
883+
* c = (T)(a * ((T)~(T)0/255)) >> (sizeof(T) - 1) * CHAR_BIT;
886884
*/
887-
a = a - ((a >> 1) & (npy_@type@) MAGIC_ARRAY[0]);
888-
a = ((a & (npy_@type@) MAGIC_ARRAY[1])) + ((a >> 2) & (npy_@type@) MAGIC_ARRAY[1]);
889-
a = (a + (a >> 4)) & (npy_@type@) MAGIC_ARRAY[2];
890-
return (npy_@type@) (a * (npy_@type@) MAGIC_ARRAY[3]) >> ((NPY_SIZEOF_@STYPE@ - 1) * CHAR_BIT);
885+
a = a - ((a >> 1) & (npy_@type@) TO_BITS_LEN(MAGIC)[0]);
886+
a = ((a & (npy_@type@) TO_BITS_LEN(MAGIC)[1])) + ((a >> 2) & (npy_@type@) TO_BITS_LEN(MAGIC)[1]);
887+
a = (a + (a >> 4)) & (npy_@type@) TO_BITS_LEN(MAGIC)[2];
888+
return (npy_@type@) (a * (npy_@type@) TO_BITS_LEN(MAGIC)[3]) >> ((NPY_SIZEOF_@STYPE@ - 1) * CHAR_BIT);
891889
}
892890
/**end repeat**/
893891

numpy/core/tests/test_umath.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,8 +1760,9 @@ def test_reduce(self):
17601760
class TestBitwiseUFuncs:
17611761

17621762
_all_ints_bits = [
1763-
np.dtype(c).itemsize * 8 for c in np.typecodes["AllInteger"]]
1764-
bitwise_types = [np.dtype(c) for c in '?' + np.typecodes["AllInteger"] + 'O']
1763+
np.dtype(c).itemsize * 8 for c in np.typecodes["AllInteger"]]
1764+
bitwise_types = [
1765+
np.dtype(c) for c in '?' + np.typecodes["AllInteger"] + 'O']
17651766
bitwise_bits = [
17661767
2, # boolean type
17671768
*_all_ints_bits, # All integers

0 commit comments

Comments
 (0)