2 * Copyright 2012-15 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
26 #ifndef __DAL_FIXED31_32_H__
27 #define __DAL_FIXED31_32_H__
31 #define FIXED31_32_BITS_PER_FRACTIONAL_PART 32
35 * Arithmetic operations on real numbers
36 * represented as fixed-point numbers.
37 * There are: 1 bit for sign,
38 * 31 bit for integer part,
39 * 32 bits for fractional part.
42 * Currently, overflows and underflows are asserted;
43 * no special result returned.
55 static const struct fixed31_32 dal_fixed31_32_zero = { 0 };
56 static const struct fixed31_32 dal_fixed31_32_epsilon = { 1LL };
57 static const struct fixed31_32 dal_fixed31_32_half = { 0x80000000LL };
58 static const struct fixed31_32 dal_fixed31_32_one = { 0x100000000LL };
60 static const struct fixed31_32 dal_fixed31_32_pi = { 13493037705LL };
61 static const struct fixed31_32 dal_fixed31_32_two_pi = { 26986075409LL };
62 static const struct fixed31_32 dal_fixed31_32_e = { 11674931555LL };
63 static const struct fixed31_32 dal_fixed31_32_ln2 = { 2977044471LL };
64 static const struct fixed31_32 dal_fixed31_32_ln2_div_2 = { 1488522236LL };
68 * Initialization routines
73 * result = numerator / denominator
75 struct fixed31_32 dal_fixed31_32_from_fraction(
83 struct fixed31_32 dal_fixed31_32_from_int_nonconst(int64_t arg);
84 static inline struct fixed31_32 dal_fixed31_32_from_int(int64_t arg)
86 if (__builtin_constant_p(arg)) {
87 struct fixed31_32 res;
88 BUILD_BUG_ON((LONG_MIN > arg) || (arg > LONG_MAX));
89 res.value = arg << FIXED31_32_BITS_PER_FRACTIONAL_PART;
92 return dal_fixed31_32_from_int_nonconst(arg);
104 static inline struct fixed31_32 dal_fixed31_32_neg(struct fixed31_32 arg)
106 struct fixed31_32 res;
108 res.value = -arg.value;
115 * result = abs(arg) := (arg >= 0) ? arg : -arg
117 static inline struct fixed31_32 dal_fixed31_32_abs(struct fixed31_32 arg)
120 return dal_fixed31_32_neg(arg);
127 * Binary relational operators
132 * result = arg1 < arg2
134 static inline bool dal_fixed31_32_lt(struct fixed31_32 arg1,
135 struct fixed31_32 arg2)
137 return arg1.value < arg2.value;
142 * result = arg1 <= arg2
144 static inline bool dal_fixed31_32_le(struct fixed31_32 arg1,
145 struct fixed31_32 arg2)
147 return arg1.value <= arg2.value;
152 * result = arg1 == arg2
154 static inline bool dal_fixed31_32_eq(struct fixed31_32 arg1,
155 struct fixed31_32 arg2)
157 return arg1.value == arg2.value;
162 * result = min(arg1, arg2) := (arg1 <= arg2) ? arg1 : arg2
164 static inline struct fixed31_32 dal_fixed31_32_min(struct fixed31_32 arg1,
165 struct fixed31_32 arg2)
167 if (arg1.value <= arg2.value)
175 * result = max(arg1, arg2) := (arg1 <= arg2) ? arg2 : arg1
177 static inline struct fixed31_32 dal_fixed31_32_max(struct fixed31_32 arg1,
178 struct fixed31_32 arg2)
180 if (arg1.value <= arg2.value)
188 * | min_value, when arg <= min_value
189 * result = | arg, when min_value < arg < max_value
190 * | max_value, when arg >= max_value
192 static inline struct fixed31_32 dal_fixed31_32_clamp(
193 struct fixed31_32 arg,
194 struct fixed31_32 min_value,
195 struct fixed31_32 max_value)
197 if (dal_fixed31_32_le(arg, min_value))
199 else if (dal_fixed31_32_le(max_value, arg))
207 * Binary shift operators
212 * result = arg << shift
214 struct fixed31_32 dal_fixed31_32_shl(
215 struct fixed31_32 arg,
220 * result = arg >> shift
222 static inline struct fixed31_32 dal_fixed31_32_shr(
223 struct fixed31_32 arg,
226 struct fixed31_32 res;
227 res.value = arg.value >> shift;
233 * Binary additive operators
238 * result = arg1 + arg2
240 struct fixed31_32 dal_fixed31_32_add(
241 struct fixed31_32 arg1,
242 struct fixed31_32 arg2);
246 * result = arg1 + arg2
248 static inline struct fixed31_32 dal_fixed31_32_add_int(struct fixed31_32 arg1,
251 return dal_fixed31_32_add(arg1,
252 dal_fixed31_32_from_int(arg2));
257 * result = arg1 - arg2
259 struct fixed31_32 dal_fixed31_32_sub(
260 struct fixed31_32 arg1,
261 struct fixed31_32 arg2);
265 * result = arg1 - arg2
267 static inline struct fixed31_32 dal_fixed31_32_sub_int(struct fixed31_32 arg1,
270 return dal_fixed31_32_sub(arg1,
271 dal_fixed31_32_from_int(arg2));
277 * Binary multiplicative operators
282 * result = arg1 * arg2
284 struct fixed31_32 dal_fixed31_32_mul(
285 struct fixed31_32 arg1,
286 struct fixed31_32 arg2);
291 * result = arg1 * arg2
293 static inline struct fixed31_32 dal_fixed31_32_mul_int(struct fixed31_32 arg1,
296 return dal_fixed31_32_mul(arg1,
297 dal_fixed31_32_from_int(arg2));
302 * result = square(arg) := arg * arg
304 struct fixed31_32 dal_fixed31_32_sqr(
305 struct fixed31_32 arg);
309 * result = arg1 / arg2
311 static inline struct fixed31_32 dal_fixed31_32_div_int(struct fixed31_32 arg1,
314 return dal_fixed31_32_from_fraction(arg1.value,
315 dal_fixed31_32_from_int(arg2).value);
320 * result = arg1 / arg2
322 static inline struct fixed31_32 dal_fixed31_32_div(struct fixed31_32 arg1,
323 struct fixed31_32 arg2)
325 return dal_fixed31_32_from_fraction(arg1.value,
331 * Reciprocal function
336 * result = reciprocal(arg) := 1 / arg
339 * No special actions taken in case argument is zero.
341 struct fixed31_32 dal_fixed31_32_recip(
342 struct fixed31_32 arg);
346 * Trigonometric functions
351 * result = sinc(arg) := sin(arg) / arg
354 * Argument specified in radians,
355 * internally it's normalized to [-2pi...2pi] range.
357 struct fixed31_32 dal_fixed31_32_sinc(
358 struct fixed31_32 arg);
365 * Argument specified in radians,
366 * internally it's normalized to [-2pi...2pi] range.
368 struct fixed31_32 dal_fixed31_32_sin(
369 struct fixed31_32 arg);
376 * Argument specified in radians
377 * and should be in [-2pi...2pi] range -
378 * passing arguments outside that range
379 * will cause incorrect result!
381 struct fixed31_32 dal_fixed31_32_cos(
382 struct fixed31_32 arg);
386 * Transcendent functions
394 * Currently, function is verified for abs(arg) <= 1.
396 struct fixed31_32 dal_fixed31_32_exp(
397 struct fixed31_32 arg);
404 * Currently, abs(arg) should be less than 1.
405 * No normalization is done.
406 * Currently, no special actions taken
407 * in case of invalid argument(s). Take care!
409 struct fixed31_32 dal_fixed31_32_log(
410 struct fixed31_32 arg);
419 * result = pow(arg1, arg2)
422 * Currently, abs(arg1) should be less than 1. Take care!
424 struct fixed31_32 dal_fixed31_32_pow(
425 struct fixed31_32 arg1,
426 struct fixed31_32 arg2);
435 * result = floor(arg) := greatest integer lower than or equal to arg
437 int32_t dal_fixed31_32_floor(
438 struct fixed31_32 arg);
442 * result = round(arg) := integer nearest to arg
444 int32_t dal_fixed31_32_round(
445 struct fixed31_32 arg);
449 * result = ceil(arg) := lowest integer greater than or equal to arg
451 int32_t dal_fixed31_32_ceil(
452 struct fixed31_32 arg);
454 /* the following two function are used in scaler hw programming to convert fixed
455 * point value to format 2 bits from integer part and 19 bits from fractional
456 * part. The same applies for u0d19, 0 bits from integer part and 19 bits from
460 uint32_t dal_fixed31_32_u2d19(
461 struct fixed31_32 arg);
463 uint32_t dal_fixed31_32_u0d19(
464 struct fixed31_32 arg);
467 uint32_t dal_fixed31_32_clamp_u0d14(
468 struct fixed31_32 arg);
470 uint32_t dal_fixed31_32_clamp_u0d10(
471 struct fixed31_32 arg);