1/* 2 * Copyright (c) 2018-2021 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#include "helpers.h" 25 26#if defined(DATA_TYPE) && defined(OPERATION) 27 28// Calculate exponential 29#define exp_op(input) exp(input) 30// Calculate reverse square root 31#define rsqrt_op(input) rsqrt(input) 32// Calculate negative 33#define neg_op(input) (-input) 34// Calculate sine 35#define sin_op(input) sin(input) 36// Calculate abs for floating point values 37#define fabs_op(input) fabs(input) 38// Calculate natural_log 39#define natural_log_op(input) log(input) 40// Calculate round (Cannot use round function as it rounds halfway cases away from zero). 41#if defined(VEC_SIZE) 42#define VEC_TYPE VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) 43#define round_op(input) CONVERT(CONVERT_SAT_ROUND(input, VEC_DATA_TYPE(int, VEC_SIZE), rte), VEC_TYPE) 44#define logical_not_op(input) CONVERT(CONVERT(!input, VEC_TYPE) & ((VEC_TYPE)0x1), VEC_TYPE) 45#else // defined(VEC_SIZE) 46#define round_op(input) CONVERT(CONVERT_SAT_ROUND(input, int, rte), DATA_TYPE) 47#define logical_not_op(input) ((!input) & 0x1) 48#endif // defined(VEC_SIZE) 49 50/** Applies element wise unary operator in a tensor. 51 * 52 * @param[in] in_ptr Pointer to the source image. Supported data types: F16/32. 53 * @param[in] in_stride_x Stride of the source tensor in X dimension (in bytes) 54 * @param[in] in_step_x in_stride_x * number of elements along X processed per workitem(in bytes) 55 * @param[in] in_stride_y Stride of the source tensor in Y dimension (in bytes) 56 * @param[in] in_step_y in_stride_y * number of elements along Y processed per workitem(in bytes) 57 * @param[in] in_stride_z Stride of the source tensor in Z dimension (in bytes) 58 * @param[in] in_step_z in_stride_z * number of elements along Z processed per workitem(in bytes) 59 * @param[in] in_offset_first_element_in_bytes Offset of the first element in the source image 60 * @param[out] out_ptr Pointer to the destination image. Supported data types: F16/32. 61 * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes) 62 * @param[in] out_step_x out_stride_x * number of elements along X processed per workitem(in bytes) 63 * @param[in] out_step_y Stride of the destination tensor in Y dimension (in bytes) 64 * @param[in] out_step_y out_stride_y * number of elements along Y processed per workitem(in bytes) 65 * @param[in] out_stride_z Stride of the destination tensor in Z dimension (in bytes) 66 * @param[in] out_step_z out_stride_z * number of elements along Z processed per workitem(in bytes) 67 * @param[in] out_offset_first_element_in_bytes Offset of the first element in the destination image 68 */ 69__kernel void elementwise_unary( 70 TENSOR3D_DECLARATION(in), 71 TENSOR3D_DECLARATION(out)) 72{ 73 Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(in); 74 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(out); 75 76#if defined(VEC_SIZE) && defined(LAST_ACCESSED_X) 77 // Check if access on width gets out of bounds 78 // If it does shift access vector to access elements within bounds 79 const int xi = (int)(get_global_id(0) * VEC_SIZE); 80 in.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * in_stride_x; 81 out.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * out_stride_x; 82 83 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) 84 data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)in.ptr); 85 86 VSTORE(VEC_SIZE) 87 (OPERATION(data), 0, (__global DATA_TYPE *)out.ptr); 88#else // !defined(VEC_SIZE) || !defined(LAST_ACCESSED_X) 89 *((__global DATA_TYPE *)(out.ptr)) = (DATA_TYPE)(OPERATION(*((__global DATA_TYPE *)in.ptr))); 90#endif // defined(VEC_SIZE) && defined(LAST_ACCESSED_X) 91} 92#endif // defined(DATA_TYPE) && defined(OPERATION) 93