blob: 9fd3a6f899ce681a7d6a16f08fd7a8e7deba5848 [file] [log] [blame]
Georgios Pinitase5f8fd62017-06-23 18:03:44 +01001/*
2 * Copyright (c) 2017 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#ifndef ARM_COMPUTE_FIXED_POINT_H
25#define ARM_COMPUTE_FIXED_POINT_H
26
27#define TYPE_ALIAS(type, alias) \
28 typedef type alias; \
29 typedef type alias##x##1; \
30 typedef type##2 alias##x##2; \
31 typedef type##3 alias##x##3; \
32 typedef type##4 alias##x##4; \
33 typedef type##8 alias##x##8; \
34 typedef type##16 alias##x##16;
35
36TYPE_ALIAS(char, qs8)
37TYPE_ALIAS(short, qs16)
Gian Marco Iodice8a383692017-07-03 17:41:47 +010038TYPE_ALIAS(int, qs32)
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010039
40#define qs8_MIN ((char)CHAR_MIN)
41#define qs8_MAX ((char)CHAR_MAX)
42#define qs16_MIN ((short)SHRT_MIN)
43#define qs16_MAX ((short)SHRT_MAX)
Gian Marco Iodice8a383692017-07-03 17:41:47 +010044#define qs32_MIN ((int)INT_MIN)
45#define qs32_MAX ((int)INT_MAX)
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010046
47#define qu8_MIN ((uchar)0)
48#define qu8_MAX ((uchar)UCHAR_MAX)
49#define qu16_MIN ((ushort)0)
50#define qu16_MAX ((ushort)USHRT_MAX)
Gian Marco Iodice8a383692017-07-03 17:41:47 +010051#define qu32_MIN ((uint)0)
52#define qu32_MAX ((uint)UINT_MAX)
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010053
54#define qs8_TYPE char
55#define qs8x1_TYPE char
56#define qs8x2_TYPE char2
57#define qs8x4_TYPE char4
58#define qs8x8_TYPE char8
59#define qs8x16_TYPE char16
60
61#define qs16_TYPE short
62#define qs16x1_TYPE short
63#define qs16x2_TYPE short2
64#define qs16x4_TYPE short4
65#define qs16x8_TYPE short8
66#define qs16x16_TYPE short16
67
Gian Marco Iodice8a383692017-07-03 17:41:47 +010068#define qs32_TYPE int
69#define qs32x1_TYPE int
70#define qs32x2_TYPE int2
71#define qs32x4_TYPE int4
72#define qs32x8_TYPE int8
73#define qs32x16_TYPE int16
74
Michalis Spyroud7e82812017-06-20 15:00:14 +010075/* All internal constants are represented in the maximum supported fixed point format (QS16),
76 * thus we define an additional shift parameter required to convert the constant
77 * from the maximum supported format to the require one.
78 */
79#define qs8_SHIFT 8
80#define qs16_SHIFT 0
81
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010082#undef VEC_DATA_TYPE_STR
83#undef VEC_DATA_TYPE
84#undef CONVERT_STR
85#undef CONVERT
86#undef CONVERT_SAT_STR
87#undef CONVERT_SAT
88
89#define VEC_DATA_TYPE_STR(type, size) type##x##size
90#define VEC_DATA_TYPE(type, size) VEC_DATA_TYPE_STR(type, size)
91
92#define CONVERT_STR3(x, type, rtype) (convert_##rtype((x)))
93#define CONVERT_STR2(x, type, rtype) CONVERT_STR3(x, type, rtype)
94#define CONVERT_STR(x, type) CONVERT_STR2(x, type, type##_TYPE)
95#define CONVERT(x, type) CONVERT_STR(x, type)
96
97#define CONVERT_SAT_STR3(x, type, rtype) (convert_##rtype##_sat((x)))
98#define CONVERT_SAT_STR2(x, type, rtype) CONVERT_SAT_STR3(x, type, rtype)
99#define CONVERT_SAT_STR(x, type) CONVERT_SAT_STR2(x, type, type##_TYPE)
100#define CONVERT_SAT(x, type) CONVERT_SAT_STR(x, type)
101
Michalis Spyroud7e82812017-06-20 15:00:14 +0100102/** Computes max of fixed point types.
103 *
104 * @param[in] type the actual data type.
105 *
106 * @return The result of the fixed point maximum.
107 */
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100108#define MAXQ_IMPL(type) \
109 inline type max_##type(type VopA, type VopB) \
110 { \
111 return max(VopA, VopB); \
112 }
113
114MAXQ_IMPL(qs8x1)
115MAXQ_IMPL(qs8x2)
116MAXQ_IMPL(qs8x4)
117MAXQ_IMPL(qs8x8)
118MAXQ_IMPL(qs8x16)
Georgios Pinitas09796752017-07-10 16:05:21 +0100119MAXQ_IMPL(qs16x1)
120MAXQ_IMPL(qs16x2)
121MAXQ_IMPL(qs16x4)
122MAXQ_IMPL(qs16x8)
123MAXQ_IMPL(qs16x16)
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100124
125#define MAX_OP_EXPAND_STR(a, b, type, size) max_##type##x##size((a), (b))
126#define MAX_OP_EXPAND(a, b, type, size) MAX_OP_EXPAND_STR(a, b, type, size)
127
Michalis Spyroud7e82812017-06-20 15:00:14 +0100128/** Computes saturated addition of fixed point types.
129 *
130 * @param[in] type the actual data type.
131 *
132 * @return The result of the fixed point addition. The result is saturated in case of overflow
133 */
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100134#define ADDQ_SAT_IMPL(type) \
135 inline type add_sat_##type(type VopA, type VopB) \
136 { \
137 return add_sat(VopA, VopB); \
138 }
139
140ADDQ_SAT_IMPL(qs8x1)
141ADDQ_SAT_IMPL(qs8x2)
142ADDQ_SAT_IMPL(qs8x4)
143ADDQ_SAT_IMPL(qs8x8)
144ADDQ_SAT_IMPL(qs8x16)
Gian Marco Iodice7d323a62017-07-05 20:05:23 +0100145ADDQ_SAT_IMPL(qs16x1)
146ADDQ_SAT_IMPL(qs16x2)
147ADDQ_SAT_IMPL(qs16x4)
148ADDQ_SAT_IMPL(qs16x8)
149ADDQ_SAT_IMPL(qs16x16)
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100150
151#define ADD_SAT_OP_EXPAND_STR(a, b, type, size) add_sat_##type##x##size((a), (b))
152#define ADD_SAT_OP_EXPAND(a, b, type, size) ADD_SAT_OP_EXPAND_STR(a, b, type, size)
153
Michalis Spyroud7e82812017-06-20 15:00:14 +0100154/** Computes saturated subtraction of fixed point types.
155 *
156 * @param[in] type the actual data type.
157 *
158 * @return The result of the fixed point subtraction. The result is saturated in case of overflow
159 */
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100160#define SUBQ_SAT_IMPL(type) \
161 inline type sub_sat_##type(type VopA, type VopB) \
162 { \
163 return sub_sat(VopA, VopB); \
164 }
165
166SUBQ_SAT_IMPL(qs8x1)
167SUBQ_SAT_IMPL(qs8x2)
168SUBQ_SAT_IMPL(qs8x4)
169SUBQ_SAT_IMPL(qs8x8)
170SUBQ_SAT_IMPL(qs8x16)
Georgios Pinitas09796752017-07-10 16:05:21 +0100171SUBQ_SAT_IMPL(qs16x1)
172SUBQ_SAT_IMPL(qs16x2)
173SUBQ_SAT_IMPL(qs16x4)
174SUBQ_SAT_IMPL(qs16x8)
175SUBQ_SAT_IMPL(qs16x16)
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100176
177#define SUB_SAT_OP_EXPAND_STR(a, b, type, size) sub_sat_##type##x##size((a), (b))
178#define SUB_SAT_OP_EXPAND(a, b, type, size) SUB_SAT_OP_EXPAND_STR(a, b, type, size)
179
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100180/* Multiply of two fixed point numbers
181 *
182 * @param[in] type the actual data type.
183 * @param[in] itype the intermediate data type.
184 *
185 * @return The result of the fixed point multiplication.
186 */
187#define MULQ_IMPL(type, itype) \
188 inline type mul_##type(type VopA, type VopB, int fixed_point_position) \
189 { \
190 itype round_val = (itype)(1 << (fixed_point_position - 1)); \
191 itype res = CONVERT((VopA), itype) * CONVERT((VopB), itype) + round_val; \
192 return CONVERT((res >> (itype)fixed_point_position), type); \
193 }
194
195MULQ_IMPL(qs8x16, qs16x16)
196MULQ_IMPL(qs16x16, qs32x16)
197
198#define MUL_OP_EXPAND_STR(a, b, type, size, position) mul_##type##x##size((a), (b), (position))
199#define MUL_OP_EXPAND(a, b, type, size, position) MUL_OP_EXPAND_STR(a, b, type, size, position)
200
201/* Saturate multiply of two fixed point numbers
202 *
203 * @param[in] type the actual data type.
204 * @param[in] itype the intermediate data type.
205 *
206 * @return The result of the fixed point multiplication. The result is saturated in case of overflow
207 */
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100208#define MULQ_SAT_IMPL(type, itype) \
209 inline type mul_sat_##type(type VopA, type VopB, int fixed_point_position) \
210 { \
211 itype round_val = (itype)(1 << (fixed_point_position - 1)); \
212 itype res = mad_sat(CONVERT((VopA), itype), CONVERT((VopB), itype), round_val); \
213 return CONVERT_SAT((res >> (itype)fixed_point_position), type); \
214 }
215
216MULQ_SAT_IMPL(qs8x16, qs16x16)
Gian Marco Iodice8a383692017-07-03 17:41:47 +0100217MULQ_SAT_IMPL(qs16x8, qs32x8)
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100218MULQ_SAT_IMPL(qs16x16, qs32x16)
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100219
220#define MUL_SAT_OP_EXPAND_STR(a, b, type, size, position) mul_sat_##type##x##size((a), (b), (position))
221#define MUL_SAT_OP_EXPAND(a, b, type, size, position) MUL_SAT_OP_EXPAND_STR(a, b, type, size, position)
222
Michalis Spyroud7e82812017-06-20 15:00:14 +0100223/** Saturate multiply-accumulate
224 *
225 * @param[in] type the actual data type.
226 * @param[in] itype the intermediate data type.
227 *
228 * @return The result of the fixed point multiply-accumulate. The result is saturated in case of overflow
229 */
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100230#define MLAQ_SAT_IMPL(type, itype) \
231 type mla_sat_##type(type VopA, type VopB, type VopC, int fixed_point_position) \
232 { \
233 itype res = mad_sat(CONVERT(VopB, itype), CONVERT(VopC, itype), (itype)(1 << (fixed_point_position - 1))); \
234 return add_sat(VopA, CONVERT_SAT(res >> (itype)fixed_point_position, type)); \
235 }
236
237MLAQ_SAT_IMPL(qs8x8, qs16x8)
238MLAQ_SAT_IMPL(qs8x16, qs16x16)
Gian Marco Iodice8a383692017-07-03 17:41:47 +0100239MLAQ_SAT_IMPL(qs16x8, qs32x8)
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100240
241#define MLA_SAT_OP_EXPAND_STR(a, b, c, type, size, position) mla_sat_##type##x##size((a), (b), (c), (position))
242#define MLA_SAT_OP_EXPAND(a, b, c, type, size, position) MLA_SAT_OP_EXPAND_STR(a, b, c, type, size, position)
243
Michalis Spyroud7e82812017-06-20 15:00:14 +0100244/** Saturate multiply-accumulate long
245 *
246 * @param[in] type the actual data type.
247 * @param[in] itype the intermediate data type.
248 *
249 * @return The result of the fixed point multiply-accumulate long. The result is saturated in case of overflow
250 */
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100251#define MLALQ_SAT_IMPL(type, itype) \
252 itype mlal_sat_##type(itype VopA, type VopB, type VopC, int fixed_point_position) \
253 { \
254 itype res = mad_sat(CONVERT(VopB, itype), CONVERT(VopC, itype), (itype)(1 << (fixed_point_position - 1))); \
255 return add_sat(VopA, res >> (itype)fixed_point_position); \
256 }
257
258MLALQ_SAT_IMPL(qs8x8, qs16x8)
Gian Marco Iodice8a383692017-07-03 17:41:47 +0100259MLALQ_SAT_IMPL(qs16x8, qs32x8)
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100260
261#define MLAL_SAT_OP_EXPAND_STR(a, b, c, type, size, position) mlal_sat_##type##x##size((a), (b), (c), (position))
262#define MLAL_SAT_OP_EXPAND(a, b, c, type, size, position) MLAL_SAT_OP_EXPAND_STR(a, b, c, type, size, position)
263
264/** Saturate division of two fixed point numbers
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100265 *
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100266 * @param[in] stype the actual scalar data type.
267 * @param[in] type the actual data type.
268 * @param[in] itype the intermediate data type.
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100269 *
270 * @return The result of the fixed point division. The result is saturated in case of overflow
271 */
272#define DIVQ_SAT_IMPL(stype, type, itype) \
273 inline type div_sat_##type(type VopA, type VopB, int fixed_point_position) \
274 { \
275 itype conv_a = CONVERT((VopA), itype); \
276 itype denominator = CONVERT((VopB), itype); \
277 itype numerator = conv_a << (itype)(fixed_point_position); \
278 itype res = select(numerator / denominator, select((itype)stype##_MAX, (itype)stype##_MIN, conv_a < (itype)0), denominator == (itype)0); \
279 return CONVERT_SAT((res), type); \
280 }
281
282DIVQ_SAT_IMPL(qs8, qs8x16, qs16x16)
Georgios Pinitas09796752017-07-10 16:05:21 +0100283DIVQ_SAT_IMPL(qs16, qs16x16, qs32x16)
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100284
285#define DIV_SAT_OP_EXPAND_STR(a, b, type, size, position) div_sat_##type##x##size((a), (b), (position))
286#define DIV_SAT_OP_EXPAND(a, b, type, size, position) DIV_SAT_OP_EXPAND_STR(a, b, type, size, position)
287
Michalis Spyroud7e82812017-06-20 15:00:14 +0100288/** Saturate exponential of a fixed point vector
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100289 *
Michalis Spyroud7e82812017-06-20 15:00:14 +0100290 * @param[in] stype the actual scalar data type.
291 * @param[in] type the actual data type.
292 * @param[in] size the number of the calculated elements.
293 *
294 * @return The result of the fixed point exponential. The result is saturated in case of overflow
295 */
296#define EXPQ_IMPL(stype, type, size) \
297 inline type exp_sat_##type(type VopA, int fixed_point_position) \
298 { \
299 type const_one = (type)(1 << (fixed_point_position)); \
300 type ln2 = (type)((((0x58B9 >> (14 - fixed_point_position))) + 1) >> 1); \
301 type inv_ln2 = (type)((((0x38AA >> (14 - fixed_point_position)) + 1) >> 1)) | const_one; \
302 type A = (type)(((0x7FBA >> (14 - fixed_point_position)) + 1) >> 1); \
303 type B = (type)(((0x3FE9 >> (14 - fixed_point_position)) + 1) >> 1); \
304 type C = (type)(((0x1693 >> (14 - fixed_point_position)) + 1) >> 1); \
305 type D = (type)(((0x0592 >> (14 - fixed_point_position)) + 1) >> 1); \
306 type m = MUL_SAT_OP_EXPAND(VopA, inv_ln2, stype, size, fixed_point_position); \
307 type dec_m = m >> (type)fixed_point_position; \
308 type alpha = MUL_SAT_OP_EXPAND(dec_m << (type)fixed_point_position, ln2, stype, size, fixed_point_position); \
309 alpha = CONVERT(abs_diff(VopA, alpha), type); \
310 type sum = add_sat(MUL_SAT_OP_EXPAND(alpha, D, stype, size, fixed_point_position), C); \
311 sum = add_sat(MUL_SAT_OP_EXPAND(alpha, sum, stype, size, fixed_point_position), B); \
312 sum = add_sat(MUL_SAT_OP_EXPAND(alpha, sum, stype, size, fixed_point_position), A); \
313 sum = add_sat(MUL_SAT_OP_EXPAND(alpha, sum, stype, size, fixed_point_position), const_one); \
314 return select(select(sum << dec_m, sum >> -dec_m, dec_m < (type)0), (type)stype##_MAX, clz(sum) <= dec_m); \
315 }
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100316
Michalis Spyroud7e82812017-06-20 15:00:14 +0100317EXPQ_IMPL(qs8, qs8x16, 16)
Georgios Pinitas09796752017-07-10 16:05:21 +0100318EXPQ_IMPL(qs16, qs16x16, 16)
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100319
Michalis Spyroud7e82812017-06-20 15:00:14 +0100320#define EXP_OP_EXPAND_STR(a, type, size, position) exp_sat_##type##x##size((a), (position))
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100321#define EXP_OP_EXPAND(a, type, size, position) EXP_OP_EXPAND_STR(a, type, size, position)
322
Michalis Spyroud7e82812017-06-20 15:00:14 +0100323/** Saturate logarithm of a fixed point vector
324 *
325 * @param[in] stype the actual scalar data type.
326 * @param[in] type the actual data type.
327 * @param[in] size the number of the calculated elements.
328 *
329 * @return The result of the fixed point logarithm. The result is saturated in case of overflow
330 */
331#define LOGQ_IMPL(stype, type, size) \
332 inline type log_sat_##type(type VopA, int fixed_point_position) \
333 { \
334 type const_one = (type)(1 << (fixed_point_position)); \
335 type ln2 = (type)(0x58B9 >> (15 - fixed_point_position)); \
336 type A = (type)(0x5C0F >> (14 - fixed_point_position)); \
337 type B = -(type)(0x56AE >> (15 - fixed_point_position)); \
338 type C = (type)(0x2933 >> (15 - fixed_point_position)); \
339 type D = -(type)(0x0AA7 >> (15 - fixed_point_position)); \
340 type inter_a = select(VopA, DIV_SAT_OP_EXPAND(const_one, VopA, stype, size, fixed_point_position), VopA < const_one); \
341 type shift_val = (type)(15 - stype##_SHIFT) - clz(inter_a >> (type)fixed_point_position); \
342 inter_a = inter_a >> shift_val; \
343 inter_a = sub_sat(inter_a, const_one); \
344 type sum = add_sat(MUL_SAT_OP_EXPAND(inter_a, D, stype, size, fixed_point_position), C); \
345 sum = add_sat(MUL_SAT_OP_EXPAND(inter_a, sum, stype, size, fixed_point_position), B); \
346 sum = add_sat(MUL_SAT_OP_EXPAND(inter_a, sum, stype, size, fixed_point_position), A); \
347 sum = MUL_SAT_OP_EXPAND(inter_a, sum, stype, size, fixed_point_position); \
348 sum = MUL_SAT_OP_EXPAND(add_sat(sum, shift_val << (type)fixed_point_position), ln2, stype, size, fixed_point_position); \
349 return select(select(sum, -sum, VopA < const_one), (type)0, VopA < (type)0); \
350 }
351
352LOGQ_IMPL(qs8, qs8x16, 16)
353
354#define LOG_OP_EXPAND_STR(a, type, size, position) log_sat_##type##x##size((a), (position))
355#define LOG_OP_EXPAND(a, type, size, position) LOG_OP_EXPAND_STR(a, type, size, position)
356
357/** Saturate inverse square root of a fixed point vector
358 *
359 * @param[in] stype the actual scalar data type.
360 * @param[in] type the actual data type.
361 * @param[in] size the number of the calculated elements.
362 *
363 * @return The result of the fixed point inverse square root. The result is saturated in case of overflow
364 */
365#define INVSQRTQ_IMPL(stype, type, size) \
366 inline type invsqrt_sat_##type(type VopA, int fixed_point_position) \
367 { \
368 type const_three = (type)(3 << (fixed_point_position)); \
369 type shift_value = (type)(16 - stype##_SHIFT) - (clz(VopA) + (type)fixed_point_position); \
370 type temp = select(VopA >> shift_value, VopA << (-shift_value), shift_value < (type)0); \
371 type x = temp; \
372 x = MUL_SAT_OP_EXPAND(x, sub_sat(const_three, MUL_SAT_OP_EXPAND(MUL_SAT_OP_EXPAND(x, x, stype, size, fixed_point_position), temp, stype, size, fixed_point_position)), stype, size, fixed_point_position) >> 1; \
373 x = MUL_SAT_OP_EXPAND(x, sub_sat(const_three, MUL_SAT_OP_EXPAND(MUL_SAT_OP_EXPAND(x, x, stype, size, fixed_point_position), temp, stype, size, fixed_point_position)), stype, size, fixed_point_position) >> 1; \
374 x = MUL_SAT_OP_EXPAND(x, sub_sat(const_three, MUL_SAT_OP_EXPAND(MUL_SAT_OP_EXPAND(x, x, stype, size, fixed_point_position), temp, stype, size, fixed_point_position)), stype, size, fixed_point_position) >> 1; \
375 type res = select(x >> (shift_value >> 1), x << ((-shift_value) >> 1), shift_value < (type)0); \
376 return select(res, stype##_MAX, res < (type)0); \
377 }
378
379INVSQRTQ_IMPL(qs8, qs8x16, 16)
380
381#define INVSQRT_OP_EXPAND_STR(a, type, size, position) invsqrt_sat_##type##x##size((a), (position))
382#define INVSQRT_OP_EXPAND(a, type, size, position) INVSQRT_OP_EXPAND_STR(a, type, size, position)
383
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100384#endif // ARM_COMPUTE_FIXED_POINT_H