In x86/amd64 world sizeof(long long) is 8.
Let me quote quite insightful 8 year old mail by Zack Weinberg:
Scott Robert Ladd writes:
On a 64-bit AMD64 architecture, GCC defines
long longas 64 bits, the same as along.Given that certain 64-bit instructions (multiply) produce 128-bit results, doesn't it seem logical the
long longbe defined as 128 bits?No, for two reasons:
The choice of 64-bit '
long long' has been written into the ABI of most LP64-model operating systems; we can't unilaterally change it.This is actually the correct choice, as it removes the aberration that makes '
long' not the widest basic integral type. There is lots and lots of code in the wild written to the assumption thatsizeof(long) >= sizeof(size_t)- this is at least potentially broken by ABIs where long long is wider than long.(This was an extremely contentious topic during the development of C99. As best as I can tell from an outside perspective, '
long long' was only standardized due to pressure from Microsoft who can't for some reason implement an LP64 model. Everyone else hated the idea of making 'long' not necessarily the widest basic integral type.)Best current practice appears to be to provide an "extended integral type"
__int128. This doesn't have the problems of 'long long' because it's not a basic integral type (in particular, it cannot be used forsize_t).zw
long long is widest basic integral type. It's 64-bit long on any non-dead-old architectures/ABIs I know. This allows for going with simple cross-platform (well, at least for many 32/64-bit architectures) typedefs:
typedef char s8;
typedef unsigned char u8;
typedef short s16;
typedef unsigned short u16;
typedef int s32;
typedef unsigned int u32;
typedef long long s64;
typedef unsigned long long u64;
that are nicer than intXX_t, because:
PRId64/PRIu64%lld/%llu only since 2005)But how portable this solution is can be expressed by answers to the following question.
What are the architectures/ABIs where sizeof(long long) != 8?
If you cannot provide any recent/modern ones, then go ahead with the old ones, but only if they are still in use.
TI TMS320C55x architecture has CHAR_BIT of 16-bit and long long of 40-bit. Although the 40-bit long long violates ISO, sizeof (long long) is different from 8.
Actually nearly all the C99 implementations with CHAR_BIT > 8 have sizeof (long long) != 8.
TMS320C55x Optimizing C/C++ Compiler User’s Guide (2003) http://www.ti.com/lit/ug/spru281f/spru281f.pdf
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With