// Test program for platform dependent things like type sizes, endianess, alignment, ... // See also https://wiki.debian.org/ArchitectureSpecificsMemo. // // Todo: Show more environment parameters like the growth direction of the heap, // alignment and representation, page sizes, bit padding etc.. // And a preprocessor version with preprocessor output, like // #if INT_MAX == 0x7fff // # warning "INT_MAX is 0x7fff" // #elif INT_MAX == 0x7fffffff // # warning "INT_MAX is 0x7fffffff" // This is usefull for a) Cross-Compiling and b) checking for freestanding implementations without printf. // Dr. Rolf Freitag 2020-02-08 #include #include #include #include #include #include #include #include /* Estimate the main endianess. void endianess () { union { uint64_t uli; uint32_t ui[2]; uint16_t usi[4]; uint8_t uc[8]; } u_end = { 1}; printf ("Endianess: "); if (1 == u_end.uc[0]) { printf ("LITTLE_ENDIAN\n"); return; } else { if (1 == u_end.usi[1]) { printf ("PDP_ENDIAN\n"); return; } else { if (1 == u_end.uc[7]) { printf ("BIG_ENDIAN\n"); return; } } } printf ("UNKNOWN_ENDIAN\n"); return; } */ static void test_size_align (const char *name, size_t size, size_t align) { printf ("sizeof(%s) = %zu\n", name, size); if (size != align) printf ("alignment(%s) = %zu\n", name, align); return; } #define TEST_SIZE_ALIGN(type, name) \ struct test_align_##name { char a; type b; }; \ test_size_align(#type, sizeof(type), offsetof(struct test_align_##name, b)) static void test_endian (void) { #if __BYTE_ORDER == __LITTLE_ENDIAN printf ("byte order = little endian\n"); #elif __BYTE_ORDER == __BIG_ENDIAN printf ("byte order = big endian\n"); #else printf ("byte order = mybe pdp endian\n"); #endif return; } static void test_char (void) { if ((int) (char) -1 == -1) printf ("char signedness = signed\n"); else if ((int) (char) -1 == 255) printf ("char signedness = unsigned\n"); return; } static void test_int (void) { if ((int)(-1) < 0) printf ("int signedness = signed\n"); else printf ("char signedness = unsigned\n"); return; } static void test_stack (void) { void *a = alloca (8); void *b = alloca (8); if (a > b) printf ("stack = grows down\n"); else printf ("stack = grows up\n"); return; } int main () { printf ("Size of byte in bits is %d.\n", CHAR_BIT); printf("maximum number of bytes in a multibyte character, MB_LEN_MAX = %d\n", MB_LEN_MAX); printf ("Size of char (in bytes) is %lu.\n", sizeof (char)); printf ("Size of wide char is %lu.\n", sizeof (wchar_t)); printf ("Size of time_t is %lu, time_t is %s.\n", sizeof(time_t), ((time_t)(-1) < 0)? "signed" : "unsigned"); TEST_SIZE_ALIGN (short, short); TEST_SIZE_ALIGN (int, int); TEST_SIZE_ALIGN (long, long); TEST_SIZE_ALIGN (long long, long_long); TEST_SIZE_ALIGN (float, float); TEST_SIZE_ALIGN (double, double); TEST_SIZE_ALIGN (long double, long_double); TEST_SIZE_ALIGN (void *, pointer); TEST_SIZE_ALIGN (size_t, size_t); test_endian (); test_char (); test_int (); test_stack (); return 0; }