arm_compute v18.02
[platform/upstream/armcl.git] / src / core / CL / cl_kernels / mean_stddev.cl
1 /*
2  * Copyright (c) 2016, 2017 ARM Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "helpers.h"
25
26 #pragma OPENCL EXTENSION cl_khr_int64_base_atomics : enable
27 #pragma OPENCL EXTENSION cl_khr_int64_extended_atomics : enable
28
29 /** This function calculates the sum and sum of squares of a given input image.
30  *
31  * @note To enable calculation sum of squares -DSTDDEV should be passed as a preprocessor argument.
32  *
33  * @param[in]  src_ptr                           Pointer to the source image. Supported data types: U8
34  * @param[in]  src_stride_x                      Stride of the source image in X dimension (in bytes)
35  * @param[in]  src_step_x                        src_stride_x * number of elements along X processed per workitem(in bytes)
36  * @param[in]  src_stride_y                      Stride of the source image in Y dimension (in bytes)
37  * @param[in]  src_step_y                        src_stride_y * number of elements along Y processed per workitem(in bytes)
38  * @param[in]  src_offset_first_element_in_bytes The offset of the first element in the source image
39  * @param[in]  height                            Height of the input image
40  * @param[out] global_sum                        Global sum of all elements
41  * @param[out] global_sum_sq                     Global sum of squares of all elements
42  */
43 __kernel void mean_stddev_accumulate(
44     IMAGE_DECLARATION(src),
45     uint     height,
46     __global ulong *global_sum
47 #ifdef STDDEV
48     ,
49     __global ulong *global_sum_sq
50 #endif /* STDDEV */
51 )
52 {
53     // Get pixels pointer
54     Image src = CONVERT_TO_IMAGE_STRUCT(src);
55
56     uint8 tmp_sum = 0;
57 #ifdef STDDEV
58     uint8 tmp_sum_sq = 0;
59 #endif /* STDDEV */
60     // Calculate partial sum
61     for(int i = 0; i < height; i++)
62     {
63         // Load data
64         uint8 data = convert_uint8(vload8(0, offset(&src, 0, i)));
65
66         tmp_sum += data;
67 #ifdef STDDEV
68         tmp_sum_sq += data * data;
69 #endif /* STDDEV */
70     }
71     // Perform reduction
72     tmp_sum.s0123 += tmp_sum.s4567;
73     tmp_sum.s01 += tmp_sum.s23;
74     atom_add(global_sum, tmp_sum.s0 + tmp_sum.s1);
75
76 #ifdef STDDEV
77     tmp_sum_sq.s0123 += tmp_sum_sq.s4567;
78     tmp_sum_sq.s01 += tmp_sum_sq.s23;
79     atom_add(global_sum_sq, tmp_sum_sq.s0 + tmp_sum_sq.s1);
80 #endif /* STDDEV */
81 }
82
83 #pragma OPENCL EXTENSION cl_khr_int64_base_atomics : disable
84 #pragma OPENCL EXTENSION cl_khr_int64_extended_atomics : disable