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(
75 long long denominator);
81 struct fixed31_32 dc_fixpt_from_int_nonconst(long long arg);
82 static inline struct fixed31_32 dc_fixpt_from_int(long long arg)
84 if (__builtin_constant_p(arg)) {
85 struct fixed31_32 res;
86 BUILD_BUG_ON((LONG_MIN > arg) || (arg > LONG_MAX));
87 res.value = arg << FIXED31_32_BITS_PER_FRACTIONAL_PART;
90 return dc_fixpt_from_int_nonconst(arg);
102 static inline struct fixed31_32 dc_fixpt_neg(struct fixed31_32 arg)
104 struct fixed31_32 res;
106 res.value = -arg.value;
113 * result = abs(arg) := (arg >= 0) ? arg : -arg
115 static inline struct fixed31_32 dc_fixpt_abs(struct fixed31_32 arg)
118 return dc_fixpt_neg(arg);
125 * Binary relational operators
130 * result = arg1 < arg2
132 static inline bool dc_fixpt_lt(struct fixed31_32 arg1,
133 struct fixed31_32 arg2)
135 return arg1.value < arg2.value;
140 * result = arg1 <= arg2
142 static inline bool dc_fixpt_le(struct fixed31_32 arg1,
143 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,
153 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,
163 struct fixed31_32 arg2)
165 if (arg1.value <= arg2.value)
173 * result = max(arg1, arg2) := (arg1 <= arg2) ? arg2 : arg1
175 static inline struct fixed31_32 dc_fixpt_max(struct fixed31_32 arg1,
176 struct fixed31_32 arg2)
178 if (arg1.value <= arg2.value)
186 * | min_value, when arg <= min_value
187 * result = | arg, when min_value < arg < max_value
188 * | max_value, when arg >= max_value
190 static inline struct fixed31_32 dc_fixpt_clamp(
191 struct fixed31_32 arg,
192 struct fixed31_32 min_value,
193 struct fixed31_32 max_value)
195 if (dc_fixpt_le(arg, min_value))
197 else if (dc_fixpt_le(max_value, arg))
205 * Binary shift operators
210 * result = arg << shift
212 struct fixed31_32 dc_fixpt_shl(
213 struct fixed31_32 arg,
214 unsigned char shift);
218 * result = arg >> shift
220 static inline struct fixed31_32 dc_fixpt_shr(
221 struct fixed31_32 arg,
224 struct fixed31_32 res;
225 res.value = arg.value >> shift;
231 * Binary additive operators
236 * result = arg1 + arg2
238 struct fixed31_32 dc_fixpt_add(
239 struct fixed31_32 arg1,
240 struct fixed31_32 arg2);
244 * result = arg1 + arg2
246 static inline struct fixed31_32 dc_fixpt_add_int(struct fixed31_32 arg1,
249 return dc_fixpt_add(arg1,
250 dc_fixpt_from_int(arg2));
255 * result = arg1 - arg2
257 struct fixed31_32 dc_fixpt_sub(
258 struct fixed31_32 arg1,
259 struct fixed31_32 arg2);
263 * result = arg1 - arg2
265 static inline struct fixed31_32 dc_fixpt_sub_int(struct fixed31_32 arg1,
268 return dc_fixpt_sub(arg1,
269 dc_fixpt_from_int(arg2));
275 * Binary multiplicative operators
280 * result = arg1 * arg2
282 struct fixed31_32 dc_fixpt_mul(
283 struct fixed31_32 arg1,
284 struct fixed31_32 arg2);
289 * result = arg1 * arg2
291 static inline struct fixed31_32 dc_fixpt_mul_int(struct fixed31_32 arg1,
294 return dc_fixpt_mul(arg1,
295 dc_fixpt_from_int(arg2));
300 * result = square(arg) := arg * arg
302 struct fixed31_32 dc_fixpt_sqr(
303 struct fixed31_32 arg);
307 * result = arg1 / arg2
309 static inline struct fixed31_32 dc_fixpt_div_int(struct fixed31_32 arg1,
312 return dc_fixpt_from_fraction(arg1.value,
313 dc_fixpt_from_int(arg2).value);
318 * result = arg1 / arg2
320 static inline struct fixed31_32 dc_fixpt_div(struct fixed31_32 arg1,
321 struct fixed31_32 arg2)
323 return dc_fixpt_from_fraction(arg1.value,
329 * Reciprocal function
334 * result = reciprocal(arg) := 1 / arg
337 * No special actions taken in case argument is zero.
339 struct fixed31_32 dc_fixpt_recip(
340 struct fixed31_32 arg);
344 * Trigonometric functions
349 * result = sinc(arg) := sin(arg) / arg
352 * Argument specified in radians,
353 * internally it's normalized to [-2pi...2pi] range.
355 struct fixed31_32 dc_fixpt_sinc(
356 struct fixed31_32 arg);
363 * Argument specified in radians,
364 * internally it's normalized to [-2pi...2pi] range.
366 struct fixed31_32 dc_fixpt_sin(
367 struct fixed31_32 arg);
374 * Argument specified in radians
375 * and should be in [-2pi...2pi] range -
376 * passing arguments outside that range
377 * will cause incorrect result!
379 struct fixed31_32 dc_fixpt_cos(
380 struct fixed31_32 arg);
384 * Transcendent functions
392 * Currently, function is verified for abs(arg) <= 1.
394 struct fixed31_32 dc_fixpt_exp(
395 struct fixed31_32 arg);
402 * Currently, abs(arg) should be less than 1.
403 * No normalization is done.
404 * Currently, no special actions taken
405 * in case of invalid argument(s). Take care!
407 struct fixed31_32 dc_fixpt_log(
408 struct fixed31_32 arg);
417 * result = pow(arg1, arg2)
420 * Currently, abs(arg1) should be less than 1. Take care!
422 struct fixed31_32 dc_fixpt_pow(
423 struct fixed31_32 arg1,
424 struct fixed31_32 arg2);
433 * result = floor(arg) := greatest integer lower than or equal to arg
436 struct fixed31_32 arg);
440 * result = round(arg) := integer nearest to arg
443 struct fixed31_32 arg);
447 * result = ceil(arg) := lowest integer greater than or equal to arg
450 struct fixed31_32 arg);
452 /* the following two function are used in scaler hw programming to convert fixed
453 * point value to format 2 bits from integer part and 19 bits from fractional
454 * part. The same applies for u0d19, 0 bits from integer part and 19 bits from
458 unsigned int dc_fixpt_u2d19(
459 struct fixed31_32 arg);
461 unsigned int dc_fixpt_u0d19(
462 struct fixed31_32 arg);
465 unsigned int dc_fixpt_clamp_u0d14(
466 struct fixed31_32 arg);
468 unsigned int dc_fixpt_clamp_u0d10(
469 struct fixed31_32 arg);
472 struct fixed31_32 arg);