Skip to content

Instantly share code, notes, and snippets.

@cjcliffe
Created June 2, 2011 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjcliffe/1005551 to your computer and use it in GitHub Desktop.
Save cjcliffe/1005551 to your computer and use it in GitHub Desktop.
Testing Mesa inverse function vs. http://jsperf.com/inverse-matrix-4x4/5
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
void gluInvertMatrix(const double m[16], double invOut[16])
{
double inv[16], det;
int i;
inv[0] = m[5]*m[10]*m[15] - m[5]*m[11]*m[14] - m[9]*m[6]*m[15]
+ m[9]*m[7]*m[14] + m[13]*m[6]*m[11] - m[13]*m[7]*m[10];
inv[4] = -m[4]*m[10]*m[15] + m[4]*m[11]*m[14] + m[8]*m[6]*m[15]
- m[8]*m[7]*m[14] - m[12]*m[6]*m[11] + m[12]*m[7]*m[10];
inv[8] = m[4]*m[9]*m[15] - m[4]*m[11]*m[13] - m[8]*m[5]*m[15]
+ m[8]*m[7]*m[13] + m[12]*m[5]*m[11] - m[12]*m[7]*m[9];
inv[12] = -m[4]*m[9]*m[14] + m[4]*m[10]*m[13] + m[8]*m[5]*m[14]
- m[8]*m[6]*m[13] - m[12]*m[5]*m[10] + m[12]*m[6]*m[9];
inv[1] = -m[1]*m[10]*m[15] + m[1]*m[11]*m[14] + m[9]*m[2]*m[15]
- m[9]*m[3]*m[14] - m[13]*m[2]*m[11] + m[13]*m[3]*m[10];
inv[5] = m[0]*m[10]*m[15] - m[0]*m[11]*m[14] - m[8]*m[2]*m[15]
+ m[8]*m[3]*m[14] + m[12]*m[2]*m[11] - m[12]*m[3]*m[10];
inv[9] = -m[0]*m[9]*m[15] + m[0]*m[11]*m[13] + m[8]*m[1]*m[15]
- m[8]*m[3]*m[13] - m[12]*m[1]*m[11] + m[12]*m[3]*m[9];
inv[13] = m[0]*m[9]*m[14] - m[0]*m[10]*m[13] - m[8]*m[1]*m[14]
+ m[8]*m[2]*m[13] + m[12]*m[1]*m[10] - m[12]*m[2]*m[9];
inv[2] = m[1]*m[6]*m[15] - m[1]*m[7]*m[14] - m[5]*m[2]*m[15]
+ m[5]*m[3]*m[14] + m[13]*m[2]*m[7] - m[13]*m[3]*m[6];
inv[6] = -m[0]*m[6]*m[15] + m[0]*m[7]*m[14] + m[4]*m[2]*m[15]
- m[4]*m[3]*m[14] - m[12]*m[2]*m[7] + m[12]*m[3]*m[6];
inv[10] = m[0]*m[5]*m[15] - m[0]*m[7]*m[13] - m[4]*m[1]*m[15]
+ m[4]*m[3]*m[13] + m[12]*m[1]*m[7] - m[12]*m[3]*m[5];
inv[14] = -m[0]*m[5]*m[14] + m[0]*m[6]*m[13] + m[4]*m[1]*m[14]
- m[4]*m[2]*m[13] - m[12]*m[1]*m[6] + m[12]*m[2]*m[5];
inv[3] = -m[1]*m[6]*m[11] + m[1]*m[7]*m[10] + m[5]*m[2]*m[11]
- m[5]*m[3]*m[10] - m[9]*m[2]*m[7] + m[9]*m[3]*m[6];
inv[7] = m[0]*m[6]*m[11] - m[0]*m[7]*m[10] - m[4]*m[2]*m[11]
+ m[4]*m[3]*m[10] + m[8]*m[2]*m[7] - m[8]*m[3]*m[6];
inv[11] = -m[0]*m[5]*m[11] + m[0]*m[7]*m[9] + m[4]*m[1]*m[11]
- m[4]*m[3]*m[9] - m[8]*m[1]*m[7] + m[8]*m[3]*m[5];
inv[15] = m[0]*m[5]*m[10] - m[0]*m[6]*m[9] - m[4]*m[1]*m[10]
+ m[4]*m[2]*m[9] + m[8]*m[1]*m[6] - m[8]*m[2]*m[5];
det = m[0]*inv[0] + m[1]*inv[4] + m[2]*inv[8] + m[3]*inv[12];
if (det == 0)
return;
det = 1.0 / det;
for (i = 0; i < 16; i++)
invOut[i] = inv[i] * det;
}
struct timeval time_val;
struct timezone time_zone;
unsigned long getMilliseconds() {
unsigned long sys_ms;
gettimeofday(&time_val,&time_zone);
sys_ms = (unsigned long)time_val.tv_usec;
sys_ms /= 1000;
sys_ms += (unsigned long)(time_val.tv_sec*1000);
return sys_ms;
}
#define NUM_CYCLES 1000000
void main(void) {
double mvMatrix[16] = {0.9284766908852594, 0, 0, 0, 0, 0.8620689655172415, 0.3713906763541037, 0, 0, -0.3448275862068966, 0.9284766908852594, 0, 0, -2.220446049250313e-16, -5.385164807134505, 1};
double *resultMatrix;
unsigned long start_time;
unsigned long end_time;
double seconds;
start_time = getMilliseconds();
unsigned long i;
for (i = 0; i < NUM_CYCLES; i++) {
resultMatrix = (double *)malloc(sizeof(double)*16); // added alloc/free in inner loop for added fairness :P
gluInvertMatrix(mvMatrix,resultMatrix);
free(resultMatrix);
}
end_time = getMilliseconds();
printf("Total Milliseconds: %lu\n",end_time-start_time);
printf("Total Seconds: %f\n",((double)(end_time-start_time))/1000.0);
printf("Cycles/Second: %f\n",((double)NUM_CYCLES)/(((double)(end_time-start_time))/1000.0));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment