blob: 2c100c2e284b390f49d393f6dc4f864a898fbd41 [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)
38
39#define qs8_MIN ((char)CHAR_MIN)
40#define qs8_MAX ((char)CHAR_MAX)
41#define qs16_MIN ((short)SHRT_MIN)
42#define qs16_MAX ((short)SHRT_MAX)
43
44#define qu8_MIN ((uchar)0)
45#define qu8_MAX ((uchar)UCHAR_MAX)
46#define qu16_MIN ((ushort)0)
47#define qu16_MAX ((ushort)USHRT_MAX)
48
49#define qs8_TYPE char
50#define qs8x1_TYPE char
51#define qs8x2_TYPE char2
52#define qs8x4_TYPE char4
53#define qs8x8_TYPE char8
54#define qs8x16_TYPE char16
55
56#define qs16_TYPE short
57#define qs16x1_TYPE short
58#define qs16x2_TYPE short2
59#define qs16x4_TYPE short4
60#define qs16x8_TYPE short8
61#define qs16x16_TYPE short16
62
63#undef VEC_DATA_TYPE_STR
64#undef VEC_DATA_TYPE
65#undef CONVERT_STR
66#undef CONVERT
67#undef CONVERT_SAT_STR
68#undef CONVERT_SAT
69
70#define VEC_DATA_TYPE_STR(type, size) type##x##size
71#define VEC_DATA_TYPE(type, size) VEC_DATA_TYPE_STR(type, size)
72
73#define CONVERT_STR3(x, type, rtype) (convert_##rtype((x)))
74#define CONVERT_STR2(x, type, rtype) CONVERT_STR3(x, type, rtype)
75#define CONVERT_STR(x, type) CONVERT_STR2(x, type, type##_TYPE)
76#define CONVERT(x, type) CONVERT_STR(x, type)
77
78#define CONVERT_SAT_STR3(x, type, rtype) (convert_##rtype##_sat((x)))
79#define CONVERT_SAT_STR2(x, type, rtype) CONVERT_SAT_STR3(x, type, rtype)
80#define CONVERT_SAT_STR(x, type) CONVERT_SAT_STR2(x, type, type##_TYPE)
81#define CONVERT_SAT(x, type) CONVERT_SAT_STR(x, type)
82
83/* Computes max of fixed point types.
84 *
85 * @param[in] type is the actual data type.
86 *
87 * @return The result of the fixed point vector maximum.
88 */
89#define MAXQ_IMPL(type) \
90 inline type max_##type(type VopA, type VopB) \
91 { \
92 return max(VopA, VopB); \
93 }
94
95MAXQ_IMPL(qs8x1)
96MAXQ_IMPL(qs8x2)
97MAXQ_IMPL(qs8x4)
98MAXQ_IMPL(qs8x8)
99MAXQ_IMPL(qs8x16)
100
101#define MAX_OP_EXPAND_STR(a, b, type, size) max_##type##x##size((a), (b))
102#define MAX_OP_EXPAND(a, b, type, size) MAX_OP_EXPAND_STR(a, b, type, size)
103
104/* Computes saturated addition of fixed point types.
105 *
106 * @param[in] type is the actual data type.
107 *
108 * @return The result of the fixed point vector addition. The result is saturated in case of overflow
109 */
110#define ADDQ_SAT_IMPL(type) \
111 inline type add_sat_##type(type VopA, type VopB) \
112 { \
113 return add_sat(VopA, VopB); \
114 }
115
116ADDQ_SAT_IMPL(qs8x1)
117ADDQ_SAT_IMPL(qs8x2)
118ADDQ_SAT_IMPL(qs8x4)
119ADDQ_SAT_IMPL(qs8x8)
120ADDQ_SAT_IMPL(qs8x16)
121
122#define ADD_SAT_OP_EXPAND_STR(a, b, type, size) add_sat_##type##x##size((a), (b))
123#define ADD_SAT_OP_EXPAND(a, b, type, size) ADD_SAT_OP_EXPAND_STR(a, b, type, size)
124
125/* Computes saturated subtraction of fixed point types.
126 *
127 * @param[in] type is the actual data type.
128 *
129 * @return The result of the fixed point vector subtraction. The result is saturated in case of overflow
130 */
131#define SUBQ_SAT_IMPL(type) \
132 inline type sub_sat_##type(type VopA, type VopB) \
133 { \
134 return sub_sat(VopA, VopB); \
135 }
136
137SUBQ_SAT_IMPL(qs8x1)
138SUBQ_SAT_IMPL(qs8x2)
139SUBQ_SAT_IMPL(qs8x4)
140SUBQ_SAT_IMPL(qs8x8)
141SUBQ_SAT_IMPL(qs8x16)
142
143#define SUB_SAT_OP_EXPAND_STR(a, b, type, size) sub_sat_##type##x##size((a), (b))
144#define SUB_SAT_OP_EXPAND(a, b, type, size) SUB_SAT_OP_EXPAND_STR(a, b, type, size)
145
146/* Saturate multiply of two fixed point vectors
147 *
148 * @param[in] type is the actual data type.
149 * @param[in] itype is the intermediate data type.
150 *
151 * @return The result of the fixed point vector subtraction. The result is saturated in case of overflow
152 */
153#define MULQ_SAT_IMPL(type, itype) \
154 inline type mul_sat_##type(type VopA, type VopB, int fixed_point_position) \
155 { \
156 itype round_val = (itype)(1 << (fixed_point_position - 1)); \
157 itype res = mad_sat(CONVERT((VopA), itype), CONVERT((VopB), itype), round_val); \
158 return CONVERT_SAT((res >> (itype)fixed_point_position), type); \
159 }
160
161MULQ_SAT_IMPL(qs8x16, qs16x16)
162
163#define MUL_SAT_OP_EXPAND_STR(a, b, type, size, position) mul_sat_##type##x##size((a), (b), (position))
164#define MUL_SAT_OP_EXPAND(a, b, type, size, position) MUL_SAT_OP_EXPAND_STR(a, b, type, size, position)
165
166/** Saturate division of two fixed point vectors
167 *
168 * @param[in] stype is the actual scalar data type.
169 * @param[in] type is the actual data type.
170 * @param[in] itype is the intermediate data type.
171 *
172 * @return The result of the fixed point division. The result is saturated in case of overflow
173 */
174#define DIVQ_SAT_IMPL(stype, type, itype) \
175 inline type div_sat_##type(type VopA, type VopB, int fixed_point_position) \
176 { \
177 itype conv_a = CONVERT((VopA), itype); \
178 itype denominator = CONVERT((VopB), itype); \
179 itype numerator = conv_a << (itype)(fixed_point_position); \
180 itype res = select(numerator / denominator, select((itype)stype##_MAX, (itype)stype##_MIN, conv_a < (itype)0), denominator == (itype)0); \
181 return CONVERT_SAT((res), type); \
182 }
183
184DIVQ_SAT_IMPL(qs8, qs8x16, qs16x16)
185
186#define DIV_SAT_OP_EXPAND_STR(a, b, type, size, position) div_sat_##type##x##size((a), (b), (position))
187#define DIV_SAT_OP_EXPAND(a, b, type, size, position) DIV_SAT_OP_EXPAND_STR(a, b, type, size, position)
188
189/** Saturate exponential fixed point 8 bit (16 elements)
190 *
191 * @param[in] a 8 bit fixed point input vector
192 * @param[in] fixed_point_position Fixed point position that expresses the number of bits for the fractional part of the number
193 *
194 * @return The result of the 8 bit fixed point exponential. The result is saturated in case of overflow
195 */
196qs8x16 inline exp_qs8x16(qs8x16 a, int fixed_point_position)
197{
198 // Constants (literal constants are calculated by converting the respective float to the fixed point with the highest supported fixed point position)
199 char16 const_one = (char16)(1 << (fixed_point_position));
200 char16 ln2 = (char16)(((0x58 >> (6 - fixed_point_position)) + 1) >> 1); // 0.693147
201 char16 inv_ln2 = ((char16)(((0x38 >> (6 - (fixed_point_position))) + 1) >> 1)) | const_one; // 1.442695
202 char16 A = (char16)(((0x7F >> (6 - (fixed_point_position))) + 1) >> 1); // 0.9978546
203 char16 B = (char16)(((0x3F >> (6 - (fixed_point_position))) + 1) >> 1); // 0.4994721
204 char16 C = (char16)(((0x16 >> (6 - (fixed_point_position))) + 1) >> 1); // 0.1763723
205 char16 D = (char16)(((0x05 >> (6 - (fixed_point_position))) + 1) >> 1); // 0.0435108
206
207 // Perform range reduction [-log(2),log(2)]
208 char16 m = mul_sat_qs8x16(a, inv_ln2, fixed_point_position);
209
210 // get decimal part of m
211 char16 dec_m = m >> (char16)fixed_point_position;
212
213 char16 alpha = mul_sat_qs8x16(dec_m << (char16)fixed_point_position, ln2, fixed_point_position);
214 alpha = convert_char16(abs_diff(a, alpha));
215
216 // Polynomial expansion
217 char16 sum = add_sat_qs8x16(mul_sat_qs8x16(alpha, D, fixed_point_position), C);
218 sum = add_sat_qs8x16(mul_sat_qs8x16(alpha, sum, fixed_point_position), B);
219 sum = add_sat_qs8x16(mul_sat_qs8x16(alpha, sum, fixed_point_position), A);
220 sum = add_sat_qs8x16(mul_sat_qs8x16(alpha, sum, fixed_point_position), const_one);
221
222 // Reconstruct and saturate result
223 return select(select(sum << dec_m, sum >> -dec_m, dec_m < (char16)0), (char16)0x7F, clz(sum) <= dec_m);
224}
225
226#define EXP_OP_EXPAND_STR(a, type, size, position) exp_##type##x##size((a), (position))
227#define EXP_OP_EXPAND(a, type, size, position) EXP_OP_EXPAND_STR(a, type, size, position)
228
229#endif // ARM_COMPUTE_FIXED_POINT_H