arm_compute v18.02
[platform/upstream/armcl.git] / src / core / CL / cl_kernels / arithmetic_op.cl
1 /*
2  * Copyright (c) 2016, 2017 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
26 #if defined(FIXED_POINT_POSITION)
27 #include "fixed_point.h"
28 #endif /* FIXED_POINT_POSITION */
29
30 #ifdef SATURATE
31 #define ADD(x, y) add_sat((x), (y))
32 #define SUB(x, y) sub_sat((x), (y))
33 #else /* SATURATE */
34 #define ADD(x, y) (x) + (y)
35 #define SUB(x, y) (x) - (y)
36 #endif /* SATURATE */
37
38 /** This function adds two tensors.
39  *
40  * @attention The input and output data_types need to be passed at compile time using -DDATA_TYPE_IN1, -DDATA_TYPE_IN2 and -DDATA_TYPE_OUT:
41  * e.g. -DDATA_TYPE_IN1=uchar -DDATA_TYPE_IN2=uchar -DDATA_TYPE_OUT=short
42  * @attention To perform saturating operation -DSATURATE has to be passed to the compiler otherwise wrapping policy will be used.
43  *
44  * @param[in]  in1_ptr                           Pointer to the source tensor. Supported data types: U8/QS8/QS16/S16/F16/F32
45  * @param[in]  in1_stride_x                      Stride of the source tensor in X dimension (in bytes)
46  * @param[in]  in1_step_x                        in1_stride_x * number of elements along X processed per workitem(in bytes)
47  * @param[in]  in1_stride_y                      Stride of the source tensor in Y dimension (in bytes)
48  * @param[in]  in1_step_y                        in1_stride_y * number of elements along Y processed per workitem(in bytes)
49  * @param[in]  in1_stride_z                      Stride of the source tensor in Z dimension (in bytes)
50  * @param[in]  in1_step_z                        in1_stride_z * number of elements along Z processed per workitem(in bytes)
51  * @param[in]  in1_offset_first_element_in_bytes The offset of the first element in the source tensor
52  * @param[in]  in2_ptr                           Pointer to the source tensor. Supported data types: U8/QS8 (only if @p in1_ptr is QS8), QS16 (only if @p in1_ptr is QS16), S16/F16/F32
53  * @param[in]  in2_stride_x                      Stride of the source tensor in X dimension (in bytes)
54  * @param[in]  in2_step_x                        in2_stride_x * number of elements along X processed per workitem(in bytes)
55  * @param[in]  in2_stride_y                      Stride of the source tensor in Y dimension (in bytes)
56  * @param[in]  in2_step_y                        in2_stride_y * number of elements along Y processed per workitem(in bytes)
57  * @param[in]  in2_stride_z                      Stride of the source tensor in Z dimension (in bytes)
58  * @param[in]  in2_step_z                        in2_stride_z * number of elements along Z processed per workitem(in bytes)
59  * @param[in]  in2_offset_first_element_in_bytes The offset of the first element in the source tensor
60  * @param[out] out_ptr                           Pointer to the destination tensor. Supported data types: U8 (only if both inputs are U8), QS8 (only if both inputs are QS8), QS16 (only if both inputs are QS16), S16/F16/F32
61  * @param[in]  out_stride_x                      Stride of the destination tensor in X dimension (in bytes)
62  * @param[in]  out_step_x                        out_stride_x * number of elements along X processed per workitem(in bytes)
63  * @param[in]  out_stride_y                      Stride of the destination tensor in Y dimension (in bytes)
64  * @param[in]  out_step_y                        out_stride_y * number of elements along Y processed per workitem(in bytes)
65  * @param[in]  out_stride_z                      Stride of the source tensor in Z dimension (in bytes)
66  * @param[in]  out_step_z                        out_stride_z * number of elements along Z processed per workitem(in bytes)
67  * @param[in]  out_offset_first_element_in_bytes The offset of the first element in the destination tensor
68  */
69 __kernel void arithmetic_add(
70     TENSOR3D_DECLARATION(in1),
71     TENSOR3D_DECLARATION(in2),
72     TENSOR3D_DECLARATION(out))
73 {
74     // Get pixels pointer
75     Tensor3D in1 = CONVERT_TO_TENSOR3D_STRUCT(in1);
76     Tensor3D in2 = CONVERT_TO_TENSOR3D_STRUCT(in2);
77     Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(out);
78
79     // Load values
80     VEC_DATA_TYPE(DATA_TYPE_OUT, 16)
81     in_a = CONVERT(vload16(0, (__global DATA_TYPE_IN1 *)in1.ptr), VEC_DATA_TYPE(DATA_TYPE_OUT, 16));
82     VEC_DATA_TYPE(DATA_TYPE_OUT, 16)
83     in_b = CONVERT(vload16(0, (__global DATA_TYPE_IN2 *)in2.ptr), VEC_DATA_TYPE(DATA_TYPE_OUT, 16));
84
85     // Calculate and store result
86     vstore16(ADD(in_a, in_b), 0, (__global DATA_TYPE_OUT *)out.ptr);
87 }
88
89 /** This function subtracts one tensors from another.
90  *
91  * @attention The input and output data_types need to be passed at compile time using -DDATA_TYPE_IN1, -DDATA_TYPE_IN2 and -DDATA_TYPE_OUT:
92  * e.g. -DDATA_TYPE_IN1=uchar -DDATA_TYPE_IN2=uchar -DDATA_TYPE_OUT=short
93  * @attention To perform saturating operation -DSATURATE has to be passed to the compiler otherwise wrapping policy will be used.
94  *
95  * @param[in]  in1_ptr                           Pointer to the source tensor. Supported data types: U8, S16
96  * @param[in]  in1_stride_x                      Stride of the source tensor in X dimension (in bytes)
97  * @param[in]  in1_step_x                        in1_stride_x * number of elements along X processed per workitem(in bytes)
98  * @param[in]  in1_stride_y                      Stride of the source tensor in Y dimension (in bytes)
99  * @param[in]  in1_step_y                        in1_stride_y * number of elements along Y processed per workitem(in bytes)
100  * @param[in]  in1_stride_z                      Stride of the source tensor in Z dimension (in bytes)
101  * @param[in]  in1_step_z                        in1_stride_z * number of elements along Z processed per workitem(in bytes)
102  * @param[in]  in1_offset_first_element_in_bytes The offset of the first element in the source tensor
103  * @param[in]  in2_ptr                           Pointer to the source tensor. Supported data types: U8, S16
104  * @param[in]  in2_stride_x                      Stride of the source tensor in X dimension (in bytes)
105  * @param[in]  in2_step_x                        in2_stride_x * number of elements along X processed per workitem(in bytes)
106  * @param[in]  in2_stride_y                      Stride of the source tensor in Y dimension (in bytes)
107  * @param[in]  in2_step_y                        in2_stride_y * number of elements along Y processed per workitem(in bytes)
108  * @param[in]  in2_stride_z                      Stride of the source tensor in Z dimension (in bytes)
109  * @param[in]  in2_step_z                        in2_stride_z * number of elements along Z processed per workitem(in bytes)
110  * @param[in]  in2_offset_first_element_in_bytes The offset of the first element in the source tensor
111  * @param[out] out_ptr                           Pointer to the destination tensor. Supported data types: U8, S16
112  * @param[in]  out_stride_x                      Stride of the destination tensor in X dimension (in bytes)
113  * @param[in]  out_step_x                        out_stride_x * number of elements along X processed per workitem(in bytes)
114  * @param[in]  out_stride_y                      Stride of the destination tensor in Y dimension (in bytes)
115  * @param[in]  out_step_y                        out_stride_y * number of elements along Y processed per workitem(in bytes)
116  * @param[in]  out_stride_z                      Stride of the source tensor in Z dimension (in bytes)
117  * @param[in]  out_step_z                        out_stride_z * number of elements along Z processed per workitem(in bytes)
118  * @param[in]  out_offset_first_element_in_bytes The offset of the first element in the destination tensor
119  */
120 __kernel void arithmetic_sub(
121     TENSOR3D_DECLARATION(in1),
122     TENSOR3D_DECLARATION(in2),
123     TENSOR3D_DECLARATION(out))
124 {
125     // Get pixels pointer
126     Tensor3D in1 = CONVERT_TO_TENSOR3D_STRUCT(in1);
127     Tensor3D in2 = CONVERT_TO_TENSOR3D_STRUCT(in2);
128     Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(out);
129
130     // Load values
131     VEC_DATA_TYPE(DATA_TYPE_OUT, 16)
132     in_a = CONVERT(vload16(0, (__global DATA_TYPE_IN1 *)in1.ptr), VEC_DATA_TYPE(DATA_TYPE_OUT, 16));
133     VEC_DATA_TYPE(DATA_TYPE_OUT, 16)
134     in_b = CONVERT(vload16(0, (__global DATA_TYPE_IN2 *)in2.ptr), VEC_DATA_TYPE(DATA_TYPE_OUT, 16));
135
136     // Calculate and store result
137     vstore16(SUB(in_a, in_b), 0, (__global DATA_TYPE_OUT *)out.ptr);
138 }