2 * Copyright (c) 2016, 2017 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
26 /** Calculates L1 normalization between two inputs.
28 * @param[in] a First input. Supported data types: S16, S32
29 * @param[in] b Second input. Supported data types: S16, S32
31 * @return L1 normalization magnitude result. Supported data types: S16, S32
33 inline VEC_DATA_TYPE(DATA_TYPE, 16) magnitude_l1(VEC_DATA_TYPE(DATA_TYPE, 16) a, VEC_DATA_TYPE(DATA_TYPE, 16) b)
35 return CONVERT_SAT(add_sat(abs(a), abs(b)), VEC_DATA_TYPE(DATA_TYPE, 16));
38 /** Calculates L2 normalization between two inputs.
40 * @param[in] a First input. Supported data types: S16, S32
41 * @param[in] b Second input. Supported data types: S16, S32
43 * @return L2 normalization magnitude result. Supported data types: S16, S32
45 inline VEC_DATA_TYPE(DATA_TYPE, 16) magnitude_l2(int16 a, int16 b)
47 return CONVERT_SAT((sqrt(convert_float16((convert_uint16(a * a) + convert_uint16(b * b)))) + 0.5f),
48 VEC_DATA_TYPE(DATA_TYPE, 16));
51 /** Calculates unsigned phase between two inputs.
53 * @param[in] a First input. Supported data types: S16, S32
54 * @param[in] b Second input. Supported data types: S16, S32
56 * @return Unsigned phase mapped in the interval [0, 180]. Supported data types: U8
58 inline uchar16 phase_unsigned(VEC_DATA_TYPE(DATA_TYPE, 16) a, VEC_DATA_TYPE(DATA_TYPE, 16) b)
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);
65 /** Calculates signed phase between two inputs.
67 * @param[in] a First input. Supported data types: S16, S32
68 * @param[in] b Second input. Supported data types: S16, S32
70 * @return Signed phase mapped in the interval [0, 256). Supported data types: U8
72 inline uchar16 phase_signed(VEC_DATA_TYPE(DATA_TYPE, 16) a, VEC_DATA_TYPE(DATA_TYPE, 16) b)
74 float16 arct = atan2pi(convert_float16(b), convert_float16(a));
75 arct = select(arct, arct + 2, arct < 0.0f);
77 return convert_uchar16(convert_int16(mad(arct, 128, 0.5f)) & 0xFFu);
81 #define MAGNITUDE_OP(x, y) magnitude_l1((x), (y))
83 #define MAGNITUDE_OP(x, y) magnitude_l2(convert_int16(x), convert_int16(y))
85 #define MAGNITUDE_OP(x, y)
89 #define PHASE_OP(x, y) phase_unsigned((x), (y))
91 #define PHASE_OP(x, y) phase_signed((x), (y))
93 #define PHASE_OP(x, y)
96 /** Calculate the magnitude and phase of given the gradients of an image.
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).
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
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
130 __kernel void magnitude_phase(
131 IMAGE_DECLARATION(gx),
132 IMAGE_DECLARATION(gy)
135 IMAGE_DECLARATION(magnitude)
139 IMAGE_DECLARATION(phase)
143 // Get pixels pointer
144 Image gx = CONVERT_TO_IMAGE_STRUCT(gx);
145 Image gy = CONVERT_TO_IMAGE_STRUCT(gy);
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);
153 // Calculate and store the results
155 Image magnitude = CONVERT_TO_IMAGE_STRUCT(magnitude);
156 vstore16(MAGNITUDE_OP(in_a, in_b), 0, (__global DATA_TYPE *)magnitude.ptr);
159 Image phase = CONVERT_TO_IMAGE_STRUCT(phase);
160 vstore16(PHASE_OP(in_a, in_b), 0, phase.ptr);