Michele Di Giorgio | 4622ac1 | 2018-06-27 16:41:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016-2018 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 | #ifdef SATURATE |
| 27 | #define ADD(x, y) add_sat((x), (y)) |
| 28 | #define SUB(x, y) sub_sat((x), (y)) |
| 29 | #else /* SATURATE */ |
| 30 | #define ADD(x, y) (x) + (y) |
| 31 | #define SUB(x, y) (x) - (y) |
| 32 | #endif /* SATURATE */ |
| 33 | |
| 34 | #if defined(OFFSET) |
| 35 | |
| 36 | /** This function adds two tensors. |
| 37 | * |
| 38 | * @attention The quantization offset must be passed at compile time using -DOFFSET, i.e. -DOFFSET=10 |
| 39 | * @attention To perform saturating operation -DSATURATE has to be passed to the compiler otherwise wrapping policy will be used. |
| 40 | * |
| 41 | * @param[in] in1_ptr Pointer to the source tensor. Supported data types: QASYMM8 |
| 42 | * @param[in] in1_stride_x Stride of the source tensor in X dimension (in bytes) |
| 43 | * @param[in] in1_step_x in1_stride_x * number of elements along X processed per workitem(in bytes) |
| 44 | * @param[in] in1_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 45 | * @param[in] in1_step_y in1_stride_y * number of elements along Y processed per workitem(in bytes) |
| 46 | * @param[in] in1_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 47 | * @param[in] in1_step_z in1_stride_z * number of elements along Z processed per workitem(in bytes) |
| 48 | * @param[in] in1_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 49 | * @param[in] in2_ptr Pointer to the source tensor. Supported data types: same as @p in1_ptr |
| 50 | * @param[in] in2_stride_x Stride of the source tensor in X dimension (in bytes) |
| 51 | * @param[in] in2_step_x in2_stride_x * number of elements along X processed per workitem(in bytes) |
| 52 | * @param[in] in2_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 53 | * @param[in] in2_step_y in2_stride_y * number of elements along Y processed per workitem(in bytes) |
| 54 | * @param[in] in2_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 55 | * @param[in] in2_step_z in2_stride_z * number of elements along Z processed per workitem(in bytes) |
| 56 | * @param[in] in2_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 57 | * @param[out] out_ptr Pointer to the destination tensor. Supported data types: same as @p in1_ptr |
| 58 | * @param[in] out_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 59 | * @param[in] out_step_x out_stride_x * number of elements along X processed per workitem(in bytes) |
| 60 | * @param[in] out_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 61 | * @param[in] out_step_y out_stride_y * number of elements along Y processed per workitem(in bytes) |
| 62 | * @param[in] out_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 63 | * @param[in] out_step_z out_stride_z * number of elements along Z processed per workitem(in bytes) |
| 64 | * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 65 | */ |
| 66 | __kernel void arithmetic_add_quantized( |
| 67 | TENSOR3D_DECLARATION(in1), |
| 68 | TENSOR3D_DECLARATION(in2), |
| 69 | TENSOR3D_DECLARATION(out)) |
| 70 | { |
| 71 | // Get pixels pointer |
| 72 | Tensor3D in1 = CONVERT_TO_TENSOR3D_STRUCT(in1); |
| 73 | Tensor3D in2 = CONVERT_TO_TENSOR3D_STRUCT(in2); |
| 74 | Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(out); |
| 75 | |
| 76 | // Load values |
| 77 | const short16 in_a = CONVERT(vload16(0, (__global uchar *)in1.ptr), short16); |
| 78 | const short16 in_b = CONVERT(vload16(0, (__global uchar *)in2.ptr), short16); |
| 79 | const short16 offset = OFFSET; |
| 80 | |
| 81 | // Calculate result |
| 82 | short16 res = ADD(in_a, SUB(in_b, offset)); |
| 83 | |
| 84 | res = max((short16)0, min(res, (short16)255)); |
| 85 | |
| 86 | // Store result |
| 87 | vstore16(CONVERT(res, uchar16), 0, (__global uchar *)out.ptr); |
| 88 | } |
| 89 | #endif /* defined(OFFSET) */ |