Bitfield subtlety

Torbjörn Granlund tg at gmplib.org
Tue Mar 27 22:08:01 UTC 2018


I am playing with a GMP version which allows greater mpz numbers than
what is currently handled.  The idea is to combine the _mp_size and
_mp_alloc fields such that _mp_size becomes 40 bits while _mp_alloc
becomes a custom float.

I ran into some problems with C and C++ when it comes to bit fields.

In C++, a bit field whose base type is not explicitly signed or unsigned
gets undefined signedness.  It would be desirable to use stdint.h's
int64_t, but while that is signed, it is not guaranteed to be explicitly
so.

In C, at least old C, allows just (signed or unsigned ) int as base
types.  I.e., long is not allowed, and thus int64_t is also not allowed.


Does anybody on this list know if C99 is more tolerant wrt base type? If
so, will implicity signed basetypes result in signed bit fields (unlike
C++)?

I see no nice solution to the C++ rule.  I suppose that if we decide to
go with bit fields here, we will simply not support problematic C++
compilers.  Or we arm autoconf to probe for this too, and stay away from
stdint.h.

-- 
Torbjörn
Please encrypt, key id 0xC8601622


More information about the gmp-devel mailing list