Coverage Report

Created: 2025-08-26 06:59

/src/libavif/ext/aom/aom_dsp/mathutils.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2017, Alliance for Open Media. All rights reserved.
3
 *
4
 * This source code is subject to the terms of the BSD 2 Clause License and
5
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6
 * was not distributed with this source code in the LICENSE file, you can
7
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8
 * Media Patent License 1.0 was not distributed with this source code in the
9
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10
 */
11
12
#ifndef AOM_AOM_DSP_MATHUTILS_H_
13
#define AOM_AOM_DSP_MATHUTILS_H_
14
15
#include <assert.h>
16
#include <math.h>
17
#include <string.h>
18
19
#include "aom_dsp/aom_dsp_common.h"
20
21
static const double TINY_NEAR_ZERO = 1.0E-16;
22
23
// Solves Ax = b, where x and b are column vectors of size nx1 and A is nxn
24
99.7k
static inline int linsolve(int n, double *A, int stride, double *b, double *x) {
25
99.7k
  int i, j, k;
26
99.7k
  double c;
27
  // Forward elimination
28
397k
  for (k = 0; k < n - 1; k++) {
29
    // Bring the largest magnitude to the diagonal position
30
892k
    for (i = n - 1; i > k; i--) {
31
594k
      if (fabs(A[(i - 1) * stride + k]) < fabs(A[i * stride + k])) {
32
2.46M
        for (j = 0; j < n; j++) {
33
1.96M
          c = A[i * stride + j];
34
1.96M
          A[i * stride + j] = A[(i - 1) * stride + j];
35
1.96M
          A[(i - 1) * stride + j] = c;
36
1.96M
        }
37
493k
        c = b[i];
38
493k
        b[i] = b[i - 1];
39
493k
        b[i - 1] = c;
40
493k
      }
41
594k
    }
42
897k
    for (i = k; i < n - 1; i++) {
43
599k
      if (fabs(A[k * stride + k]) < TINY_NEAR_ZERO) return 0;
44
599k
      c = A[(i + 1) * stride + k] / A[k * stride + k];
45
2.99M
      for (j = 0; j < n; j++) A[(i + 1) * stride + j] -= c * A[k * stride + j];
46
599k
      b[i + 1] -= c * b[k];
47
599k
    }
48
298k
  }
49
  // Backward substitution
50
499k
  for (i = n - 1; i >= 0; i--) {
51
399k
    if (fabs(A[i * stride + i]) < TINY_NEAR_ZERO) return 0;
52
399k
    c = 0;
53
999k
    for (j = i + 1; j <= n - 1; j++) c += A[i * stride + j] * x[j];
54
399k
    x[i] = (b[i] - c) / A[i * stride + i];
55
399k
  }
56
57
99.7k
  return 1;
58
99.7k
}
Unexecuted instantiation: pickrst.c:linsolve
Unexecuted instantiation: temporal_filter.c:linsolve
Unexecuted instantiation: noise_model.c:linsolve
ransac.c:linsolve
Line
Count
Source
24
99.7k
static inline int linsolve(int n, double *A, int stride, double *b, double *x) {
25
99.7k
  int i, j, k;
26
99.7k
  double c;
27
  // Forward elimination
28
397k
  for (k = 0; k < n - 1; k++) {
29
    // Bring the largest magnitude to the diagonal position
30
892k
    for (i = n - 1; i > k; i--) {
31
594k
      if (fabs(A[(i - 1) * stride + k]) < fabs(A[i * stride + k])) {
32
2.46M
        for (j = 0; j < n; j++) {
33
1.96M
          c = A[i * stride + j];
34
1.96M
          A[i * stride + j] = A[(i - 1) * stride + j];
35
1.96M
          A[(i - 1) * stride + j] = c;
36
1.96M
        }
37
493k
        c = b[i];
38
493k
        b[i] = b[i - 1];
39
493k
        b[i - 1] = c;
40
493k
      }
41
594k
    }
42
897k
    for (i = k; i < n - 1; i++) {
43
599k
      if (fabs(A[k * stride + k]) < TINY_NEAR_ZERO) return 0;
44
599k
      c = A[(i + 1) * stride + k] / A[k * stride + k];
45
2.99M
      for (j = 0; j < n; j++) A[(i + 1) * stride + j] -= c * A[k * stride + j];
46
599k
      b[i + 1] -= c * b[k];
47
599k
    }
48
298k
  }
49
  // Backward substitution
50
499k
  for (i = n - 1; i >= 0; i--) {
51
399k
    if (fabs(A[i * stride + i]) < TINY_NEAR_ZERO) return 0;
52
399k
    c = 0;
53
999k
    for (j = i + 1; j <= n - 1; j++) c += A[i * stride + j] * x[j];
54
399k
    x[i] = (b[i] - c) / A[i * stride + i];
55
399k
  }
56
57
99.7k
  return 1;
58
99.7k
}
Unexecuted instantiation: ml.c:linsolve
Unexecuted instantiation: temporal_filter_sse2.c:linsolve
Unexecuted instantiation: highbd_temporal_filter_sse2.c:linsolve
Unexecuted instantiation: highbd_temporal_filter_avx2.c:linsolve
59
60
////////////////////////////////////////////////////////////////////////////////
61
// Least-squares
62
// Solves for n-dim x in a least squares sense to minimize |Ax - b|^2
63
// The solution is simply x = (A'A)^-1 A'b or simply the solution for
64
// the system: A'A x = A'b
65
//
66
// This process is split into three steps in order to avoid needing to
67
// explicitly allocate the A matrix, which may be very large if there
68
// are many equations to solve.
69
//
70
// The process for using this is (in pseudocode):
71
//
72
// Allocate mat (size n*n), y (size n), a (size n), x (size n)
73
// least_squares_init(mat, y, n)
74
// for each equation a . x = b {
75
//    least_squares_accumulate(mat, y, a, b, n)
76
// }
77
// least_squares_solve(mat, y, x, n)
78
//
79
// where:
80
// * mat, y are accumulators for the values A'A and A'b respectively,
81
// * a, b are the coefficients of each individual equation,
82
// * x is the result vector
83
// * and n is the problem size
84
100k
static inline void least_squares_init(double *mat, double *y, int n) {
85
100k
  memset(mat, 0, n * n * sizeof(double));
86
100k
  memset(y, 0, n * sizeof(double));
87
100k
}
Unexecuted instantiation: pickrst.c:least_squares_init
Unexecuted instantiation: temporal_filter.c:least_squares_init
Unexecuted instantiation: noise_model.c:least_squares_init
ransac.c:least_squares_init
Line
Count
Source
84
100k
static inline void least_squares_init(double *mat, double *y, int n) {
85
100k
  memset(mat, 0, n * n * sizeof(double));
86
100k
  memset(y, 0, n * sizeof(double));
87
100k
}
Unexecuted instantiation: ml.c:least_squares_init
Unexecuted instantiation: temporal_filter_sse2.c:least_squares_init
Unexecuted instantiation: highbd_temporal_filter_sse2.c:least_squares_init
Unexecuted instantiation: highbd_temporal_filter_avx2.c:least_squares_init
88
89
// Round the given positive value to nearest integer
90
11.0M
static AOM_FORCE_INLINE int iroundpf(float x) {
91
11.0M
  assert(x >= 0.0);
92
11.0M
  return (int)(x + 0.5f);
93
11.0M
}
Unexecuted instantiation: pickrst.c:iroundpf
Unexecuted instantiation: temporal_filter.c:iroundpf
Unexecuted instantiation: noise_model.c:iroundpf
Unexecuted instantiation: ransac.c:iroundpf
Unexecuted instantiation: ml.c:iroundpf
Unexecuted instantiation: temporal_filter_sse2.c:iroundpf
Unexecuted instantiation: highbd_temporal_filter_sse2.c:iroundpf
highbd_temporal_filter_avx2.c:iroundpf
Line
Count
Source
90
11.0M
static AOM_FORCE_INLINE int iroundpf(float x) {
91
11.0M
  assert(x >= 0.0);
92
11.0M
  return (int)(x + 0.5f);
93
11.0M
}
94
95
static inline void least_squares_accumulate(double *mat, double *y,
96
541k
                                            const double *a, double b, int n) {
97
2.70M
  for (int i = 0; i < n; i++) {
98
10.8M
    for (int j = 0; j < n; j++) {
99
8.66M
      mat[i * n + j] += a[i] * a[j];
100
8.66M
    }
101
2.16M
  }
102
2.70M
  for (int i = 0; i < n; i++) {
103
2.16M
    y[i] += a[i] * b;
104
2.16M
  }
105
541k
}
Unexecuted instantiation: pickrst.c:least_squares_accumulate
Unexecuted instantiation: temporal_filter.c:least_squares_accumulate
Unexecuted instantiation: noise_model.c:least_squares_accumulate
ransac.c:least_squares_accumulate
Line
Count
Source
96
541k
                                            const double *a, double b, int n) {
97
2.70M
  for (int i = 0; i < n; i++) {
98
10.8M
    for (int j = 0; j < n; j++) {
99
8.66M
      mat[i * n + j] += a[i] * a[j];
100
8.66M
    }
101
2.16M
  }
102
2.70M
  for (int i = 0; i < n; i++) {
103
2.16M
    y[i] += a[i] * b;
104
2.16M
  }
105
541k
}
Unexecuted instantiation: ml.c:least_squares_accumulate
Unexecuted instantiation: temporal_filter_sse2.c:least_squares_accumulate
Unexecuted instantiation: highbd_temporal_filter_sse2.c:least_squares_accumulate
Unexecuted instantiation: highbd_temporal_filter_avx2.c:least_squares_accumulate
106
107
static inline int least_squares_solve(double *mat, double *y, double *x,
108
99.6k
                                      int n) {
109
99.6k
  return linsolve(n, mat, n, y, x);
110
99.6k
}
Unexecuted instantiation: pickrst.c:least_squares_solve
Unexecuted instantiation: temporal_filter.c:least_squares_solve
Unexecuted instantiation: noise_model.c:least_squares_solve
ransac.c:least_squares_solve
Line
Count
Source
108
99.6k
                                      int n) {
109
99.6k
  return linsolve(n, mat, n, y, x);
110
99.6k
}
Unexecuted instantiation: ml.c:least_squares_solve
Unexecuted instantiation: temporal_filter_sse2.c:least_squares_solve
Unexecuted instantiation: highbd_temporal_filter_sse2.c:least_squares_solve
Unexecuted instantiation: highbd_temporal_filter_avx2.c:least_squares_solve
111
112
// Matrix multiply
113
static inline void multiply_mat(const double *m1, const double *m2, double *res,
114
                                const int m1_rows, const int inner_dim,
115
0
                                const int m2_cols) {
116
0
  double sum;
117
118
0
  int row, col, inner;
119
0
  for (row = 0; row < m1_rows; ++row) {
120
0
    for (col = 0; col < m2_cols; ++col) {
121
0
      sum = 0;
122
0
      for (inner = 0; inner < inner_dim; ++inner)
123
0
        sum += m1[row * inner_dim + inner] * m2[inner * m2_cols + col];
124
0
      *(res++) = sum;
125
0
    }
126
0
  }
127
0
}
Unexecuted instantiation: pickrst.c:multiply_mat
Unexecuted instantiation: temporal_filter.c:multiply_mat
Unexecuted instantiation: noise_model.c:multiply_mat
Unexecuted instantiation: ransac.c:multiply_mat
Unexecuted instantiation: ml.c:multiply_mat
Unexecuted instantiation: temporal_filter_sse2.c:multiply_mat
Unexecuted instantiation: highbd_temporal_filter_sse2.c:multiply_mat
Unexecuted instantiation: highbd_temporal_filter_avx2.c:multiply_mat
128
129
11.0M
static inline float approx_exp(float y) {
130
11.0M
#define A ((1 << 23) / 0.69314718056f)  // (1 << 23) / ln(2)
131
11.0M
#define B \
132
11.0M
  127  // Offset for the exponent according to IEEE floating point standard.
133
11.0M
#define C 60801  // Magic number controls the accuracy of approximation
134
11.0M
  union {
135
11.0M
    float as_float;
136
11.0M
    int32_t as_int32;
137
11.0M
  } container;
138
11.0M
  container.as_int32 = ((int32_t)(y * A)) + ((B << 23) - C);
139
11.0M
  return container.as_float;
140
11.0M
#undef A
141
11.0M
#undef B
142
11.0M
#undef C
143
11.0M
}
Unexecuted instantiation: pickrst.c:approx_exp
Unexecuted instantiation: temporal_filter.c:approx_exp
Unexecuted instantiation: noise_model.c:approx_exp
Unexecuted instantiation: ransac.c:approx_exp
Unexecuted instantiation: ml.c:approx_exp
Unexecuted instantiation: temporal_filter_sse2.c:approx_exp
Unexecuted instantiation: highbd_temporal_filter_sse2.c:approx_exp
highbd_temporal_filter_avx2.c:approx_exp
Line
Count
Source
129
11.0M
static inline float approx_exp(float y) {
130
11.0M
#define A ((1 << 23) / 0.69314718056f)  // (1 << 23) / ln(2)
131
11.0M
#define B \
132
11.0M
  127  // Offset for the exponent according to IEEE floating point standard.
133
11.0M
#define C 60801  // Magic number controls the accuracy of approximation
134
11.0M
  union {
135
11.0M
    float as_float;
136
11.0M
    int32_t as_int32;
137
11.0M
  } container;
138
11.0M
  container.as_int32 = ((int32_t)(y * A)) + ((B << 23) - C);
139
11.0M
  return container.as_float;
140
11.0M
#undef A
141
11.0M
#undef B
142
11.0M
#undef C
143
11.0M
}
144
#endif  // AOM_AOM_DSP_MATHUTILS_H_