blob: 03414105e61d98b3ac41959c57c61de6282e3d5b [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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
Michele Di Giorgio72818342017-07-13 11:03:35 +010026#if defined(FIXED_POINT_POSITION)
27#include "fixed_point.h"
28#endif /* FIXED_POINT_POSITION */
29
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#ifdef SATURATE
31#define ADD(x, y) add_sat((x), (y))
32#define SUB(x, y) sub_sat((x), (y))
Anthony Barbierac69aa12017-07-03 17:39:37 +010033#else /* SATURATE */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#define ADD(x, y) (x) + (y)
35#define SUB(x, y) (x) - (y)
Anthony Barbierac69aa12017-07-03 17:39:37 +010036#endif /* SATURATE */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
38/** This function add two images.
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 *
Michele Di Giorgio72818342017-07-13 11:03:35 +010044 * @param[in] in1_ptr Pointer to the source image. Supported data types: U8/QS8/QS16/S16/F16/F32
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045 * @param[in] in1_stride_x Stride of the source image 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 image 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_offset_first_element_in_bytes The offset of the first element in the source image
Michele Di Giorgio72818342017-07-13 11:03:35 +010050 * @param[in] in2_ptr Pointer to the source image. Supported data types: U8/QS8 (only if @p in1_ptr is QS8), QS16 (only if @p in1_ptr is QS16), S16/F16/F32
Anthony Barbier6ff3b192017-09-04 18:44:23 +010051 * @param[in] in2_stride_x Stride of the source image in X dimension (in bytes)
52 * @param[in] in2_step_x in2_stride_x * number of elements along X processed per workitem(in bytes)
53 * @param[in] in2_stride_y Stride of the source image in Y dimension (in bytes)
54 * @param[in] in2_step_y in2_stride_y * number of elements along Y processed per workitem(in bytes)
55 * @param[in] in2_offset_first_element_in_bytes The offset of the first element in the source image
Michele Di Giorgio72818342017-07-13 11:03:35 +010056 * @param[out] out_ptr Pointer to the destination image. 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
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057 * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes)
58 * @param[in] out_step_x out_stride_x * number of elements along X processed per workitem(in bytes)
59 * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes)
60 * @param[in] out_step_y out_stride_y * number of elements along Y processed per workitem(in bytes)
61 * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination image
62 */
63__kernel void arithmetic_add(
64 IMAGE_DECLARATION(in1),
65 IMAGE_DECLARATION(in2),
66 IMAGE_DECLARATION(out))
67{
68 // Get pixels pointer
69 Image in1 = CONVERT_TO_IMAGE_STRUCT(in1);
70 Image in2 = CONVERT_TO_IMAGE_STRUCT(in2);
71 Image out = CONVERT_TO_IMAGE_STRUCT(out);
72
73 // Load values
74 VEC_DATA_TYPE(DATA_TYPE_OUT, 16)
75 in_a = CONVERT(vload16(0, (__global DATA_TYPE_IN1 *)in1.ptr), VEC_DATA_TYPE(DATA_TYPE_OUT, 16));
76 VEC_DATA_TYPE(DATA_TYPE_OUT, 16)
77 in_b = CONVERT(vload16(0, (__global DATA_TYPE_IN2 *)in2.ptr), VEC_DATA_TYPE(DATA_TYPE_OUT, 16));
78
79 // Calculate and store result
80 vstore16(ADD(in_a, in_b), 0, (__global DATA_TYPE_OUT *)out.ptr);
81}
82
83/** This function subtracts one image from another.
84 *
85 * @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:
86 * e.g. -DDATA_TYPE_IN1=uchar -DDATA_TYPE_IN2=uchar -DDATA_TYPE_OUT=short
87 * @attention To perform saturating operation -DSATURATE has to be passed to the compiler otherwise wrapping policy will be used.
88 *
89 * @param[in] in1_ptr Pointer to the source image. Supported data types: U8, S16
90 * @param[in] in1_stride_x Stride of the source image in X dimension (in bytes)
91 * @param[in] in1_step_x in1_stride_x * number of elements along X processed per workitem(in bytes)
92 * @param[in] in1_stride_y Stride of the source image in Y dimension (in bytes)
93 * @param[in] in1_step_y in1_stride_y * number of elements along Y processed per workitem(in bytes)
94 * @param[in] in1_offset_first_element_in_bytes The offset of the first element in the source image
95 * @param[in] in2_ptr Pointer to the source image. Supported data types: U8, S16
96 * @param[in] in2_stride_x Stride of the source image in X dimension (in bytes)
97 * @param[in] in2_step_x in2_stride_x * number of elements along X processed per workitem(in bytes)
98 * @param[in] in2_stride_y Stride of the source image in Y dimension (in bytes)
99 * @param[in] in2_step_y in2_stride_y * number of elements along Y processed per workitem(in bytes)
100 * @param[in] in2_offset_first_element_in_bytes The offset of the first element in the source image
101 * @param[out] out_ptr Pointer to the destination image. Supported data types: U8, S16
102 * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes)
103 * @param[in] out_step_x out_stride_x * number of elements along X processed per workitem(in bytes)
104 * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes)
105 * @param[in] out_step_y out_stride_y * number of elements along Y processed per workitem(in bytes)
106 * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination image
107 */
108__kernel void arithmetic_sub(
109 IMAGE_DECLARATION(in1),
110 IMAGE_DECLARATION(in2),
111 IMAGE_DECLARATION(out))
112{
113 // Get pixels pointer
114 Image in1 = CONVERT_TO_IMAGE_STRUCT(in1);
115 Image in2 = CONVERT_TO_IMAGE_STRUCT(in2);
116 Image out = CONVERT_TO_IMAGE_STRUCT(out);
117
118 // Load values
119 VEC_DATA_TYPE(DATA_TYPE_OUT, 16)
120 in_a = CONVERT(vload16(0, (__global DATA_TYPE_IN1 *)in1.ptr), VEC_DATA_TYPE(DATA_TYPE_OUT, 16));
121 VEC_DATA_TYPE(DATA_TYPE_OUT, 16)
122 in_b = CONVERT(vload16(0, (__global DATA_TYPE_IN2 *)in2.ptr), VEC_DATA_TYPE(DATA_TYPE_OUT, 16));
123
124 // Calculate and store result
125 vstore16(SUB(in_a, in_b), 0, (__global DATA_TYPE_OUT *)out.ptr);
126}