문제

I'm currently porting some code from another platform and bools on the new platform are 1-byte sized. This is breaking our loading code as the values are stored as 32-bit values. Furthermore, speed is a critical issue on our platform and we would like to use 32-bit bools as the processor runs at 32-bits natively and requires extra operations to compare non 32-bit bools.

Is there a way to force gcc to use 32-bit bools instead of 8-bit bools?

도움이 되었습니까?

해결책

You could create your own class that uses int32_t internally but behaves like a bool. It does mean you'd have to rename the fields you specifically want to use that type, which is more work but affords better control and isolation, and you can still use real bools elsewhere. I'd personally prefer that to any #define hackery, which might bite somewhere unexpected. I'd also caution against assuming a 32-bit int will be usefully faster than a single byte... other factors like pipelining, memory latencies, cache sizes etc. could render the difference insignificant or even make 32-bit ints slower, so you might want to benchmark it in your system with representatitive data handling.

다른 팁

Add #define BOOL_TYPE_SIZE 4 to gcc/config/i386/i386.h and recompile gcc ;)

The size of bool is implementation defined (5.3.3), and gcc doesn't appear to provide an option to configure this at run-time.

Hopefully your implementation-defined code is isolated. If so, change your bools to ints, or change your loading code to deal with sizeof() == 1 instead of 4.

(Or, for the crazy, alter gcc to treat bool as a 4-byte type.)

Edit: Paul Tomblin's suggestion of using #define may not be legal [see here], but it does work under gcc 4.1.2, at least. [Link] However, if you don't hit every usage of bool the size mismatch will almost certainly bite you.

#define bool int

You need to separate your internal data structures from the storage/loading code. Simply store the bools in your internal data structures in your platform's native bool type and do the appropriate conversion from/to the storage's one byte bools when reading/writing the data.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top