1 /*
2 * By downloading, copying, installing or using the software you agree to this license.
3 * If you do not agree to this license, do not download, install,
4 * copy or use the software.
5 *
6 *
7 * License Agreement
8 * For Open Source Computer Vision Library
9 * (3-clause BSD License)
10 *
11 * Copyright (C) 2014, NVIDIA Corporation, all rights reserved.
12 * Third party copyrights are property of their respective owners.
13 *
14 * Redistribution and use in source and binary forms, with or without modification,
15 * are permitted provided that the following conditions are met:
16 *
17 * * Redistributions of source code must retain the above copyright notice,
18 * this list of conditions and the following disclaimer.
19 *
20 * * Redistributions in binary form must reproduce the above copyright notice,
21 * this list of conditions and the following disclaimer in the documentation
22 * and/or other materials provided with the distribution.
23 *
24 * * Neither the names of the copyright holders nor the names of the contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * This software is provided by the copyright holders and contributors "as is" and
29 * any express or implied warranties, including, but not limited to, the implied
30 * warranties of merchantability and fitness for a particular purpose are disclaimed.
31 * In no event shall copyright holders or contributors be liable for any direct,
32 * indirect, incidental, special, exemplary, or consequential damages
33 * (including, but not limited to, procurement of substitute goods or services;
34 * loss of use, data, or profits; or business interruption) however caused
35 * and on any theory of liability, whether in contract, strict liability,
36 * or tort (including negligence or otherwise) arising in any way out of
37 * the use of this software, even if advised of the possibility of such damage.
38 */
39
40 #include "common.hpp"
41
42 #include <cmath>
43
44 namespace CAROTENE_NS {
45
meanStdDev(const Size2D & size,const u8 * srcBase,ptrdiff_t srcStride,f32 * pMean,f32 * pStdDev)46 void meanStdDev(const Size2D &size,
47 const u8 * srcBase, ptrdiff_t srcStride,
48 f32 * pMean, f32 * pStdDev)
49 {
50 internal::assertSupportedConfiguration();
51 #ifdef CAROTENE_NEON
52 f64 fsum = 0.0f, fsqsum = 0.0f;
53 sqsum(size, srcBase, srcStride, &fsum, &fsqsum, 1);
54
55 // calc mean and stddev
56 f64 itotal = 1.0 / size.total();
57 f64 mean = fsum * itotal;
58 f64 stddev = sqrt(std::max(fsqsum * itotal - mean * mean, 0.0));
59
60 if (pMean)
61 *pMean = mean;
62 if (pStdDev)
63 *pStdDev = stddev;
64 #else
65 (void)size;
66 (void)srcBase;
67 (void)srcStride;
68 (void)pMean;
69 (void)pStdDev;
70 #endif
71 }
72
meanStdDev(const Size2D & size,const u16 * srcBase,ptrdiff_t srcStride,f32 * pMean,f32 * pStdDev)73 void meanStdDev(const Size2D &size,
74 const u16 * srcBase, ptrdiff_t srcStride,
75 f32 * pMean, f32 * pStdDev)
76 {
77 internal::assertSupportedConfiguration();
78 #ifdef CAROTENE_NEON
79 size_t blockSize0 = 1 << 10, roiw4 = size.width & ~3;
80 f64 fsum = 0.0f, fsqsum = 0.0f;
81
82 f32 arsum[8];
83 uint32x4_t v_zero = vdupq_n_u32(0u), v_sum;
84 float32x4_t v_zero_f = vdupq_n_f32(0.0f), v_sqsum;
85
86 for (size_t i = 0; i < size.height; ++i)
87 {
88 const u16 * src = internal::getRowPtr(srcBase, srcStride, i);
89 size_t j = 0u;
90
91 while (j < roiw4)
92 {
93 size_t blockSize = std::min(roiw4 - j, blockSize0) + j;
94 v_sum = v_zero;
95 v_sqsum = v_zero_f;
96
97 for ( ; j + 16 < blockSize ; j += 16)
98 {
99 internal::prefetch(src + j);
100 uint16x8_t v_src0 = vld1q_u16(src + j), v_src1 = vld1q_u16(src + j + 8);
101
102 // 0
103 uint32x4_t v_srclo = vmovl_u16(vget_low_u16(v_src0));
104 uint32x4_t v_srchi = vmovl_u16(vget_high_u16(v_src0));
105 v_sum = vaddq_u32(v_sum, vaddq_u32(v_srclo, v_srchi));
106 float32x4_t v_srclo_f = vcvtq_f32_u32(v_srclo);
107 float32x4_t v_srchi_f = vcvtq_f32_u32(v_srchi);
108 v_sqsum = vmlaq_f32(v_sqsum, v_srclo_f, v_srclo_f);
109 v_sqsum = vmlaq_f32(v_sqsum, v_srchi_f, v_srchi_f);
110
111 // 1
112 v_srclo = vmovl_u16(vget_low_u16(v_src1));
113 v_srchi = vmovl_u16(vget_high_u16(v_src1));
114 v_sum = vaddq_u32(v_sum, vaddq_u32(v_srclo, v_srchi));
115 v_srclo_f = vcvtq_f32_u32(v_srclo);
116 v_srchi_f = vcvtq_f32_u32(v_srchi);
117 v_sqsum = vmlaq_f32(v_sqsum, v_srclo_f, v_srclo_f);
118 v_sqsum = vmlaq_f32(v_sqsum, v_srchi_f, v_srchi_f);
119 }
120
121 for ( ; j < blockSize; j += 4)
122 {
123 uint32x4_t v_src = vmovl_u16(vld1_u16(src + j));
124 float32x4_t v_src_f = vcvtq_f32_u32(v_src);
125 v_sum = vaddq_u32(v_sum, v_src);
126 v_sqsum = vmlaq_f32(v_sqsum, v_src_f, v_src_f);
127 }
128
129 vst1q_f32(arsum, vcvtq_f32_u32(v_sum));
130 vst1q_f32(arsum + 4, v_sqsum);
131
132 fsum += (f64)arsum[0] + arsum[1] + arsum[2] + arsum[3];
133 fsqsum += (f64)arsum[4] + arsum[5] + arsum[6] + arsum[7];
134 }
135
136 // collect a few last elements in the current row
137 for ( ; j < size.width; ++j)
138 {
139 f32 srcval = src[j];
140 fsum += srcval;
141 fsqsum += srcval * srcval;
142 }
143 }
144
145 // calc mean and stddev
146 f64 itotal = 1.0 / size.total();
147 f64 mean = fsum * itotal;
148 f64 stddev = sqrt(std::max(fsqsum * itotal - mean * mean, 0.0));
149
150 if (pMean)
151 *pMean = mean;
152 if (pStdDev)
153 *pStdDev = stddev;
154 #else
155 (void)size;
156 (void)srcBase;
157 (void)srcStride;
158 (void)pMean;
159 (void)pStdDev;
160 #endif
161 }
162
163 } // namespace CAROTENE_NS
164