Thread: Fixed points on 64 bit machine

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    244

    Unhappy Fixed points on 64 bit machine

    Hello,

    i have a general question about fixed point maths: for the math intensive part of my project, i've replaced floats with fixed points. i'm using the __int32 type for that. it's much faster than using floats on my x86 machine, but when i try it on my x64 bit windows 7 machine, the float variant is much faster.

    i have no clue why...
    please help me out

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Post smallest compilable code that shows this behaviour.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    http://xload.dev-ch.de/3de17e9f3ce57...point-test.zip

    it's in VirtualGL.cpp and defined by #define USE_FIXPOINT

    i just use "typedef __int32 fixint" which is indeed faster on x86, but not on x64. yet i don't know if it's about the x86/x64 or about something else...

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Have you profiled the code? Which function is taking all the time?

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Since x86-64 can do 32-bit math at native speed, I'm tempted to say the problem is probably elsewhere.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    the vglTriangleLine() function which draws the pixels takes the very most time.

    and i tried to make example code:
    Code:
    #include <time.h>
    #include <iostream>
    using namespace std;
    
    #define USE_FIXINT                // comment this out if you want float instead of fixpoints
    #ifdef USE_FIXINT
        typedef __int32 fixint;
    #else
        typedef float fixint;
    #endif
    
    int main()
    {
        int clo = clock();
    
        fixint x = 10, y = 2;
        for (int i = 0; i < 100000000; i++)
        {
            x *= y;
        }
    
        cout << (clock() - clo) << endl;
        cin.get();
        return 0;
    }
    i tested this on my x86 machine. using fixpoints: 360 ms, using floats: 12500 ms. i haven't tested it on my x64 machine yet, tho.

    edit: i asked a friend, he said that my other machine propably has a FPU (floating processing unit) which can process floats faster than i can process fixed ints. can this be true?
    Last edited by Devils Child; 11-11-2010 at 02:18 AM.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    On my machine (x86-64 Linux), int takes 0.293s, and float takes 0.361s.

    No difference between 32-bit and 64-bit.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    hm, i couldn't test this little example on x64 yet. the renderer (duno if you could compile it) is slower on 64 bit & fix points.

    maybe it's because the "int" type is 64 bit on a 64 bit machine. i have replaced ALL "int" with "__int32" and it's no use. what do you think? anything else might be the cause? are you able to run my engine?

    i guess i have to work more on it when i'm back home at my x64 machine.

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    On x86-64, int is still 32-bit, for compatibility reasons, even though it really should be 64-bit.

    Long would be 32 or 64-bit on 64-bit, and long long 64-bit for sure.

    I don't have Visual Studio on this machine, so I can't run your code.

    You really need to profile it, though. It's entirely possible that it's nothing to do with your code.

    Maybe your SDL on x86-64 is slower for some reason.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    i can't compare if SDL in particular is slower, but i can compare that floats are faster than fixpoints one one computer (x64). and that on the 32 bit computer, fixpoints are faster than floats.

    i asked a friend, he said that my other machine propably has a FPU (floating processing unit) which can process floats faster than i can process fixed ints. can this be true?

  11. #11
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    All processors made in the past 20 years has a FPU.

    Floating point should still be slower than ints.

    i can't compare if SDL in particular is slower, but i can compare that floats are faster than fixpoints one one computer (x64). and that on the 32 bit computer, fixpoints are faster than floats.
    Do you have a non-SDL example that shows this?

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    yes, i posted the source code, but i will be able to test it on my x64 bit machine in a couple of hours, then i probably get more clear about that.

  13. #13
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Well, it's not showing that for me. I get equal performance.

    You may want to mark x and y as volatile, so compiler won't optimize them out.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    - better FPU

    - better scheduling of instructions. INT and FPU can execute in parallel if you do it right. This can reduce the cost of FP down to zero.

    - worse INT performance if int is 32 bit, as it takes an extra step to chop off the unwanted bits.

    There are very few "guaranteed" performance tweaks. Mostly, it depends on exactly what you're doing.

    The only real tool worth having is a profiler to help you understand what your actual code is actually up to in this actual implementation. Anything you read (especially 20+ year old hacks which get recirculated every so often) should be regarded with increasingly large pinches of salt.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    - worse INT performance if int is 32 bit, as it takes an extra step to chop off the unwanted bits.
    That shouldn't be the case on x86-64. The 32-bit registers (EAX, EBX, ECX, EDX) are still there. 64-bit ones are just extensions (RAX, RBX, RCX, RDX), like EAX vs AX vs AL, etc. It can still do 32-bit at full speed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c# OpenSubKey problem on 64 bit windows vs 32 bit.
    By Striph in forum C# Programming
    Replies: 3
    Last Post: 03-22-2010, 06:07 AM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. CProg Fantasy Football version pi
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 155
    Last Post: 12-26-2006, 04:30 PM
  4. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM