[Numpy-discussion] code review request: masked dtype transfers

Nathaniel Smith njs at pobox.com
Sat Jul 9 22:53:04 EDT 2011


On Sat, Jul 9, 2011 at 7:24 PM, Mark Wiebe <mwwiebe at gmail.com> wrote:
> On Sat, Jul 9, 2011 at 2:38 PM, Nathaniel Smith <njs at pobox.com> wrote:
>>
>> On Sat, Jul 9, 2011 at 11:29 AM, Mark Wiebe <mwwiebe at gmail.com> wrote:
>> > typedef npy_uint8 npy_mask;
>> > #define NPY_MASK NPY_UINT8
>> > #define NPY_MASK_ISEXPOSED(mask) (((mask)&0x01) != 0)
>> > #define NPY_MASK_GETPAYLOAD(mask) (((npy_mask)mask) >> 1)
>> > #define NPY_MASK_MAKEMASK(exposed, payload) ((npy_mask)(exposed&0x01) |
>> > (npy_mask)(payload << 1))
>>
>> Even better, these should be inline functions instead of macros... (or
>> is there some horrible old compiler that we care about that that
>> wouldn't work for?)
>
> That's a good idea, it's always worthwhile to use the little bit of type
> checking the C compiler will let you. NumPy has a macro NPY_INLINE which is
> used to define inline functions.

Well, type checking, plus avoiding some of the horrible bizarre
pitfalls of using macros. Try running NPY_MASK_MAKEMASK(value > 2,
payload1 || payload2) with the above definitions...

It's a lot easier to just use functions and not have to spend energy
checking for that sort of nonsense :-).

-- Nathaniel



More information about the NumPy-Discussion mailing list