Thread: conversion from double to float - loss of data

  1. #16
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    Huh, so the actual processor architecture is called x86 and the FPU in the x86 is called x87?
    That's a remnant from the days where the FPU was on a physically separate chip. This chip was numbered XXX+1 where XXX was the CPU model. So the math coprocessor for a 80286 was called an 80287, the 80386 had an 80387. The 80486-DX (but not the -SX) was the first model (if I remember properly) to have an integrated FPU. If you went cheap and got the -SX, you could physically install a math coprocessor next to the main CPU at a later date.

    These days I'd call the floating point unit just "FPU."

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    x87 is nowadays often used to distinguish between the SSE and the old instruction sets.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I did some research on this very topic some time back and found that the instructions for double and float at the FPU assembly level execute at almost identical speeds. If I remember correctly the opcodes are the same and it is the data size that is the difference (or the operand size). So if you looked at the assembly code you would see the same opcodes being used regardless of the C data type. I remember coding a bilinear interpolation in pure asm using the FPU and it was very fast. Fordy came along and showed me how to make it faster. Now this is a moot point since I get it for free from the GPU.

    The native data type for the FPU is larger than both float and double. So at the assembly level the FPU really doesn't care and I believe uses saturation to fill the unused bits. But what some have said about being able to store more floats than doubles in the data type does hold some weight so that may be the key here.

    In the end I doubt seriously if you will see a huge performance hit by simply using doubles instead of floats. The surefire way to tell is to code an algorithm and profile it using floats and then using doubles. You won't find a big difference. The FPU is EXTREMELY fast at floating point operations and has come a very long way from the first gen math-co's. So much that I now believe that a floating point operation is as fast as an integer operation. Previously look up tables were used but now the time it takes to look-up the value and the possible cache thrash to do so is actually slower than just telling the FPU to do the operation.

    Now when it comes to graphics most GPUs appear on the surface to work with floats so it would probably be best to maintain data type consistency with them. However I'm not sure what they use internally.

    An interesting note is that my manual on the 486 processor shows that all FPU instructions execute at the same speed regardless of the underlying data type. The clock cycle counts are identical with float or double. Newer Intel manuals do not show the clock cycle counts. I could not find any clock cycle counts in my IA32 or IA64 tech refs.
    Last edited by VirtualAce; 03-03-2008 at 05:29 PM.

  4. #19
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The x87 always works on 80-bit units. That's the size of the FPU registers. To work with any data type, you load the values into the register stack and do something. Size normalization is done during the load, so the opcodes are the same.

    GPUs have only recently learned to handle 64-bit units. They're probably still better at dealing with 32-bit units. Also, since bandwidth remains the #1 bottleneck in graphics (unless you use absurdly complex shaders), cutting the data size in half is probably a good thing.

    And of course, there's the 128-bit SSE registers that can fit 4 32-bit values, but only 2 64-bits. A well-parallelized algorithm could execute up to twice as fast.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #20
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by laserlight View Post
    You can find them in Annex E (Implementation limits) of the 1999 edition of the C Standard, but I am not sure where to find a draft. Strictly speaking the C++ Standard refers to the 1990 version (same as 1989) of the C Standard, but I do not think these implementation limits have changed.
    I found this one:

    http://msdn2.microsoft.com/en-us/lib...e1(VS.71).aspx

    Is that really all? Does C++ not defnine any sizes, like char uses 1 byte or int uses [bus size] bits?

  6. #21
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    char uses 1 byte, because that's the C++ definition of a byte. char also has a minimum of 8 bits.
    Beyond that, there's only relations.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Im stuck....
    By dAzed in forum C++ Programming
    Replies: 8
    Last Post: 10-11-2004, 04:50 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. Configurations give different results
    By Hubas in forum Windows Programming
    Replies: 2
    Last Post: 04-11-2003, 11:43 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM