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 const struct fixed31_32 dc_fixpt_two_pi
= { 26986075409LL };
30 static const struct fixed31_32 dc_fixpt_ln2
= { 2977044471LL };
31 static const struct fixed31_32 dc_fixpt_ln2_div_2
= { 1488522236LL };
33 static inline unsigned long long abs_i64(
37 return (unsigned long long)arg
;
39 return (unsigned long long)(-arg
);
44 * result = dividend / divisor
45 * *remainder = dividend % divisor
47 static inline unsigned long long complete_integer_division_u64(
48 unsigned long long dividend
,
49 unsigned long long divisor
,
50 unsigned long long *remainder
)
52 unsigned long long result
;
56 result
= div64_u64_rem(dividend
, divisor
, remainder
);
62 #define FRACTIONAL_PART_MASK \
63 ((1ULL << FIXED31_32_BITS_PER_FRACTIONAL_PART) - 1)
65 #define GET_INTEGER_PART(x) \
66 ((x) >> FIXED31_32_BITS_PER_FRACTIONAL_PART)
68 #define GET_FRACTIONAL_PART(x) \
69 (FRACTIONAL_PART_MASK & (x))
71 struct fixed31_32
dc_fixpt_from_fraction(long long numerator
, long long denominator
)
73 struct fixed31_32 res
;
75 bool arg1_negative
= numerator
< 0;
76 bool arg2_negative
= denominator
< 0;
78 unsigned long long arg1_value
= arg1_negative
? -numerator
: numerator
;
79 unsigned long long arg2_value
= arg2_negative
? -denominator
: denominator
;
81 unsigned long long remainder
;
83 /* determine integer part */
85 unsigned long long res_value
= complete_integer_division_u64(
86 arg1_value
, arg2_value
, &remainder
);
88 ASSERT(res_value
<= LONG_MAX
);
90 /* determine fractional part */
92 unsigned int i
= FIXED31_32_BITS_PER_FRACTIONAL_PART
;
99 if (remainder
>= arg2_value
) {
101 remainder
-= arg2_value
;
108 unsigned long long summand
= (remainder
<< 1) >= arg2_value
;
110 ASSERT(res_value
<= LLONG_MAX
- summand
);
112 res_value
+= summand
;
115 res
.value
= (long long)res_value
;
117 if (arg1_negative
^ arg2_negative
)
118 res
.value
= -res
.value
;
123 struct fixed31_32
dc_fixpt_mul(struct fixed31_32 arg1
, struct fixed31_32 arg2
)
125 struct fixed31_32 res
;
127 bool arg1_negative
= arg1
.value
< 0;
128 bool arg2_negative
= arg2
.value
< 0;
130 unsigned long long arg1_value
= arg1_negative
? -arg1
.value
: arg1
.value
;
131 unsigned long long arg2_value
= arg2_negative
? -arg2
.value
: arg2
.value
;
133 unsigned long long arg1_int
= GET_INTEGER_PART(arg1_value
);
134 unsigned long long arg2_int
= GET_INTEGER_PART(arg2_value
);
136 unsigned long long arg1_fra
= GET_FRACTIONAL_PART(arg1_value
);
137 unsigned long long arg2_fra
= GET_FRACTIONAL_PART(arg2_value
);
139 unsigned long long tmp
;
141 res
.value
= arg1_int
* arg2_int
;
143 ASSERT(res
.value
<= LONG_MAX
);
145 res
.value
<<= FIXED31_32_BITS_PER_FRACTIONAL_PART
;
147 tmp
= arg1_int
* arg2_fra
;
149 ASSERT(tmp
<= (unsigned long long)(LLONG_MAX
- res
.value
));
153 tmp
= arg2_int
* arg1_fra
;
155 ASSERT(tmp
<= (unsigned long long)(LLONG_MAX
- res
.value
));
159 tmp
= arg1_fra
* arg2_fra
;
161 tmp
= (tmp
>> FIXED31_32_BITS_PER_FRACTIONAL_PART
) +
162 (tmp
>= (unsigned long long)dc_fixpt_half
.value
);
164 ASSERT(tmp
<= (unsigned long long)(LLONG_MAX
- res
.value
));
168 if (arg1_negative
^ arg2_negative
)
169 res
.value
= -res
.value
;
174 struct fixed31_32
dc_fixpt_sqr(struct fixed31_32 arg
)
176 struct fixed31_32 res
;
178 unsigned long long arg_value
= abs_i64(arg
.value
);
180 unsigned long long arg_int
= GET_INTEGER_PART(arg_value
);
182 unsigned long long arg_fra
= GET_FRACTIONAL_PART(arg_value
);
184 unsigned long long tmp
;
186 res
.value
= arg_int
* arg_int
;
188 ASSERT(res
.value
<= LONG_MAX
);
190 res
.value
<<= FIXED31_32_BITS_PER_FRACTIONAL_PART
;
192 tmp
= arg_int
* arg_fra
;
194 ASSERT(tmp
<= (unsigned long long)(LLONG_MAX
- res
.value
));
198 ASSERT(tmp
<= (unsigned long long)(LLONG_MAX
- res
.value
));
202 tmp
= arg_fra
* arg_fra
;
204 tmp
= (tmp
>> FIXED31_32_BITS_PER_FRACTIONAL_PART
) +
205 (tmp
>= (unsigned long long)dc_fixpt_half
.value
);
207 ASSERT(tmp
<= (unsigned long long)(LLONG_MAX
- res
.value
));
214 struct fixed31_32
dc_fixpt_recip(struct fixed31_32 arg
)
218 * Good idea to use Newton's method
223 return dc_fixpt_from_fraction(
228 struct fixed31_32
dc_fixpt_sinc(struct fixed31_32 arg
)
230 struct fixed31_32 square
;
232 struct fixed31_32 res
= dc_fixpt_one
;
236 struct fixed31_32 arg_norm
= arg
;
240 dc_fixpt_abs(arg
))) {
241 arg_norm
= dc_fixpt_sub(
247 dc_fixpt_two_pi
.value
)));
250 square
= dc_fixpt_sqr(arg_norm
);
264 if (arg
.value
!= arg_norm
.value
)
266 dc_fixpt_mul(res
, arg_norm
),
272 struct fixed31_32
dc_fixpt_sin(struct fixed31_32 arg
)
279 struct fixed31_32
dc_fixpt_cos(struct fixed31_32 arg
)
281 /* TODO implement argument normalization */
283 const struct fixed31_32 square
= dc_fixpt_sqr(arg
);
285 struct fixed31_32 res
= dc_fixpt_one
;
309 * Calculated as Taylor series.
311 static struct fixed31_32
fixed31_32_exp_from_taylor_series(struct fixed31_32 arg
)
315 struct fixed31_32 res
= dc_fixpt_from_fraction(
318 /* TODO find correct res */
320 ASSERT(dc_fixpt_lt(arg
, dc_fixpt_one
));
339 struct fixed31_32
dc_fixpt_exp(struct fixed31_32 arg
)
344 * exp(x) = exp(r + m * ln(2)) = (1 << m) * exp(r),
345 * where m = round(x / ln(2)), r = x - m * ln(2)
350 dc_fixpt_abs(arg
))) {
351 int m
= dc_fixpt_round(
356 struct fixed31_32 r
= dc_fixpt_sub(
370 fixed31_32_exp_from_taylor_series(r
),
373 return dc_fixpt_div_int(
374 fixed31_32_exp_from_taylor_series(r
),
376 } else if (arg
.value
!= 0)
377 return fixed31_32_exp_from_taylor_series(arg
);
382 struct fixed31_32
dc_fixpt_log(struct fixed31_32 arg
)
384 struct fixed31_32 res
= dc_fixpt_neg(dc_fixpt_one
);
385 /* TODO improve 1st estimation */
387 struct fixed31_32 error
;
389 ASSERT(arg
.value
> 0);
390 /* TODO if arg is negative, return NaN */
391 /* TODO if arg is zero, return -INF */
394 struct fixed31_32 res1
= dc_fixpt_add(
402 error
= dc_fixpt_sub(
407 /* TODO determine max_allowed_error based on quality of exp() */
408 } while (abs_i64(error
.value
) > 100ULL);
414 /* this function is a generic helper to translate fixed point value to
415 * specified integer format that will consist of integer_bits integer part and
416 * fractional_bits fractional part. For example it is used in
417 * dc_fixpt_u2d19 to receive 2 bits integer part and 19 bits fractional
418 * part in 32 bits. It is used in hw programming (scaler)
421 static inline unsigned int ux_dy(
423 unsigned int integer_bits
,
424 unsigned int fractional_bits
)
426 /* 1. create mask of integer part */
427 unsigned int result
= (1 << integer_bits
) - 1;
428 /* 2. mask out fractional part */
429 unsigned int fractional_part
= FRACTIONAL_PART_MASK
& value
;
430 /* 3. shrink fixed point integer part to be of integer_bits width*/
431 result
&= GET_INTEGER_PART(value
);
432 /* 4. make space for fractional part to be filled in after integer */
433 result
<<= fractional_bits
;
434 /* 5. shrink fixed point fractional part to of fractional_bits width*/
435 fractional_part
>>= FIXED31_32_BITS_PER_FRACTIONAL_PART
- fractional_bits
;
436 /* 6. merge the result */
437 return result
| fractional_part
;
440 static inline unsigned int clamp_ux_dy(
442 unsigned int integer_bits
,
443 unsigned int fractional_bits
,
444 unsigned int min_clamp
)
446 unsigned int truncated_val
= ux_dy(value
, integer_bits
, fractional_bits
);
448 if (value
>= (1LL << (integer_bits
+ FIXED31_32_BITS_PER_FRACTIONAL_PART
)))
449 return (1 << (integer_bits
+ fractional_bits
)) - 1;
450 else if (truncated_val
> min_clamp
)
451 return truncated_val
;
456 unsigned int dc_fixpt_u4d19(struct fixed31_32 arg
)
458 return ux_dy(arg
.value
, 4, 19);
461 unsigned int dc_fixpt_u3d19(struct fixed31_32 arg
)
463 return ux_dy(arg
.value
, 3, 19);
466 unsigned int dc_fixpt_u2d19(struct fixed31_32 arg
)
468 return ux_dy(arg
.value
, 2, 19);
471 unsigned int dc_fixpt_u0d19(struct fixed31_32 arg
)
473 return ux_dy(arg
.value
, 0, 19);
476 unsigned int dc_fixpt_clamp_u0d14(struct fixed31_32 arg
)
478 return clamp_ux_dy(arg
.value
, 0, 14, 1);
481 unsigned int dc_fixpt_clamp_u0d10(struct fixed31_32 arg
)
483 return clamp_ux_dy(arg
.value
, 0, 10, 1);
486 int dc_fixpt_s4d19(struct fixed31_32 arg
)
489 return -(int)ux_dy(dc_fixpt_abs(arg
).value
, 4, 19);
491 return ux_dy(arg
.value
, 4, 19);