Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2018 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 | #include "warp_helpers.h" |
| 26 | |
| 27 | #if defined(DATA_TYPE) && defined(VEC_SIZE) && defined(OPERATION) |
| 28 | /** Calculate reverse square root |
| 29 | * |
| 30 | * @param[in] input Pointer to the first element. |
| 31 | * |
| 32 | * @return reverse square root |
| 33 | */ |
| 34 | inline VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) inverse_sqrt(const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) input) |
| 35 | { |
| 36 | return rsqrt(input); |
| 37 | } |
| 38 | |
| 39 | /** Calculate exponential |
| 40 | * |
| 41 | * @param[in] input Pointer to the first element. |
| 42 | * |
| 43 | * @return exponential |
| 44 | */ |
| 45 | inline VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) exponential(const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) input) |
| 46 | { |
| 47 | return exp(input); |
| 48 | } |
| 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 image in X dimension (in bytes) |
| 54 | * @param[in] in_step_x in_stride_x * number of elements along X processed per work item (in bytes) |
| 55 | * @param[in] in_offset_first_element_in_bytes Offset of the first element in the source image |
| 56 | * @param[out] out_ptr Pointer to the destination image. Supported data types: F16/32. |
| 57 | * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes) |
| 58 | * @param[in] out_step_y out_stride_y * number of elements along Y processed per work item (in bytes) |
| 59 | * @param[in] out_offset_first_element_in_bytes Offset of the first element in the destination image |
| 60 | */ |
| 61 | __kernel void elementwise_unary( |
| 62 | VECTOR_DECLARATION(in), |
| 63 | VECTOR_DECLARATION(out)) |
| 64 | { |
| 65 | Vector in = CONVERT_TO_VECTOR_STRUCT(in); |
| 66 | Vector out = CONVERT_TO_VECTOR_STRUCT(out); |
| 67 | |
| 68 | #if defined(VEC_SIZE) && defined(LAST_ACCESSED_X) |
| 69 | // Check if access on width gets out of bounds |
| 70 | // If it does shift access vector to access elements within bounds |
| 71 | const int xi = (int)(get_global_id(0) * VEC_SIZE); |
| 72 | in.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * in_stride_x; |
| 73 | out.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * out_stride_x; |
| 74 | |
| 75 | VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 76 | data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)in.ptr); |
| 77 | |
| 78 | VSTORE(VEC_SIZE) |
| 79 | (OPERATION(data), 0, (__global DATA_TYPE *)out.ptr); |
| 80 | #else // !defined(VEC_SIZE) || !defined(LAST_ACCESSED_X) |
| 81 | VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 82 | data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)in.ptr); |
| 83 | VSTORE(VEC_SIZE) |
| 84 | (OPERATION(data), 0, (__global DATA_TYPE *)out.ptr); |
| 85 | #endif // defined(VEC_SIZE) && defined(LAST_ACCESSED_X) |
| 86 | } |
| 87 | #endif // defined(DATA_TYPE) && defined(VEC_SIZE) && defined(OPERATION) |