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__
30 #define LLONG_MAX 9223372036854775807ll
33 #define LLONG_MIN (-LLONG_MAX - 1ll)
36 #define FIXED31_32_BITS_PER_FRACTIONAL_PART 32
38 #define LLONG_MIN (1LL<<63)
41 #define LLONG_MAX (-1LL>>1)
46 * Arithmetic operations on real numbers
47 * represented as fixed-point numbers.
48 * There are: 1 bit for sign,
49 * 31 bit for integer part,
50 * 32 bits for fractional part.
53 * Currently, overflows and underflows are asserted;
54 * no special result returned.
67 static const struct fixed31_32 dc_fixpt_zero = { 0 };
68 static const struct fixed31_32 dc_fixpt_epsilon = { 1LL };
69 static const struct fixed31_32 dc_fixpt_half = { 0x80000000LL };
70 static const struct fixed31_32 dc_fixpt_one = { 0x100000000LL };
74 * Initialization routines
79 * result = numerator / denominator
81 struct fixed31_32 dc_fixpt_from_fraction(long long numerator, long long denominator);
87 static inline struct fixed31_32 dc_fixpt_from_int(int arg)
89 struct fixed31_32 res;
91 res.value = (long long) arg << FIXED31_32_BITS_PER_FRACTIONAL_PART;
105 static inline struct fixed31_32 dc_fixpt_neg(struct fixed31_32 arg)
107 struct fixed31_32 res;
109 res.value = -arg.value;
116 * result = abs(arg) := (arg >= 0) ? arg : -arg
118 static inline struct fixed31_32 dc_fixpt_abs(struct fixed31_32 arg)
121 return dc_fixpt_neg(arg);
128 * Binary relational operators
133 * result = arg1 < arg2
135 static inline bool dc_fixpt_lt(struct fixed31_32 arg1, struct fixed31_32 arg2)
137 return arg1.value < arg2.value;
142 * result = arg1 <= arg2
144 static inline bool dc_fixpt_le(struct fixed31_32 arg1, struct fixed31_32 arg2)
146 return arg1.value <= arg2.value;
151 * result = arg1 == arg2
153 static inline bool dc_fixpt_eq(struct fixed31_32 arg1, struct fixed31_32 arg2)
155 return arg1.value == arg2.value;
160 * result = min(arg1, arg2) := (arg1 <= arg2) ? arg1 : arg2
162 static inline struct fixed31_32 dc_fixpt_min(struct fixed31_32 arg1, struct fixed31_32 arg2)
164 if (arg1.value <= arg2.value)
172 * result = max(arg1, arg2) := (arg1 <= arg2) ? arg2 : arg1
174 static inline struct fixed31_32 dc_fixpt_max(struct fixed31_32 arg1, struct fixed31_32 arg2)
176 if (arg1.value <= arg2.value)
184 * | min_value, when arg <= min_value
185 * result = | arg, when min_value < arg < max_value
186 * | max_value, when arg >= max_value
188 static inline struct fixed31_32 dc_fixpt_clamp(
189 struct fixed31_32 arg,
190 struct fixed31_32 min_value,
191 struct fixed31_32 max_value)
193 if (dc_fixpt_le(arg, min_value))
195 else if (dc_fixpt_le(max_value, arg))
203 * Binary shift operators
208 * result = arg << shift
210 static inline struct fixed31_32 dc_fixpt_shl(struct fixed31_32 arg, unsigned char shift)
212 ASSERT(((arg.value >= 0) && (arg.value <= LLONG_MAX >> shift)) ||
213 ((arg.value < 0) && (arg.value >= ~(LLONG_MAX >> shift))));
215 arg.value = arg.value << shift;
222 * result = arg >> shift
224 static inline struct fixed31_32 dc_fixpt_shr(struct fixed31_32 arg, unsigned char shift)
226 bool negative = arg.value < 0;
229 arg.value = -arg.value;
230 arg.value = arg.value >> shift;
232 arg.value = -arg.value;
238 * Binary additive operators
243 * result = arg1 + arg2
245 static inline struct fixed31_32 dc_fixpt_add(struct fixed31_32 arg1, struct fixed31_32 arg2)
247 struct fixed31_32 res;
249 ASSERT(((arg1.value >= 0) && (LLONG_MAX - arg1.value >= arg2.value)) ||
250 ((arg1.value < 0) && (LLONG_MIN - arg1.value <= arg2.value)));
252 res.value = arg1.value + arg2.value;
259 * result = arg1 + arg2
261 static inline struct fixed31_32 dc_fixpt_add_int(struct fixed31_32 arg1, int arg2)
263 return dc_fixpt_add(arg1, dc_fixpt_from_int(arg2));
268 * result = arg1 - arg2
270 static inline struct fixed31_32 dc_fixpt_sub(struct fixed31_32 arg1, struct fixed31_32 arg2)
272 struct fixed31_32 res;
274 ASSERT(((arg2.value >= 0) && (LLONG_MIN + arg2.value <= arg1.value)) ||
275 ((arg2.value < 0) && (LLONG_MAX + arg2.value >= arg1.value)));
277 res.value = arg1.value - arg2.value;
284 * result = arg1 - arg2
286 static inline struct fixed31_32 dc_fixpt_sub_int(struct fixed31_32 arg1, int arg2)
288 return dc_fixpt_sub(arg1, dc_fixpt_from_int(arg2));
294 * Binary multiplicative operators
299 * result = arg1 * arg2
301 struct fixed31_32 dc_fixpt_mul(struct fixed31_32 arg1, struct fixed31_32 arg2);
306 * result = arg1 * arg2
308 static inline struct fixed31_32 dc_fixpt_mul_int(struct fixed31_32 arg1, int arg2)
310 return dc_fixpt_mul(arg1, dc_fixpt_from_int(arg2));
315 * result = square(arg) := arg * arg
317 struct fixed31_32 dc_fixpt_sqr(struct fixed31_32 arg);
321 * result = arg1 / arg2
323 static inline struct fixed31_32 dc_fixpt_div_int(struct fixed31_32 arg1, long long arg2)
325 return dc_fixpt_from_fraction(arg1.value, dc_fixpt_from_int((int)arg2).value);
330 * result = arg1 / arg2
332 static inline struct fixed31_32 dc_fixpt_div(struct fixed31_32 arg1, struct fixed31_32 arg2)
334 return dc_fixpt_from_fraction(arg1.value, arg2.value);
339 * Reciprocal function
344 * result = reciprocal(arg) := 1 / arg
347 * No special actions taken in case argument is zero.
349 struct fixed31_32 dc_fixpt_recip(struct fixed31_32 arg);
353 * Trigonometric functions
358 * result = sinc(arg) := sin(arg) / arg
361 * Argument specified in radians,
362 * internally it's normalized to [-2pi...2pi] range.
364 struct fixed31_32 dc_fixpt_sinc(struct fixed31_32 arg);
371 * Argument specified in radians,
372 * internally it's normalized to [-2pi...2pi] range.
374 struct fixed31_32 dc_fixpt_sin(struct fixed31_32 arg);
381 * Argument specified in radians
382 * and should be in [-2pi...2pi] range -
383 * passing arguments outside that range
384 * will cause incorrect result!
386 struct fixed31_32 dc_fixpt_cos(struct fixed31_32 arg);
390 * Transcendent functions
398 * Currently, function is verified for abs(arg) <= 1.
400 struct fixed31_32 dc_fixpt_exp(struct fixed31_32 arg);
407 * Currently, abs(arg) should be less than 1.
408 * No normalization is done.
409 * Currently, no special actions taken
410 * in case of invalid argument(s). Take care!
412 struct fixed31_32 dc_fixpt_log(struct fixed31_32 arg);
421 * result = pow(arg1, arg2)
424 * Currently, abs(arg1) should be less than 1. Take care!
426 static inline struct fixed31_32 dc_fixpt_pow(struct fixed31_32 arg1, struct fixed31_32 arg2)
429 return arg2.value == 0 ? dc_fixpt_one : dc_fixpt_zero;
444 * result = floor(arg) := greatest integer lower than or equal to arg
446 static inline int dc_fixpt_floor(struct fixed31_32 arg)
448 unsigned long long arg_value = arg.value > 0 ? arg.value : -arg.value;
451 return (int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
453 return -(int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
458 * result = round(arg) := integer nearest to arg
460 static inline int dc_fixpt_round(struct fixed31_32 arg)
462 unsigned long long arg_value = arg.value > 0 ? arg.value : -arg.value;
464 const long long summand = dc_fixpt_half.value;
466 ASSERT(LLONG_MAX - (long long)arg_value >= summand);
468 arg_value += summand;
471 return (int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
473 return -(int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
478 * result = ceil(arg) := lowest integer greater than or equal to arg
480 static inline int dc_fixpt_ceil(struct fixed31_32 arg)
482 unsigned long long arg_value = arg.value > 0 ? arg.value : -arg.value;
484 const long long summand = dc_fixpt_one.value -
485 dc_fixpt_epsilon.value;
487 ASSERT(LLONG_MAX - (long long)arg_value >= summand);
489 arg_value += summand;
492 return (int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
494 return -(int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
497 /* the following two function are used in scaler hw programming to convert fixed
498 * point value to format 2 bits from integer part and 19 bits from fractional
499 * part. The same applies for u0d19, 0 bits from integer part and 19 bits from
503 unsigned int dc_fixpt_u4d19(struct fixed31_32 arg);
505 unsigned int dc_fixpt_u3d19(struct fixed31_32 arg);
507 unsigned int dc_fixpt_u2d19(struct fixed31_32 arg);
509 unsigned int dc_fixpt_u0d19(struct fixed31_32 arg);
511 unsigned int dc_fixpt_clamp_u0d14(struct fixed31_32 arg);
513 unsigned int dc_fixpt_clamp_u0d10(struct fixed31_32 arg);
515 int dc_fixpt_s4d19(struct fixed31_32 arg);
517 static inline struct fixed31_32 dc_fixpt_truncate(struct fixed31_32 arg, unsigned int frac_bits)
519 bool negative = arg.value < 0;
521 if (frac_bits >= FIXED31_32_BITS_PER_FRACTIONAL_PART) {
522 ASSERT(frac_bits == FIXED31_32_BITS_PER_FRACTIONAL_PART);
527 arg.value = -arg.value;
528 arg.value &= (~0ULL) << (FIXED31_32_BITS_PER_FRACTIONAL_PART - frac_bits);
530 arg.value = -arg.value;
534 struct fixed31_32 dc_fixpt_from_ux_dy(unsigned int value, unsigned int integer_bits, unsigned int fractional_bits);
535 struct fixed31_32 dc_fixpt_from_int_dy(unsigned int int_value,
536 unsigned int frac_value,
537 unsigned int integer_bits,
538 unsigned int fractional_bits);