blob: 866b7ee2b0f87b0a4ede1423849b447872fb84b6 [file] [log] [blame]
Michalis Spyroue9362622018-11-23 17:41:37 +00001/*
Usama Arifeb312ef2019-05-13 17:45:54 +01002 * Copyright (c) 2018-2019 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)
Michalis Spyroue9362622018-11-23 17:41:37 +000041
42/** Applies element wise unary operator in a tensor.
43 *
44 * @param[in] in_ptr Pointer to the source image. Supported data types: F16/32.
45 * @param[in] in_stride_x Stride of the source image in X dimension (in bytes)
46 * @param[in] in_step_x in_stride_x * number of elements along X processed per work item (in bytes)
47 * @param[in] in_offset_first_element_in_bytes Offset of the first element in the source image
48 * @param[out] out_ptr Pointer to the destination image. Supported data types: F16/32.
49 * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes)
50 * @param[in] out_step_y out_stride_y * number of elements along Y processed per work item (in bytes)
51 * @param[in] out_offset_first_element_in_bytes Offset of the first element in the destination image
52 */
53__kernel void elementwise_unary(
54 VECTOR_DECLARATION(in),
55 VECTOR_DECLARATION(out))
56{
57 Vector in = CONVERT_TO_VECTOR_STRUCT(in);
58 Vector out = CONVERT_TO_VECTOR_STRUCT(out);
59
60#if defined(VEC_SIZE) && defined(LAST_ACCESSED_X)
61 // Check if access on width gets out of bounds
62 // If it does shift access vector to access elements within bounds
63 const int xi = (int)(get_global_id(0) * VEC_SIZE);
64 in.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * in_stride_x;
65 out.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * out_stride_x;
66
67 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
68 data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)in.ptr);
69
70 VSTORE(VEC_SIZE)
71 (OPERATION(data), 0, (__global DATA_TYPE *)out.ptr);
72#else // !defined(VEC_SIZE) || !defined(LAST_ACCESSED_X)
Michalis Spyrou7bb6fb82018-12-07 11:24:36 +000073 *((__global DATA_TYPE *)(out.ptr)) = (DATA_TYPE)(OPERATION(*((__global DATA_TYPE *)in.ptr)));
Michalis Spyroue9362622018-11-23 17:41:37 +000074#endif // defined(VEC_SIZE) && defined(LAST_ACCESSED_X)
75}
Michalis Spyrou7bb6fb82018-12-07 11:24:36 +000076#endif // defined(DATA_TYPE) && defined(OPERATION)