blob: 3e557c0550b9ec1046e025c883a237960a497ac7 [file] [log] [blame]
Michalis Spyroue9362622018-11-23 17:41:37 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 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)
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 Spyroue9362622018-11-23 17:41:37 +000047
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 Spyrou58307942020-01-17 16:36:46 +000051 * @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 Spyroue9362622018-11-23 17:41:37 +000057 * @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 Spyrou58307942020-01-17 16:36:46 +000060 * @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 Spyroue9362622018-11-23 17:41:37 +000065 * @param[in] out_offset_first_element_in_bytes Offset of the first element in the destination image
66 */
67__kernel void elementwise_unary(
Michalis Spyrou58307942020-01-17 16:36:46 +000068 TENSOR3D_DECLARATION(in),
69 TENSOR3D_DECLARATION(out))
Michalis Spyroue9362622018-11-23 17:41:37 +000070{
Michalis Spyrou58307942020-01-17 16:36:46 +000071 Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(in);
72 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(out);
Michalis Spyroue9362622018-11-23 17:41:37 +000073
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 Spyrou7bb6fb82018-12-07 11:24:36 +000087 *((__global DATA_TYPE *)(out.ptr)) = (DATA_TYPE)(OPERATION(*((__global DATA_TYPE *)in.ptr)));
Michalis Spyroue9362622018-11-23 17:41:37 +000088#endif // defined(VEC_SIZE) && defined(LAST_ACCESSED_X)
89}
Michalis Spyrou7bb6fb82018-12-07 11:24:36 +000090#endif // defined(DATA_TYPE) && defined(OPERATION)