Sign in to follow this  
MadMax1992

Assembly (specially SSE and MMX)

Recommended Posts

I've done some research on fast 3d calculus, and almost every site, tutorial and book advises to program several functions (functions that affect vectors and matrices) directly in assembly. I found a program that checks things your processor can do (SSE, MMX, 3DNow!), now I'm wondering how to implent them! The problem is, I've never programmed in assembly before, are there any tutorials on the internet? Or can somebody at least give me a quick workshop :-) P.S. Please don't reply with "Premature optimization is the root of all evil ;-)" Thanks in advance, Max Henger

Share this post


Link to post
Share on other sites
You can search Google for resources to learn ("assembly tutorial", "x86 assembly tutorial," "sse tutorial," et cetera), but be aware that since you don't know assembly, you will without a doubt produce assembly for your vector operations that will be much slower than those written in C++ and optimized by the compiler.

Don't plan for this to be more than a learning excursion; it will not optimize your math library.

Share this post


Link to post
Share on other sites
This is pretty essential reading if you are interested in doing assembly for Intel processors. You could always disassemble your library and see what assembler the compiler produces and see what optimisations you can make directly.

http://developer.intel.com/products/processor/manuals/index.htm

Share this post


Link to post
Share on other sites
Thanks for that link Cromulent!

And jpetrie: offcourse it's just learning... but isn't code written directly in assembly (when only the necesary code is used) always faster then code in plain C++ and optimized by the compiler. I know that people who make a compiler REALLY know what they're doing. But things like compilers can never perfectly execute something like a matrix multiplication... right?

Share this post


Link to post
Share on other sites
Quote:
Original post by MadMax1992
but isn't code written directly in assembly (when only the necesary code is used) always faster then code in plain C++ and optimized by the compiler. I know that people who make a compiler REALLY know what they're doing. But things like compilers can never perfectly execute something like a matrix multiplication... right?
No, because the compiler knows more than you do. For example, it knows all the details about which instructions should be spaced apart a little more to avoid data hazards, and it knows when x87 FPU instructions are faster than SSE instructions, and so on. There are so many little details that affect performance nowadays that it's kinda pointless trying to beat the compiler.

However, it's not great at recognising when it can operate on four sets of data simultaneously (the basis for SSE) - you need to help it, but that doesn't mean you need to use assembly. There are a set of functions for SSE called 'intrinsics' - C/C++ functions that map very closely to SSE instructions - that you can use to write your code instead. You get to use regular variables and C++ statements, and the optimizer is then able to take care of things like register allocation, the exact ordering of instructions, etc. They're also typesafe.

Take a look at the 'xmmintrin.h' header to see what's on offer, and look up functions like _mm_add_ps() in MSDN to see the docs.

Share this post


Link to post
Share on other sites
Even if assembly programming is not going to speed up your program, it's great to know what's going on. It gives you much better understanding of computers, and why languages are designed the way they are.

I found this tutorial (PDF) to be really good:
http://www.drpaulcarter.com/pcasm/index.php

Might be a bit outdated, and perhaps it's not 100% correct, and not at all about SSE and MMX, but still a very good introduction to ASM.

Share this post


Link to post
Share on other sites
Only crumby languages are designed around a specific ISA [or even with considerations of a single ISA]. Learning assembly really doesn't help you a whole lot with respect to learning how computers do their thing, since in almost every case ['almost' for when you need access to semantics utterly inexpressible in the native language]. Leave the ISA's to the compiler designers unless you need to dirty your hands with them [especially one as complicated and nasty as x86, which would be the assembly you'd be diving into in all likelihood].

Seriously, no great and holy understanding of the metal comes from learning an ISA.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this