How can 12-bytes long double fit in 64 bits register?

94 views Asked by At

Let's assume that there is a long double variable in memory. According to what i've found in the Net it is 12-bytes big. How can CPU operate on it if registers size is 64 bits? Is it divided somehow?

1

There are 1 answers

0
Peter Cordes On

It doesn't. And the x87 hardware format is 80 bits (10 bytes) long, the width of the legacy x87 FPU registers (which still exist, and are useful for wider precision if you don't want to use something like Kahan summation or double-double techniques.


The extra 2 bytes in sizeof(long double) are padding to make sizeof(long double) a multiple of alignof(long double) == 4 in the i386 System V ABI (32-bit code).

In the x86-64 System V ABI, sizeof and alignof(long double) were bumped up to 16 so it can't split across cache lines. This is 6 bytes of padding, still 10 bytes of actual data accessed by fld m80 and fstp m80 instructions (which are several times slower than normal dword or qword float or double loads and stores, see my answer on the retrocomputing Q&A).

80-bit long double is never loaded into 128-bit XMM registers (except to copy it), or into 64-bit integer registers (except to copy it, or do integer stuff with the mantissa and exponent fields, like when implementing log or exp.) There are only HW instructions for doing math on it in the x87 registers.

Note that MSVC for x86 and x64 uses 64-bit long double, with the same format as double. GCC has options to be ABI compatible with that, or to have long double be the 80-bit x87 type to allow higher-precision FP conveniently. But on Windows beware of 32-bit libraries that set the x87 FPU precision bits to round off to fewer bits (which speeds up div and sqrt somewhat). See https://randomascii.wordpress.com/2012/03/21/intermediate-floating-point-precision/ re: DirectX libraries doing that.