Thread: question about inline _asm and speed

  1. #1
    Banned
    Join Date
    Jan 2003
    Posts
    1,708

    question about inline _asm and speed

    Will _asm {} in C++ (if well written) necessarily be faster? Is it just the same (speedwise) as doing 'regular' assembly (i.e using NASM or MASM or something).

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Inline assembly will just place the assembly code you write right where you wrote it. Depending on your compiler, it will be a different kind of assembly but it will output just what you write. It probably will be faster than having an external file since then you have to call the function instead of having it inline.

    As long as you know what you're doing in that __asm block, you're fine. If not, don't do it or read up on it.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Speedy5
    If not, don't do it or read up on it.
    Lol! We should use parantheses in our daily language too. That sentence can be read in two ways:

    If not, don't (do it or read up on it).

    If not, (don't do it) or (read up on it).

    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    Originally posted by Magos
    Lol! We should use parantheses in our daily language too. That sentence can be read in two ways:

    If not, don't (do it or read up on it).

    If not, (don't do it) or (read up on it).

    Yes, English can certainly be ambiguous at times, and I've thought about this exact same thing. It's funny that you need to know the context in which it was written to understand which way he meant. There really should be a way to explicitly state which method was meant.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Originally posted by Magos
    Lol! We should use parantheses in our daily language too. That sentence can be read in two ways:

    If not, don't (do it or read up on it).

    If not, (don't do it) or (read up on it).

    HAHAHAHA!
    Yep, number one. I told this to friends at school and they blew up into laughter! They said English isn't C++ so parenthesis are stupid. Maybe square brackets?

  6. #6
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    keep in mind that inlined ASM will make your programs hardware specific. for example, if you write assembly in your code for the IA32 architecture, your program is now useless to anyone using a different chip. If you inline direct hardware instructions to other devices (vid card, etc...) the program is now specific to only machines using these devices.

    most compilers these days can do so much optimizing that inline assembly really wont create much in the way of speed.

  7. #7
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    Thanks you guys (you too vVv). You've provided a lot of information already. I'm hesitant to jump into asm because its a new language, and I'm hearing other people tell me that Visual Studio will optimize the inline assembly away, no matter how well I do it (i.e i might as well just leave it in C/C++ or get an external compiler). I wanted to implement it to make mathematical equations run as fast as they possibly can. I am probably going to buy an assembly book before posting again, or just hold off altogether until a later date (which may be a good idea).

  8. #8
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    getrusage()
    What is that exactly (what OS).

    that's a very useful site, and I'm pleased to say i'm already doing what they suggest (I'm testing the most expensive parts of my program and seeing how long it takes to do it, from that information I can estimate how many times per second each part of my program can run...this gives me a good idea of how expensive each part of my program is).

    Salem (or anyone else) can you give me names of win32 api functions I can use to check performance (other than GetTickCount() and timeGetTime, I mean like how can I grab the amount of memory being used at that particular instant, all I need is names then I head over to msdn).

    The intent is to save time by not initializing x if it's already zero. In reality, the test to see whether it's zero or not will take up about as much time as setting it to zero itself would have.
    x = 0;
    Stuff like that i am already aware of, that it is too expensive to test to see if the variable is initialized or not, its better to just initialize anyway.

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    I don't really recommend you getting a book for ASM. Not only are they hard to find but I didn't find them useful (not a lot of people do ASM). But feel free to prove me wrong...

    Here are a few links to get you started. I have a bunch more if interested. All here use the Intel style syntax (the other major one is AT&T syntax, yuck! what a mess!). I highly recommend you get over to Intel's web site and download their Assembly manuals and optimizations stuff. They're PDF files.

    All the instructions available in modern CPU's using the Intel-ASM style syntax:
    http://moss.csc.ncsu.edu/doc/nasm-do.../nasmdoca.html

    A simple, straight-forward table of the most commonly used instructions:
    http://www.suteki.nu/x86/

    Simple explanation of the SIMD instructions (Single Instruction, Multiple Data: MMX, SSE, SSE2):
    http://www.enel.ucalgary.ca/People/S...tion-Final.ppt

  10. #10
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    dude Salem that's pretty cool stuff! Did it take you long to write (probably not for you).

    Before I go hacking through tutorials I want to know if what i want to do is even possible or worthwhile. I have a bunch of overloaded operators from my Vector class so I can add, subtract, multiply and divide. Would it be possible and beneficial (speed wise) to take the following code:
    Code:
    void	Player::RotateVector(float & Angle, Vector & VecToRotAbout, Vector & VecToRotate) {
    	float	cosTheta = cosf(Angle);
    	float	sinTheta = sinf(Angle);	
    
    	Vector	RotatedVector; 
    	
    	RotatedVector = (VecToRotate * cosTheta) + (CrossProduct(VecToRotAbout, VecToRotate) * sinTheta) + VecToRotAbout * DotProduct(VecToRotAbout, VecToRotate) * (1 - cosTheta);
    
    	VecToRotate = RotatedVector;
    }
    and re-write it using assembly? I don't know if it is possible, or if it would be beneficial. I just wanted to ask that before I go off on a tangent for no reason.
    EDIT:
    that code gets called at least eight times per pass per spaceship...I'm wondering if it is too much (I haven't noticed any slowdown yet, but will I be affected in the future as the game progresses?)

  11. #11
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    It can be SLIGHTLY improved using assembler... even more if that CrossProduct function can be inlined. But first, there are some C++ optimizations you can do. What's your compiler? Is it VC++? Then use __fastcall, put on all optimizations, look at documentation. But yea, ASM can slightly improve it but the difference will be SOO minimal.

  12. #12
    Shadow12345
    Guest
    That's really all I needed to know. Combined with the sites you guys gave me I all set I think.

Popular pages Recent additions subscribe to a feed