libk  typesize.c at [d77ef26adf]

File arch/typesize.c artifact 750b65151f part of check-in d77ef26adf


#include <stdio.h>
#include <limits.h>
#include <stddef.h>

#ifndef _emit_m4_include
#	define sflag(flag,val) printf("-D"#flag"=\"%s\" ", val)
#	define mflag(flag,val) printf("-D"#flag"=-%llu ", val + 1)
#	define iflag(flag,val) printf("-D"#flag"=%llu ", val)
#	define fflag(flag,val) printf("-D"#flag"=%Lf ", val)
#else
#	define sflag(flag,val) printf("define(`"#flag"',`%s')dnl\n", val)
#	define mflag(flag,val) printf("define(`"#flag"',`%llu')dnl\n", val + 1)
#	define iflag(flag,val) printf("define(`"#flag"',`%llu')dnl\n", val)
#	define fflag(flag,val) printf("define(`"#flag"',`%Lf')dnl\n", val)
#endif
#define type_found(val) (found_type & (val / 8))
#define checkbits(type,x) \
	if(!type_found(x) && sizeof(type) == (x/CHAR_BIT)) \
		sflag(type_bit##x, #type), found_type |= (x / 8)

#define describe_integral(type,label) { \
	unsigned type maxval = -1; \
	unsigned type smaxval = (maxval/2) - 1; \
	iflag(type_max_u_##label, maxval); \
	iflag(type_max_s_##label, smaxval); \
	mflag(type_min_s_##label, smaxval); \
	iflag(type_##label##_bits, (CHAR_BIT * sizeof(type))); \
	checkbits(type, 8); else checkbits(type,16); else \
	checkbits(type,32); else checkbits(type,64); else \
	checkbits(type,128); else \
	if (!found_sz && sizeof(type) == sizeof(size_t)) \
		found_sz = 1, sflag(type_sz, "unsigned " #type); \
	if (!found_ofs && sizeof(type) == sizeof(ptrdiff_t)) \
		found_ofs = 1, sflag(type_offset, "signed " #type); \
}

int main() {
	unsigned char etest[sizeof(int)] = {0xFF,0x00};
	if ((*(int*)etest) == 0xFF) sflag(prop_endian,"low");
		else sflag(prop_endian,"high");
	
	int found_sz = 0, found_ofs = 0, found_type = 0;
	iflag(arch_byte_bits,CHAR_BIT);
	describe_integral(char,char);
	describe_integral(short,short);
	describe_integral(int,int);
	describe_integral(long,long);
	describe_integral(long long,llong);
	printf("\n");
	return 0;
}