Managed to get a working macro that can calculate the size of an integer type based on the unsigned max given (up to supported size anyway), the problem I'm having is silencing the preprocessor warnings, I know in VC this could be circumvented by using hard integers (because it for some reason supports using them in the preprocessor, at least in the cases I tried anyways) but how would I go about silencing them in various other compilers (like gcc & clang)
Incidentally I have -Wall and -Wextra set at the moment, also here's the snippet with an example usage:
Code:
#define MAX_FOR_SIZE(PRV) ((PRV * PRV) + PRV + PRV)
#define MAX_FOR_1BYTE UCHAR_MAX
#define MAX_FOR_2BYTE MAX_FOR_SIZE(MAX_FOR_1BYTE)
#define MAX_FOR_4BYTE MAX_FOR_SIZE(MAX_FOR_2BYTE)
#define MAX_FOR_8BYTE MAX_FOR_SIZE(MAX_FOR_4BYTE)
#define _SIZEOF(UMAX,SIZE,TRUE) \
(((UMAX) > (MAX_FOR_##SIZE##BYTE)) ? (TRUE) : SIZE)
#define SIZEOF(UMAX) \
_SIZEOF(UMAX,1,_SIZEOF(UMAX,2,_SIZEOF(UMAX,4,_SIZEOF(UMAX,8,16))))
#ifndef SIZEOF_INTEGRAL
#if 0
#if MAX_FOR_2BYTE <= MAX_FOR_1BYTE
#define SIZEOF_INTEGRAL 1
#elif MAX_FOR_4BYTE <= MAX_FOR_2BYTE
#define SIZEOF_INTEGRAL 2
#elif MAX_FOR_8BYTE <= MAX_FOR_4BYTE
#define SIZEOF_INTEGRAL 4
#elif MAX_FOR_8BYTE <= ~0u
#define SIZEOF_INTEGRAL 8
#else
#define SIZEOF_INTEGRAL 16
#endif /* RESULT <= ORIGINAL */
#else
#define SIZEOF_INTEGRAL SIZEOF(~0u)
#endif
#endif /* SIZEOF_INTEGRAL */
Edit: Also here's some output using those very values:
Code:
...
./alu.AppImage
test.c:257: main() MAX_FOR_1BYTE = FF
test.c:258: main() MAX_FOR_2BYTE = FFFF
test.c:259: main() MAX_FOR_4BYTE = FFFFFFFF
test.c:260: main() MAX_FOR_8BYTE = FFFFFFFF
test.c:261: main() '==========================================='
test.c:262: main() SIZEOF_INTEGRAL = 4
test.c:263: main() SIZEOF_SHRT = 2
test.c:264: main() SIZEOF_INT = 4
test.c:265: main() SIZEOF_LONG = 8
test.c:266: main() '==========================================='
test.c:267: main() UCHAR_MAX = FF
test.c:268: main() USHRT_MAX = FFFF
test.c:269: main() UINT_MAX = FFFFFFFF
test.c:270: main() ULONG_MAX = FFFFFFFFFFFFFFFF
test.c:271: main() '==========================================='
test.c:273: main() 'Initiating ALU to 0...'
test.c:276: main() 'Pre-allocating 16 ALU registers...'
Compilation finished successfully.