Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Architectures/ABIs where sizeof(long long) != 8

Tags:

c

abi

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 long as 64 bits, the same as a long.

Given that certain 64-bit instructions (multiply) produce 128-bit results, doesn't it seem logical the long long be defined as 128 bits?

No, for two reasons:

  1. 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.

  2. 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 that sizeof(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 for size_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:

  • they use same underlying type for 64-bit integers on different platforms
  • allows avoiding verbose PRId64/PRIu64
    (I am well aware that Visual C++ supports %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.

like image 470
przemoc Avatar asked Jul 15 '12 12:07

przemoc


1 Answers

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

like image 142
ouah Avatar answered Oct 24 '22 15:10

ouah