c++visual-studio-codegcc

Why do I get 3 distinct sizes for long doubles in c++


As mentioned in the question, I am getting 3 different values for the size of a long double.

The intellisense of my editor (VScode) says that it is 8 bytes using sizeof(long double).

std::numeric_limits<long double>::digits, ::max_exponent returns 64 and 16384 (2^14) respectively. So the standard library says it is somewhere around 11 - 12 bytes. (I std::cout-ed the results)

Then when I do std::cout << sizeof(long double), it prints 16 (bytes).

I have no clue why I am getting 3 different values for the sizeof long double. Btw I don't want to actually use long doubles, I was just playing around when I noticed this.


Compiler & Editor Info

I am using vscode and the intellisense mode is windows-gcc-x64 and my gcc target is x86_64-w64-mingw32. So it is a 64 bit compiler.


Solution

  • long double in MSVC is double and IntelliSense respects MSVC ABI, therefore showing 8 bytes.

    Windows API does not use long double, so compilers are safe and free to select any size of long double. GCC made it to occupy 16 bytes.

    it is somewhere around 11 - 12 bytes.

    It's rather 10 bytes. Most processors are limited by 80 bit extended doubles. So, long double precision is limited by CPU floating point implementations, this is 64 binary digits and 16384 exponent value.

    So,

    1. IntelliSense is mistaken when not taking GCC into account.
    2. It's showing 10 bytes is the true floating point math.
    3. long double is 16 bytes in GCC for getting properly aligned long double in arrays and struct members.

    Per long double on Wikipedia:

    On the x86 architecture, most C compilers implement long double as the 80-bit extended precision type supported by x86 hardware (generally stored as 12 or 16 bytes to maintain data structure alignment), as specified in the C99 / C11 standards (IEC 60559 floating-point arithmetic (Annex F)). An exception is Microsoft Visual C++ for x86, which makes long double a synonym for double.