blob: 48197d6473610244d93314a800501ee223294bb3 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2016, 2017 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/** Calculates L1 normalization between two inputs.
27 *
28 * @param[in] a First input. Supported data types: S16, S32
29 * @param[in] b Second input. Supported data types: S16, S32
30 *
31 * @return L1 normalization magnitude result. Supported data types: S16, S32
32 */
33inline VEC_DATA_TYPE(DATA_TYPE, 16) magnitude_l1(VEC_DATA_TYPE(DATA_TYPE, 16) a, VEC_DATA_TYPE(DATA_TYPE, 16) b)
34{
35 return CONVERT_SAT(add_sat(abs(a), abs(b)), VEC_DATA_TYPE(DATA_TYPE, 16));
36}
37
38/** Calculates L2 normalization between two inputs.
39 *
40 * @param[in] a First input. Supported data types: S16, S32
41 * @param[in] b Second input. Supported data types: S16, S32
42 *
43 * @return L2 normalization magnitude result. Supported data types: S16, S32
44 */
45inline VEC_DATA_TYPE(DATA_TYPE, 16) magnitude_l2(int16 a, int16 b)
46{
47 return CONVERT_SAT((sqrt(convert_float16((convert_uint16(a * a) + convert_uint16(b * b)))) + 0.5f),
48 VEC_DATA_TYPE(DATA_TYPE, 16));
49}
50
51/** Calculates unsigned phase between two inputs.
52 *
53 * @param[in] a First input. Supported data types: S16, S32
54 * @param[in] b Second input. Supported data types: S16, S32
55 *
56 * @return Unsigned phase mapped in the interval [0, 180]. Supported data types: U8
57 */
58inline uchar16 phase_unsigned(VEC_DATA_TYPE(DATA_TYPE, 16) a, VEC_DATA_TYPE(DATA_TYPE, 16) b)
59{
60 float16 angle_deg_f32 = atan2pi(convert_float16(b), convert_float16(a)) * (float16)180.0f;
61 angle_deg_f32 = select(angle_deg_f32, (float16)180.0f + angle_deg_f32, angle_deg_f32 < (float16)0.0f);
62 return convert_uchar16(angle_deg_f32);
63}
64
65/** Calculates signed phase between two inputs.
66 *
67 * @param[in] a First input. Supported data types: S16, S32
68 * @param[in] b Second input. Supported data types: S16, S32
69 *
70 * @return Signed phase mapped in the interval [0, 256). Supported data types: U8
71 */
72inline uchar16 phase_signed(VEC_DATA_TYPE(DATA_TYPE, 16) a, VEC_DATA_TYPE(DATA_TYPE, 16) b)
73{
74 float16 arct = atan2pi(convert_float16(b), convert_float16(a));
75 arct = select(arct, arct + 2, arct < 0.0f);
76
Georgios Pinitasc000fb82017-11-29 13:50:15 +000077 return convert_uchar16(convert_int16(mad(arct, 128, 0.5f)) & (int16)0xFFu);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078}
79
80#if(1 == MAGNITUDE)
81#define MAGNITUDE_OP(x, y) magnitude_l1((x), (y))
82#elif(2 == MAGNITUDE)
83#define MAGNITUDE_OP(x, y) magnitude_l2(convert_int16(x), convert_int16(y))
Anthony Barbierac69aa12017-07-03 17:39:37 +010084#else /* MAGNITUDE */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010085#define MAGNITUDE_OP(x, y)
Anthony Barbierac69aa12017-07-03 17:39:37 +010086#endif /* MAGNITUDE */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087
88#if(1 == PHASE)
89#define PHASE_OP(x, y) phase_unsigned((x), (y))
90#elif(2 == PHASE)
91#define PHASE_OP(x, y) phase_signed((x), (y))
Anthony Barbierac69aa12017-07-03 17:39:37 +010092#else /* PHASE */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093#define PHASE_OP(x, y)
Anthony Barbierac69aa12017-07-03 17:39:37 +010094#endif /* PHASE */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095
96/** Calculate the magnitude and phase of given the gradients of an image.
97 *
98 * @note Magnitude calculation supported: L1 normalization(type = 1) and L2 normalization(type = 2).
99 * @note Phase calculation supported: Unsigned(type = 1) [0,128] and Signed(type = 2) [0,256).
100 *
101 * @attention To enable phase calculation -DPHASE="phase_calculation_type_id" must be provided at compile time. eg -DPHASE=1
102 * @attention To enable magnitude calculation -DMAGNITUDE="magnitude_calculation_type_id" must be provided at compile time. eg -DMAGNITUDE=1
103 * @attention Datatype of the two inputs is passed at compile time using -DDATA_TYPE. e.g -DDATA_TYPE=short. Supported data_types are: short and int
104 *
105 * @param[in] gx_ptr Pointer to the first source image (gradient X). Supported data types: S16, S32
106 * @param[in] gx_stride_x Stride of the source image in X dimension (in bytes)
107 * @param[in] gx_step_x gx_stride_x * number of elements along X processed per workitem(in bytes)
108 * @param[in] gx_stride_y Stride of the source image in Y dimension (in bytes)
109 * @param[in] gx_step_y gx_stride_y * number of elements along Y processed per workitem(in bytes)
110 * @param[in] gx_offset_first_element_in_bytes The offset of the first element in the source image
111 * @param[in] gy_ptr Pointer to the second source image (gradient Y) . Supported data types: S16, S32
112 * @param[in] gy_stride_x Stride of the destination image in X dimension (in bytes)
113 * @param[in] gy_step_x gy_stride_x * number of elements along X processed per workitem(in bytes)
114 * @param[in] gy_stride_y Stride of the destination image in Y dimension (in bytes)
115 * @param[in] gy_step_y gy_stride_y * number of elements along Y processed per workitem(in bytes)
116 * @param[in] gy_offset_first_element_in_bytes The offset of the first element in the destination image
117 * @param[out] magnitude_ptr Pointer to the magnitude destination image. Supported data types: S16, S32
118 * @param[in] magnitude_stride_x Stride of the source image in X dimension (in bytes)
119 * @param[in] magnitude_step_x magnitude_stride_x * number of elements along X processed per workitem(in bytes)
120 * @param[in] magnitude_stride_y Stride of the source image in Y dimension (in bytes)
121 * @param[in] magnitude_step_y magnitude_stride_y * number of elements along Y processed per workitem(in bytes)
122 * @param[in] magnitude_offset_first_element_in_bytes The offset of the first element in the source image
123 * @param[out] phase_ptr Pointer to the phase destination image. Supported data types: U8
124 * @param[in] phase_stride_x Stride of the destination image in X dimension (in bytes)
125 * @param[in] phase_step_x phase_stride_x * number of elements along X processed per workitem(in bytes)
126 * @param[in] phase_stride_y Stride of the destination image in Y dimension (in bytes)
127 * @param[in] phase_step_y phase_stride_y * number of elements along Y processed per workitem(in bytes)
128 * @param[in] phase_offset_first_element_in_bytes The offset of the first element in the destination image
129 * */
130__kernel void magnitude_phase(
131 IMAGE_DECLARATION(gx),
132 IMAGE_DECLARATION(gy)
133#ifdef MAGNITUDE
134 ,
135 IMAGE_DECLARATION(magnitude)
Anthony Barbierac69aa12017-07-03 17:39:37 +0100136#endif /* MAGNITUDE */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137#ifdef PHASE
138 ,
139 IMAGE_DECLARATION(phase)
Anthony Barbierac69aa12017-07-03 17:39:37 +0100140#endif /* PHASE */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100141)
142{
143 // Get pixels pointer
144 Image gx = CONVERT_TO_IMAGE_STRUCT(gx);
145 Image gy = CONVERT_TO_IMAGE_STRUCT(gy);
146
147 // Load values
148 VEC_DATA_TYPE(DATA_TYPE, 16)
149 in_a = vload16(0, (__global DATA_TYPE *)gx.ptr);
150 VEC_DATA_TYPE(DATA_TYPE, 16)
151 in_b = vload16(0, (__global DATA_TYPE *)gy.ptr);
152
153 // Calculate and store the results
154#ifdef MAGNITUDE
155 Image magnitude = CONVERT_TO_IMAGE_STRUCT(magnitude);
156 vstore16(MAGNITUDE_OP(in_a, in_b), 0, (__global DATA_TYPE *)magnitude.ptr);
Anthony Barbierac69aa12017-07-03 17:39:37 +0100157#endif /* MAGNITUDE */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158#ifdef PHASE
159 Image phase = CONVERT_TO_IMAGE_STRUCT(phase);
160 vstore16(PHASE_OP(in_a, in_b), 0, phase.ptr);
Anthony Barbierac69aa12017-07-03 17:39:37 +0100161#endif /* PHASE */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100162}