Search code examples
carchitecturedatamodel

Finding Data Model(s) in C


Is it safe to assume:

#if defined(_M_X64) || defined(_M_AMD64) || (defined(_M_IA64) && !defined(__itanuim__))
/* LLP64 Data Model */
#elif defined(__amd64__) || defined(__ia64__) || defined(__ia64) || defined(__itanuim__)
/* LP64 Data Model */
#else
/* 32-bits Data Model(s) */
#endif

for Unix, BSD, Windows, Linux and HP? or I'm totally wrong :-) or missing something?

Thanks


Solution

  • If you really don't have UINT_MAX and Co available through include files you can at least determine UINTMAX_MAX:

    typedef unsigned long long uintmax_t;
    typedef signed long long intmax_t;
    
    // start testing with the smaller values
    #if -1U == 4294967295U
    # define UINTMAX_MAX 4294967295
    #elif -1U == 18446744073709551615U
    ...
    #endif
    

    The preprocessor computes with the same width as uintmax_t. So you'd know at least that part of the architecture.

    For other things I would write a test code that creates a small include file:

    printf("#define CHAR_IS_SIGNED %d\n", ((char)-1 < 0));
    printf("#define UCHAR_MAX %hhu\n", (unsigned char)-1);
    printf("#define USHRT_MAX %hu\n", (unsigned short)-1);
    printf("#define UINT_MAX %u\n", (unsigned)-1);
    printf("#define ULONG_MAX %lu\n", (unsigned long)-1);
    printf("#define ULLONG_MAX %llu\n", (unsigned long long)-1);
    

    Under reasonable assumptions (two's complement, no padding bits) you may then deduce the values for the signed types and also you'd be able to do the correct typedef for the uintXX_t types.