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)) |
| 44 | #else // defined(VEC_SIZE |
| 45 | #define round_op(input) CONVERT(CONVERT_SAT_ROUND(input, int, rte), DATA_TYPE) |
| 46 | #endif // defined(VEC_SIZE |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 47 | |
| 48 | /** Applies element wise unary operator in a tensor. |
| 49 | * |
| 50 | * @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] | 51 | * @param[in] in_stride_x Stride of the source tensor in X dimension (in bytes) |
| 52 | * @param[in] in_step_x in_stride_x * number of elements along X processed per workitem(in bytes) |
| 53 | * @param[in] in_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 54 | * @param[in] in_step_y in_stride_y * number of elements along Y processed per workitem(in bytes) |
| 55 | * @param[in] in_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 56 | * @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] | 57 | * @param[in] in_offset_first_element_in_bytes Offset of the first element in the source image |
| 58 | * @param[out] out_ptr Pointer to the destination image. Supported data types: F16/32. |
| 59 | * @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] | 60 | * @param[in] out_step_x out_stride_x * number of elements along X processed per workitem(in bytes) |
| 61 | * @param[in] out_step_y Stride of the destination tensor in Y dimension (in bytes) |
| 62 | * @param[in] out_step_y out_stride_y * number of elements along Y processed per workitem(in bytes) |
| 63 | * @param[in] out_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 64 | * @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] | 65 | * @param[in] out_offset_first_element_in_bytes Offset of the first element in the destination image |
| 66 | */ |
| 67 | __kernel void elementwise_unary( |
Michalis Spyrou | 5830794 | 2020-01-17 16:36:46 +0000 | [diff] [blame] | 68 | TENSOR3D_DECLARATION(in), |
| 69 | TENSOR3D_DECLARATION(out)) |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 70 | { |
Michalis Spyrou | 5830794 | 2020-01-17 16:36:46 +0000 | [diff] [blame] | 71 | Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(in); |
| 72 | Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(out); |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 73 | |
| 74 | #if defined(VEC_SIZE) && defined(LAST_ACCESSED_X) |
| 75 | // Check if access on width gets out of bounds |
| 76 | // If it does shift access vector to access elements within bounds |
| 77 | const int xi = (int)(get_global_id(0) * VEC_SIZE); |
| 78 | in.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * in_stride_x; |
| 79 | out.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * out_stride_x; |
| 80 | |
| 81 | VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 82 | data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)in.ptr); |
| 83 | |
| 84 | VSTORE(VEC_SIZE) |
| 85 | (OPERATION(data), 0, (__global DATA_TYPE *)out.ptr); |
| 86 | #else // !defined(VEC_SIZE) || !defined(LAST_ACCESSED_X) |
Michalis Spyrou | 7bb6fb8 | 2018-12-07 11:24:36 +0000 | [diff] [blame] | 87 | *((__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] | 88 | #endif // defined(VEC_SIZE) && defined(LAST_ACCESSED_X) |
| 89 | } |
Michalis Spyrou | 7bb6fb8 | 2018-12-07 11:24:36 +0000 | [diff] [blame] | 90 | #endif // defined(DATA_TYPE) && defined(OPERATION) |