ONE - On-device Neural Engine
Loading...
Searching...
No Matches
repeat.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * Copyright (c) 2019-2020 ARM Limited.
19 *
20 * SPDX-License-Identifier: MIT
21 *
22 * Permission is hereby granted, free of charge, to any person obtaining a copy
23 * of this software and associated documentation files (the "Software"), to
24 * deal in the Software without restriction, including without limitation the
25 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
26 * sell copies of the Software, and to permit persons to whom the Software is
27 * furnished to do so, subject to the following conditions:
28 *
29 * The above copyright notice and this permission notice shall be included in all
30 * copies or substantial portions of the Software.
31 *
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38 * SOFTWARE.
39 */
40#ifndef ARM_COMPUTE_REPEAT_H
41#define ARM_COMPUTE_REPEAT_H
42
43#include "helpers.h"
44
46// Repeat macros with 3 param, excluding the implicit ID param
47#define REPEAT_3_1(P_X, P_A, P_B, P_C) P_X##_DEF(0, P_A, P_B, P_C)
48#define REPEAT_3_2(P_X, P_A, P_B, P_C) \
49 P_X##_DEF(1, P_A, P_B, P_C); \
50 REPEAT_3_1(P_X, P_A, P_B, P_C)
51#define REPEAT_3_3(P_X, P_A, P_B, P_C) \
52 P_X##_DEF(2, P_A, P_B, P_C); \
53 REPEAT_3_2(P_X, P_A, P_B, P_C)
54#define REPEAT_3_4(P_X, P_A, P_B, P_C) \
55 P_X##_DEF(3, P_A, P_B, P_C); \
56 REPEAT_3_3(P_X, P_A, P_B, P_C)
57#define REPEAT_3_5(P_X, P_A, P_B, P_C) \
58 P_X##_DEF(4, P_A, P_B, P_C); \
59 REPEAT_3_4(P_X, P_A, P_B, P_C)
60#define REPEAT_3_6(P_X, P_A, P_B, P_C) \
61 P_X##_DEF(5, P_A, P_B, P_C); \
62 REPEAT_3_5(P_X, P_A, P_B, P_C)
63#define REPEAT_3_7(P_X, P_A, P_B, P_C) \
64 P_X##_DEF(6, P_A, P_B, P_C); \
65 REPEAT_3_6(P_X, P_A, P_B, P_C)
66#define REPEAT_3_8(P_X, P_A, P_B, P_C) \
67 P_X##_DEF(7, P_A, P_B, P_C); \
68 REPEAT_3_7(P_X, P_A, P_B, P_C)
69#define REPEAT_3_9(P_X, P_A, P_B, P_C) \
70 P_X##_DEF(8, P_A, P_B, P_C); \
71 REPEAT_3_8(P_X, P_A, P_B, P_C)
72#define REPEAT_3_10(P_X, P_A, P_B, P_C) \
73 P_X##_DEF(9, P_A, P_B, P_C); \
74 REPEAT_3_9(P_X, P_A, P_B, P_C)
75#define REPEAT_3_11(P_X, P_A, P_B, P_C) \
76 P_X##_DEF(A, P_A, P_B, P_C); \
77 REPEAT_3_10(P_X, P_A, P_B, P_C)
78#define REPEAT_3_12(P_X, P_A, P_B, P_C) \
79 P_X##_DEF(B, P_A, P_B, P_C); \
80 REPEAT_3_11(P_X, P_A, P_B, P_C)
81#define REPEAT_3_13(P_X, P_A, P_B, P_C) \
82 P_X##_DEF(C, P_A, P_B, P_C); \
83 REPEAT_3_12(P_X, P_A, P_B, P_C)
84#define REPEAT_3_14(P_X, P_A, P_B, P_C) \
85 P_X##_DEF(D, P_A, P_B, P_C); \
86 REPEAT_3_13(P_X, P_A, P_B, P_C)
87#define REPEAT_3_15(P_X, P_A, P_B, P_C) \
88 P_X##_DEF(E, P_A, P_B, P_C); \
89 REPEAT_3_14(P_X, P_A, P_B, P_C)
90#define REPEAT_3_16(P_X, P_A, P_B, P_C) \
91 P_X##_DEF(F, P_A, P_B, P_C); \
92 REPEAT_3_15(P_X, P_A, P_B, P_C)
93
94#define REPEAT_DEF_3_N(P_NUM, P_OP, P_A, P_B, P_C) \
95 REPEAT_3_##P_NUM(P_OP, P_A, P_B, P_C) // One level of indirection to ensure order of expansion
96 // does not affect preprocessing P_NUM
97#define REPEAT_3_N(P_NUM, P_OP, P_A, P_B, P_C) REPEAT_DEF_3_N(P_NUM, P_OP, P_A, P_B, P_C)
98
99// Repeat macros with 4 param, excluding the implicit ID param
100#define REPEAT_4_1(P_X, P_A, P_B, P_C, P_D) P_X##_DEF(0, P_A, P_B, P_C, P_D)
101#define REPEAT_4_2(P_X, P_A, P_B, P_C, P_D) \
102 P_X##_DEF(1, P_A, P_B, P_C, P_D); \
103 REPEAT_4_1(P_X, P_A, P_B, P_C, P_D)
104#define REPEAT_4_3(P_X, P_A, P_B, P_C, P_D) \
105 P_X##_DEF(2, P_A, P_B, P_C, P_D); \
106 REPEAT_4_2(P_X, P_A, P_B, P_C, P_D)
107#define REPEAT_4_4(P_X, P_A, P_B, P_C, P_D) \
108 P_X##_DEF(3, P_A, P_B, P_C, P_D); \
109 REPEAT_4_3(P_X, P_A, P_B, P_C, P_D)
110#define REPEAT_4_5(P_X, P_A, P_B, P_C, P_D) \
111 P_X##_DEF(4, P_A, P_B, P_C, P_D); \
112 REPEAT_4_4(P_X, P_A, P_B, P_C, P_D)
113#define REPEAT_4_6(P_X, P_A, P_B, P_C, P_D) \
114 P_X##_DEF(5, P_A, P_B, P_C, P_D); \
115 REPEAT_4_5(P_X, P_A, P_B, P_C, P_D)
116#define REPEAT_4_7(P_X, P_A, P_B, P_C, P_D) \
117 P_X##_DEF(6, P_A, P_B, P_C, P_D); \
118 REPEAT_4_6(P_X, P_A, P_B, P_C, P_D)
119#define REPEAT_4_8(P_X, P_A, P_B, P_C, P_D) \
120 P_X##_DEF(7, P_A, P_B, P_C, P_D); \
121 REPEAT_4_7(P_X, P_A, P_B, P_C, P_D)
122#define REPEAT_4_9(P_X, P_A, P_B, P_C, P_D) \
123 P_X##_DEF(8, P_A, P_B, P_C, P_D); \
124 REPEAT_4_8(P_X, P_A, P_B, P_C, P_D)
125#define REPEAT_4_10(P_X, P_A, P_B, P_C, P_D) \
126 P_X##_DEF(9, P_A, P_B, P_C, P_D); \
127 REPEAT_4_9(P_X, P_A, P_B, P_C, P_D)
128#define REPEAT_4_11(P_X, P_A, P_B, P_C, P_D) \
129 P_X##_DEF(A, P_A, P_B, P_C, P_D); \
130 REPEAT_4_10(P_X, P_A, P_B, P_C, P_D)
131#define REPEAT_4_12(P_X, P_A, P_B, P_C, P_D) \
132 P_X##_DEF(B, P_A, P_B, P_C, P_D); \
133 REPEAT_4_11(P_X, P_A, P_B, P_C, P_D)
134#define REPEAT_4_13(P_X, P_A, P_B, P_C, P_D) \
135 P_X##_DEF(C, P_A, P_B, P_C, P_D); \
136 REPEAT_4_12(P_X, P_A, P_B, P_C, P_D)
137#define REPEAT_4_14(P_X, P_A, P_B, P_C, P_D) \
138 P_X##_DEF(D, P_A, P_B, P_C, P_D); \
139 REPEAT_4_13(P_X, P_A, P_B, P_C, P_D)
140#define REPEAT_4_15(P_X, P_A, P_B, P_C, P_D) \
141 P_X##_DEF(E, P_A, P_B, P_C, P_D); \
142 REPEAT_4_14(P_X, P_A, P_B, P_C, P_D)
143#define REPEAT_4_16(P_X, P_A, P_B, P_C, P_D) \
144 P_X##_DEF(F, P_A, P_B, P_C, P_D); \
145 REPEAT_4_15(P_X, P_A, P_B, P_C, P_D)
146
147#define REPEAT_DEF_4_N(P_NUM, P_OP, P_A, P_B, P_C, P_D) \
148 REPEAT_4_##P_NUM(P_OP, P_A, P_B, P_C, P_D) // One level of indirection to ensure order of
149 // expansion does not affect preprocessing P_NUM
150#define REPEAT_4_N(P_NUM, P_OP, P_A, P_B, P_C, P_D) REPEAT_DEF_4_N(P_NUM, P_OP, P_A, P_B, P_C, P_D)
151
152// Macro for initializing N variables. Generates N statements that defines VAR##N =
153// RHS_ACCESSOR_DEF(...)
154#define VAR_INIT_TO_CONST_DEF(ID, TYPE, VAR, VAL) TYPE VAR##ID = VAL
155#define REPEAT_VAR_INIT_TO_CONST(N, TYPE, VAR, VAL) REPEAT_3_N(N, VAR_INIT_TO_CONST, TYPE, VAR, VAL)
156
157// Macro for initializing N variables by converting the data type. Generates N statements that
158// defines VAR##N = RHS_ACCESSOR_DEF(...)
159#define VAR_INIT_CONVERT_SAT_DEF(ID, TYPE_OUT, VAR_IN, VAR_OUT) \
160 TYPE_OUT VAR_OUT##ID = CONVERT_SAT(VAR_IN##ID, TYPE_OUT)
161#define REPEAT_VAR_INIT_CONVERT_SAT(N, TYPE_OUT, VAR_IN, VAR_OUT) \
162 REPEAT_3_N(N, VAR_INIT_CONVERT_SAT, TYPE_OUT, VAR_IN, VAR_OUT)
163
164// Macro for adding a constant to N variables. Generates N statements that defines VAR##N
165// =RHS_ACCESSOR_DEF(...)
166#define ADD_CONST_TO_VAR_DEF(ID, TYPE, VAR, VAL) VAR##ID += (TYPE)VAL
167#define REPEAT_ADD_CONST_TO_VAR(N, TYPE, VAR, VAL) REPEAT_3_N(N, ADD_CONST_TO_VAR, TYPE, VAR, VAL)
168
169// Macro for multiplying N variables (VAR_B) by a constant (VAL) and adding to other N variables
170// (VAR_A). Generates N statements that defines VAR_A##N =RHS_ACCESSOR_DEF(...)
171#define MLA_VAR_WITH_CONST_VEC_DEF(ID, VAR_A, VAR_B, VAL) VAR_A##ID += VAR_B##ID * VAL
172#define REPEAT_MLA_VAR_WITH_CONST_VEC(N, VAR_A, VAR_B, VAL) \
173 REPEAT_3_N(N, MLA_VAR_WITH_CONST_VEC, VAR_A, VAR_B, VAL)
174
175// Macro for adding a vector to N-variables. Generates N statements that defines VAR##N
176// =RHS_ACCESSOR_DEF(...)
177#define ADD_VECTOR_TO_VAR_DEF(ID, TYPE, VAR, VEC) VAR##ID += VEC
178#define REPEAT_ADD_VECTOR_TO_VAR(N, VAR, VEC) REPEAT_3_N(N, ADD_VECTOR_TO_VAR, "", VAR, VEC)
179
180// Macro for adding a two N-variables. Generates N statements that defines VAR##N
181// =RHS_ACCESSOR_DEF(...)
182#define ADD_TWO_VARS_DEF(ID, TYPE, VAR_A, VAR_B) VAR_A##ID += VAR_B##ID
183#define REPEAT_ADD_TWO_VARS(N, VAR_A, VAR_B) REPEAT_3_N(N, ADD_TWO_VARS, "", VAR_A, VAR_B)
184
185// Macro for performing Max between a constant and N variables. Generates N statements that defines
186// VAR##N =RHS_ACCESSOR_DEF(...)
187#define MAX_CONST_VAR_DEF(ID, TYPE, VAR, VAL) VAR##ID = max(VAR##ID, (TYPE)VAL)
188#define REPEAT_MAX_CONST_VAR(N, TYPE, VAR, VAL) REPEAT_3_N(N, MAX_CONST_VAR, TYPE, VAR, VAL)
189
190// Macro for performing Min between a constant and N variables. Generates N statements that defines
191// VAR##N =RHS_ACCESSOR_DEF(...)
192#define MIN_CONST_VAR_DEF(ID, TYPE, VAR, VAL) VAR##ID = min(VAR##ID, (TYPE)VAL)
193#define REPEAT_MIN_CONST_VAR(N, TYPE, VAR, VAL) REPEAT_3_N(N, MIN_CONST_VAR, TYPE, VAR, VAL)
194
195// Macro for performing ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE to N variables. Generates N
196// statements that defines VAR##N =RHS_ACCESSOR_DEF(...)
197#define ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE_DEF(ID, SIZE, VAR, RES_MUL, RES_SHIFT) \
198 VAR##ID = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(VAR##ID, RES_MUL, RES_SHIFT, SIZE)
199#define REPEAT_ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(N, SIZE, VAR, RES_MUL, RES_SHIFT) \
200 REPEAT_4_N(N, ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE, SIZE, VAR, RES_MUL, RES_SHIFT)
201
202// Macro for performing ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE to N variables. Generates N
203// statements that defines VAR##N =RHS_ACCESSOR_DEF(...)
204#define ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE_DEF(ID, SIZE, VAR, RES_MUL, RES_SHIFT) \
205 VAR##ID = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(VAR##ID, RES_MUL, RES_SHIFT, SIZE)
206#define REPEAT_ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(N, SIZE, VAR, RES_MUL, RES_SHIFT) \
207 REPEAT_4_N(N, ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE, SIZE, VAR, RES_MUL, RES_SHIFT)
208
209// Macro for performing per-channel ASYMM_MULT_BY_QUANT_MULTIPLIER to N variables.
210#define ASYMM_MULT_BY_QUANT_MULTIPLIER_PER_CHANNEL_DEF(ID, SIZE, VAR, RES_MUL, RES_SHIFT) \
211 ({ \
212 VEC_DATA_TYPE(int, N0) \
213 VAR##ID_shift_lt0 = \
214 ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(VAR##ID, RES_MUL, RES_SHIFT, N0); \
215 VEC_DATA_TYPE(int, N0) \
216 VAR##ID_shift_gt0 = \
217 ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(VAR##ID, RES_MUL, RES_SHIFT, N0); \
218 VAR##ID = select(VAR##ID_shift_lt0, VAR##ID_shift_gt0, RES_SHIFT >= 0); \
219 })
220#define REPEAT_ASYMM_MULT_BY_QUANT_MULTIPLIER_PER_CHANNEL(N, SIZE, VAR, RES_MUL, RES_SHIFT) \
221 REPEAT_4_N(N, ASYMM_MULT_BY_QUANT_MULTIPLIER_PER_CHANNEL, SIZE, VAR, RES_MUL, RES_SHIFT)
222
223#endif // ARM_COMPUTE_REPEAT_H