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
31 #define LLONG_MIN (1LL<<63)
34 #define LLONG_MAX (-1LL>>1)
39 * Arithmetic operations on real numbers
40 * represented as fixed-point numbers.
41 * There are: 1 bit for sign,
42 * 31 bit for integer part,
43 * 32 bits for fractional part.
46 * Currently, overflows and underflows are asserted;
47 * no special result returned.
60 static const struct fixed31_32 dc_fixpt_zero = { 0 };
61 static const struct fixed31_32 dc_fixpt_epsilon = { 1LL };
62 static const struct fixed31_32 dc_fixpt_half = { 0x80000000LL };
63 static const struct fixed31_32 dc_fixpt_one = { 0x100000000LL };
65 static const struct fixed31_32 dc_fixpt_pi = { 13493037705LL };
66 static const struct fixed31_32 dc_fixpt_two_pi = { 26986075409LL };
67 static const struct fixed31_32 dc_fixpt_e = { 11674931555LL };
68 static const struct fixed31_32 dc_fixpt_ln2 = { 2977044471LL };
69 static const struct fixed31_32 dc_fixpt_ln2_div_2 = { 1488522236LL };
73 * Initialization routines
78 * result = numerator / denominator
80 struct fixed31_32 dc_fixpt_from_fraction(long long numerator, long long denominator);
86 static inline struct fixed31_32 dc_fixpt_from_int(int arg)
88 struct fixed31_32 res;
90 res.value = (long long) arg << FIXED31_32_BITS_PER_FRACTIONAL_PART;
104 static inline struct fixed31_32 dc_fixpt_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 dc_fixpt_abs(struct fixed31_32 arg)
120 return dc_fixpt_neg(arg);
127 * Binary relational operators
132 * result = arg1 < arg2
134 static inline bool dc_fixpt_lt(struct fixed31_32 arg1, struct fixed31_32 arg2)
136 return arg1.value < arg2.value;
141 * result = arg1 <= arg2
143 static inline bool dc_fixpt_le(struct fixed31_32 arg1, struct fixed31_32 arg2)
145 return arg1.value <= arg2.value;
150 * result = arg1 == arg2
152 static inline bool dc_fixpt_eq(struct fixed31_32 arg1, struct fixed31_32 arg2)
154 return arg1.value == arg2.value;
159 * result = min(arg1, arg2) := (arg1 <= arg2) ? arg1 : arg2
161 static inline struct fixed31_32 dc_fixpt_min(struct fixed31_32 arg1, struct fixed31_32 arg2)
163 if (arg1.value <= arg2.value)
171 * result = max(arg1, arg2) := (arg1 <= arg2) ? arg2 : arg1
173 static inline struct fixed31_32 dc_fixpt_max(struct fixed31_32 arg1, struct fixed31_32 arg2)
175 if (arg1.value <= arg2.value)
183 * | min_value, when arg <= min_value
184 * result = | arg, when min_value < arg < max_value
185 * | max_value, when arg >= max_value
187 static inline struct fixed31_32 dc_fixpt_clamp(
188 struct fixed31_32 arg,
189 struct fixed31_32 min_value,
190 struct fixed31_32 max_value)
192 if (dc_fixpt_le(arg, min_value))
194 else if (dc_fixpt_le(max_value, arg))
202 * Binary shift operators
207 * result = arg << shift
209 static inline struct fixed31_32 dc_fixpt_shl(struct fixed31_32 arg, unsigned char shift)
211 ASSERT(((arg.value >= 0) && (arg.value <= LLONG_MAX >> shift)) ||
212 ((arg.value < 0) && (arg.value >= ~(LLONG_MAX >> shift))));
214 arg.value = arg.value << shift;
221 * result = arg >> shift
223 static inline struct fixed31_32 dc_fixpt_shr(struct fixed31_32 arg, unsigned char shift)
225 bool negative = arg.value < 0;
228 arg.value = -arg.value;
229 arg.value = arg.value >> shift;
231 arg.value = -arg.value;
237 * Binary additive operators
242 * result = arg1 + arg2
244 static inline struct fixed31_32 dc_fixpt_add(struct fixed31_32 arg1, struct fixed31_32 arg2)
246 struct fixed31_32 res;
248 ASSERT(((arg1.value >= 0) && (LLONG_MAX - arg1.value >= arg2.value)) ||
249 ((arg1.value < 0) && (LLONG_MIN - arg1.value <= arg2.value)));
251 res.value = arg1.value + arg2.value;
258 * result = arg1 + arg2
260 static inline struct fixed31_32 dc_fixpt_add_int(struct fixed31_32 arg1, int arg2)
262 return dc_fixpt_add(arg1, dc_fixpt_from_int(arg2));
267 * result = arg1 - arg2
269 static inline struct fixed31_32 dc_fixpt_sub(struct fixed31_32 arg1, struct fixed31_32 arg2)
271 struct fixed31_32 res;
273 ASSERT(((arg2.value >= 0) && (LLONG_MIN + arg2.value <= arg1.value)) ||
274 ((arg2.value < 0) && (LLONG_MAX + arg2.value >= arg1.value)));
276 res.value = arg1.value - arg2.value;
283 * result = arg1 - arg2
285 static inline struct fixed31_32 dc_fixpt_sub_int(struct fixed31_32 arg1, int arg2)
287 return dc_fixpt_sub(arg1, dc_fixpt_from_int(arg2));
293 * Binary multiplicative operators
298 * result = arg1 * arg2
300 struct fixed31_32 dc_fixpt_mul(struct fixed31_32 arg1, struct fixed31_32 arg2);
305 * result = arg1 * arg2
307 static inline struct fixed31_32 dc_fixpt_mul_int(struct fixed31_32 arg1, int arg2)
309 return dc_fixpt_mul(arg1, dc_fixpt_from_int(arg2));
314 * result = square(arg) := arg * arg
316 struct fixed31_32 dc_fixpt_sqr(struct fixed31_32 arg);
320 * result = arg1 / arg2
322 static inline struct fixed31_32 dc_fixpt_div_int(struct fixed31_32 arg1, long long arg2)
324 return dc_fixpt_from_fraction(arg1.value, dc_fixpt_from_int(arg2).value);
329 * result = arg1 / arg2
331 static inline struct fixed31_32 dc_fixpt_div(struct fixed31_32 arg1, struct fixed31_32 arg2)
333 return dc_fixpt_from_fraction(arg1.value, arg2.value);
338 * Reciprocal function
343 * result = reciprocal(arg) := 1 / arg
346 * No special actions taken in case argument is zero.
348 struct fixed31_32 dc_fixpt_recip(struct fixed31_32 arg);
352 * Trigonometric functions
357 * result = sinc(arg) := sin(arg) / arg
360 * Argument specified in radians,
361 * internally it's normalized to [-2pi...2pi] range.
363 struct fixed31_32 dc_fixpt_sinc(struct fixed31_32 arg);
370 * Argument specified in radians,
371 * internally it's normalized to [-2pi...2pi] range.
373 struct fixed31_32 dc_fixpt_sin(struct fixed31_32 arg);
380 * Argument specified in radians
381 * and should be in [-2pi...2pi] range -
382 * passing arguments outside that range
383 * will cause incorrect result!
385 struct fixed31_32 dc_fixpt_cos(struct fixed31_32 arg);
389 * Transcendent functions
397 * Currently, function is verified for abs(arg) <= 1.
399 struct fixed31_32 dc_fixpt_exp(struct fixed31_32 arg);
406 * Currently, abs(arg) should be less than 1.
407 * No normalization is done.
408 * Currently, no special actions taken
409 * in case of invalid argument(s). Take care!
411 struct fixed31_32 dc_fixpt_log(struct fixed31_32 arg);
420 * result = pow(arg1, arg2)
423 * Currently, abs(arg1) should be less than 1. Take care!
425 static inline struct fixed31_32 dc_fixpt_pow(struct fixed31_32 arg1, struct fixed31_32 arg2)
440 * result = floor(arg) := greatest integer lower than or equal to arg
442 static inline int dc_fixpt_floor(struct fixed31_32 arg)
444 unsigned long long arg_value = arg.value > 0 ? arg.value : -arg.value;
447 return (int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
449 return -(int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
454 * result = round(arg) := integer nearest to arg
456 static inline int dc_fixpt_round(struct fixed31_32 arg)
458 unsigned long long arg_value = arg.value > 0 ? arg.value : -arg.value;
460 const long long summand = dc_fixpt_half.value;
462 ASSERT(LLONG_MAX - (long long)arg_value >= summand);
464 arg_value += summand;
467 return (int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
469 return -(int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
474 * result = ceil(arg) := lowest integer greater than or equal to arg
476 static inline int dc_fixpt_ceil(struct fixed31_32 arg)
478 unsigned long long arg_value = arg.value > 0 ? arg.value : -arg.value;
480 const long long summand = dc_fixpt_one.value -
481 dc_fixpt_epsilon.value;
483 ASSERT(LLONG_MAX - (long long)arg_value >= summand);
485 arg_value += summand;
488 return (int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
490 return -(int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
493 /* the following two function are used in scaler hw programming to convert fixed
494 * point value to format 2 bits from integer part and 19 bits from fractional
495 * part. The same applies for u0d19, 0 bits from integer part and 19 bits from
499 unsigned int dc_fixpt_u3d19(struct fixed31_32 arg);
501 unsigned int dc_fixpt_u2d19(struct fixed31_32 arg);
503 unsigned int dc_fixpt_u0d19(struct fixed31_32 arg);
505 unsigned int dc_fixpt_clamp_u0d14(struct fixed31_32 arg);
507 unsigned int dc_fixpt_clamp_u0d10(struct fixed31_32 arg);
509 int dc_fixpt_s4d19(struct fixed31_32 arg);
511 static inline struct fixed31_32 dc_fixpt_truncate(struct fixed31_32 arg, unsigned int frac_bits)
513 bool negative = arg.value < 0;
515 if (frac_bits >= FIXED31_32_BITS_PER_FRACTIONAL_PART) {
516 ASSERT(frac_bits == FIXED31_32_BITS_PER_FRACTIONAL_PART);
521 arg.value = -arg.value;
522 arg.value &= (~0LL) << (FIXED31_32_BITS_PER_FRACTIONAL_PART - frac_bits);
524 arg.value = -arg.value;