arm_compute v18.02
[platform/upstream/armcl.git] / src / core / CL / cl_kernels / non_linear_filter_helpers.h
1 /*
2  * Copyright (c) 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
25 /** Sorts element-wise two vectors.
26  *
27  * @param[in, out] a First vector
28  * @param[in, out] b Second vector
29  */
30 #define SORT(a, b)                  \
31     {                               \
32         uchar8 min_val = min(a, b); \
33         uchar8 max_val = max(a, b); \
34         a              = min_val;   \
35         b              = max_val;   \
36     }
37
38 // Sorting networks below were generated using http://pages.ripco.net/~jgamble/nw.html
39
40 /** Sorting network to sort 5 vectors of 8 elements and return their median.
41  *
42  * @param[in] p0 First element vector
43  * @param[in] p1 Second element vector
44  * @param[in] p2 Third element vector
45  * @param[in] p3 Fourth element vector
46  * @param[in] p4 Fifth element vector
47  *
48  * @return Median values for 8 elements.
49  */
50 inline uchar8 sort5(uchar8 p0, uchar8 p1, uchar8 p2, uchar8 p3, uchar8 p4)
51 {
52     SORT(p0, p1);
53     SORT(p2, p3);
54     SORT(p0, p2);
55     SORT(p1, p3);
56     SORT(p1, p2);
57     SORT(p0, p4);
58     SORT(p1, p4);
59     SORT(p2, p4);
60
61     return p2;
62 }
63
64 /** Sorting network to sort 9 vectors of 8 elements and return their median.
65  *
66  * @param[in] p0 First element vector
67  * @param[in] p1 Second element vector
68  * @param[in] p2 Third element vector
69  * @param[in] p3 Fourth element vector
70  * @param[in] p4 Fifth element vector
71  * @param[in] p5 Sixth element vector
72  * @param[in] p6 Seventh element vector
73  * @param[in] p7 Eigth element vector
74  * @param[in] p8 Ninth element vector
75  *
76  * @return Median values for 8 elements.
77  */
78 inline uchar8 sort9(uchar8 p0, uchar8 p1, uchar8 p2, uchar8 p3, uchar8 p4, uchar8 p5, uchar8 p6, uchar8 p7, uchar8 p8)
79 {
80     SORT(p1, p2);
81     SORT(p4, p5);
82     SORT(p7, p8);
83     SORT(p0, p1);
84     SORT(p3, p4);
85     SORT(p6, p7);
86     SORT(p1, p2);
87     SORT(p4, p5);
88     SORT(p7, p8);
89     SORT(p0, p3);
90     SORT(p5, p8);
91     SORT(p4, p7);
92     SORT(p3, p6);
93     SORT(p1, p4);
94     SORT(p2, p5);
95     SORT(p4, p7);
96     SORT(p4, p2);
97     SORT(p6, p4);
98     SORT(p4, p2);
99
100     return p4;
101 }
102
103 /** Calculate the minimum of a sliding window of size 3.
104  *
105  * @param val Values to calculate the minimum values
106  *
107  * @return Minimum values of 8 elements on a sliding window of size 3.
108  */
109 inline uchar8 row_reduce_min_3(uchar16 val)
110 {
111     return min(val.s01234567, min(val.s12345678, val.s23456789));
112 }
113
114 /** Calculate the maximum of a sliding window of size 3.
115  *
116  * @param val Values to calculate the maximum values
117  *
118  * @return Maximum values of 8 elements on a sliding window of size 3.
119  */
120 inline uchar8 row_reduce_max_3(uchar16 val)
121 {
122     return max(val.s01234567, max(val.s12345678, val.s23456789));
123 }
124
125 /** Calculate the minimum of a sliding window of size 5.
126  *
127  * @param val Values to calculate the minimum values
128  *
129  * @return Minimum values of 8 elements on a sliding window of size 5.
130  */
131 inline uchar8 row_reduce_min_5(uchar16 val)
132 {
133     return min(val.s01234567, min(min(val.s12345678, val.s23456789), min(val.s3456789A, val.s456789AB)));
134 }
135
136 /** Calculate the maximum of a sliding window of size 5.
137  *
138  * @param val Values to calculate the maximum values
139  *
140  * @return Maximum values of 8 elements on a sliding window of size 5.
141  */
142 inline uchar8 row_reduce_max_5(uchar16 val)
143 {
144     return max(val.s01234567, max(max(val.s12345678, val.s23456789), max(val.s3456789A, val.s456789AB)));
145 }