blob: d2d9d97d333791a9255b624e02964672c0e56b35 [file] [log] [blame]
Michalis Spyroue9362622018-11-23 17:41:37 +00001/*
Teresa Charlin5bf44122021-01-18 10:28:12 +00002 * Copyright (c) 2018-2021 Arm Limited.
Michalis Spyroue9362622018-11-23 17:41:37 +00003 *
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 Spyrou7bb6fb82018-12-07 11:24:36 +000027#if defined(DATA_TYPE) && defined(OPERATION)
28
giuros01f24411f2019-05-16 11:47:13 +010029// Calculate exponential
30#define exp_op(input) exp(input)
Usama Arifeb312ef2019-05-13 17:45:54 +010031// Calculate reverse square root
giuros01f24411f2019-05-16 11:47:13 +010032#define rsqrt_op(input) rsqrt(input)
Usama Arifeb312ef2019-05-13 17:45:54 +010033// Calculate negative
giuros01f24411f2019-05-16 11:47:13 +010034#define neg_op(input) (-input)
Michalis Spyrou0af44182019-05-17 14:04:47 +010035// Calculate sine
giuros01f24411f2019-05-16 11:47:13 +010036#define sin_op(input) sin(input)
37// Calculate abs for floating point values
38#define fabs_op(input) fabs(input)
Usama Arifac33d7e2019-05-20 14:21:40 +010039// Calculate natural_log
40#define natural_log_op(input) log(input)
Usama Arif6a4d5422019-05-24 14:53:59 +010041// Calculate round (Cannot use round function as it rounds halfway cases away from zero).
42#if defined(VEC_SIZE)
Teresa Charlin5bf44122021-01-18 10:28:12 +000043#define VEC_TYPE VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
44#define round_op(input) CONVERT(CONVERT_SAT_ROUND(input, VEC_DATA_TYPE(int, VEC_SIZE), rte), VEC_TYPE)
45#define logical_not_op(input) CONVERT(CONVERT(!input, VEC_TYPE) & ((VEC_TYPE)0x1), VEC_TYPE)
Sang-Hoon Park75eea332020-11-13 13:44:13 +000046#else // defined(VEC_SIZE)
Usama Arif6a4d5422019-05-24 14:53:59 +010047#define round_op(input) CONVERT(CONVERT_SAT_ROUND(input, int, rte), DATA_TYPE)
Sang-Hoon Park75eea332020-11-13 13:44:13 +000048#define logical_not_op(input) ((!input) & 0x1)
Giorgio Arenabaeef3d2020-11-24 11:42:44 +000049#endif // defined(VEC_SIZE)
Michalis Spyroue9362622018-11-23 17:41:37 +000050
51/** Applies element wise unary operator in a tensor.
52 *
53 * @param[in] in_ptr Pointer to the source image. Supported data types: F16/32.
Michalis Spyrou58307942020-01-17 16:36:46 +000054 * @param[in] in_stride_x Stride of the source tensor in X dimension (in bytes)
55 * @param[in] in_step_x in_stride_x * number of elements along X processed per workitem(in bytes)
56 * @param[in] in_stride_y Stride of the source tensor in Y dimension (in bytes)
57 * @param[in] in_step_y in_stride_y * number of elements along Y processed per workitem(in bytes)
58 * @param[in] in_stride_z Stride of the source tensor in Z dimension (in bytes)
59 * @param[in] in_step_z in_stride_z * number of elements along Z processed per workitem(in bytes)
Michalis Spyroue9362622018-11-23 17:41:37 +000060 * @param[in] in_offset_first_element_in_bytes Offset of the first element in the source image
61 * @param[out] out_ptr Pointer to the destination image. Supported data types: F16/32.
62 * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes)
Michalis Spyrou58307942020-01-17 16:36:46 +000063 * @param[in] out_step_x out_stride_x * number of elements along X processed per workitem(in bytes)
64 * @param[in] out_step_y Stride of the destination tensor in Y dimension (in bytes)
65 * @param[in] out_step_y out_stride_y * number of elements along Y processed per workitem(in bytes)
66 * @param[in] out_stride_z Stride of the destination tensor in Z dimension (in bytes)
67 * @param[in] out_step_z out_stride_z * number of elements along Z processed per workitem(in bytes)
Michalis Spyroue9362622018-11-23 17:41:37 +000068 * @param[in] out_offset_first_element_in_bytes Offset of the first element in the destination image
69 */
70__kernel void elementwise_unary(
Michalis Spyrou58307942020-01-17 16:36:46 +000071 TENSOR3D_DECLARATION(in),
72 TENSOR3D_DECLARATION(out))
Michalis Spyroue9362622018-11-23 17:41:37 +000073{
Michalis Spyrou58307942020-01-17 16:36:46 +000074 Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(in);
75 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(out);
Michalis Spyroue9362622018-11-23 17:41:37 +000076
77#if defined(VEC_SIZE) && defined(LAST_ACCESSED_X)
78 // Check if access on width gets out of bounds
79 // If it does shift access vector to access elements within bounds
80 const int xi = (int)(get_global_id(0) * VEC_SIZE);
81 in.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * in_stride_x;
82 out.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * out_stride_x;
83
84 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
85 data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)in.ptr);
86
87 VSTORE(VEC_SIZE)
88 (OPERATION(data), 0, (__global DATA_TYPE *)out.ptr);
89#else // !defined(VEC_SIZE) || !defined(LAST_ACCESSED_X)
Michalis Spyrou7bb6fb82018-12-07 11:24:36 +000090 *((__global DATA_TYPE *)(out.ptr)) = (DATA_TYPE)(OPERATION(*((__global DATA_TYPE *)in.ptr)));
Michalis Spyroue9362622018-11-23 17:41:37 +000091#endif // defined(VEC_SIZE) && defined(LAST_ACCESSED_X)
92}
Michalis Spyrou7bb6fb82018-12-07 11:24:36 +000093#endif // defined(DATA_TYPE) && defined(OPERATION)