Jump to content

  • Log In with Google      Sign In   
  • Create Account


D3DXMatrixMultiply implimentation?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Gondolin   Members   -  Reputation: 143

Like
0Likes
Like

Posted 01 January 2007 - 05:46 PM

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?

Sponsor:

#2 Promit   Moderators   -  Reputation: 4700

Like
0Likes
Like

Posted 01 January 2007 - 06:04 PM

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.

#3 Gondolin   Members   -  Reputation: 143

Like
1Likes
Like

Posted 02 January 2007 - 08:24 PM

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).




#4 renman29   Members   -  Reputation: 189

Like
1Likes
Like

Posted 02 January 2007 - 09:15 PM

Most impressive. :)

#5 exorcist_bob   Members   -  Reputation: 187

Like
0Likes
Like

Posted 03 January 2007 - 04:19 AM

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]

#6 S1CA   Members   -  Reputation: 1394

Like
0Likes
Like

Posted 03 January 2007 - 12:27 PM

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).

#7 Gondolin   Members   -  Reputation: 143

Like
0Likes
Like

Posted 03 January 2007 - 05:40 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS