blob: 2650654e4aa4df44c1fc87a21bf042bad4aba890 [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
Usama Arifeb312ef2019-05-13 17:45:54 +010029// Calculate Exponential
30#define exponential(input) exp(input)
31// Calculate reverse square root
32#define inverse_sqrt(input) rsqrt(input)
33// Calculate negative
34#define neg(input) (-input)
Michalis Spyroue9362622018-11-23 17:41:37 +000035
36/** Applies element wise unary operator in a tensor.
37 *
38 * @param[in] in_ptr Pointer to the source image. Supported data types: F16/32.
39 * @param[in] in_stride_x Stride of the source image in X dimension (in bytes)
40 * @param[in] in_step_x in_stride_x * number of elements along X processed per work item (in bytes)
41 * @param[in] in_offset_first_element_in_bytes Offset of the first element in the source image
42 * @param[out] out_ptr Pointer to the destination image. Supported data types: F16/32.
43 * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes)
44 * @param[in] out_step_y out_stride_y * number of elements along Y processed per work item (in bytes)
45 * @param[in] out_offset_first_element_in_bytes Offset of the first element in the destination image
46 */
47__kernel void elementwise_unary(
48 VECTOR_DECLARATION(in),
49 VECTOR_DECLARATION(out))
50{
51 Vector in = CONVERT_TO_VECTOR_STRUCT(in);
52 Vector out = CONVERT_TO_VECTOR_STRUCT(out);
53
54#if defined(VEC_SIZE) && defined(LAST_ACCESSED_X)
55 // Check if access on width gets out of bounds
56 // If it does shift access vector to access elements within bounds
57 const int xi = (int)(get_global_id(0) * VEC_SIZE);
58 in.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * in_stride_x;
59 out.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * out_stride_x;
60
61 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
62 data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)in.ptr);
63
64 VSTORE(VEC_SIZE)
65 (OPERATION(data), 0, (__global DATA_TYPE *)out.ptr);
66#else // !defined(VEC_SIZE) || !defined(LAST_ACCESSED_X)
Michalis Spyrou7bb6fb82018-12-07 11:24:36 +000067 *((__global DATA_TYPE *)(out.ptr)) = (DATA_TYPE)(OPERATION(*((__global DATA_TYPE *)in.ptr)));
Michalis Spyroue9362622018-11-23 17:41:37 +000068#endif // defined(VEC_SIZE) && defined(LAST_ACCESSED_X)
69}
Michalis Spyrou7bb6fb82018-12-07 11:24:36 +000070#endif // defined(DATA_TYPE) && defined(OPERATION)