blob: 4de7fc576bd8b04a238fe64fe3b77d88aa6860dc [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
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100102/** Computes saturating absolute value of fixed point vector.
103 *
104 * @param[in] type the actual data type.
105 *
106 * @return The result of the fixed point absolute value.
107 */
108#define ABSQ_SAT_IMPL(type) \
109 inline type abs_##type##_sat(type VopA) \
110 { \
111 return CONVERT_SAT(abs(VopA), type); \
112 }
113
114ABSQ_SAT_IMPL(qs8x16)
115ABSQ_SAT_IMPL(qs16x8)
116
117#define ABS_SAT_OP_EXPAND_STR(a, type, size) abs_##type##x##size##_sat((a))
118#define ABS_SAT_OP_EXPAND(a, type, size) ABS_SAT_OP_EXPAND_STR(a, type, size)
119
Michalis Spyroud7e82812017-06-20 15:00:14 +0100120/** Computes max of fixed point types.
121 *
122 * @param[in] type the actual data type.
123 *
124 * @return The result of the fixed point maximum.
125 */
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100126#define MAXQ_IMPL(type) \
127 inline type max_##type(type VopA, type VopB) \
128 { \
129 return max(VopA, VopB); \
130 }
131
132MAXQ_IMPL(qs8x1)
133MAXQ_IMPL(qs8x2)
134MAXQ_IMPL(qs8x4)
135MAXQ_IMPL(qs8x8)
136MAXQ_IMPL(qs8x16)
Georgios Pinitas09796752017-07-10 16:05:21 +0100137MAXQ_IMPL(qs16x1)
138MAXQ_IMPL(qs16x2)
139MAXQ_IMPL(qs16x4)
140MAXQ_IMPL(qs16x8)
141MAXQ_IMPL(qs16x16)
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100142
143#define MAX_OP_EXPAND_STR(a, b, type, size) max_##type##x##size((a), (b))
144#define MAX_OP_EXPAND(a, b, type, size) MAX_OP_EXPAND_STR(a, b, type, size)
145
Michalis Spyroud7e82812017-06-20 15:00:14 +0100146/** Computes saturated addition of fixed point types.
147 *
148 * @param[in] type the actual data type.
149 *
150 * @return The result of the fixed point addition. The result is saturated in case of overflow
151 */
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100152#define ADDQ_SAT_IMPL(type) \
153 inline type add_sat_##type(type VopA, type VopB) \
154 { \
155 return add_sat(VopA, VopB); \
156 }
157
158ADDQ_SAT_IMPL(qs8x1)
159ADDQ_SAT_IMPL(qs8x2)
160ADDQ_SAT_IMPL(qs8x4)
161ADDQ_SAT_IMPL(qs8x8)
162ADDQ_SAT_IMPL(qs8x16)
Gian Marco Iodice7d323a62017-07-05 20:05:23 +0100163ADDQ_SAT_IMPL(qs16x1)
164ADDQ_SAT_IMPL(qs16x2)
165ADDQ_SAT_IMPL(qs16x4)
166ADDQ_SAT_IMPL(qs16x8)
167ADDQ_SAT_IMPL(qs16x16)
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100168
169#define ADD_SAT_OP_EXPAND_STR(a, b, type, size) add_sat_##type##x##size((a), (b))
170#define ADD_SAT_OP_EXPAND(a, b, type, size) ADD_SAT_OP_EXPAND_STR(a, b, type, size)
171
Michalis Spyroud7e82812017-06-20 15:00:14 +0100172/** Computes saturated subtraction of fixed point types.
173 *
174 * @param[in] type the actual data type.
175 *
176 * @return The result of the fixed point subtraction. The result is saturated in case of overflow
177 */
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100178#define SUBQ_SAT_IMPL(type) \
179 inline type sub_sat_##type(type VopA, type VopB) \
180 { \
181 return sub_sat(VopA, VopB); \
182 }
183
184SUBQ_SAT_IMPL(qs8x1)
185SUBQ_SAT_IMPL(qs8x2)
186SUBQ_SAT_IMPL(qs8x4)
187SUBQ_SAT_IMPL(qs8x8)
188SUBQ_SAT_IMPL(qs8x16)
Georgios Pinitas09796752017-07-10 16:05:21 +0100189SUBQ_SAT_IMPL(qs16x1)
190SUBQ_SAT_IMPL(qs16x2)
191SUBQ_SAT_IMPL(qs16x4)
192SUBQ_SAT_IMPL(qs16x8)
193SUBQ_SAT_IMPL(qs16x16)
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100194
195#define SUB_SAT_OP_EXPAND_STR(a, b, type, size) sub_sat_##type##x##size((a), (b))
196#define SUB_SAT_OP_EXPAND(a, b, type, size) SUB_SAT_OP_EXPAND_STR(a, b, type, size)
197
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100198/* Multiply of two fixed point numbers
199 *
200 * @param[in] type the actual data type.
201 * @param[in] itype the intermediate data type.
202 *
203 * @return The result of the fixed point multiplication.
204 */
205#define MULQ_IMPL(type, itype) \
206 inline type mul_##type(type VopA, type VopB, int fixed_point_position) \
207 { \
208 itype round_val = (itype)(1 << (fixed_point_position - 1)); \
209 itype res = CONVERT((VopA), itype) * CONVERT((VopB), itype) + round_val; \
210 return CONVERT((res >> (itype)fixed_point_position), type); \
211 }
212
213MULQ_IMPL(qs8x16, qs16x16)
214MULQ_IMPL(qs16x16, qs32x16)
215
216#define MUL_OP_EXPAND_STR(a, b, type, size, position) mul_##type##x##size((a), (b), (position))
217#define MUL_OP_EXPAND(a, b, type, size, position) MUL_OP_EXPAND_STR(a, b, type, size, position)
218
219/* Saturate multiply of two fixed point numbers
220 *
221 * @param[in] type the actual data type.
222 * @param[in] itype the intermediate data type.
223 *
224 * @return The result of the fixed point multiplication. The result is saturated in case of overflow
225 */
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100226#define MULQ_SAT_IMPL(type, itype) \
227 inline type mul_sat_##type(type VopA, type VopB, int fixed_point_position) \
228 { \
229 itype round_val = (itype)(1 << (fixed_point_position - 1)); \
230 itype res = mad_sat(CONVERT((VopA), itype), CONVERT((VopB), itype), round_val); \
231 return CONVERT_SAT((res >> (itype)fixed_point_position), type); \
232 }
233
234MULQ_SAT_IMPL(qs8x16, qs16x16)
Gian Marco Iodice8a383692017-07-03 17:41:47 +0100235MULQ_SAT_IMPL(qs16x8, qs32x8)
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100236MULQ_SAT_IMPL(qs16x16, qs32x16)
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100237
238#define MUL_SAT_OP_EXPAND_STR(a, b, type, size, position) mul_sat_##type##x##size((a), (b), (position))
239#define MUL_SAT_OP_EXPAND(a, b, type, size, position) MUL_SAT_OP_EXPAND_STR(a, b, type, size, position)
240
Michalis Spyroud7e82812017-06-20 15:00:14 +0100241/** Saturate multiply-accumulate
242 *
243 * @param[in] type the actual data type.
244 * @param[in] itype the intermediate data type.
245 *
246 * @return The result of the fixed point multiply-accumulate. The result is saturated in case of overflow
247 */
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100248#define MLAQ_SAT_IMPL(type, itype) \
249 type mla_sat_##type(type VopA, type VopB, type VopC, int fixed_point_position) \
250 { \
251 itype res = mad_sat(CONVERT(VopB, itype), CONVERT(VopC, itype), (itype)(1 << (fixed_point_position - 1))); \
252 return add_sat(VopA, CONVERT_SAT(res >> (itype)fixed_point_position, type)); \
253 }
254
255MLAQ_SAT_IMPL(qs8x8, qs16x8)
256MLAQ_SAT_IMPL(qs8x16, qs16x16)
Gian Marco Iodice8a383692017-07-03 17:41:47 +0100257MLAQ_SAT_IMPL(qs16x8, qs32x8)
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100258
259#define MLA_SAT_OP_EXPAND_STR(a, b, c, type, size, position) mla_sat_##type##x##size((a), (b), (c), (position))
260#define MLA_SAT_OP_EXPAND(a, b, c, type, size, position) MLA_SAT_OP_EXPAND_STR(a, b, c, type, size, position)
261
Michalis Spyroud7e82812017-06-20 15:00:14 +0100262/** Saturate multiply-accumulate long
263 *
264 * @param[in] type the actual data type.
265 * @param[in] itype the intermediate data type.
266 *
267 * @return The result of the fixed point multiply-accumulate long. The result is saturated in case of overflow
268 */
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100269#define MLALQ_SAT_IMPL(type, itype) \
270 itype mlal_sat_##type(itype VopA, type VopB, type VopC, int fixed_point_position) \
271 { \
272 itype res = mad_sat(CONVERT(VopB, itype), CONVERT(VopC, itype), (itype)(1 << (fixed_point_position - 1))); \
273 return add_sat(VopA, res >> (itype)fixed_point_position); \
274 }
275
276MLALQ_SAT_IMPL(qs8x8, qs16x8)
Gian Marco Iodice8a383692017-07-03 17:41:47 +0100277MLALQ_SAT_IMPL(qs16x8, qs32x8)
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100278
279#define MLAL_SAT_OP_EXPAND_STR(a, b, c, type, size, position) mlal_sat_##type##x##size((a), (b), (c), (position))
280#define MLAL_SAT_OP_EXPAND(a, b, c, type, size, position) MLAL_SAT_OP_EXPAND_STR(a, b, c, type, size, position)
281
282/** Saturate division of two fixed point numbers
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100283 *
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100284 * @param[in] stype the actual scalar data type.
285 * @param[in] type the actual data type.
286 * @param[in] itype the intermediate data type.
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100287 *
288 * @return The result of the fixed point division. The result is saturated in case of overflow
289 */
290#define DIVQ_SAT_IMPL(stype, type, itype) \
291 inline type div_sat_##type(type VopA, type VopB, int fixed_point_position) \
292 { \
293 itype conv_a = CONVERT((VopA), itype); \
294 itype denominator = CONVERT((VopB), itype); \
295 itype numerator = conv_a << (itype)(fixed_point_position); \
296 itype res = select(numerator / denominator, select((itype)stype##_MAX, (itype)stype##_MIN, conv_a < (itype)0), denominator == (itype)0); \
297 return CONVERT_SAT((res), type); \
298 }
299
300DIVQ_SAT_IMPL(qs8, qs8x16, qs16x16)
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100301DIVQ_SAT_IMPL(qs16, qs16x8, qs32x8)
Georgios Pinitas09796752017-07-10 16:05:21 +0100302DIVQ_SAT_IMPL(qs16, qs16x16, qs32x16)
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100303
304#define DIV_SAT_OP_EXPAND_STR(a, b, type, size, position) div_sat_##type##x##size((a), (b), (position))
305#define DIV_SAT_OP_EXPAND(a, b, type, size, position) DIV_SAT_OP_EXPAND_STR(a, b, type, size, position)
306
Michalis Spyroud7e82812017-06-20 15:00:14 +0100307/** Saturate exponential of a fixed point vector
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100308 *
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100309 * @note Implemented approach uses taylor polynomial to approximate the exponential function.
310 *
Michalis Spyroud7e82812017-06-20 15:00:14 +0100311 * @param[in] stype the actual scalar data type.
312 * @param[in] type the actual data type.
313 * @param[in] size the number of the calculated elements.
314 *
315 * @return The result of the fixed point exponential. The result is saturated in case of overflow
316 */
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100317#define EXPQ_IMPL(stype, type, size) \
318 inline type exp_sat_##type(type VopA, int fixed_point_position) \
319 { \
320 type const_one = (type)(1 << (fixed_point_position)); \
321 type ln2 = (type)((((0x58B9 >> (14 - fixed_point_position))) + 1) >> 1); \
322 type inv_ln2 = (type)((((0x38AA >> (14 - fixed_point_position)) + 1) >> 1)) | const_one; \
323 type A = (type)(((0x7FBA >> (14 - fixed_point_position)) + 1) >> 1); \
324 type B = (type)(((0x3FE9 >> (14 - fixed_point_position)) + 1) >> 1); \
325 type C = (type)(((0x1693 >> (14 - fixed_point_position)) + 1) >> 1); \
326 type D = (type)(((0x0592 >> (14 - fixed_point_position)) + 1) >> 1); \
327 type m = MUL_SAT_OP_EXPAND(VopA, inv_ln2, stype, size, fixed_point_position); \
328 type dec_m = m >> (type)fixed_point_position; \
329 type alpha = MUL_SAT_OP_EXPAND(dec_m << (type)fixed_point_position, ln2, stype, size, fixed_point_position); \
330 alpha = CONVERT(abs_diff(VopA, alpha), type); \
331 type sum = add_sat(MUL_SAT_OP_EXPAND(alpha, D, stype, size, fixed_point_position), C); \
332 sum = add_sat(MUL_SAT_OP_EXPAND(alpha, sum, stype, size, fixed_point_position), B); \
333 sum = add_sat(MUL_SAT_OP_EXPAND(alpha, sum, stype, size, fixed_point_position), A); \
334 sum = add_sat(MUL_SAT_OP_EXPAND(alpha, sum, stype, size, fixed_point_position), const_one); \
335 return select((type)stype##_MAX, select(sum << dec_m, sum >> -dec_m, dec_m < (type)0), clz(sum) > dec_m); /* Saturate result if needed */ \
Michalis Spyroud7e82812017-06-20 15:00:14 +0100336 }
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100337
Michalis Spyroud7e82812017-06-20 15:00:14 +0100338EXPQ_IMPL(qs8, qs8x16, 16)
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100339EXPQ_IMPL(qs16, qs16x8, 8)
Georgios Pinitas09796752017-07-10 16:05:21 +0100340EXPQ_IMPL(qs16, qs16x16, 16)
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100341
Michalis Spyroud7e82812017-06-20 15:00:14 +0100342#define EXP_OP_EXPAND_STR(a, type, size, position) exp_sat_##type##x##size((a), (position))
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100343#define EXP_OP_EXPAND(a, type, size, position) EXP_OP_EXPAND_STR(a, type, size, position)
344
Michalis Spyroud7e82812017-06-20 15:00:14 +0100345/** Saturate logarithm of a fixed point vector
346 *
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100347 * @note Implemented approach uses taylor polynomial to approximate the logarithm function.
348 *
Michalis Spyroud7e82812017-06-20 15:00:14 +0100349 * @param[in] stype the actual scalar data type.
350 * @param[in] type the actual data type.
351 * @param[in] size the number of the calculated elements.
352 *
353 * @return The result of the fixed point logarithm. The result is saturated in case of overflow
354 */
355#define LOGQ_IMPL(stype, type, size) \
356 inline type log_sat_##type(type VopA, int fixed_point_position) \
357 { \
358 type const_one = (type)(1 << (fixed_point_position)); \
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100359 type ln2 = (type)(0x58B9 >> (15 - fixed_point_position)); /* 1.4384189 */ \
360 type A = (type)(0x5C0F >> (14 - fixed_point_position)); /* 1.4384189 */ \
361 type B = -(type)(0x56AE >> (15 - fixed_point_position)); /* -0.6771900 */ \
362 type C = (type)(0x2933 >> (15 - fixed_point_position)); /* 0.3218538 */ \
363 type D = -(type)(0x0AA7 >> (15 - fixed_point_position)); /* -0.0832229 */ \
Michalis Spyroud7e82812017-06-20 15:00:14 +0100364 type inter_a = select(VopA, DIV_SAT_OP_EXPAND(const_one, VopA, stype, size, fixed_point_position), VopA < const_one); \
365 type shift_val = (type)(15 - stype##_SHIFT) - clz(inter_a >> (type)fixed_point_position); \
366 inter_a = inter_a >> shift_val; \
367 inter_a = sub_sat(inter_a, const_one); \
368 type sum = add_sat(MUL_SAT_OP_EXPAND(inter_a, D, stype, size, fixed_point_position), C); \
369 sum = add_sat(MUL_SAT_OP_EXPAND(inter_a, sum, stype, size, fixed_point_position), B); \
370 sum = add_sat(MUL_SAT_OP_EXPAND(inter_a, sum, stype, size, fixed_point_position), A); \
371 sum = MUL_SAT_OP_EXPAND(inter_a, sum, stype, size, fixed_point_position); \
372 sum = MUL_SAT_OP_EXPAND(add_sat(sum, shift_val << (type)fixed_point_position), ln2, stype, size, fixed_point_position); \
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100373 return select(select(sum, -sum, VopA < const_one), (type)0, VopA < (type)0); /* Saturate result if needed */ \
Michalis Spyroud7e82812017-06-20 15:00:14 +0100374 }
375
376LOGQ_IMPL(qs8, qs8x16, 16)
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100377LOGQ_IMPL(qs16, qs16x8, 8)
Michalis Spyroud7e82812017-06-20 15:00:14 +0100378
379#define LOG_OP_EXPAND_STR(a, type, size, position) log_sat_##type##x##size((a), (position))
380#define LOG_OP_EXPAND(a, type, size, position) LOG_OP_EXPAND_STR(a, type, size, position)
381
382/** Saturate inverse square root of a fixed point vector
383 *
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100384 * @note Implemented approach uses Newton's method to approximate the inverse square root function.
385 *
Michalis Spyroud7e82812017-06-20 15:00:14 +0100386 * @param[in] stype the actual scalar data type.
387 * @param[in] type the actual data type.
388 * @param[in] size the number of the calculated elements.
389 *
390 * @return The result of the fixed point inverse square root. The result is saturated in case of overflow
391 */
392#define INVSQRTQ_IMPL(stype, type, size) \
393 inline type invsqrt_sat_##type(type VopA, int fixed_point_position) \
394 { \
395 type const_three = (type)(3 << (fixed_point_position)); \
396 type shift_value = (type)(16 - stype##_SHIFT) - (clz(VopA) + (type)fixed_point_position); \
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100397 type temp = select(VopA >> shift_value, select((type)stype##_MAX, VopA << (-shift_value), clz(VopA) > (-shift_value)), shift_value < (type)0); \
Michalis Spyroud7e82812017-06-20 15:00:14 +0100398 type x = temp; \
399 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; \
400 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; \
401 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; \
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100402 if(sizeof((stype)(1)) > 1) /* Perform more iterations if datatype is QS16 */ \
403 { \
404 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; \
405 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; \
406 } \
407 type shift_value2 = select(shift_value >> 1, (-shift_value) >> 1, shift_value < (type)0); \
408 return select(x >> shift_value2, select((type)stype##_MAX, x << shift_value2, clz(x) > shift_value2), shift_value < (type)0); /* Saturate result if needed */ \
Michalis Spyroud7e82812017-06-20 15:00:14 +0100409 }
410
411INVSQRTQ_IMPL(qs8, qs8x16, 16)
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100412INVSQRTQ_IMPL(qs16, qs16x8, 8)
Michalis Spyroud7e82812017-06-20 15:00:14 +0100413
414#define INVSQRT_OP_EXPAND_STR(a, type, size, position) invsqrt_sat_##type##x##size((a), (position))
415#define INVSQRT_OP_EXPAND(a, type, size, position) INVSQRT_OP_EXPAND_STR(a, type, size, position)
416
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100417/** Saturate hyperbolic tangent of a fixed point vector
418 *
419 * tanh(x) = (e^2x - 1)/(e^2x + 1)
420 *
421 * @param[in] stype the actual scalar data type.
422 * @param[in] type the actual data type.
423 * @param[in] size the number of the calculated elements.
424 *
425 * @return The result of the fixed point hyperbolic tangent. The result is saturated in case of overflow
426 */
427#define TANHQ_IMPL(stype, type, size) \
428 inline type tanh_sat_##type(type VopA, int fixed_point_position) \
429 { \
430 type const_one = (type)(1 << (fixed_point_position)); \
431 type const_two = (type)(2 << (fixed_point_position)); \
432 type exp2x = EXP_OP_EXPAND(MUL_SAT_OP_EXPAND(const_two, VopA, stype, size, fixed_point_position), stype, size, fixed_point_position); \
433 type num = SUB_SAT_OP_EXPAND(exp2x, const_one, stype, size); \
434 type den = ADD_SAT_OP_EXPAND(exp2x, const_one, stype, size); \
435 return DIV_SAT_OP_EXPAND(num, den, stype, size, fixed_point_position); \
436 }
437
438TANHQ_IMPL(qs8, qs8x16, 16)
439TANHQ_IMPL(qs16, qs16x8, 8)
440
441#define TANH_OP_EXPAND_STR(a, type, size, position) tanh_sat_##type##x##size((a), (position))
442#define TANH_OP_EXPAND(a, type, size, position) TANH_OP_EXPAND_STR(a, type, size, position)
443
steniu01da37e2f2017-06-29 10:14:58 +0100444#define floatx16 float16
445#define float16_TYPE float16
446
447#define CONVERTQ_DOWN_IMPL(in_type, out_type) \
448 inline out_type convert_##out_type##_##in_type(in_type a, int fixed_point_position) \
449 { \
450 return CONVERT(a * (1 << fixed_point_position) + select((in_type)-0.5, (in_type)0.5, isgreater(a, (in_type)0)), out_type); \
451 }
452
453CONVERTQ_DOWN_IMPL(float16, qs8x16)
454CONVERTQ_DOWN_IMPL(float16, qs16x16)
455
456#define CONVERTQ_DOWN_SAT_IMPL(in_type, out_type) \
457 inline out_type convert_##out_type##_##in_type##_sat(in_type a, int fixed_point_position) \
458 { \
459 return CONVERT_SAT(a * (1 << fixed_point_position) + select((in_type)-0.5, (in_type)0.5, isgreater(a, (in_type)0)), out_type); \
460 }
461
462CONVERTQ_DOWN_SAT_IMPL(float16, qs8x16)
463CONVERTQ_DOWN_SAT_IMPL(float16, qs16x16)
464
465#define CONVERTQ_UP_IMPL(in_type, out_type) \
466 inline out_type convert_##out_type##_##in_type(in_type a, int fixed_point_position) \
467 { \
468 return CONVERT(a, out_type) / (1 << fixed_point_position); \
469 }
470
471CONVERTQ_UP_IMPL(qs8x16, float16)
472CONVERTQ_UP_IMPL(qs16x16, float16)
473
Michalis Spyrou172e5702017-06-26 14:18:47 +0100474#define SQCVT_SAT_IMPL(type) \
475 inline type sqcvt_##type##_sat(float a, int fixed_point_position) \
476 { \
477 return CONVERT_SAT((a * (1 << fixed_point_position) + ((a < 0) ? -0.5f : 0.5f)), type); \
478 }
479
480SQCVT_SAT_IMPL(qs8)
481SQCVT_SAT_IMPL(qs16)
482
483#define SQCVT_SAT_OP_EXPAND_STR(a, type, position) sqcvt_##type##_sat((a), (position))
484#define SQCVT_SAT_OP_EXPAND(a, type, position) SQCVT_SAT_OP_EXPAND_STR((a), type, position)
485
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100486#endif // ARM_COMPUTE_FIXED_POINT_H