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 arct = atan2pi(convert_float16(b), convert_float16(a));
61 arct = select(arct, arct + 2, arct < 0.0f);
63 return convert_uchar16(convert_int16(mad(arct, 90, 0.5f)) & 0xFFu);
66 /** Calculates signed phase between two inputs.
68 * @param[in] a First input. Supported data types: S16, S32
69 * @param[in] b Second input. Supported data types: S16, S32
71 * @return Signed phase mapped in the interval [0, 256). Supported data types: U8
73 inline uchar16 phase_signed(VEC_DATA_TYPE(DATA_TYPE, 16) a, VEC_DATA_TYPE(DATA_TYPE, 16) b)
75 float16 arct = atan2pi(convert_float16(b), convert_float16(a));
76 arct = select(arct, arct + 2, arct < 0.0f);
78 return convert_uchar16(convert_int16(mad(arct, 128, 0.5f)) & 0xFFu);
82 #define MAGNITUDE_OP(x, y) magnitude_l1((x), (y))
84 #define MAGNITUDE_OP(x, y) magnitude_l2(convert_int16(x), convert_int16(y))
86 #define MAGNITUDE_OP(x, y)
90 #define PHASE_OP(x, y) phase_unsigned((x), (y))
92 #define PHASE_OP(x, y) phase_signed((x), (y))
94 #define PHASE_OP(x, y)
97 /** Calculate the magnitude and phase of given the gradients of an image.
99 * @note Magnitude calculation supported: L1 normalization(type = 1) and L2 normalization(type = 2).
100 * @note Phase calculation supported: Unsigned(type = 1) [0,128] and Signed(type = 2) [0,256).
102 * @attention To enable phase calculation -DPHASE="phase_calculation_type_id" must be provided at compile time. eg -DPHASE=1
103 * @attention To enable magnitude calculation -DMAGNITUDE="magnitude_calculation_type_id" must be provided at compile time. eg -DMAGNITUDE=1
104 * @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
106 * @param[in] gx_ptr Pointer to the first source image (gradient X). Supported data types: S16, S32
107 * @param[in] gx_stride_x Stride of the source image in X dimension (in bytes)
108 * @param[in] gx_step_x gx_stride_x * number of elements along X processed per workitem(in bytes)
109 * @param[in] gx_stride_y Stride of the source image in Y dimension (in bytes)
110 * @param[in] gx_step_y gx_stride_y * number of elements along Y processed per workitem(in bytes)
111 * @param[in] gx_offset_first_element_in_bytes The offset of the first element in the source image
112 * @param[in] gy_ptr Pointer to the second source image (gradient Y) . Supported data types: S16, S32
113 * @param[in] gy_stride_x Stride of the destination image in X dimension (in bytes)
114 * @param[in] gy_step_x gy_stride_x * number of elements along X processed per workitem(in bytes)
115 * @param[in] gy_stride_y Stride of the destination image in Y dimension (in bytes)
116 * @param[in] gy_step_y gy_stride_y * number of elements along Y processed per workitem(in bytes)
117 * @param[in] gy_offset_first_element_in_bytes The offset of the first element in the destination image
118 * @param[out] magnitude_ptr Pointer to the magnitude destination image. Supported data types: S16, S32
119 * @param[in] magnitude_stride_x Stride of the source image in X dimension (in bytes)
120 * @param[in] magnitude_step_x magnitude_stride_x * number of elements along X processed per workitem(in bytes)
121 * @param[in] magnitude_stride_y Stride of the source image in Y dimension (in bytes)
122 * @param[in] magnitude_step_y magnitude_stride_y * number of elements along Y processed per workitem(in bytes)
123 * @param[in] magnitude_offset_first_element_in_bytes The offset of the first element in the source image
124 * @param[out] phase_ptr Pointer to the phase destination image. Supported data types: U8
125 * @param[in] phase_stride_x Stride of the destination image in X dimension (in bytes)
126 * @param[in] phase_step_x phase_stride_x * number of elements along X processed per workitem(in bytes)
127 * @param[in] phase_stride_y Stride of the destination image in Y dimension (in bytes)
128 * @param[in] phase_step_y phase_stride_y * number of elements along Y processed per workitem(in bytes)
129 * @param[in] phase_offset_first_element_in_bytes The offset of the first element in the destination image
131 __kernel void magnitude_phase(
132 IMAGE_DECLARATION(gx),
133 IMAGE_DECLARATION(gy)
136 IMAGE_DECLARATION(magnitude)
140 IMAGE_DECLARATION(phase)
144 // Get pixels pointer
145 Image gx = CONVERT_TO_IMAGE_STRUCT(gx);
146 Image gy = CONVERT_TO_IMAGE_STRUCT(gy);
149 VEC_DATA_TYPE(DATA_TYPE, 16)
150 in_a = vload16(0, (__global DATA_TYPE *)gx.ptr);
151 VEC_DATA_TYPE(DATA_TYPE, 16)
152 in_b = vload16(0, (__global DATA_TYPE *)gy.ptr);
154 // Calculate and store the results
156 Image magnitude = CONVERT_TO_IMAGE_STRUCT(magnitude);
157 vstore16(MAGNITUDE_OP(in_a, in_b), 0, (__global DATA_TYPE *)magnitude.ptr);
160 Image phase = CONVERT_TO_IMAGE_STRUCT(phase);
161 vstore16(PHASE_OP(in_a, in_b), 0, phase.ptr);