forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Shift operator in Matrix Function Question

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
BlackDragon777



Joined: 15 Sep 2005
Posts: 32

PostPosted: Sat Sep 24, 2005 1:40 am    Post subject: Shift operator in Matrix Function Question Reply with quote

Hmmm I read the post that shine sent me about rotating triangles and their properties. I have a question though.

Why does the author of the code choose to use a shift operation for the indicies of his array of numbers in his matrix?

Here is an example of his code

Code:


void matrix_identity(float* matrix)
{
   matrix[(0<<2)+0] = 1.0f;
   matrix[(0<<2)+1] = 0.0f;
   matrix[(0<<2)+2] = 0.0f;
   matrix[(0<<2)+3] = 0.0f;

   matrix[(1<<2)+0] = 0.0f;
   matrix[(1<<2)+1] = 1.0f;
   matrix[(1<<2)+2] = 0.0f;
   matrix[(1<<2)+3] = 0.0f;

   matrix[(2<<2)+0] = 0.0f;
   matrix[(2<<2)+1] = 0.0f;
   matrix[(2<<2)+2] = 1.0f;
   matrix[(2<<2)+3] = 0.0f;

   matrix[(3<<2)+0] = 0.0f;
   matrix[(3<<2)+1] = 0.0f;
   matrix[(3<<2)+2] = 0.0f;
   matrix[(3<<2)+3] = 1.0f;
}



I see that this is a 4x4 matrix of verticies. I also understand what he his doing. For example,

Code:

matrix[(3<<2)+3]


That would come out to being the index of 15, the value in his matrix. My question is isn't it easier to just index like so,

Code:

matrix[15]


Is there any added bonus or benefit by indexing with shift operations?

Please enlighten me.
Back to top
View user's profile Send private message
ooPo
Site Admin


Joined: 17 Jan 2004
Posts: 2032
Location: Canada

PostPosted: Sat Sep 24, 2005 2:23 am    Post subject: Reply with quote

My guess is ease of reading?

Code:
matrix[(2<<2)+0] = 0.0f;

A quick glance lets you know you're modifying (2,0).

The compiler should also optimize it to a static number when it gets compiled so performance isn't a concern.
Back to top
View user's profile Send private message Visit poster's website
BlackDragon777



Joined: 15 Sep 2005
Posts: 32

PostPosted: Sat Sep 24, 2005 4:03 am    Post subject: Reply with quote

Oh! Ok I see it now! Yeah I guess that would help a lot with readability. Thanks ooPo
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group