2 * Copyright © 2012 Collabora, Ltd.
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
32 #include "../shared/matrix.h"
34 struct inverse_matrix {
35 double LU[16]; /* column-major */
36 unsigned perm[4]; /* permutation */
39 static struct timespec begin_time;
44 clock_gettime(CLOCK_MONOTONIC, &begin_time);
52 clock_gettime(CLOCK_MONOTONIC, &t);
53 return (double)(t.tv_sec - begin_time.tv_sec) +
54 1e-9 * (t.tv_nsec - begin_time.tv_nsec);
58 det3x3(const float *c0, const float *c1, const float *c2)
61 c0[0] * c1[1] * c2[2] +
62 c1[0] * c2[1] * c0[2] +
63 c2[0] * c0[1] * c1[2] -
64 c0[2] * c1[1] * c2[0] -
65 c1[2] * c2[1] * c0[0] -
66 c2[2] * c0[1] * c1[0];
70 determinant(const struct weston_matrix *m)
74 /* develop on last row */
75 det -= m->d[3 + 0 * 4] * det3x3(&m->d[4], &m->d[8], &m->d[12]);
76 det += m->d[3 + 1 * 4] * det3x3(&m->d[0], &m->d[8], &m->d[12]);
77 det -= m->d[3 + 2 * 4] * det3x3(&m->d[0], &m->d[4], &m->d[12]);
78 det += m->d[3 + 3 * 4] * det3x3(&m->d[0], &m->d[4], &m->d[8]);
80 /* develop on first row */
81 det += m->d[0 + 0 * 4] * det3x3(&m->d[5], &m->d[9], &m->d[13]);
82 det -= m->d[0 + 1 * 4] * det3x3(&m->d[1], &m->d[9], &m->d[13]);
83 det += m->d[0 + 2 * 4] * det3x3(&m->d[1], &m->d[5], &m->d[13]);
84 det -= m->d[0 + 3 * 4] * det3x3(&m->d[1], &m->d[5], &m->d[9]);
90 print_permutation_matrix(const struct inverse_matrix *m)
92 const unsigned *p = m->perm;
93 const char *row[4] = {
100 printf(" P =\n%s%s%s%s", row[p[0]], row[p[1]], row[p[2]], row[p[3]]);
104 print_LU_decomposition(const struct inverse_matrix *m)
110 for (r = 0; r < 4; ++r) {
113 for (c = 0; c < 4; ++c) {
115 v = m->LU[r + c * 4];
120 printf(" %12.6f", v);
125 for (c = 0; c < 4; ++c) {
127 v = m->LU[r + c * 4];
130 printf(" %12.6f", v);
137 print_inverse_data_matrix(const struct inverse_matrix *m)
141 for (r = 0; r < 4; ++r) {
142 for (c = 0; c < 4; ++c)
143 printf(" %12.6f", m->LU[r + c * 4]);
147 printf("permutation: ");
148 for (r = 0; r < 4; ++r)
149 printf(" %u", m->perm[r]);
154 print_matrix(const struct weston_matrix *m)
158 for (r = 0; r < 4; ++r) {
159 for (c = 0; c < 4; ++c)
160 printf(" %14.6e", m->d[r + c * 4]);
169 return r / (double)(RAND_MAX / 2) - 1.0f;
173 randomize_matrix(struct weston_matrix *m)
176 for (i = 0; i < 16; ++i)
178 m->d[i] = frand() * exp(10.0 * frand());
184 /* Take a matrix, compute inverse, multiply together
185 * and subtract the identity matrix to get the error matrix.
186 * Return the largest absolute value from the error matrix.
189 test_inverse(struct weston_matrix *m)
192 struct inverse_matrix q;
195 if (matrix_invert(q.LU, q.perm, m) != 0)
198 for (i = 0; i < 4; ++i)
199 inverse_transform(q.LU, q.perm, &m->d[i * 4]);
206 for (i = 0; i < 16; ++i) {
207 double err = fabs(m->d[i]);
217 TEST_NOT_INVERTIBLE_OK,
225 struct weston_matrix m;
228 randomize_matrix(&m);
229 det = determinant(&m);
231 errsup = test_inverse(&m);
235 if (fabs(det) < 1e-5 && isinf(errsup))
236 return TEST_NOT_INVERTIBLE_OK;
238 printf("test fail, det: %g, error sup: %g\n", det, errsup);
251 test_loop_precision(void)
253 int counts[TEST_COUNT] = { 0 };
255 printf("\nRunning a test loop for 10 seconds...\n");
262 printf("tests: %d ok, %d not invertible but ok, %d failed.\n"
263 "Total: %d iterations.\n",
264 counts[TEST_OK], counts[TEST_NOT_INVERTIBLE_OK],
266 counts[TEST_OK] + counts[TEST_NOT_INVERTIBLE_OK] +
270 static void __attribute__((noinline))
271 test_loop_speed_matrixvector(void)
273 struct weston_matrix m;
274 struct weston_vector v = { { 0.5, 0.5, 0.5, 1.0 } };
275 unsigned long count = 0;
278 printf("\nRunning 3 s test on weston_matrix_transform()...\n");
280 weston_matrix_init(&m);
286 weston_matrix_transform(&m, &v);
291 printf("%lu iterations in %f seconds, avg. %.1f us/iter.\n",
292 count, t, 1e9 * t / count);
295 static void __attribute__((noinline))
296 test_loop_speed_inversetransform(void)
298 struct weston_matrix m;
299 struct inverse_matrix inv;
300 struct weston_vector v = { { 0.5, 0.5, 0.5, 1.0 } };
301 unsigned long count = 0;
304 printf("\nRunning 3 s test on inverse_transform()...\n");
306 weston_matrix_init(&m);
307 matrix_invert(inv.LU, inv.perm, &m);
313 inverse_transform(inv.LU, inv.perm, v.f);
318 printf("%lu iterations in %f seconds, avg. %.1f us/iter.\n",
319 count, t, 1e9 * t / count);
322 static void __attribute__((noinline))
323 test_loop_speed_invert(void)
325 struct weston_matrix m;
326 struct inverse_matrix inv;
327 unsigned long count = 0;
330 printf("\nRunning 3 s test on matrix_invert()...\n");
332 weston_matrix_init(&m);
338 matrix_invert(inv.LU, inv.perm, &m);
343 printf("%lu iterations in %f seconds, avg. %.1f ns/iter.\n",
344 count, t, 1e9 * t / count);
347 static void __attribute__((noinline))
348 test_loop_speed_invert_explicit(void)
350 struct weston_matrix m;
351 unsigned long count = 0;
354 printf("\nRunning 3 s test on weston_matrix_invert()...\n");
356 weston_matrix_init(&m);
362 weston_matrix_invert(&m, &m);
367 printf("%lu iterations in %f seconds, avg. %.1f ns/iter.\n",
368 count, t, 1e9 * t / count);
373 struct sigaction ding;
374 struct weston_matrix M;
375 struct inverse_matrix Q;
380 ding.sa_handler = stopme;
381 sigemptyset(&ding.sa_mask);
383 sigaction(SIGALRM, &ding, NULL);
387 M.d[0] = 3.0; M.d[4] = 17.0; M.d[8] = 10.0; M.d[12] = 0.0;
388 M.d[1] = 2.0; M.d[5] = 4.0; M.d[9] = -2.0; M.d[13] = 0.0;
389 M.d[2] = 6.0; M.d[6] = 18.0; M.d[10] = -12; M.d[14] = 0.0;
390 M.d[3] = 0.0; M.d[7] = 0.0; M.d[11] = 0.0; M.d[15] = 1.0;
392 ret = matrix_invert(Q.LU, Q.perm, &M);
393 printf("ret = %d\n", ret);
394 printf("det = %g\n\n", determinant(&M));
399 print_inverse_data_matrix(&Q);
400 printf("P * A = L * U\n");
401 print_permutation_matrix(&Q);
402 print_LU_decomposition(&Q);
405 printf("a random matrix:\n");
406 randomize_matrix(&M);
407 det = determinant(&M);
409 errsup = test_inverse(&M);
410 printf("\nThe matrix multiplied by its inverse, error:\n");
412 printf("max abs error: %g, original determinant %g\n", errsup, det);
414 test_loop_precision();
415 test_loop_speed_matrixvector();
416 test_loop_speed_inversetransform();
417 test_loop_speed_invert();
418 test_loop_speed_invert_explicit();