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 struct inverse_matrix {
33 double LU[16]; /* column-major */
34 unsigned perm[4]; /* permutation */
37 static struct timespec begin_time;
42 clock_gettime(CLOCK_MONOTONIC, &begin_time);
50 clock_gettime(CLOCK_MONOTONIC, &t);
51 return (double)(t.tv_sec - begin_time.tv_sec) +
52 1e-9 * (t.tv_nsec - begin_time.tv_nsec);
56 det3x3(const float *c0, const float *c1, const float *c2)
59 c0[0] * c1[1] * c2[2] +
60 c1[0] * c2[1] * c0[2] +
61 c2[0] * c0[1] * c1[2] -
62 c0[2] * c1[1] * c2[0] -
63 c1[2] * c2[1] * c0[0] -
64 c2[2] * c0[1] * c1[0];
68 determinant(const struct weston_matrix *m)
72 /* develop on last row */
73 det -= m->d[3 + 0 * 4] * det3x3(&m->d[4], &m->d[8], &m->d[12]);
74 det += m->d[3 + 1 * 4] * det3x3(&m->d[0], &m->d[8], &m->d[12]);
75 det -= m->d[3 + 2 * 4] * det3x3(&m->d[0], &m->d[4], &m->d[12]);
76 det += m->d[3 + 3 * 4] * det3x3(&m->d[0], &m->d[4], &m->d[8]);
78 /* develop on first row */
79 det += m->d[0 + 0 * 4] * det3x3(&m->d[5], &m->d[9], &m->d[13]);
80 det -= m->d[0 + 1 * 4] * det3x3(&m->d[1], &m->d[9], &m->d[13]);
81 det += m->d[0 + 2 * 4] * det3x3(&m->d[1], &m->d[5], &m->d[13]);
82 det -= m->d[0 + 3 * 4] * det3x3(&m->d[1], &m->d[5], &m->d[9]);
88 print_permutation_matrix(const struct inverse_matrix *m)
90 const unsigned *p = m->perm;
91 const char *row[4] = {
98 printf(" P =\n%s%s%s%s", row[p[0]], row[p[1]], row[p[2]], row[p[3]]);
102 print_LU_decomposition(const struct inverse_matrix *m)
108 for (r = 0; r < 4; ++r) {
111 for (c = 0; c < 4; ++c) {
113 v = m->LU[r + c * 4];
118 printf(" %12.6f", v);
123 for (c = 0; c < 4; ++c) {
125 v = m->LU[r + c * 4];
128 printf(" %12.6f", v);
135 print_inverse_data_matrix(const struct inverse_matrix *m)
139 for (r = 0; r < 4; ++r) {
140 for (c = 0; c < 4; ++c)
141 printf(" %12.6f", m->LU[r + c * 4]);
145 printf("permutation: ");
146 for (r = 0; r < 4; ++r)
147 printf(" %u", m->perm[r]);
152 print_matrix(const struct weston_matrix *m)
156 for (r = 0; r < 4; ++r) {
157 for (c = 0; c < 4; ++c)
158 printf(" %14.6e", m->d[r + c * 4]);
167 return r / (double)(RAND_MAX / 2) - 1.0f;
171 randomize_matrix(struct weston_matrix *m)
174 for (i = 0; i < 16; ++i)
176 m->d[i] = frand() * exp(10.0 * frand());
182 /* Take a matrix, compute inverse, multiply together
183 * and subtract the identity matrix to get the error matrix.
184 * Return the largest absolute value from the error matrix.
187 test_inverse(struct weston_matrix *m)
190 struct inverse_matrix q;
193 if (matrix_invert(q.LU, q.perm, m) != 0)
196 for (i = 0; i < 4; ++i)
197 inverse_transform(q.LU, q.perm, &m->d[i * 4]);
204 for (i = 0; i < 16; ++i) {
205 double err = fabs(m->d[i]);
215 TEST_NOT_INVERTIBLE_OK,
223 struct weston_matrix m;
226 randomize_matrix(&m);
227 det = determinant(&m);
229 errsup = test_inverse(&m);
233 if (fabs(det) < 1e-5 && isinf(errsup))
234 return TEST_NOT_INVERTIBLE_OK;
236 printf("test fail, det: %g, error sup: %g\n", det, errsup);
249 test_loop_precision(void)
251 int counts[TEST_COUNT] = { 0 };
253 printf("\nRunning a test loop for 10 seconds...\n");
260 printf("tests: %d ok, %d not invertible but ok, %d failed.\n"
261 "Total: %d iterations.\n",
262 counts[TEST_OK], counts[TEST_NOT_INVERTIBLE_OK],
264 counts[TEST_OK] + counts[TEST_NOT_INVERTIBLE_OK] +
268 static void __attribute__((noinline))
269 test_loop_speed_matrixvector(void)
271 struct weston_matrix m;
272 struct weston_vector v = { { 0.5, 0.5, 0.5, 1.0 } };
273 unsigned long count = 0;
276 printf("\nRunning 3 s test on weston_matrix_transform()...\n");
278 weston_matrix_init(&m);
284 weston_matrix_transform(&m, &v);
289 printf("%lu iterations in %f seconds, avg. %.1f us/iter.\n",
290 count, t, 1e9 * t / count);
293 static void __attribute__((noinline))
294 test_loop_speed_inversetransform(void)
296 struct weston_matrix m;
297 struct inverse_matrix inv;
298 struct weston_vector v = { { 0.5, 0.5, 0.5, 1.0 } };
299 unsigned long count = 0;
302 printf("\nRunning 3 s test on inverse_transform()...\n");
304 weston_matrix_init(&m);
305 matrix_invert(inv.LU, inv.perm, &m);
311 inverse_transform(inv.LU, inv.perm, v.f);
316 printf("%lu iterations in %f seconds, avg. %.1f us/iter.\n",
317 count, t, 1e9 * t / count);
320 static void __attribute__((noinline))
321 test_loop_speed_invert(void)
323 struct weston_matrix m;
324 struct inverse_matrix inv;
325 unsigned long count = 0;
328 printf("\nRunning 3 s test on matrix_invert()...\n");
330 weston_matrix_init(&m);
336 matrix_invert(inv.LU, inv.perm, &m);
341 printf("%lu iterations in %f seconds, avg. %.1f ns/iter.\n",
342 count, t, 1e9 * t / count);
345 static void __attribute__((noinline))
346 test_loop_speed_invert_explicit(void)
348 struct weston_matrix m;
349 unsigned long count = 0;
352 printf("\nRunning 3 s test on weston_matrix_invert()...\n");
354 weston_matrix_init(&m);
360 weston_matrix_invert(&m, &m);
365 printf("%lu iterations in %f seconds, avg. %.1f ns/iter.\n",
366 count, t, 1e9 * t / count);
371 struct sigaction ding;
372 struct weston_matrix M;
373 struct inverse_matrix Q;
378 ding.sa_handler = stopme;
379 sigemptyset(&ding.sa_mask);
381 sigaction(SIGALRM, &ding, NULL);
385 M.d[0] = 3.0; M.d[4] = 17.0; M.d[8] = 10.0; M.d[12] = 0.0;
386 M.d[1] = 2.0; M.d[5] = 4.0; M.d[9] = -2.0; M.d[13] = 0.0;
387 M.d[2] = 6.0; M.d[6] = 18.0; M.d[10] = -12; M.d[14] = 0.0;
388 M.d[3] = 0.0; M.d[7] = 0.0; M.d[11] = 0.0; M.d[15] = 1.0;
390 ret = matrix_invert(Q.LU, Q.perm, &M);
391 printf("ret = %d\n", ret);
392 printf("det = %g\n\n", determinant(&M));
397 print_inverse_data_matrix(&Q);
398 printf("P * A = L * U\n");
399 print_permutation_matrix(&Q);
400 print_LU_decomposition(&Q);
403 printf("a random matrix:\n");
404 randomize_matrix(&M);
405 det = determinant(&M);
407 errsup = test_inverse(&M);
408 printf("\nThe matrix multiplied by its inverse, error:\n");
410 printf("max abs error: %g, original determinant %g\n", errsup, det);
412 test_loop_precision();
413 test_loop_speed_matrixvector();
414 test_loop_speed_inversetransform();
415 test_loop_speed_invert();
416 test_loop_speed_invert_explicit();