NumPy currently has a reasonable selection of bitwise operators. However, one missing class are bit deposit and extract functions (also known as expand and compress). The general prototype is deposit(mask, x) and extract(mask, x). The extract function goes through each entry in mask and, if it is set, extracts the corresponding bit from x and concatenates it into the return value. The deposit function does the opposite: the first bit in x goes to the location of the first non-zero in the mask, the second bit in x goes to the location of the second non-zero in the mask, and so on, with everything being or'ed together and returned.
These functions have a variety of uses. A particularly nice one is if you want to Morton order a set of integers in N dimensions which reduces down to N deposit calls followed by some | and <<. More importantly, these functions are very difficult to efficiently emulate in NumPy as a large number of explicit <<, &, and | operations are required. Indeed, these functions are so important that recent CPUs have support for them in hardware (x86 has PDEP and PEXT for ~10 years, AVX-512 has similar vector versions but called compress and expand, SVE also has variants, and they'll be part of RISC-V, too). The functions are also under consideration for future revisions of the C++ standard (see https://eisenwave.github.io/cpp-proposals/bit-permutations.html#applications-of-bit-compress-and-bit-expand which also gives some other use cases). It would therefore be nice if they were supported by NumPy even if, initially, without hardware support (straight emulation will still be 10~20 times faster than what one can accomplish in NumPy simply because everything happens on a per-element basis in registers and these methods expose a fair amount of instruction level parallelism). The implementation should not be too complicated (the above link shows a reference implementation in C++). Regards, Freddie. _______________________________________________ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-le...@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: arch...@mail-archive.com