aboutsummaryrefslogtreecommitdiff
path: root/src/core/CL/cl_kernels/non_linear_filter_helpers.h
blob: 77da2091b01beac4c86cfa83f33de65dd8c9874c (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
/*
 * 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.
 */

/** Sorts element-wise two vectors.
 *
 * @param[in, out] a First vector
 * @param[in, out] b Second vector
 */
#define SORT(a, b)                  \
    {                               \
        uchar8 min_val = min(a, b); \
        uchar8 max_val = max(a, b); \
        a              = min_val;   \
        b              = max_val;   \
    }

// Sorting networks below were generated using http://pages.ripco.net/~jgamble/nw.html

/** Sorting network to sort 5 vectors of 8 elements and return their median.
 *
 * @param[in] p0 First element vector
 * @param[in] p1 Second element vector
 * @param[in] p2 Third element vector
 * @param[in] p3 Fourth element vector
 * @param[in] p4 Fifth element vector
 *
 * @return Median values for 8 elements.
 */
inline uchar8 sort5(uchar8 p0, uchar8 p1, uchar8 p2, uchar8 p3, uchar8 p4)
{
    SORT(p0, p1);
    SORT(p2, p3);
    SORT(p0, p2);
    SORT(p1, p3);
    SORT(p1, p2);
    SORT(p0, p4);
    SORT(p1, p4);
    SORT(p2, p4);

    return p2;
}

/** Sorting network to sort 9 vectors of 8 elements and return their median.
 *
 * @param[in] p0 First element vector
 * @param[in] p1 Second element vector
 * @param[in] p2 Third element vector
 * @param[in] p3 Fourth element vector
 * @param[in] p4 Fifth element vector
 * @param[in] p5 Sixth element vector
 * @param[in] p6 Seventh element vector
 * @param[in] p7 Eigth element vector
 * @param[in] p8 Ninth element vector
 *
 * @return Median values for 8 elements.
 */
inline uchar8 sort9(uchar8 p0, uchar8 p1, uchar8 p2, uchar8 p3, uchar8 p4, uchar8 p5, uchar8 p6, uchar8 p7, uchar8 p8)
{
    SORT(p1, p2);
    SORT(p4, p5);
    SORT(p7, p8);
    SORT(p0, p1);
    SORT(p3, p4);
    SORT(p6, p7);
    SORT(p1, p2);
    SORT(p4, p5);
    SORT(p7, p8);
    SORT(p0, p3);
    SORT(p5, p8);
    SORT(p4, p7);
    SORT(p3, p6);
    SORT(p1, p4);
    SORT(p2, p5);
    SORT(p4, p7);
    SORT(p4, p2);
    SORT(p6, p4);
    SORT(p4, p2);

    return p4;
}

/** Calculate the minimum of a sliding window of size 3.
 *
 * @param val Values to calculate the minimum values
 *
 * @return Minimum values of 8 elements on a sliding window of size 3.
 */
inline uchar8 row_reduce_min_3(uchar16 val)
{
    return min(val.s01234567, min(val.s12345678, val.s23456789));
}

/** Calculate the maximum of a sliding window of size 3.
 *
 * @param val Values to calculate the maximum values
 *
 * @return Maximum values of 8 elements on a sliding window of size 3.
 */
inline uchar8 row_reduce_max_3(uchar16 val)
{
    return max(val.s01234567, max(val.s12345678, val.s23456789));
}

/** Calculate the minimum of a sliding window of size 5.
 *
 * @param val Values to calculate the minimum values
 *
 * @return Minimum values of 8 elements on a sliding window of size 5.
 */
inline uchar8 row_reduce_min_5(uchar16 val)
{
    return min(val.s01234567, min(min(val.s12345678, val.s23456789), min(val.s3456789A, val.s456789AB)));
}

/** Calculate the maximum of a sliding window of size 5.
 *
 * @param val Values to calculate the maximum values
 *
 * @return Maximum values of 8 elements on a sliding window of size 5.
 */
inline uchar8 row_reduce_max_5(uchar16 val)
{
    return max(val.s01234567, max(max(val.s12345678, val.s23456789), max(val.s3456789A, val.s456789AB)));
}