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__
29 #define FIXED31_32_BITS_PER_FRACTIONAL_PART 32
33 * Arithmetic operations on real numbers
34 * represented as fixed-point numbers.
35 * There are: 1 bit for sign,
36 * 31 bit for integer part,
37 * 32 bits for fractional part.
40 * Currently, overflows and underflows are asserted;
41 * no special result returned.
53 static const struct fixed31_32 dc_fixpt_zero = { 0 };
54 static const struct fixed31_32 dc_fixpt_epsilon = { 1LL };
55 static const struct fixed31_32 dc_fixpt_half = { 0x80000000LL };
56 static const struct fixed31_32 dc_fixpt_one = { 0x100000000LL };
58 static const struct fixed31_32 dc_fixpt_pi = { 13493037705LL };
59 static const struct fixed31_32 dc_fixpt_two_pi = { 26986075409LL };
60 static const struct fixed31_32 dc_fixpt_e = { 11674931555LL };
61 static const struct fixed31_32 dc_fixpt_ln2 = { 2977044471LL };
62 static const struct fixed31_32 dc_fixpt_ln2_div_2 = { 1488522236LL };
66 * Initialization routines
71 * result = numerator / denominator
73 struct fixed31_32 dc_fixpt_from_fraction(long long numerator, long long denominator);
79 static inline struct fixed31_32 dc_fixpt_from_int(int arg)
81 struct fixed31_32 res;
83 res.value = (long long) arg << FIXED31_32_BITS_PER_FRACTIONAL_PART;
97 static inline struct fixed31_32 dc_fixpt_neg(struct fixed31_32 arg)
99 struct fixed31_32 res;
101 res.value = -arg.value;
108 * result = abs(arg) := (arg >= 0) ? arg : -arg
110 static inline struct fixed31_32 dc_fixpt_abs(struct fixed31_32 arg)
113 return dc_fixpt_neg(arg);
120 * Binary relational operators
125 * result = arg1 < arg2
127 static inline bool dc_fixpt_lt(struct fixed31_32 arg1, struct fixed31_32 arg2)
129 return arg1.value < arg2.value;
134 * result = arg1 <= arg2
136 static inline bool dc_fixpt_le(struct fixed31_32 arg1, struct fixed31_32 arg2)
138 return arg1.value <= arg2.value;
143 * result = arg1 == arg2
145 static inline bool dc_fixpt_eq(struct fixed31_32 arg1, struct fixed31_32 arg2)
147 return arg1.value == arg2.value;
152 * result = min(arg1, arg2) := (arg1 <= arg2) ? arg1 : arg2
154 static inline struct fixed31_32 dc_fixpt_min(struct fixed31_32 arg1, struct fixed31_32 arg2)
156 if (arg1.value <= arg2.value)
164 * result = max(arg1, arg2) := (arg1 <= arg2) ? arg2 : arg1
166 static inline struct fixed31_32 dc_fixpt_max(struct fixed31_32 arg1, struct fixed31_32 arg2)
168 if (arg1.value <= arg2.value)
176 * | min_value, when arg <= min_value
177 * result = | arg, when min_value < arg < max_value
178 * | max_value, when arg >= max_value
180 static inline struct fixed31_32 dc_fixpt_clamp(
181 struct fixed31_32 arg,
182 struct fixed31_32 min_value,
183 struct fixed31_32 max_value)
185 if (dc_fixpt_le(arg, min_value))
187 else if (dc_fixpt_le(max_value, arg))
195 * Binary shift operators
200 * result = arg << shift
202 static inline struct fixed31_32 dc_fixpt_shl(struct fixed31_32 arg, unsigned char shift)
204 struct fixed31_32 res;
206 ASSERT(((arg.value >= 0) && (arg.value <= LLONG_MAX >> shift)) ||
207 ((arg.value < 0) && (arg.value >= LLONG_MIN >> shift)));
209 res.value = arg.value << shift;
216 * result = arg >> shift
218 static inline struct fixed31_32 dc_fixpt_shr(struct fixed31_32 arg, unsigned char shift)
220 struct fixed31_32 res;
221 res.value = arg.value >> shift;
227 * Binary additive operators
232 * result = arg1 + arg2
234 static inline struct fixed31_32 dc_fixpt_add(struct fixed31_32 arg1, struct fixed31_32 arg2)
236 struct fixed31_32 res;
238 ASSERT(((arg1.value >= 0) && (LLONG_MAX - arg1.value >= arg2.value)) ||
239 ((arg1.value < 0) && (LLONG_MIN - arg1.value <= arg2.value)));
241 res.value = arg1.value + arg2.value;
248 * result = arg1 + arg2
250 static inline struct fixed31_32 dc_fixpt_add_int(struct fixed31_32 arg1, int arg2)
252 return dc_fixpt_add(arg1, dc_fixpt_from_int(arg2));
257 * result = arg1 - arg2
259 static inline struct fixed31_32 dc_fixpt_sub(struct fixed31_32 arg1, struct fixed31_32 arg2)
261 struct fixed31_32 res;
263 ASSERT(((arg2.value >= 0) && (LLONG_MIN + arg2.value <= arg1.value)) ||
264 ((arg2.value < 0) && (LLONG_MAX + arg2.value >= arg1.value)));
266 res.value = arg1.value - arg2.value;
273 * result = arg1 - arg2
275 static inline struct fixed31_32 dc_fixpt_sub_int(struct fixed31_32 arg1, int arg2)
277 return dc_fixpt_sub(arg1, dc_fixpt_from_int(arg2));
283 * Binary multiplicative operators
288 * result = arg1 * arg2
290 struct fixed31_32 dc_fixpt_mul(struct fixed31_32 arg1, struct fixed31_32 arg2);
295 * result = arg1 * arg2
297 static inline struct fixed31_32 dc_fixpt_mul_int(struct fixed31_32 arg1, int arg2)
299 return dc_fixpt_mul(arg1, dc_fixpt_from_int(arg2));
304 * result = square(arg) := arg * arg
306 struct fixed31_32 dc_fixpt_sqr(struct fixed31_32 arg);
310 * result = arg1 / arg2
312 static inline struct fixed31_32 dc_fixpt_div_int(struct fixed31_32 arg1, long long arg2)
314 return dc_fixpt_from_fraction(arg1.value, dc_fixpt_from_int(arg2).value);
319 * result = arg1 / arg2
321 static inline struct fixed31_32 dc_fixpt_div(struct fixed31_32 arg1, struct fixed31_32 arg2)
323 return dc_fixpt_from_fraction(arg1.value, arg2.value);
328 * Reciprocal function
333 * result = reciprocal(arg) := 1 / arg
336 * No special actions taken in case argument is zero.
338 struct fixed31_32 dc_fixpt_recip(struct fixed31_32 arg);
342 * Trigonometric functions
347 * result = sinc(arg) := sin(arg) / arg
350 * Argument specified in radians,
351 * internally it's normalized to [-2pi...2pi] range.
353 struct fixed31_32 dc_fixpt_sinc(struct fixed31_32 arg);
360 * Argument specified in radians,
361 * internally it's normalized to [-2pi...2pi] range.
363 struct fixed31_32 dc_fixpt_sin(struct fixed31_32 arg);
370 * Argument specified in radians
371 * and should be in [-2pi...2pi] range -
372 * passing arguments outside that range
373 * will cause incorrect result!
375 struct fixed31_32 dc_fixpt_cos(struct fixed31_32 arg);
379 * Transcendent functions
387 * Currently, function is verified for abs(arg) <= 1.
389 struct fixed31_32 dc_fixpt_exp(struct fixed31_32 arg);
396 * Currently, abs(arg) should be less than 1.
397 * No normalization is done.
398 * Currently, no special actions taken
399 * in case of invalid argument(s). Take care!
401 struct fixed31_32 dc_fixpt_log(struct fixed31_32 arg);
410 * result = pow(arg1, arg2)
413 * Currently, abs(arg1) should be less than 1. Take care!
415 static inline struct fixed31_32 dc_fixpt_pow(struct fixed31_32 arg1, struct fixed31_32 arg2)
430 * result = floor(arg) := greatest integer lower than or equal to arg
432 static inline int dc_fixpt_floor(struct fixed31_32 arg)
434 unsigned long long arg_value = arg.value > 0 ? arg.value : -arg.value;
437 return (int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
439 return -(int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
444 * result = round(arg) := integer nearest to arg
446 static inline int dc_fixpt_round(struct fixed31_32 arg)
448 unsigned long long arg_value = arg.value > 0 ? arg.value : -arg.value;
450 const long long summand = dc_fixpt_half.value;
452 ASSERT(LLONG_MAX - (long long)arg_value >= summand);
454 arg_value += summand;
457 return (int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
459 return -(int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
464 * result = ceil(arg) := lowest integer greater than or equal to arg
466 static inline int dc_fixpt_ceil(struct fixed31_32 arg)
468 unsigned long long arg_value = arg.value > 0 ? arg.value : -arg.value;
470 const long long summand = dc_fixpt_one.value -
471 dc_fixpt_epsilon.value;
473 ASSERT(LLONG_MAX - (long long)arg_value >= summand);
475 arg_value += summand;
478 return (int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
480 return -(int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
483 /* the following two function are used in scaler hw programming to convert fixed
484 * point value to format 2 bits from integer part and 19 bits from fractional
485 * part. The same applies for u0d19, 0 bits from integer part and 19 bits from
489 unsigned int dc_fixpt_u2d19(struct fixed31_32 arg);
491 unsigned int dc_fixpt_u0d19(struct fixed31_32 arg);
493 unsigned int dc_fixpt_clamp_u0d14(struct fixed31_32 arg);
495 unsigned int dc_fixpt_clamp_u0d10(struct fixed31_32 arg);
497 int dc_fixpt_s4d19(struct fixed31_32 arg);