aboutsummaryrefslogtreecommitdiff
path: root/src/core/CL/cl_kernels/minmaxloc.cl
blob: 1045f22fb130c9dd76d16d1c35dbe51b2eaac6d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Copyright (c) 2017 Arm Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include "helpers.h"
#include "types.h"

#ifndef DATA_TYPE_MIN
#define DATA_TYPE_MIN 0x0
#endif /* DATA_TYPE_MIN */

#ifndef DATA_TYPE_MAX
#define DATA_TYPE_MAX 0xFF
#endif /* DATA_TYPE_MAX */

inline int FloatFlip(float val)
{
    union
    {
        int   int_val;
        float flt_val;
    } u_val;
    u_val.flt_val = val;
    return (u_val.int_val >= 0) ? u_val.int_val : u_val.int_val ^ 0x7FFFFFFF;
}

__constant VEC_DATA_TYPE(DATA_TYPE, 16) type_min = (VEC_DATA_TYPE(DATA_TYPE, 16))(DATA_TYPE_MIN);
__constant VEC_DATA_TYPE(DATA_TYPE, 16) type_max = (VEC_DATA_TYPE(DATA_TYPE, 16))(DATA_TYPE_MAX);
__constant int16 idx16 = (int16)(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);

/** This function identifies the min and maximum value of an input image.
 *
 * @note Input image data type must be passed as a preprocessor argument using -DDATA_TYPE.
 * Moreover, the minimum and maximum value of the given data type must be provided using -DDATA_TYPE_MIN and -DDATA_TYPE_MAX respectively.
 * @note In case image width is not a multiple of 16 then -DNON_MULTIPLE_OF_16 must be passed.
 *
 * @param[in]  src_ptr                           Pointer to the source image. Supported data types: U8
 * @param[in]  src_stride_x                      Stride of the source image in X dimension (in bytes)
 * @param[in]  src_step_x                        src_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  src_stride_y                      Stride of the source image in Y dimension (in bytes)
 * @param[in]  src_step_y                        src_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  src_offset_first_element_in_bytes The offset of the first element in the source image
 * @param[out] min_max                           Pointer to buffer with minimum value in position 0 and maximum value in position 1
 * @param[in]  width                             Input image width
 */
__kernel void minmax(
    IMAGE_DECLARATION(src),
    __global int *min_max,
    int           width)
{
    Image src = CONVERT_TO_IMAGE_STRUCT(src);

    // Initialize local minimum and local maximum
    VEC_DATA_TYPE(DATA_TYPE, 16)
    local_min = type_max;
    VEC_DATA_TYPE(DATA_TYPE, 16)
    local_max = type_min;

    // Calculate min/max of row
    int i = 0;
    for(; i + 16 <= width; i += 16)
    {
        VEC_DATA_TYPE(DATA_TYPE, 16)
        data      = vload16(0, (__global DATA_TYPE *)offset(&src, i, 0));
        local_min = min(data, local_min);
        local_max = max(data, local_max);
    }

#ifdef NON_MULTIPLE_OF_16
    // Handle non multiple of 16
    VEC_DATA_TYPE(DATA_TYPE, 16)
    data = vload16(0, (__global DATA_TYPE *)offset(&src, i, 0));
#ifdef IS_DATA_TYPE_FLOAT
    int16 valid_indices = (i + idx16) < width;
#else  /* IS_DATA_TYPE_FLOAT */
    VEC_DATA_TYPE(DATA_TYPE, 16)
    valid_indices = CONVERT((i + idx16) < width, VEC_DATA_TYPE(DATA_TYPE, 16));
#endif /* IS_DATA_TYPE_FLOAT */
    local_max = max(local_max, select(type_min, data, valid_indices));
    local_min = min(local_min, select(type_max, data, valid_indices));
#endif /* NON_MULTIPLE_OF_16 */

    // Perform min/max reduction
    local_min.s01234567 = min(local_min.s01234567, local_min.s89ABCDEF);
    local_max.s01234567 = max(local_max.s01234567, local_max.s89ABCDEF);

    local_min.s0123 = min(local_min.s0123, local_min.s4567);
    local_max.s0123 = max(local_max.s0123, local_max.s4567);

    local_min.s01 = min(local_min.s01, local_min.s23);
    local_max.s01 = max(local_max.s01, local_max.s23);

    local_min.s0 = min(local_min.s0, local_min.s1);
    local_max.s0 = max(local_max.s0, local_max.s1);

    // Update global min/max
#ifdef IS_DATA_TYPE_FLOAT
    atomic_min(&min_max[0], FloatFlip(local_min.s0));
    atomic_max(&min_max[1], FloatFlip(local_max.s0));
#else  /* IS_DATA_TYPE_FLOAT */
    atomic_min(&min_max[0], local_min.s0);
    atomic_max(&min_max[1], local_max.s0);
#endif /* IS_DATA_TYPE_FLOAT */
}

