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 #include "dm_services.h"
27 #include "include/fixed31_32.h"
29 static inline uint64_t abs_i64(
35 return (uint64_t)(-arg);
40 * result = dividend / divisor
41 * *remainder = dividend % divisor
43 static inline uint64_t complete_integer_division_u64(
52 result = div64_u64_rem(dividend, divisor, remainder);
58 #define FRACTIONAL_PART_MASK \
59 ((1ULL << FIXED31_32_BITS_PER_FRACTIONAL_PART) - 1)
61 #define GET_INTEGER_PART(x) \
62 ((x) >> FIXED31_32_BITS_PER_FRACTIONAL_PART)
64 #define GET_FRACTIONAL_PART(x) \
65 (FRACTIONAL_PART_MASK & (x))
67 struct fixed31_32 dal_fixed31_32_from_fraction(
71 struct fixed31_32 res;
73 bool arg1_negative = numerator < 0;
74 bool arg2_negative = denominator < 0;
76 uint64_t arg1_value = arg1_negative ? -numerator : numerator;
77 uint64_t arg2_value = arg2_negative ? -denominator : denominator;
81 /* determine integer part */
83 uint64_t res_value = complete_integer_division_u64(
84 arg1_value, arg2_value, &remainder);
86 ASSERT(res_value <= LONG_MAX);
88 /* determine fractional part */
90 uint32_t i = FIXED31_32_BITS_PER_FRACTIONAL_PART;
97 if (remainder >= arg2_value) {
99 remainder -= arg2_value;
106 uint64_t summand = (remainder << 1) >= arg2_value;
108 ASSERT(res_value <= LLONG_MAX - summand);
110 res_value += summand;
113 res.value = (int64_t)res_value;
115 if (arg1_negative ^ arg2_negative)
116 res.value = -res.value;
121 struct fixed31_32 dal_fixed31_32_from_int_nonconst(
124 struct fixed31_32 res;
126 ASSERT((LONG_MIN <= arg) && (arg <= LONG_MAX));
128 res.value = arg << FIXED31_32_BITS_PER_FRACTIONAL_PART;
133 struct fixed31_32 dal_fixed31_32_shl(
134 struct fixed31_32 arg,
137 struct fixed31_32 res;
139 ASSERT(((arg.value >= 0) && (arg.value <= LLONG_MAX >> shift)) ||
140 ((arg.value < 0) && (arg.value >= LLONG_MIN >> shift)));
142 res.value = arg.value << shift;
147 struct fixed31_32 dal_fixed31_32_add(
148 struct fixed31_32 arg1,
149 struct fixed31_32 arg2)
151 struct fixed31_32 res;
153 ASSERT(((arg1.value >= 0) && (LLONG_MAX - arg1.value >= arg2.value)) ||
154 ((arg1.value < 0) && (LLONG_MIN - arg1.value <= arg2.value)));
156 res.value = arg1.value + arg2.value;
161 struct fixed31_32 dal_fixed31_32_sub(
162 struct fixed31_32 arg1,
163 struct fixed31_32 arg2)
165 struct fixed31_32 res;
167 ASSERT(((arg2.value >= 0) && (LLONG_MIN + arg2.value <= arg1.value)) ||
168 ((arg2.value < 0) && (LLONG_MAX + arg2.value >= arg1.value)));
170 res.value = arg1.value - arg2.value;
175 struct fixed31_32 dal_fixed31_32_mul(
176 struct fixed31_32 arg1,
177 struct fixed31_32 arg2)
179 struct fixed31_32 res;
181 bool arg1_negative = arg1.value < 0;
182 bool arg2_negative = arg2.value < 0;
184 uint64_t arg1_value = arg1_negative ? -arg1.value : arg1.value;
185 uint64_t arg2_value = arg2_negative ? -arg2.value : arg2.value;
187 uint64_t arg1_int = GET_INTEGER_PART(arg1_value);
188 uint64_t arg2_int = GET_INTEGER_PART(arg2_value);
190 uint64_t arg1_fra = GET_FRACTIONAL_PART(arg1_value);
191 uint64_t arg2_fra = GET_FRACTIONAL_PART(arg2_value);
195 res.value = arg1_int * arg2_int;
197 ASSERT(res.value <= LONG_MAX);
199 res.value <<= FIXED31_32_BITS_PER_FRACTIONAL_PART;
201 tmp = arg1_int * arg2_fra;
203 ASSERT(tmp <= (uint64_t)(LLONG_MAX - res.value));
207 tmp = arg2_int * arg1_fra;
209 ASSERT(tmp <= (uint64_t)(LLONG_MAX - res.value));
213 tmp = arg1_fra * arg2_fra;
215 tmp = (tmp >> FIXED31_32_BITS_PER_FRACTIONAL_PART) +
216 (tmp >= (uint64_t)dal_fixed31_32_half.value);
218 ASSERT(tmp <= (uint64_t)(LLONG_MAX - res.value));
222 if (arg1_negative ^ arg2_negative)
223 res.value = -res.value;
228 struct fixed31_32 dal_fixed31_32_sqr(
229 struct fixed31_32 arg)
231 struct fixed31_32 res;
233 uint64_t arg_value = abs_i64(arg.value);
235 uint64_t arg_int = GET_INTEGER_PART(arg_value);
237 uint64_t arg_fra = GET_FRACTIONAL_PART(arg_value);
241 res.value = arg_int * arg_int;
243 ASSERT(res.value <= LONG_MAX);
245 res.value <<= FIXED31_32_BITS_PER_FRACTIONAL_PART;
247 tmp = arg_int * arg_fra;
249 ASSERT(tmp <= (uint64_t)(LLONG_MAX - res.value));
253 ASSERT(tmp <= (uint64_t)(LLONG_MAX - res.value));
257 tmp = arg_fra * arg_fra;
259 tmp = (tmp >> FIXED31_32_BITS_PER_FRACTIONAL_PART) +
260 (tmp >= (uint64_t)dal_fixed31_32_half.value);
262 ASSERT(tmp <= (uint64_t)(LLONG_MAX - res.value));
269 struct fixed31_32 dal_fixed31_32_recip(
270 struct fixed31_32 arg)
274 * Good idea to use Newton's method
279 return dal_fixed31_32_from_fraction(
280 dal_fixed31_32_one.value,
284 struct fixed31_32 dal_fixed31_32_sinc(
285 struct fixed31_32 arg)
287 struct fixed31_32 square;
289 struct fixed31_32 res = dal_fixed31_32_one;
293 struct fixed31_32 arg_norm = arg;
295 if (dal_fixed31_32_le(
296 dal_fixed31_32_two_pi,
297 dal_fixed31_32_abs(arg))) {
298 arg_norm = dal_fixed31_32_sub(
300 dal_fixed31_32_mul_int(
301 dal_fixed31_32_two_pi,
304 dal_fixed31_32_two_pi.value)));
307 square = dal_fixed31_32_sqr(arg_norm);
310 res = dal_fixed31_32_sub(
312 dal_fixed31_32_div_int(
321 if (arg.value != arg_norm.value)
322 res = dal_fixed31_32_div(
323 dal_fixed31_32_mul(res, arg_norm),
329 struct fixed31_32 dal_fixed31_32_sin(
330 struct fixed31_32 arg)
332 return dal_fixed31_32_mul(
334 dal_fixed31_32_sinc(arg));
337 struct fixed31_32 dal_fixed31_32_cos(
338 struct fixed31_32 arg)
340 /* TODO implement argument normalization */
342 const struct fixed31_32 square = dal_fixed31_32_sqr(arg);
344 struct fixed31_32 res = dal_fixed31_32_one;
349 res = dal_fixed31_32_sub(
351 dal_fixed31_32_div_int(
368 * Calculated as Taylor series.
370 static struct fixed31_32 fixed31_32_exp_from_taylor_series(
371 struct fixed31_32 arg)
375 struct fixed31_32 res = dal_fixed31_32_from_fraction(
378 /* TODO find correct res */
380 ASSERT(dal_fixed31_32_lt(arg, dal_fixed31_32_one));
383 res = dal_fixed31_32_add(
385 dal_fixed31_32_div_int(
392 return dal_fixed31_32_add(
399 struct fixed31_32 dal_fixed31_32_exp(
400 struct fixed31_32 arg)
405 * exp(x) = exp(r + m * ln(2)) = (1 << m) * exp(r),
406 * where m = round(x / ln(2)), r = x - m * ln(2)
409 if (dal_fixed31_32_le(
410 dal_fixed31_32_ln2_div_2,
411 dal_fixed31_32_abs(arg))) {
412 int32_t m = dal_fixed31_32_round(
415 dal_fixed31_32_ln2));
417 struct fixed31_32 r = dal_fixed31_32_sub(
419 dal_fixed31_32_mul_int(
425 ASSERT(dal_fixed31_32_lt(
426 dal_fixed31_32_abs(r),
427 dal_fixed31_32_one));
430 return dal_fixed31_32_shl(
431 fixed31_32_exp_from_taylor_series(r),
434 return dal_fixed31_32_div_int(
435 fixed31_32_exp_from_taylor_series(r),
437 } else if (arg.value != 0)
438 return fixed31_32_exp_from_taylor_series(arg);
440 return dal_fixed31_32_one;
443 struct fixed31_32 dal_fixed31_32_log(
444 struct fixed31_32 arg)
446 struct fixed31_32 res = dal_fixed31_32_neg(dal_fixed31_32_one);
447 /* TODO improve 1st estimation */
449 struct fixed31_32 error;
451 ASSERT(arg.value > 0);
452 /* TODO if arg is negative, return NaN */
453 /* TODO if arg is zero, return -INF */
456 struct fixed31_32 res1 = dal_fixed31_32_add(
462 dal_fixed31_32_exp(res)));
464 error = dal_fixed31_32_sub(
469 /* TODO determine max_allowed_error based on quality of exp() */
470 } while (abs_i64(error.value) > 100ULL);
475 struct fixed31_32 dal_fixed31_32_pow(
476 struct fixed31_32 arg1,
477 struct fixed31_32 arg2)
479 return dal_fixed31_32_exp(
481 dal_fixed31_32_log(arg1),
485 int32_t dal_fixed31_32_floor(
486 struct fixed31_32 arg)
488 uint64_t arg_value = abs_i64(arg.value);
491 return (int32_t)GET_INTEGER_PART(arg_value);
493 return -(int32_t)GET_INTEGER_PART(arg_value);
496 int32_t dal_fixed31_32_round(
497 struct fixed31_32 arg)
499 uint64_t arg_value = abs_i64(arg.value);
501 const int64_t summand = dal_fixed31_32_half.value;
503 ASSERT(LLONG_MAX - (int64_t)arg_value >= summand);
505 arg_value += summand;
508 return (int32_t)GET_INTEGER_PART(arg_value);
510 return -(int32_t)GET_INTEGER_PART(arg_value);
513 int32_t dal_fixed31_32_ceil(
514 struct fixed31_32 arg)
516 uint64_t arg_value = abs_i64(arg.value);
518 const int64_t summand = dal_fixed31_32_one.value -
519 dal_fixed31_32_epsilon.value;
521 ASSERT(LLONG_MAX - (int64_t)arg_value >= summand);
523 arg_value += summand;
526 return (int32_t)GET_INTEGER_PART(arg_value);
528 return -(int32_t)GET_INTEGER_PART(arg_value);
531 /* this function is a generic helper to translate fixed point value to
532 * specified integer format that will consist of integer_bits integer part and
533 * fractional_bits fractional part. For example it is used in
534 * dal_fixed31_32_u2d19 to receive 2 bits integer part and 19 bits fractional
535 * part in 32 bits. It is used in hw programming (scaler)
538 static inline uint32_t ux_dy(
540 uint32_t integer_bits,
541 uint32_t fractional_bits)
543 /* 1. create mask of integer part */
544 uint32_t result = (1 << integer_bits) - 1;
545 /* 2. mask out fractional part */
546 uint32_t fractional_part = FRACTIONAL_PART_MASK & value;
547 /* 3. shrink fixed point integer part to be of integer_bits width*/
548 result &= GET_INTEGER_PART(value);
549 /* 4. make space for fractional part to be filled in after integer */
550 result <<= fractional_bits;
551 /* 5. shrink fixed point fractional part to of fractional_bits width*/
552 fractional_part >>= FIXED31_32_BITS_PER_FRACTIONAL_PART - fractional_bits;
553 /* 6. merge the result */
554 return result | fractional_part;
557 uint32_t dal_fixed31_32_u2d19(
558 struct fixed31_32 arg)
560 return ux_dy(arg.value, 2, 19);
563 uint32_t dal_fixed31_32_u0d19(
564 struct fixed31_32 arg)
566 return ux_dy(arg.value, 0, 19);
569 uint32_t dal_fixed31_32_u0d14(
570 struct fixed31_32 arg)
572 return ux_dy(arg.value, 1, 14);
575 uint32_t dal_fixed31_32_u0d10(
576 struct fixed31_32 arg)
578 return ux_dy(arg.value, 1, 10);