It's free to join Gamasutra!|Have a question? Want to know who runs this site? Here you go.|Targeting the game development market with your product or service? Get info on advertising here.||For altering your contact information or changing email subscription preferences.
Registered members can log in here.Back to the home page.

Search articles, jobs, buyers guide, and more.

by Haim Barad
Gamasutra
January 31, 2000

Printer Friendly Version

Letters to the Editor:
Write a letter
View all letters


Features

 

Contents

Introduction

The Library

Examples

The Library

Using the Streaming SIMD Extensions, you can complete a matrix multiplication with only 16 products and 12 additions. The library provided in this article was written with the goal to get the most out of the Streaming SIMD Extensions, and to reduce the amount of time needed for matrix/vector operations. Most of the library is written in C++, except for one small section in assembly. The library’s functions are about twice as fast as the equivalent scalar version of those functions.The library includes three classes:

GPMatrix class

The GPMatrix class is a 4x4 matrix of floats.

 
class GPMatrix {
    union {
       struct {
           __m128 _L1, _L2, _L3, _L4;
       };
       struct {
           float _11, _12, _13, _14;
           float _21, _22, _23, _24;
           float _31, _32, _33, _34;
           float _41, _42, _43, _44;
       };
    };
    // ...
};
Figure. 1 The GPMatrix Class

  The class contains 16 float elements (_11 to _44). The elements are placed in four lines, where each line is represented as one SIMD variable (_L1 to _L1).
Data elements can be referenced by their row and column: Mat[0][1] or Mat(0,1).

There is one limitation for the use of this class:
The class must be 16-byte aligned, so it won’t split between too many cache lines, and to enable faster reading/writing of the class’ elements. However, the class is aligned automatically by the Intel C++ compiler.

The GPVector class

The GPVector class is a vector of 4 floats.

 
class GPVector {
    union {
        __m128 vec;
        struct {
            float x,y,z,w;
        };
    };
    // ...
};
Figure 2. The GPVector Class

  The GPVector class has the x,y,z and w elements of the vector as floats, and also represented as one SIMD variable. As with the GPMatrix class, the GPVector class must be 16-bytes aligned.

A variant of the GPVector class is the GPVector3 class, which does not have the w element. This class holds "pure" 3D vectors. However, for alignment and for other reasons, the w element, which is not used, is replaced with a spacer.

Constructors & Operators

Operators on GPMatrix:

A * B matrices multiplication
A ± B matrices addition/subtraction
±A matrix unary minus/plus
A * s  matrix multiplication with scalar
A *= B matrix multiplied by matrix
A *= s matrix multiplied by scalar
A ±= B matrix added/subtracted by matrix
  matrix transpose
  matrix inverse
  matrix determinant
  matrix min/max element
GPMatrix Constructors:
  Identity matrix
  Zero matrix
  Rotation matrices (around the X axis, Y axis and Z axis)
  Translation matrices
  Scaling matrices
Operators on GPVector:
v * M vector multiplication with matrix
v * s vector multiplication with scalar
v * w vectors dot (inner) product
v % w vectors cross product (in 3D)
v ± w vectors addition/subtraction
± v vector unary minus/plus
~ v vector normalization
v *= M vector multiplied by matrix
v *= s vector multiplied by scalar
v ±= w vector added/subtracted by vector

________________________________________________________

Examples


join | contact us | advertise | write | my profile
news | features | companies | jobs | resumes | education | product guide | projects | store



Copyright © 2003 CMP Media LLC

privacy policy
| terms of service