1/* 2 * Copyright (c) 2018-2021 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(VECTOR_SIZE) && defined(START) && defined(STEP) && defined(DATA_TYPE) && defined(VEC_SIZE_LEFTOVER) 27 28#if !defined(OFFSET_OUT) && !defined(SCALE_OUT) 29 30#if VECTOR_SIZE == 2 31#define STEP_VEC ((VEC_DATA_TYPE(DATA_TYPE, 2))(0, STEP)) 32#elif VECTOR_SIZE == 3 33#define STEP_VEC ((VEC_DATA_TYPE(DATA_TYPE, 3))(0, STEP, 2 * STEP)) 34#elif VECTOR_SIZE == 4 35#define STEP_VEC ((VEC_DATA_TYPE(DATA_TYPE, 4))(0, STEP, 2 * STEP, 3 * STEP)) 36#elif VECTOR_SIZE == 8 37#define STEP_VEC ((VEC_DATA_TYPE(DATA_TYPE, 8))(0, STEP, 2 * STEP, 3 * STEP, 4 * STEP, 5 * STEP, 6 * STEP, 7 * STEP)) 38#elif VECTOR_SIZE == 16 39#define STEP_VEC ((VEC_DATA_TYPE(DATA_TYPE, 16))(0, STEP, 2 * STEP, 3 * STEP, 4 * STEP, 5 * STEP, 6 * STEP, 7 * STEP, 8 * STEP, 9 * STEP, 10 * STEP, 11 * STEP, 12 * STEP, 13 * STEP, 14 * STEP, 15 * STEP)) 40#endif // VECTOR_SIZE == 2 41 42/** Generates a sequence of numbers starting from START and extends by increments of 'STEP' up to but not including 'END'. 43 * 44 * @note starting value of the sequence must be given as a preprocessor argument using -DSTART=value. e.g. -DSTART=0 45 * @note difference between consequtive elements of the sequence must be given as a preprocessor argument using -DSTEP=value. e.g. -DSTEP=1 46 * @note Datatype must be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short 47 * @note vector size supported by the device must be given as a preprocessor argument using -DVECTOR_SIZE=value. e.g. -DDATA_TYPE=4 48 * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVECTOR_SIZE=3. It is defined as the remainder between the input's first dimension and VECTOR_SIZE 49 * 50 * @param[out] out_ptr Pointer to the destination tensor. Supported data types: U8/S8/U16/S16/U32/S32/F16/F32. 51 * @param[in] out_stride_x Stride of the destination tensor in X dimension (in bytes) 52 * @param[in] out_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) 53 * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination tensor 54 */ 55__kernel void range( 56 VECTOR_DECLARATION(out)) 57{ 58 uint id = max((int)(get_global_id(0) * VECTOR_SIZE - (VECTOR_SIZE - VEC_SIZE_LEFTOVER) % VECTOR_SIZE), 0); 59 __global uchar *dst_ptr = out_ptr + out_offset_first_element_in_bytes + id * sizeof(DATA_TYPE); 60#if VECTOR_SIZE == 1 61 DATA_TYPE seq; 62 seq = (DATA_TYPE)START + (DATA_TYPE)id * (DATA_TYPE)STEP; 63 64 *(__global DATA_TYPE *)dst_ptr = seq; 65#else // VECTOR_SIZE == 1 66 VEC_DATA_TYPE(DATA_TYPE, VECTOR_SIZE) 67 seq0 = ((DATA_TYPE)START + (DATA_TYPE)id * (DATA_TYPE)STEP); 68 seq0 = seq0 + STEP_VEC; 69 STORE_VECTOR_SELECT(seq, DATA_TYPE, dst_ptr, VECTOR_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0) 70#endif //VECTOR_SIZE == 1 71} 72 73#else // !defined(OFFSET_OUT) && !defined(SCALE_OUT) 74 75#if VECTOR_SIZE == 2 76#define STEP_VEC ((VEC_DATA_TYPE(float, 2))(0, STEP)) 77#elif VECTOR_SIZE == 3 78#define STEP_VEC ((VEC_DATA_TYPE(float, 3))(0, STEP, 2 * STEP)) 79#elif VECTOR_SIZE == 4 80#define STEP_VEC ((VEC_DATA_TYPE(float, 4))(0, STEP, 2 * STEP, 3 * STEP)) 81#elif VECTOR_SIZE == 8 82#define STEP_VEC ((VEC_DATA_TYPE(float, 8))(0, STEP, 2 * STEP, 3 * STEP, 4 * STEP, 5 * STEP, 6 * STEP, 7 * STEP)) 83#elif VECTOR_SIZE == 16 84#define STEP_VEC ((VEC_DATA_TYPE(float, 16))(0, STEP, 2 * STEP, 3 * STEP, 4 * STEP, 5 * STEP, 6 * STEP, 7 * STEP, 8 * STEP, 9 * STEP, 10 * STEP, 11 * STEP, 12 * STEP, 13 * STEP, 14 * STEP, 15 * STEP)) 85#endif // VECTOR_SIZE == 2 86 87#define CONVERT_RTE(x, type) (convert_##type##_rte((x))) 88#define CONVERT_DOWN(x, type) CONVERT_RTE(x, type) 89 90/** Generates a sequence of numbers starting from START and extends by increments of 'STEP' up to but not including 'END'. 91 * 92 * @note starting value of the sequence must be given as a preprocessor argument using -DSTART=value. e.g. -DSTART=0 93 * @note difference between consequtive elements of the sequence must be given as a preprocessor argument using -DSTEP=value. e.g. -DSTEP=1 94 * @note Datatype must be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short 95 * @note vector size supported by the device must be given as a preprocessor argument using -DVECTOR_SIZE=vector_size. e.g. -DDATA_TYPE=4 96 * @note The quantization offset of the output must be passed at compile time using -DOFFSET_OUT, i.e. -DOFFSET_OUT=10 97 * @note The quantization scale of the output must be passed at compile time using -DSCALE_OUT, i.e. -DSCALE_OUT=10 98 * 99 * @param[out] out_ptr Pointer to the destination tensor. Supported data types: QASYMM8. 100 * @param[in] out_stride_x Stride of the destination tensor in X dimension (in bytes) 101 * @param[in] out_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) 102 * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination tensor 103 */ 104__kernel void range_quantized( 105 VECTOR_DECLARATION(out)) 106{ 107 uint id = max((int)(get_global_id(0) * VECTOR_SIZE - (VECTOR_SIZE - VEC_SIZE_LEFTOVER) % VECTOR_SIZE), 0); 108 __global uchar *dst_ptr = out_ptr + out_offset_first_element_in_bytes + id * sizeof(DATA_TYPE); 109#if VECTOR_SIZE == 1 110 float seq; 111 seq = (float)START + (float)id * (float)STEP; 112 seq = (DATA_TYPE)(int)(seq / ((float)SCALE_OUT) + (float)OFFSET_OUT); 113 seq = max(0.0f, min(seq, 255.0f)); 114 *(__global DATA_TYPE *)dst_ptr = CONVERT_SAT(CONVERT_DOWN(seq, int), DATA_TYPE); 115#else // VECTOR_SIZE == 1 116 VEC_DATA_TYPE(float, VECTOR_SIZE) 117 seq = (float)START + id * (float)STEP; 118 seq = seq + STEP_VEC; 119 seq = seq / ((VEC_DATA_TYPE(float, VECTOR_SIZE))((float)SCALE_OUT)) + ((VEC_DATA_TYPE(float, VECTOR_SIZE))((float)OFFSET_OUT)); 120 seq = max((VEC_DATA_TYPE(float, VECTOR_SIZE))(0.0f), min(seq, (VEC_DATA_TYPE(float, VECTOR_SIZE))(255.0f))); 121 VEC_DATA_TYPE(DATA_TYPE, VECTOR_SIZE) 122 res0 = CONVERT_SAT(CONVERT_DOWN(seq, VEC_DATA_TYPE(int, VECTOR_SIZE)), VEC_DATA_TYPE(DATA_TYPE, VECTOR_SIZE)); 123 STORE_VECTOR_SELECT(res, DATA_TYPE, dst_ptr, VECTOR_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0) 124#endif // VECTOR_SIZE == 1 125} 126#endif // !defined(OFFSET_OUT) && !defined(SCALE_OUT) 127 128#endif // defined(VECTOR_SIZE) && defined(START) && defined(STEP) && defined(DATA_TYPE) && defined(VEC_SIZE_LEFTOVER) 129