3

Sorry if this is a repeating post but I am unable to find the answer. I am working on an Intel i-7 machine. The sizeof(long double) is 16. However, I remember to hear somewhere that Intel coprocesor is natively using 10 bytes floating numbers (80-bits). So, why is the actual size of long double 16 and how can I access those native floating point numbers?

Marian
  • 7,402
  • 2
  • 22
  • 34

1 Answers1

6

The hardware used for floating point on this particular machine, when running in 32 bit mode, is the x87 floating point unit. It supports operations on 4 byte, 8 byte and 10 byte operands. Your 16 byte long double is actually the 10 byte type with padding. In other words only 10 of the 16 bytes are used and the remaining unused 6 bytes simply provide padding to ensure alignment.

Support for the 10 bytes extended data type is not widespread. For instance, the same machine running in x64 mode does floating point on its SSE unit. And that unit supports only 4 byte and 8 byte types. On such a machine you might find that double and long double are one and the same. Or maybe not. It depends on the compiler. And on non Intel architectures you won't find that type at all.

All this means that if you code assuming support for the 10 byte data type then you are making your code less portable.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I'm curious, if I specify a long double in my code, will the coprocessor be used with a 10 bytes one? How do they "shrink" my long double into that? – Marco A. Feb 23 '14 at 12:58
  • In the scenario described by the asker only 10 of the 16 bytes are used. The remaining 6 are unused padding to ensure good alignment. – David Heffernan Feb 23 '14 at 12:59
  • A CPU of very recent vintage is using a coprocessor from 30 years ago? – Scott Hunter Feb 23 '14 at 13:02
  • @Scott Yes indeed. On x86 that's the lowest common denominator for floating point. Some compilers will emit SSE code on x86 for performance reasons and so not have a 10 byte type available. Really the 10 byte extended double type on x87 is an anachronism. – David Heffernan Feb 23 '14 at 13:04
  • @DavidHeffernan But it's not using an ACTUAL, PHYSICAL 8087 chip (as your answer suggests), just supporting the instructions it defined. – Scott Hunter Feb 23 '14 at 13:08
  • @Scott I see. I certainly did not mean it is on a separate chip. But it is still an x87 unit. I'll try to edit to avoid that confusion. – David Heffernan Feb 23 '14 at 13:10
  • Why make it 10 if it is going to be 16 because of padding, why not just make it 16? – user16217248 Jun 26 '21 at 03:07
  • @user it's a long story, going back to the original design of the 80 bit Intel floating point type for one of their math coprocessors maybe 35-40 years ago. – David Heffernan Jun 26 '21 at 07:48
  • But I still find this weird waste of bits strange, it kind of is like making a 33 bit operating system but having to allocate 40 bits anyway because they have to come in units of 8 – user16217248 Jun 26 '21 at 17:55
  • @user have you heard of alignment and memory access being faster for aligned data? And it goes back to 1980 and the 8087 coprocessor. You can read about it if you do some websearch. – David Heffernan Jun 26 '21 at 18:35