Sign in to follow this  

D3DXMatrixMultiply implimentation?

This topic is 4031 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does anyone know how D3DXMatrixMultiply is implimented? I can't find any information on what alrogrithm it uses, but it is definitely faster than just a naive matrix multiplication in C++. Could it even be implimented in assembly?

Share this post


Link to post
Share on other sites
I'm fairly certain that the matrix multiplication, along with a number of other functions, is written to use SSE when available. You're not likely to beat it any time soon, as it's tuned by guys who know processors like whoa.

Share this post


Link to post
Share on other sites
Wow, you were right, that took a long time, but I did some heavy research on the SSE and 3DNow! SIMD instruction sets and I've finally got an implimentation that equals D3DMatrixMultiply's performance. I'm developing on an AMD processor so this is in 3DNow! MMX assembly. I don't know if it would work on an Intel or not. DX looks up your processor internally and probably chooses an SSE or 3DNow! implimentation depending on what your processor supports. I will probably do something similar if I can figure out how to test it on an Intel (though supposedly AMD processors now support SSE instructions, but I'm not sure if there is a large performance difference compared to 3DNow! on these machines).

Anyway, for anyone who wishes to use this in a math library, feel free.



enum MPARTS { MP_11=0, MP_12=4, MP_13=8, MP_14=12,
MP_21=16, MP_22=20, MP_23=24, MP_24=28,
MP_31=32, MP_32=36, MP_33=40, MP_34=44,
MP_41=48, MP_42=52, MP_43=56, MP_44=60 };

enum MROWS { MR_1=0, MR_2=16, MR_3=32, MR_4=48 };


inline Matrix& Matrix::operator *=(const Matrix& rhs)
{
__asm
{
FEMMS

mov edi, this
mov esi, rhs
MOVQ MM1, [esi+MR_1]
MOVQ MM2, [esi+MR_1+8]
MOVQ MM3, [esi+MR_2]


PSHUFW MM6, [edi+MP_11], 0x44
MOVQ MM7, MM6
PFMUL MM6, MM1
PFMUL MM7, MM2

PSHUFW MM4, [edi+MP_12], 0x44
MOVQ MM5, MM4
PFMUL MM4, MM3
MOVQ MM0, [esi+MR_2+8]
PFMUL MM5, MM0
PFADD MM6, MM4
PFADD MM7, MM5

PSHUFW MM4, [edi+MP_13], 0x44
MOVQ MM5, MM4
MOVQ MM0, [esi+MR_3]
PFMUL MM4, MM0
MOVQ MM0, [esi+MR_3+8]
PFMUL MM5, MM2
PFADD MM6, MM4
PFADD MM7, MM5

PSHUFW MM4, [edi+MP_14], 0x44
MOVQ MM5, MM4
MOVQ MM0, [esi+MR_4]
PFMUL MM4, MM0
MOVQ MM0, [esi+MR_4+8]
PFMUL MM5, MM2
PFADD MM6, MM4
PFADD MM7, MM5

MOVQ [edi+MR_1], MM6
MOVQ [edi+MR_1+8], MM7


PSHUFW MM6, [edi+MP_21], 0x44
MOVQ MM7, MM6
PFMUL MM6, MM1
PFMUL MM7, MM2

PSHUFW MM4, [edi+MP_22], 0x44
MOVQ MM5, MM4
PFMUL MM4, MM3
MOVQ MM0, [esi+MR_2+8]
PFMUL MM5, MM0
PFADD MM6, MM4
PFADD MM7, MM5

PSHUFW MM4, [edi+MP_23], 0x44
MOVQ MM5, MM4
MOVQ MM0, [esi+MR_3]
PFMUL MM4, MM0
MOVQ MM0, [esi+MR_3+8]
PFMUL MM5, MM2
PFADD MM6, MM4
PFADD MM7, MM5

PSHUFW MM4, [edi+MP_24], 0x44
MOVQ MM5, MM4
MOVQ MM0, [esi+MR_4]
PFMUL MM4, MM0
MOVQ MM0, [esi+MR_4+8]
PFMUL MM5, MM2
PFADD MM6, MM4
PFADD MM7, MM5

MOVQ [edi+MR_2], MM6
MOVQ [edi+MR_2+8], MM7


PSHUFW MM6, [edi+MP_31], 0x44
MOVQ MM7, MM6
PFMUL MM6, MM1
PFMUL MM7, MM2

PSHUFW MM4, [edi+MP_32], 0x44
MOVQ MM5, MM4
PFMUL MM4, MM3
MOVQ MM0, [esi+MR_2+8]
PFMUL MM5, MM0
PFADD MM6, MM4
PFADD MM7, MM5

PSHUFW MM4, [edi+MP_33], 0x44
MOVQ MM5, MM4
MOVQ MM0, [esi+MR_3]
PFMUL MM4, MM0
MOVQ MM0, [esi+MR_3+8]
PFMUL MM5, MM2
PFADD MM6, MM4
PFADD MM7, MM5

PSHUFW MM4, [edi+MP_34], 0x44
MOVQ MM5, MM4
MOVQ MM0, [esi+MR_4]
PFMUL MM4, MM0
MOVQ MM0, [esi+MR_4+8]
PFMUL MM5, MM2
PFADD MM6, MM4
PFADD MM7, MM5

MOVQ [edi+MR_3], MM6
MOVQ [edi+MR_3+8], MM7


PSHUFW MM6, [edi+MP_41], 0x44
MOVQ MM7, MM6
PFMUL MM6, MM1
PFMUL MM7, MM2

PSHUFW MM4, [edi+MP_42], 0x44
MOVQ MM5, MM4
PFMUL MM4, MM3
MOVQ MM0, [esi+MR_2+8]
PFMUL MM5, MM0
PFADD MM6, MM4
PFADD MM7, MM5

PSHUFW MM4, [edi+MP_43], 0x44
MOVQ MM5, MM4
MOVQ MM0, [esi+MR_3]
PFMUL MM4, MM0
MOVQ MM0, [esi+MR_3+8]
PFMUL MM5, MM2
PFADD MM6, MM4
PFADD MM7, MM5

PSHUFW MM4, [edi+MP_44], 0x44
MOVQ MM5, MM4
MOVQ MM0, [esi+MR_4]
PFMUL MM4, MM0
MOVQ MM0, [esi+MR_4+8]
PFMUL MM5, MM2
PFADD MM6, MM4
PFADD MM7, MM5

MOVQ [edi+MR_4], MM6
MOVQ [edi+MR_4+8], MM7

FEMMS
}
return *this;
}



Matrix's data here is just an array of 16 floats. This is the *= version, a generic multimatrix(m1, m2, out) would be simply to make from this.

Please post any improvements and optimizations, I'm only a beginnner in assembly. If anyone wants me to post the complete matrix class to test the code let me know. I tested this against D3DXMatrixMultiply and even doing 10000 multiplications per frame they performed extremely close (sometimes over, sometimes under, averaging nearly identical).


Share this post


Link to post
Share on other sites
Only thing is, this won't compile on a x64 platform. Microsoft pulled asm support for x64 platforms. This has forced me to not use asm, if I want to build a x64 application.

Edit: Not to sound like a real downer[smile].

[Edited by - exorcist_bob on January 3, 2007 8:19:44 PM]

Share this post


Link to post
Share on other sites
1. Unless you have good reason to (e.g. for self-education on how to implement such a thing, or for use on Linux PCs), for single operations like matrix multiplication and inversion I'd still advise using the D3DX maths functions: they're well tested, extremely optimal for a general purpose library, somebody else fixes the bugs, less memory is used for code if any other running application has the same D3DX DLL loaded.


2. To use Gondolin's code on x64, you'll either have to use a standalone assembler and put a function call to the ASM function inside the inline C++ or use intrinsics.

Incidentally, when you want to mix ASM and C/C++ in the same function, intrinsics can actually provide better performance because you leave the choice of register assignment up to the compiler, so it can do a better job of assigning C/C++ variables to registers that your low-level code uses.


3. writing, and hand-tuning ASM can be a fun activity, and it's often useful to understand ASM for debugging purposes, but do remember that the CPU manufacturers are more 'l33t' at ASM than you are, so it's always worth checking whether they've already done the hard work for you, e.g.:
http://www.intel.com/design/PentiumIII/sml/

http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/22007.pdf (See chapter 10, the principles behind the Matrix-Vector multiply can be expanded to a Matrix-Matrix multiply. Also check out AMD's ACML library).

Share this post


Link to post
Share on other sites
Quote:
Unless you have good reason to (e.g. for self-education on how to implement such a thing


Yep, this is more of a self-education implimentation. You're right, its a lot of fun tuning in ASM and when you're done you have a much better understanding of the general algorithm along with how to optimize it.

My original idea was that a game server would not have to load any DirectX DLLs...as from a design standpoint every component of DirectX is aimed for client presentation, not CPU calculations such as physics and scene management that the server does. Unfortunately including the D3DX math header seems to load the whole D3D dll which seems a waste. There are lots of math libraries out there but I was attempting to write my own that essentially impliments DX math. Even though its a lot of work and probably pointless, I'm finding that there are a few things that DX doesn't fully optimize, so its interesting to see what works better and what doesn't, even if I don't end up using this.

Share this post


Link to post
Share on other sites
Sign in to follow this