2 * Copyright © 2012 Collabora, Ltd.
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 #include "shared/matrix.h"
37 struct inverse_matrix {
38 double LU[16]; /* column-major */
39 unsigned perm[4]; /* permutation */
42 static struct timespec begin_time;
47 clock_gettime(CLOCK_MONOTONIC, &begin_time);
55 clock_gettime(CLOCK_MONOTONIC, &t);
56 return (double)(t.tv_sec - begin_time.tv_sec) +
57 1e-9 * (t.tv_nsec - begin_time.tv_nsec);
61 det3x3(const float *c0, const float *c1, const float *c2)
64 c0[0] * c1[1] * c2[2] +
65 c1[0] * c2[1] * c0[2] +
66 c2[0] * c0[1] * c1[2] -
67 c0[2] * c1[1] * c2[0] -
68 c1[2] * c2[1] * c0[0] -
69 c2[2] * c0[1] * c1[0];
73 determinant(const struct weston_matrix *m)
77 /* develop on last row */
78 det -= m->d[3 + 0 * 4] * det3x3(&m->d[4], &m->d[8], &m->d[12]);
79 det += m->d[3 + 1 * 4] * det3x3(&m->d[0], &m->d[8], &m->d[12]);
80 det -= m->d[3 + 2 * 4] * det3x3(&m->d[0], &m->d[4], &m->d[12]);
81 det += m->d[3 + 3 * 4] * det3x3(&m->d[0], &m->d[4], &m->d[8]);
83 /* develop on first row */
84 det += m->d[0 + 0 * 4] * det3x3(&m->d[5], &m->d[9], &m->d[13]);
85 det -= m->d[0 + 1 * 4] * det3x3(&m->d[1], &m->d[9], &m->d[13]);
86 det += m->d[0 + 2 * 4] * det3x3(&m->d[1], &m->d[5], &m->d[13]);
87 det -= m->d[0 + 3 * 4] * det3x3(&m->d[1], &m->d[5], &m->d[9]);
93 print_permutation_matrix(const struct inverse_matrix *m)
95 const unsigned *p = m->perm;
96 const char *row[4] = {
103 printf(" P =\n%s%s%s%s", row[p[0]], row[p[1]], row[p[2]], row[p[3]]);
107 print_LU_decomposition(const struct inverse_matrix *m)
113 for (r = 0; r < 4; ++r) {
116 for (c = 0; c < 4; ++c) {
118 v = m->LU[r + c * 4];
123 printf(" %12.6f", v);
128 for (c = 0; c < 4; ++c) {
130 v = m->LU[r + c * 4];
133 printf(" %12.6f", v);
140 print_inverse_data_matrix(const struct inverse_matrix *m)
144 for (r = 0; r < 4; ++r) {
145 for (c = 0; c < 4; ++c)
146 printf(" %12.6f", m->LU[r + c * 4]);
150 printf("permutation: ");
151 for (r = 0; r < 4; ++r)
152 printf(" %u", m->perm[r]);
157 print_matrix(const struct weston_matrix *m)
161 for (r = 0; r < 4; ++r) {
162 for (c = 0; c < 4; ++c)
163 printf(" %14.6e", m->d[r + c * 4]);
172 return r / (double)(RAND_MAX / 2) - 1.0f;
176 randomize_matrix(struct weston_matrix *m)
179 for (i = 0; i < 16; ++i)
181 m->d[i] = frand() * exp(10.0 * frand());
187 /* Take a matrix, compute inverse, multiply together
188 * and subtract the identity matrix to get the error matrix.
189 * Return the largest absolute value from the error matrix.
192 test_inverse(struct weston_matrix *m)
195 struct inverse_matrix q;
198 if (matrix_invert(q.LU, q.perm, m) != 0)
201 for (i = 0; i < 4; ++i)
202 inverse_transform(q.LU, q.perm, &m->d[i * 4]);
209 for (i = 0; i < 16; ++i) {
210 double err = fabs(m->d[i]);
220 TEST_NOT_INVERTIBLE_OK,
228 struct weston_matrix m;
231 randomize_matrix(&m);
232 det = determinant(&m);
234 errsup = test_inverse(&m);
238 if (fabs(det) < 1e-5 && isinf(errsup))
239 return TEST_NOT_INVERTIBLE_OK;
241 printf("test fail, det: %g, error sup: %g\n", det, errsup);
254 test_loop_precision(void)
256 int counts[TEST_COUNT] = { 0 };
258 printf("\nRunning a test loop for 10 seconds...\n");
265 printf("tests: %d ok, %d not invertible but ok, %d failed.\n"
266 "Total: %d iterations.\n",
267 counts[TEST_OK], counts[TEST_NOT_INVERTIBLE_OK],
269 counts[TEST_OK] + counts[TEST_NOT_INVERTIBLE_OK] +
273 static void __attribute__((noinline))
274 test_loop_speed_matrixvector(void)
276 struct weston_matrix m;
277 struct weston_vector v = { { 0.5, 0.5, 0.5, 1.0 } };
278 unsigned long count = 0;
281 printf("\nRunning 3 s test on weston_matrix_transform()...\n");
283 weston_matrix_init(&m);
289 weston_matrix_transform(&m, &v);
294 printf("%lu iterations in %f seconds, avg. %.1f ns/iter.\n",
295 count, t, 1e9 * t / count);
298 static void __attribute__((noinline))
299 test_loop_speed_inversetransform(void)
301 struct weston_matrix m;
302 struct inverse_matrix inv;
303 struct weston_vector v = { { 0.5, 0.5, 0.5, 1.0 } };
304 unsigned long count = 0;
307 printf("\nRunning 3 s test on inverse_transform()...\n");
309 weston_matrix_init(&m);
310 matrix_invert(inv.LU, inv.perm, &m);
316 inverse_transform(inv.LU, inv.perm, v.f);
321 printf("%lu iterations in %f seconds, avg. %.1f ns/iter.\n",
322 count, t, 1e9 * t / count);
325 static void __attribute__((noinline))
326 test_loop_speed_invert(void)
328 struct weston_matrix m;
329 struct inverse_matrix inv;
330 unsigned long count = 0;
333 printf("\nRunning 3 s test on matrix_invert()...\n");
335 weston_matrix_init(&m);
341 matrix_invert(inv.LU, inv.perm, &m);
346 printf("%lu iterations in %f seconds, avg. %.1f ns/iter.\n",
347 count, t, 1e9 * t / count);
350 static void __attribute__((noinline))
351 test_loop_speed_invert_explicit(void)
353 struct weston_matrix m;
354 unsigned long count = 0;
357 printf("\nRunning 3 s test on weston_matrix_invert()...\n");
359 weston_matrix_init(&m);
365 weston_matrix_invert(&m, &m);
370 printf("%lu iterations in %f seconds, avg. %.1f ns/iter.\n",
371 count, t, 1e9 * t / count);
376 struct sigaction ding;
377 struct weston_matrix M;
378 struct inverse_matrix Q;
383 ding.sa_handler = stopme;
384 sigemptyset(&ding.sa_mask);
386 sigaction(SIGALRM, &ding, NULL);
390 M.d[0] = 3.0; M.d[4] = 17.0; M.d[8] = 10.0; M.d[12] = 0.0;
391 M.d[1] = 2.0; M.d[5] = 4.0; M.d[9] = -2.0; M.d[13] = 0.0;
392 M.d[2] = 6.0; M.d[6] = 18.0; M.d[10] = -12; M.d[14] = 0.0;
393 M.d[3] = 0.0; M.d[7] = 0.0; M.d[11] = 0.0; M.d[15] = 1.0;
395 ret = matrix_invert(Q.LU, Q.perm, &M);
396 printf("ret = %d\n", ret);
397 printf("det = %g\n\n", determinant(&M));
402 print_inverse_data_matrix(&Q);
403 printf("P * A = L * U\n");
404 print_permutation_matrix(&Q);
405 print_LU_decomposition(&Q);
408 printf("a random matrix:\n");
409 randomize_matrix(&M);
410 det = determinant(&M);
412 errsup = test_inverse(&M);
413 printf("\nThe matrix multiplied by its inverse, error:\n");
415 printf("max abs error: %g, original determinant %g\n", errsup, det);
417 test_loop_precision();
418 test_loop_speed_matrixvector();
419 test_loop_speed_inversetransform();
420 test_loop_speed_invert();
421 test_loop_speed_invert_explicit();