Debian Codegraph @

src/filter/autothresh0ld/variance.h

raw symbols SCIP
1 #ifdef __SSE2__
2 #include <emmintrin.h>
3 #endif
4
5 static float find_variance(float *hist, int thresh)
6 {
7 float w_0 = 0.0, mu_0 = 0.0, w_1 = 0.0, mu_1 = 0.0;
8
9 for (int i = 0; i < thresh; ++i)
10 {
11 w_0 += hist[i];
12 mu_0 += i * hist[i];
13 }
14 for (int i = thresh; i < 256; ++i)
15 {
16 w_1 += hist[i];
17 mu_1 += i * hist[i];
18 }
19
20 float mu_diff = (mu_0/w_0 - mu_1/w_1);
21 return w_0*w_1*mu_diff*mu_diff;
22 }
23
24 #ifdef __SSE2__
25 static float _sse1_hadd_ps(__m128 v)
26 {
27 // Based on a StackOverflow answer.
28 __m128 shuf = _mm_shuffle_ps(v, v, _MM_SHUFFLE(2, 3, 0, 1));
29 __m128 sums = _mm_add_ps(v, shuf);
30 shuf = _mm_movehl_ps(shuf, sums);
31 sums = _mm_add_ss(sums, shuf);
32 return _mm_cvtss_f32(sums);
33 }
34
35 static float find_variance_sse2(float *hist, int thresh)
36 {
37 __m128 vw_0 = _mm_setzero_ps(), vmu_0 = _mm_setzero_ps(),
38 vw_1 = _mm_setzero_ps(), vmu_1 = _mm_setzero_ps();
39 __m128 vcnt = _mm_set_ps(0, 1, 2, 3);
40
41 int thresh_low = (thresh / 4 + 0) * 4,
42 thresh_up = (thresh / 4 + 1) * 4;
43
44 for (int i = 0; i < thresh_low; i+=4)
45 {
46 __m128 vhist = _mm_castsi128_ps(_mm_loadu_si128((void*)&hist[i]));
47
48 vw_0 = _mm_add_ps(vhist, vw_0);
49 vmu_0 = _mm_add_ps(_mm_mul_ps(vcnt, vhist), vw_0);
50 vcnt = _mm_add_ps(vcnt, _mm_set1_ps(4));
51 }
52
53 // This is skipped, and handled as an edge case by non-SSE code.
54 vcnt = _mm_add_ps(vcnt, _mm_set1_ps(4));
55
56 for (int i = thresh_up; i < 256; i+=4)
57 {
58 __m128 vhist = _mm_castsi128_ps(_mm_loadu_si128((void*)&hist[i]));
59
60 vw_1 = _mm_add_ps(vhist, vw_1);
61 vmu_1 = _mm_add_ps(_mm_mul_ps(vcnt, vhist), vw_1);
62 vcnt = _mm_add_ps(vcnt, _mm_set1_ps(4));
63 }
64
65 float w_0 = _sse1_hadd_ps(vw_0), mu_0 = _sse1_hadd_ps(vmu_0),
66 w_1 = _sse1_hadd_ps(vw_1), mu_1 = _sse1_hadd_ps(vmu_1);
67
68 // This edge case is here, because thresh may not be a multiple of 4.
69 for (int i = thresh_low; i < thresh; ++i)
70 {
71 w_0 += hist[i];
72 mu_0 += i * hist[i];
73 }
74
75 for (int i = thresh; i < thresh_up; ++i)
76 {
77 w_1 += hist[i];
78 mu_1 += i * hist[i];
79 }
80
81 float mu_diff = (mu_0/w_0 - mu_1/w_1);
82 return w_0*w_1*mu_diff*mu_diff;
83 }
84 #endif