Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 Arm Limited. |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 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 | #include "warp_helpers.h" |
| 26 | |
Michalis Spyrou | 7bb6fb8 | 2018-12-07 11:24:36 +0000 | [diff] [blame] | 27 | #if defined(DATA_TYPE) && defined(OPERATION) |
| 28 | |
giuros01 | f24411f | 2019-05-16 11:47:13 +0100 | [diff] [blame] | 29 | // Calculate exponential |
| 30 | #define exp_op(input) exp(input) |
Usama Arif | eb312ef | 2019-05-13 17:45:54 +0100 | [diff] [blame] | 31 | // Calculate reverse square root |
giuros01 | f24411f | 2019-05-16 11:47:13 +0100 | [diff] [blame] | 32 | #define rsqrt_op(input) rsqrt(input) |
Usama Arif | eb312ef | 2019-05-13 17:45:54 +0100 | [diff] [blame] | 33 | // Calculate negative |
giuros01 | f24411f | 2019-05-16 11:47:13 +0100 | [diff] [blame] | 34 | #define neg_op(input) (-input) |
Michalis Spyrou | 0af4418 | 2019-05-17 14:04:47 +0100 | [diff] [blame] | 35 | // Calculate sine |
giuros01 | f24411f | 2019-05-16 11:47:13 +0100 | [diff] [blame] | 36 | #define sin_op(input) sin(input) |
| 37 | // Calculate abs for floating point values |
| 38 | #define fabs_op(input) fabs(input) |
Usama Arif | ac33d7e | 2019-05-20 14:21:40 +0100 | [diff] [blame] | 39 | // Calculate natural_log |
| 40 | #define natural_log_op(input) log(input) |
Usama Arif | 6a4d542 | 2019-05-24 14:53:59 +0100 | [diff] [blame] | 41 | // Calculate round (Cannot use round function as it rounds halfway cases away from zero). |
| 42 | #if defined(VEC_SIZE) |
| 43 | #define round_op(input) CONVERT(CONVERT_SAT_ROUND(input, VEC_DATA_TYPE(int, VEC_SIZE), rte), VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)) |
Giorgio Arena | baeef3d | 2020-11-24 11:42:44 +0000 | [diff] [blame^] | 44 | #define logical_not_op(input) CONVERT((!input) & 0x1, VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)) |
Sang-Hoon Park | 75eea33 | 2020-11-13 13:44:13 +0000 | [diff] [blame] | 45 | #else // defined(VEC_SIZE) |
Usama Arif | 6a4d542 | 2019-05-24 14:53:59 +0100 | [diff] [blame] | 46 | #define round_op(input) CONVERT(CONVERT_SAT_ROUND(input, int, rte), DATA_TYPE) |
Sang-Hoon Park | 75eea33 | 2020-11-13 13:44:13 +0000 | [diff] [blame] | 47 | #define logical_not_op(input) ((!input) & 0x1) |
Giorgio Arena | baeef3d | 2020-11-24 11:42:44 +0000 | [diff] [blame^] | 48 | #endif // defined(VEC_SIZE) |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 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. |
Michalis Spyrou | 5830794 | 2020-01-17 16:36:46 +0000 | [diff] [blame] | 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) |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 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) |
Michalis Spyrou | 5830794 | 2020-01-17 16:36:46 +0000 | [diff] [blame] | 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) |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 67 | * @param[in] out_offset_first_element_in_bytes Offset of the first element in the destination image |
| 68 | */ |
| 69 | __kernel void elementwise_unary( |
Michalis Spyrou | 5830794 | 2020-01-17 16:36:46 +0000 | [diff] [blame] | 70 | TENSOR3D_DECLARATION(in), |
| 71 | TENSOR3D_DECLARATION(out)) |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 72 | { |
Michalis Spyrou | 5830794 | 2020-01-17 16:36:46 +0000 | [diff] [blame] | 73 | Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(in); |
| 74 | Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(out); |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 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) |
Michalis Spyrou | 7bb6fb8 | 2018-12-07 11:24:36 +0000 | [diff] [blame] | 89 | *((__global DATA_TYPE *)(out.ptr)) = (DATA_TYPE)(OPERATION(*((__global DATA_TYPE *)in.ptr))); |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 90 | #endif // defined(VEC_SIZE) && defined(LAST_ACCESSED_X) |
| 91 | } |
Michalis Spyrou | 7bb6fb8 | 2018-12-07 11:24:36 +0000 | [diff] [blame] | 92 | #endif // defined(DATA_TYPE) && defined(OPERATION) |