Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 Giorgio | ab0a77e | 2017-06-21 15:36:24 +0100 | [diff] [blame] | 26 | #if defined(SATURATE) |
| 27 | #define CONVERT_OP_INT_STR(x, type, size) (convert_##type##size##_sat(x)) |
| 28 | #else // SATURATE |
| 29 | #define CONVERT_OP_INT_STR(x, type, size) (convert_##type##size(x)) |
| 30 | #endif // SATURATE |
| 31 | #define CONVERT_OP_INT(x, type, size) CONVERT_OP_INT_STR(x, type, size) |
| 32 | |
| 33 | #define MUL_OP(x, y, scale, type, size) CONVERT_OP_INT((x) * (y) >> scale, type, size) |
| 34 | |
Georgios Pinitas | bf28a3c | 2018-09-18 14:34:48 +0100 | [diff] [blame] | 35 | #if defined(DATA_TYPE_IN1) && defined(DATA_TYPE_IN2) && defined(DATA_TYPE_RES) && defined(DATA_TYPE_OUT) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | /** Performs a pixelwise multiplication with integer scale of integer inputs. |
| 37 | * |
| 38 | * @attention The inputs and output data types need to be passed at compile time using -DDATA_TYPE_IN1, -DDATA_TYPE_IN2 and -DDATA_TYPE_OUT: |
| 39 | * e.g. -DDATA_TYPE_IN1=uchar -DDATA_TYPE_IN2=ushort -DDATA_TYPE_OUT=short |
| 40 | * @attention The data_type of the intermediate result of the multiplication should passed as well using -DDATA_TYPE_RES. |
| 41 | * e.g. If one of inputs is S16 -DDATA_TYPE_RES=int should be passed else -DDATA_TYPE_RES=short. |
| 42 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 43 | * @param[in] in1_ptr Pointer to the source image. Supported data types: U8/S16 |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | * @param[in] in1_stride_x Stride of the source image in X dimension (in bytes) |
| 45 | * @param[in] in1_step_x in1_stride_x * number of elements along X processed per workitem(in bytes) |
| 46 | * @param[in] in1_stride_y Stride of the source image in Y dimension (in bytes) |
| 47 | * @param[in] in1_step_y in1_stride_y * number of elements along Y processed per workitem(in bytes) |
Anthony Barbier | 9a7182e | 2017-07-11 18:36:40 +0100 | [diff] [blame] | 48 | * @param[in] in1_stride_z Stride of the source image in Y dimension (in bytes) |
| 49 | * @param[in] in1_step_z in1_stride_z * number of elements along Y processed per workitem(in bytes) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 50 | * @param[in] in1_offset_first_element_in_bytes The offset of the first element in the source image |
Michele Di Giorgio | ab0a77e | 2017-06-21 15:36:24 +0100 | [diff] [blame] | 51 | * @param[in] in2_ptr Pointer to the source image. Supported data types: same as @p in1_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 52 | * @param[in] in2_stride_x Stride of the source image in X dimension (in bytes) |
| 53 | * @param[in] in2_step_x in2_stride_x * number of elements along X processed per workitem(in bytes) |
| 54 | * @param[in] in2_stride_y Stride of the source image in Y dimension (in bytes) |
| 55 | * @param[in] in2_step_y in2_stride_y * number of elements along Y processed per workitem(in bytes) |
Anthony Barbier | 9a7182e | 2017-07-11 18:36:40 +0100 | [diff] [blame] | 56 | * @param[in] in2_stride_z Stride of the source image in Y dimension (in bytes) |
| 57 | * @param[in] in2_step_z in2_stride_z * number of elements along Y processed per workitem(in bytes) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 58 | * @param[in] in2_offset_first_element_in_bytes The offset of the first element in the source image |
Michele Di Giorgio | ab0a77e | 2017-06-21 15:36:24 +0100 | [diff] [blame] | 59 | * @param[out] out_ptr Pointer to the destination image. Supported data types: same as @p in1_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 60 | * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes) |
| 61 | * @param[in] out_step_x out_stride_x * number of elements along X processed per workitem(in bytes) |
| 62 | * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes) |
| 63 | * @param[in] out_step_y out_stride_y * number of elements along Y processed per workitem(in bytes) |
Anthony Barbier | 9a7182e | 2017-07-11 18:36:40 +0100 | [diff] [blame] | 64 | * @param[in] out_stride_z Stride of the destination image in Y dimension (in bytes) |
| 65 | * @param[in] out_step_z out_stride_z * number of elements along Y processed per workitem(in bytes) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 66 | * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination image |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 67 | * @param[in] scale Integer scaling factor. Supported data types: S32. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 68 | */ |
| 69 | __kernel void pixelwise_mul_int( |
Anthony Barbier | 9a7182e | 2017-07-11 18:36:40 +0100 | [diff] [blame] | 70 | TENSOR3D_DECLARATION(in1), |
| 71 | TENSOR3D_DECLARATION(in2), |
| 72 | TENSOR3D_DECLARATION(out), |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 73 | const uint scale) |
| 74 | { |
| 75 | // Get pixels pointer |
Anthony Barbier | 9a7182e | 2017-07-11 18:36:40 +0100 | [diff] [blame] | 76 | Tensor3D in1 = CONVERT_TO_TENSOR3D_STRUCT(in1); |
| 77 | Tensor3D in2 = CONVERT_TO_TENSOR3D_STRUCT(in2); |
| 78 | Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(out); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 79 | |
| 80 | // Load data |
| 81 | VEC_DATA_TYPE(DATA_TYPE_RES, 16) |
| 82 | in1_data = CONVERT(vload16(0, (__global DATA_TYPE_IN1 *)in1.ptr), VEC_DATA_TYPE(DATA_TYPE_RES, 16)); |
| 83 | VEC_DATA_TYPE(DATA_TYPE_RES, 16) |
| 84 | in2_data = CONVERT(vload16(0, (__global DATA_TYPE_IN2 *)in2.ptr), VEC_DATA_TYPE(DATA_TYPE_RES, 16)); |
| 85 | |
| 86 | // Perform multiplication and store result |
Michele Di Giorgio | ab0a77e | 2017-06-21 15:36:24 +0100 | [diff] [blame] | 87 | vstore16(MUL_OP(in1_data, in2_data, scale, DATA_TYPE_OUT, 16), 0, (__global DATA_TYPE_OUT *)out.ptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 88 | } |
Georgios Pinitas | bf28a3c | 2018-09-18 14:34:48 +0100 | [diff] [blame] | 89 | #endif /* defined(DATA_TYPE_IN1) && defined(DATA_TYPE_IN2) && defined(DATA_TYPE_RES) && defined(DATA_TYPE_OUT) */ |
| 90 | |
| 91 | #if defined(OFFSET_IN1) && defined(OFFSET_IN2) && defined(OFFSET_OUT) && defined(SCALE_IN1) && defined(SCALE_IN2) && defined(SCALE_OUT) |
| 92 | /** Performs a pixelwise multiplication with float scale of quantized inputs. |
| 93 | * |
| 94 | * @note The quantization offset of the first operand must be passed at compile time using -DOFFSET_IN1, e.g. -DOFFSET_IN1=10 |
| 95 | * @note The quantization offset of the second operand must be passed at compile time using -DOFFSET_IN2, e.g. -DOFFSET_IN2=10 |
| 96 | * @note The quantization offset of the output must be passed at compile time using -DOFFSET_OUT, e.g. -DOFFSET_OUT=10 |
| 97 | * @note The quantization scale of the first operand must be passed at compile time using -DSCALE_IN1, e.g. -DSCALE_IN1=10 |
| 98 | * @note The quantization scale of the second operand must be passed at compile time using -DSCALE_IN2, e.g. -DSCALE_IN2=10 |
| 99 | * @note The quantization scale of the output must be passed at compile time using -DSCALE_OUT, e.g. -DSCALE_OUT=10 |
| 100 | * @note To perform saturating operation -DSATURATE has to be passed to the compiler otherwise wrapping policy will be used. |
| 101 | * |
| 102 | * @param[in] in1_ptr Pointer to the source image. Supported data types: U8, S16, F16, F32 |
| 103 | * @param[in] in1_stride_x Stride of the source image in X dimension (in bytes) |
| 104 | * @param[in] in1_step_x in1_stride_x * number of elements along X processed per workitem(in bytes) |
| 105 | * @param[in] in1_stride_y Stride of the source image in Y dimension (in bytes) |
| 106 | * @param[in] in1_step_y in1_stride_y * number of elements along Y processed per workitem(in bytes) |
| 107 | * @param[in] in1_stride_z Stride of the source image in Y dimension (in bytes) |
| 108 | * @param[in] in1_step_z in1_stride_z * number of elements along Y processed per workitem(in bytes) |
| 109 | * @param[in] in1_offset_first_element_in_bytes The offset of the first element in the source image |
| 110 | * @param[in] in2_ptr Pointer to the source image. Supported data types: U8, S16, F16, F32 |
| 111 | * @param[in] in2_stride_x Stride of the source image in X dimension (in bytes) |
| 112 | * @param[in] in2_step_x in2_stride_x * number of elements along X processed per workitem(in bytes) |
| 113 | * @param[in] in2_stride_y Stride of the source image in Y dimension (in bytes) |
| 114 | * @param[in] in2_step_y in2_stride_y * number of elements along Y processed per workitem(in bytes) |
| 115 | * @param[in] in2_stride_z Stride of the source image in Y dimension (in bytes) |
| 116 | * @param[in] in2_step_z in2_stride_z * number of elements along Y processed per workitem(in bytes) |
| 117 | * @param[in] in2_offset_first_element_in_bytes The offset of the first element in the source image |
| 118 | * @param[out] out_ptr Pointer to the destination image. Supported data types: U8, S16, F16, F32 |
| 119 | * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes) |
| 120 | * @param[in] out_step_x out_stride_x * number of elements along X processed per workitem(in bytes) |
| 121 | * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes) |
| 122 | * @param[in] out_step_y out_stride_y * number of elements along Y processed per workitem(in bytes) |
| 123 | * @param[in] out_stride_z Stride of the destination image in Y dimension (in bytes) |
| 124 | * @param[in] out_step_z out_stride_z * number of elements along Y processed per workitem(in bytes) |
| 125 | * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination image |
| 126 | * @param[in] scale Float scaling factor. Supported data types: F32 |
| 127 | */ |
| 128 | __kernel void pixelwise_mul_quantized( |
| 129 | TENSOR3D_DECLARATION(in1), |
| 130 | TENSOR3D_DECLARATION(in2), |
| 131 | TENSOR3D_DECLARATION(out), |
| 132 | const float scale) |
| 133 | { |
| 134 | // Get pixels pointer |
| 135 | Tensor3D in1 = CONVERT_TO_TENSOR3D_STRUCT(in1); |
| 136 | Tensor3D in2 = CONVERT_TO_TENSOR3D_STRUCT(in2); |
| 137 | Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(out); |
| 138 | |
| 139 | // Load data |
| 140 | int16 in_a = CONVERT(vload16(0, (__global uchar *)in1.ptr), int16); |
| 141 | int16 in_b = CONVERT(vload16(0, (__global uchar *)in2.ptr), int16); |
| 142 | |
| 143 | // Dequantize |
| 144 | in_a -= (int16)(int)OFFSET_IN1; |
| 145 | in_b -= (int16)(int)OFFSET_IN2; |
| 146 | const float16 in1f32 = convert_float16(in_a) * (float16)(float)SCALE_IN1; |
| 147 | const float16 in2f32 = convert_float16(in_b) * (float16)(float)SCALE_IN2; |
| 148 | |
| 149 | const float16 qresf32 = (in1f32 * in2f32 * scale) / ((float16)(float)SCALE_OUT) + ((float16)((float16)OFFSET_OUT)); |
| 150 | const uchar16 res = convert_uchar16_sat(convert_int16_rte(qresf32)); |
| 151 | |
| 152 | // Store result |
| 153 | vstore16(res, 0, (__global uchar *)out.ptr); |
| 154 | } |
| 155 | #endif /* defined(OFFSET_IN1) && defined(OFFSET_IN2) && defined(OFFSET_OUT) && defined(SCALE_IN1) && defined(SCALE_IN2) && defined(SCALE_OUT) */ |