blob: d25d10e2072aa368173e72043eb9d19ed9dc0767 [file] [log] [blame]
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +00001/*
Giorgio Arenac2165452021-01-07 14:01:47 +00002 * Copyright (c) 2018-2021 Arm Limited.
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +00003 *
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
Manuel Bottinia2432052020-11-25 18:38:05 +000026#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
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000042/** 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
Manuel Bottinia2432052020-11-25 18:38:05 +000048 * @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
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000049 *
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{
Giorgio Arenac2165452021-01-07 14:01:47 +000058 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);
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000060#if VECTOR_SIZE == 1
61 DATA_TYPE seq;
62 seq = (DATA_TYPE)START + (DATA_TYPE)id * (DATA_TYPE)STEP;
63
Giorgio Arena3d3a01c2021-01-11 15:31:15 +000064 *(__global DATA_TYPE *)dst_ptr = seq;
Manuel Bottinia2432052020-11-25 18:38:05 +000065#else // VECTOR_SIZE == 1
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000066 VEC_DATA_TYPE(DATA_TYPE, VECTOR_SIZE)
Manuel Bottinia2432052020-11-25 18:38:05 +000067 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)
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000070#endif //VECTOR_SIZE == 1
71}
72
Manuel Bottinia2432052020-11-25 18:38:05 +000073#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
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000086
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{
Giorgio Arenac2165452021-01-07 14:01:47 +0000107 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);
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000109#if VECTOR_SIZE == 1
Giorgio Arenac2165452021-01-07 14:01:47 +0000110 float seq;
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000111 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);
Manuel Bottinia2432052020-11-25 18:38:05 +0000115#else // VECTOR_SIZE == 1
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000116 VEC_DATA_TYPE(float, VECTOR_SIZE)
Manuel Bottinia2432052020-11-25 18:38:05 +0000117 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)));
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000121 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));
Manuel Bottinia2432052020-11-25 18:38:05 +0000123 STORE_VECTOR_SELECT(res, DATA_TYPE, dst_ptr, VECTOR_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000124#endif // VECTOR_SIZE == 1
125}
Manuel Bottinia2432052020-11-25 18:38:05 +0000126#endif // !defined(OFFSET_OUT) && !defined(SCALE_OUT)
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000127
Manuel Bottinia2432052020-11-25 18:38:05 +0000128#endif // defined(VECTOR_SIZE) && defined(START) && defined(STEP) && defined(DATA_TYPE) && defined(VEC_SIZE_LEFTOVER)