[Numpy-discussion] Re: int16 or int32 for C int ?

Scott David Daniels Scott.Daniels at Acm.Org
Mon Jun 27 10:43:40 CDT 2005


Nicolas Pernetty wrote:
> On Mon, 27 Jun 2005 09:00:52 +0200, Francesc Altet <faltet at carabos.com>
> wrote :
>>>I would like to create a numarray array from a C buffer of 'int' but
>>>I don't quite know how to handle different 'int' representation (16
>>>or 32 bits).
> ... It was to be interfaced with legacy code which include 'int'
> declaration (which I couldn't change). So I think that I'll keep the
> 'sizeof' trick.
I would not treat the test as either-or, but rather as a triple.
So:
     if (sizeof(**a) == 4) x = NA_NewArray(a, tInt16, 2, 10, 20);
     else if (sizeof(**a) == 2) x = NA_NewArray(a, tInt16, 2, 10, 20);
     else abort(); /* or raise some exception here. */

If you compile this on a machine with 64-bit ints, it is better if it
fails here than proceed as if working with 2-byte ints.  If I am sure
I'll never run into that case, I'd do it as above (with the abort()).
A good C compiler can realize the test is constant and simply generate
one arm of the conditions anyway.  If I think it might possibly be
used on a 64-bit int system, I'd go ahead and worry about what exception
to raise (probably PyTypeError, but ...).

--Scott David Daniels
Scott.Daniels at Acm.Org





More information about the Numpy-discussion mailing list