in reply to A clean way to account for machine word sizes in XS code?

So, what I'm looking for is an easy way to figure out which native C types have 8, 16 and 32 bits apiece at compilation time and to have my C/XS code use those data types.

Not sure that it's all that easy, but one way would be to have the Makefile.PL determine the sizes of the various types, and then use that info to construct the various typedefs. The ones that will interest you are:
$Config{shortsize}
$Config{intsize}
$Config{longsize}
$Config{ivtype}
and
$Config{ivsize}

Not sure of how to determine charsize. The first 3 give you info about the sizes of the different types for the particular compiler, the last 2 give you the info about the size of the IV (the perl integer).

Depending upon what those values are, you can have WriteMakefile() pass a define symbol to the build process (look for the DEFINE documentation in ExtUtils::MakeMaker), and have the header file write the appropriate typedefs based upon which symbol was defined:
#ifdef some_symbol // one set of typedefs #endif #if def another_symbol // another set of typedefs #endif . .
I do pretty much this in Math::MPFR. I'm essentially looking only at $Config{nvsize} and $Config{ivsize} and I doubt that my approach is infallible, but it seems to work ok - afaik the failures I get there are for other reasons (most of which I don't really care about).

Cheers,
Rob

Replies are listed 'Best First'.
Re^2: A clean way to account for machine word sizes in XS code?
by DrHyde (Prior) on Aug 14, 2009 at 09:41 UTC

    char is 8 bits on every platform perl runs on.

    You could use the new C99 types, as that already has known-length types so you can declare a variable as, eg, a 16-bit int instead of just hoping that short is 16 bits.

    Unfortunately on some platforms (and possibly on some compilers on other platforms) you'll need to do some Magic to invoke the compiler in C99-mode. I suggest not bothering, until someone sends you a failure report. Then you ask them for a patch for their weird platform :-)