blob: 4ddf931e4b1731e7ab1302304627a5d3c55ac9b8 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2016-2018 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027
28/** This function calculates the sum and sum of squares of a given input image.
29 *
30 * @note To enable calculation sum of squares -DSTDDEV should be passed as a preprocessor argument.
31 *
32 * @param[in] src_ptr Pointer to the source image. Supported data types: U8
33 * @param[in] src_stride_x Stride of the source image in X dimension (in bytes)
34 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
35 * @param[in] src_stride_y Stride of the source image in Y dimension (in bytes)
36 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
37 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source image
38 * @param[in] height Height of the input image
39 * @param[out] global_sum Global sum of all elements
40 * @param[out] global_sum_sq Global sum of squares of all elements
41 */
42__kernel void mean_stddev_accumulate(
43 IMAGE_DECLARATION(src),
44 uint height,
45 __global ulong *global_sum
Anthony Barbierac69aa12017-07-03 17:39:37 +010046#ifdef STDDEV
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047 ,
48 __global ulong *global_sum_sq
Anthony Barbierac69aa12017-07-03 17:39:37 +010049#endif /* STDDEV */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010050)
51{
52 // Get pixels pointer
53 Image src = CONVERT_TO_IMAGE_STRUCT(src);
54
Anthony Barbierac69aa12017-07-03 17:39:37 +010055 uint8 tmp_sum = 0;
56#ifdef STDDEV
57 uint8 tmp_sum_sq = 0;
58#endif /* STDDEV */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059 // Calculate partial sum
60 for(int i = 0; i < height; i++)
61 {
62 // Load data
63 uint8 data = convert_uint8(vload8(0, offset(&src, 0, i)));
64
65 tmp_sum += data;
Anthony Barbierac69aa12017-07-03 17:39:37 +010066#ifdef STDDEV
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067 tmp_sum_sq += data * data;
Anthony Barbierac69aa12017-07-03 17:39:37 +010068#endif /* STDDEV */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069 }
70 // Perform reduction
71 tmp_sum.s0123 += tmp_sum.s4567;
72 tmp_sum.s01 += tmp_sum.s23;
73 atom_add(global_sum, tmp_sum.s0 + tmp_sum.s1);
74
Anthony Barbierac69aa12017-07-03 17:39:37 +010075#ifdef STDDEV
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076 tmp_sum_sq.s0123 += tmp_sum_sq.s4567;
77 tmp_sum_sq.s01 += tmp_sum_sq.s23;
78 atom_add(global_sum_sq, tmp_sum_sq.s0 + tmp_sum_sq.s1);
Anthony Barbierac69aa12017-07-03 17:39:37 +010079#endif /* STDDEV */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080}
81
82#pragma OPENCL EXTENSION cl_khr_int64_base_atomics : disable