libk  typesize.c at [85bec55157]

File arch/typesize.c artifact 9ee84449d7 part of check-in 85bec55157


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

#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)
#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() {
	int found_sz = 0, found_ofs = 0, found_type = 0;
	iflag(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;
}