Archived

This topic is now archived and is closed to further replies.

kamrann

Unrolling loops with templates?

Recommended Posts

I''m trying to write a general matrix class template, and I''m sure I''ve heard about a way to unroll loops using templates. Can anyone tell me how this is done? Also, I think it''s related, if my matrix template is going to be declared as follows: template class CMatrix { ... }; is there some way I can declare a variable number of member variables, based on ROWS and COLS, ie. for a 4x4 matrix class I''d want 16 members of type fp_type? I know I could just declare an array: fp_type elements[ROWS][COLS]; but then I got confused as to how to declare a constructor for the class that could take a list of fp_type arguments to initialize the elements. Can anyone suggest anything here? thanks Cameron

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Don''t bother unrolling loops. An optimizing compiler will do this for you.

Pass in the dimensions of the matrix in your constructor, and use that to make the 2D array. When you pass in the stuff, just pass in a 1d array of fp_types, and take it in as a pointer in your function declaration. Then copy from the pointer into your 2d array.

Share this post


Link to post
Share on other sites
I don''t recall exactly how to unrool loops using templates, I beleive it''s called template metaprogramming if you want to search around for it.

A general matrix class and an optimized 4x4 matrix class are diametrically opposed goals. Make two classes.

  
template<typename cRow, typename cCol, typename t=float>
class TMatrix
{
public:
TMatric() : m_cRows(cRow), m_cCols(cCol)
{
//This will zero out the matrix

// it works because m_tMatrix is actually a constant size

// array.

memset(m_Matrix, 0, sizeof(t)*m_cRows*m_cCols);
}
//generalized operators & methods

t& operator()(int i, int j)
{
return m_Matrix[i][j];
}
protected:
t m_Matrix[cRow][cCol];
const int m_cRows;
const int m_cCols;
};


Share this post


Link to post
Share on other sites
quote:

is there some way I can declare a variable number of member variables, based on ROWS and COLS, ie. for a 4x4 matrix class I'd want 16 members of type fp_type? I know I could just declare an array:

fp_type elements[ROWS][COLS];



That should work fine.
quote:

but then I got confused as to how to declare a constructor for the class that could take a list of fp_type arguments to initialize the elements.


You could declare it as follows:
    
template<int ROWS, int COLS, typename fp_type = float>
class CMatrix
{
// one alternative

CMatrix(fp_type m[ROWS][COLS])
{
for (int i = 0; i < ROWS; ++i)
for (int j = 0; j < COLS; ++j)
elements[i][j] = m[i][j];
// or simply:

// memcpy(elements, m, sizeof(fp_type)*ROWS*COLS);

};

// another (less typesafe) alternative

CMatrix(fp_type * m)
{
memcpy(elements, m, sizeof(fp_type)*ROWS*COLS);
};

protected:
fp_type elements[ROWS][COLS];
};


Edited by - Dactylos on October 19, 2001 7:23:29 AM

Share this post


Link to post
Share on other sites