/** This function counts the min and max occurrences in an image and tags their position.
 *
 * @note -DCOUNT_MIN_MAX should be specified if we want to count the occurrences of the minimum and maximum values.
 * @note -DLOCATE_MIN and/or -DLOCATE_MAX should be specified if we want to store the position of each occurrence on the given array.
 *
 * @param[in]  src_ptr                           Pointer to the source image. Supported data types: U8
 * @param[in]  src_stride_x                      Stride of the source image in X dimension (in bytes)
 * @param[in]  src_step_x                        src_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  src_stride_y                      Stride of the source image in Y dimension (in bytes)
 * @param[in]  src_step_y                        src_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  src_offset_first_element_in_bytes The offset of the first element in the source image
 * @param[in]  min_max                           Pointer to buffer with minimum value in position 0 and maximum value in position 1
 * @param[out] min_max_count                     Pointer to buffer with minimum value occurrences in position 0 and maximum value occurrences in position 1
 * @param[out] min_loc                           Array that holds the location of the minimum value occurrences
 * @param[in]  max_min_loc_count                 The maximum number of min value occurrences coordinates the array can hold
 * @param[out] max_loc                           Array that holds the location of the maximum value occurrences
 * @param[in]  max_max_loc_count                 The maximum number of max value occurrences coordinates the array can hold
 */
__kernel void minmaxloc(
    IMAGE_DECLARATION(src),
    __global int *min_max,
    __global uint *min_max_count
#ifdef LOCATE_MIN
    ,
    __global Coordinates2D *min_loc, uint max_min_loc_count
#endif /* LOCATE_MIN */
#ifdef LOCATE_MAX
    ,
    __global Coordinates2D *max_loc, uint max_max_loc_count
#endif /* LOCATE_MAX */
)
{
    Image src = CONVERT_TO_IMAGE_STRUCT(src);

#ifdef IS_DATA_TYPE_FLOAT
    __global float *min_max_ptr = (__global float *)min_max;
    float           min_value   = min_max_ptr[0];
    float           max_value   = min_max_ptr[1];
#else  /* IS_DATA_TYPE_FLOAT */
    int min_value = min_max[0];
    int max_value = min_max[1];
#endif /* IS_DATA_TYPE_FLOAT */

    DATA_TYPE value = *((__global DATA_TYPE *)src.ptr);
#ifdef COUNT_MIN_MAX
    if(value == min_value)
    {
        uint idx = atomic_inc(&min_max_count[0]);
#ifdef LOCATE_MIN
        if(idx < max_min_loc_count)
        {
            min_loc[idx].x = get_global_id(0);
            min_loc[idx].y = get_global_id(1);
        }
#endif /* LOCATE_MIN */
    }
    if(value == max_value)
    {
        uint idx = atomic_inc(&min_max_count[1]);
#ifdef LOCATE_MAX
        if(idx < max_max_loc_count)
        {
            max_loc[idx].x = get_global_id(0);
            max_loc[idx].y = get_global_id(1);
        }
#endif /* LOCATE_MAX */
    }
#endif /* COUNT_MIN_MAX */
}