82

I know that size of various data types can change depending on which system I am on.

I use XP 32bits, and using the sizeof() operator in C++, it seems like long double is 12 bytes, and double is 8.

However, most major sources states that long double is 8 bytes, and the range is therefore the same as a double.

How come I have 12 bytes? If long double is indeed 12 bytes, doesn't this extends the range of value also? Or the long signature is only used (the compiler figures) when the value exceed the range of a double, and thus, extends beyond 8 bytes?

phuclv
  • 37,963
  • 15
  • 156
  • 475
CppLearner
  • 16,273
  • 32
  • 108
  • 163
  • 2
    This is the worst feature in ISO C. I strongly discourage you from using it. It only leads to problems because the specification is so loose. – Jeff Hammond Aug 25 '15 at 01:16
  • @JeffHammond it can't be strict, otherwise how can C be available for 12, 16, 24, 36, 60, 72-bit systems? There's no way you can specify a fixed IEEE-754 floating-point format to run efficiently on a system with 72-bit non-IEEE float – phuclv Mar 14 '22 at 01:35

3 Answers3

76

Quoting from Wikipedia:

On the x86 architecture, most compilers implement long double as the 80-bit extended precision type supported by that hardware (sometimes stored as 12 or 16 bytes to maintain data structure .

and

Compilers may also use long double for a 128-bit quadruple precision format, which is currently implemented in software.

In other words, yes, a long double may be able to store a larger range of values than a double. But it's completely up to the compiler.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Borealid
  • 95,191
  • 9
  • 106
  • 122
  • 2
    Data types depends heavily on the architecture you're developing for. – karlphillip Aug 11 '10 at 01:01
  • It also depends on compiler options. The 80-bit type can be explicitly disabled on almost every x86 compiler. – greyfade Aug 11 '10 at 01:05
  • 1
    @karlphillip, @greyfade: Yes, I just meant "up to the compiler" in the sense that it decides how to store your data. Obviously it's limited to what is available on the platform, and of course the compiler can choose to allow a user override. – Borealid Aug 11 '10 at 01:06
  • 1
    Apple claims that `long double` is 128-bit: https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/64bitPorting/transition/transition.html – Aaron Franke Feb 04 '19 at 05:24
  • 3
    @AaronFranke that's the **size after padding**. The real underlying type is still 80-bit extended but padded to 12 bytes on x86 and 16 bytes on x86-64 for alignment reason. Almost all x86 compilers apart from MSVC do that. No one uses IEEE-754 quadruple precision for long double as that's extremely slow – phuclv Mar 26 '21 at 01:24
  • 1
    @AaronFranke that's also specified by the [x86-64 SysV ABI](https://stackoverflow.com/q/18133812/995714) which Unix (Apple included) uses. Just check `LDBL_MANT_DIG` and see https://news.ycombinator.com/item?id=19237884 – phuclv Mar 26 '21 at 01:27
20

For modern compilers on x64, Clang and GCC uses 16-byte double for long double while VC++ uses 8-byte double. In other words, with Clang and GCC you get higher precision double but for VC++ long double is same as double. The modern x86 CPUs do support these 16-byte doubles so I think Clang and GCC are doing the right thing and allows you to access lower level hardware capability using higher level language primitives.

Shital Shah
  • 63,284
  • 17
  • 238
  • 185
  • 3
    The size is 16 bytes but 6 bytes of that is padding. `long double` is always 80-bit extended by default, padded to 12/16 bytes on x86 and x86-64 respectively. You can change the size via [`-mlong-double-64/80/128` options](https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html) if you're willing to break the ABI or some APIs. There are also `-m96/128bit-long-double` to change the padding size – phuclv Mar 26 '21 at 01:32
5

The standard byte sizes for numbers are the guaranteed minimum sizes across all platforms. They may be larger on some systems, but they will never be smaller.

scott77777
  • 698
  • 2
  • 7
  • 18