Home > Enterprise >  Are exact-width integers in Cython actually platform dependent?
Are exact-width integers in Cython actually platform dependent?

Time:11-25

In Cython one can use exact-width integral types by importing them from stdint, e.g.

from libc.stdint cimport int32_t

Looking through stdint.pxd, we see that int32_t is defined as

cdef extern from "<stdint.h>" nogil:
    ...
    ctypedef signed int int32_t

Does this mean that if I use int32_t in my Cython code, this type is just an alias for signed int (int), which might in fact be only 16 bits wide?

The issue is the same for all the other integral types.

CodePudding user response:

They should be fine.

The typedefs that are really used come from the C stdint.h header, which are almost certainly right.

The Cython typedef

ctypedef signed int int32_t

Is really just so that Cython understands that the type is an integer and that it's signed. It isn't what's actually used in the generated C code. Since it's in a cdef extern block it's telling Cython "a typedef like this exists" rather than acting as the real definition.

CodePudding user response:

No.

On a platform where signed int is not 32bit wide, int32_t would be typedef'ed to a type that actually is 32bit wide.

If no such type is available -- e.g. on a platform where the maximum int width is 16, or where all ints are 64bit, or where CHAR_BIT does not equal 8 -- the exact-width types would not be defined. (Yes, the exact-width types are optional. That is why there are least-width types as well.)

Disclaimer: This is speaking from a purely C perspective. I have no experience with Cython whatsoever. But it would be very surprising (and a bug) if this would not be covered adequately in Cython as well.

And as @JörgWMittag points out in his comment, the alternative is of course to simply not support any platform where signed int isn't 32 bit wide.

  • Related